api.lua 2.0 KB

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