Guest User

Untitled

a guest
Mar 10th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. local mtver = minetest.get_version()
  2. local maxslots = (string.sub(mtver.string, 1, 4) ~= "0.4.") and 32 or 23
  3.  
  4. local function validate_size(s)
  5.     local size = s and tonumber(s) or 16
  6.     if (size == 8 or size == 10 or size == 16 or size == 23 or size == 24 or size == 32)
  7.       and size <= maxslots then
  8.         return size
  9.     else
  10.         return 16
  11.     end
  12. end
  13.  
  14. local hotbar_size_default = validate_size(minetest.settings:get("hotbar_size"))
  15.  
  16. local player_hotbar_settings = {}
  17.  
  18. local function load_hotbar_settings()
  19.     local f = io.open(minetest.get_worldpath()..DIR_DELIM.."hotbar_settings","r")
  20.     if not f then return end
  21.     local d = f:read("*all")
  22.     f:close()
  23.     player_hotbar_settings = minetest.deserialize(d)
  24. end
  25.  
  26. local function save_hotbar_settings()
  27.     local f = io.open(minetest.get_worldpath()..DIR_DELIM.."hotbar_settings","w")
  28.     if not f then
  29.         minetest.log("error","Failed to save hotbar settings")
  30.         return
  31.     end
  32.     local d = minetest.serialize(player_hotbar_settings)
  33.     f:write(d)
  34.     f:close()
  35. end
  36.  
  37. local function get_hotbar_setting(name)
  38.     return tonumber(player_hotbar_settings[name]) or hotbar_size_default
  39. end
  40.  
  41. load_hotbar_settings()
  42.  
  43. minetest.register_on_joinplayer(function(player)
  44.     local hotbar_size = validate_size(get_hotbar_setting(player:get_player_name()))
  45.     player:hud_set_hotbar_itemcount(hotbar_size)
  46.     minetest.after(0.5,function(hotbar_size)
  47.         player:hud_set_hotbar_selected_image("gui_hotbar_selected.png")
  48.         player:hud_set_hotbar_image("gui_hb_bg_"..hotbar_size..".png")
  49.     end,hotbar_size)
  50. end)
  51.  
  52. minetest.register_chatcommand("hotbar", {
  53.     params = "[size]",
  54.     description = "Sets the size of your hotbar",
  55.     func = function(name, slots)
  56.         local hotbar_size = validate_size(tonumber(slots))
  57.         player_hotbar_settings[name] = hotbar_size
  58.         local player = minetest.get_player_by_name(name)
  59.         player:hud_set_hotbar_itemcount(hotbar_size)
  60.         minetest.chat_send_player(name, "[_] Hotbar size set to " ..hotbar_size.. ".")
  61.         player:hud_set_hotbar_image("gui_hb_bg_"..hotbar_size..".png")
  62.         save_hotbar_settings()
  63.     end,
  64. })
  65.  
Add Comment
Please, Sign In to add comment