Player skin mod, supporting unified_inventory, sfinv and smart_inventory https://content.minetest.net/packages/bell07/skinsdb/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

  1. -- get current skin
  2. local storage = minetest.get_mod_storage()
  3. function skins.get_player_skin(player)
  4. local meta = player:get_meta()
  5. if meta:get("skinsdb:skin_key") then
  6. -- Move player data prior July 2018 to mod storage
  7. storage:set_string(player:get_player_name(), player:get_string("skinsdb:skin_key"))
  8. meta:set_string("skinsdb:skin_key", "")
  9. end
  10. local skin = storage:get_string(player:get_player_name())
  11. return skins.get(skin) or skins.get(skins.default)
  12. end
  13. -- Assign skin to player
  14. function skins.assign_player_skin(player, skin)
  15. local skin_obj
  16. if type(skin) == "string" then
  17. skin_obj = skins.get(skin)
  18. else
  19. skin_obj = skin
  20. end
  21. if not skin_obj then
  22. return false
  23. end
  24. if skin_obj:is_applicable_for_player(player:get_player_name()) then
  25. local skin_key = skin_obj:get_key()
  26. if skin_key == skins.default then
  27. skin_key = ""
  28. end
  29. storage:set_string(player:get_player_name(), skin_key)
  30. else
  31. return false
  32. end
  33. return true
  34. end
  35. -- update visuals
  36. function skins.update_player_skin(player)
  37. if skins.armor_loaded then
  38. -- all needed is wrapped and implemented in 3d_armor mod
  39. armor:set_player_armor(player)
  40. else
  41. -- do updates manually without 3d_armor
  42. skins.get_player_skin(player):apply_skin_to_player(player)
  43. if minetest.global_exists("sfinv") and sfinv.enabled then
  44. sfinv.set_player_inventory_formspec(player)
  45. end
  46. end
  47. end
  48. -- Assign and update - should be used on selection externally
  49. function skins.set_player_skin(player, skin)
  50. local success = skins.assign_player_skin(player, skin)
  51. if success then
  52. skins.get_player_skin(player):set_skin(player)
  53. skins.update_player_skin(player)
  54. end
  55. return success
  56. end
  57. -- Check Skin format (code stohlen from stu's multiskin)
  58. function skins.get_skin_format(file)
  59. file:seek("set", 1)
  60. if file:read(3) == "PNG" then
  61. file:seek("set", 16)
  62. local ws = file:read(4)
  63. local hs = file:read(4)
  64. local w = ws:sub(3, 3):byte() * 256 + ws:sub(4, 4):byte()
  65. local h = hs:sub(3, 3):byte() * 256 + hs:sub(4, 4):byte()
  66. if w >= 64 then
  67. if w == h then
  68. return "1.8"
  69. elseif w == h * 2 then
  70. return "1.0"
  71. end
  72. end
  73. end
  74. end