diff --git a/api.lua b/api.lua index 72bcfe7..d1b7dcd 100644 --- a/api.lua +++ b/api.lua @@ -1,3 +1,36 @@ +function upgrade_packs.meta_to_inv(player) + local meta = player:get_meta() + local inv = player:get_inventory() + local data = meta:get("upgrade_packs:ugpacks") + + inv:set_size("ugpacks", 4) + if not data then + return -- List was empty or it's a new player + end + + local list = minetest.deserialize(data) + if not list then + -- This should not happen at all + minetest.log("warning", "[upgrade_packs] Failed to deserialize " + .. "player meta of player " .. player:get_player_name()) + else + inv:set_list("ugpacks", list) + end + meta:set_string("upgrade_packs:ugpacks", "") +end + +-- Metadata cannot be accessed directly +-- If this mod is disabled, the inventory list will be unavailable +function upgrade_packs.inv_to_meta(player) + local meta = player:get_meta() + local inv = player:get_inventory() + local list = inv:get_list("ugpacks") + if list and not inv:is_empty("ugpacks") then + meta:set_string("upgrade_packs:ugpacks", minetest.serialize(list)) + end + inv:set_size("ugpacks", 0) +end + function upgrade_packs.add_wear(player, pack, amount) local lookup = upgrade_packs[pack .. "_items"] diff --git a/gui_sfinv.lua b/gui_sfinv.lua index 9b85aeb..2d560d3 100644 --- a/gui_sfinv.lua +++ b/gui_sfinv.lua @@ -3,10 +3,10 @@ sfinv.register_page("upgrade_packs:ugpacks", { get = function(self, player, context) local name = player:get_player_name() return sfinv.make_formspec(player, context, - "label[3,0;Upgrade Packs]" .. - "list[current_player;ugpacks;3,1;2,2;]" .. - "list[current_player;main;0,5;8,4;]" .. - "listring[]" - , false) + "label[3,0.7;Upgrade Packs]" .. + "list[current_player;ugpacks;3,1.5;2,2;]" .. + "listring[current_player;main]" .. + "listring[current_player;ugpacks]" + , true) end }) \ No newline at end of file diff --git a/gui_unified_inventory.lua b/gui_unified_inventory.lua index 995480e..8afcd50 100644 --- a/gui_unified_inventory.lua +++ b/gui_unified_inventory.lua @@ -9,11 +9,12 @@ unified_inventory.register_page("ugpacks", { local y = perplayer_formspec.formspec_y return { formspec = ( - "listcolors[#EEE;#EEE;#111]" .. - "label[3," .. (y + 0.2) .. ";Upgrade Packs]" .. - "list[current_player;ugpacks;3," .. (y + 1) .. ";2,2;]" .. - "listring[current_player;main]" .. - "listring[current_player;ugpacks]" - )} + "no_prepend[]" .. + "listcolors[#888;#AAA;#111]" .. + "label[3," .. (y - 0.1) .. ";Upgrade Packs]" .. + "list[current_player;ugpacks;3," .. (y + 0.7) .. ";2,2;]" .. + "list[current_player;main;0," .. (y + 3.5) .. ";8,4;]" .. + "listring[]" + ), draw_inventory = false} end }) \ No newline at end of file diff --git a/init.lua b/init.lua index b405ee4..62091dd 100644 --- a/init.lua +++ b/init.lua @@ -5,6 +5,7 @@ upgrade_packs.breath_items = {} local modpath = minetest.get_modpath("upgrade_packs") dofile(modpath .. "/api.lua") +dofile(modpath .. "/packs.lua") if minetest.get_modpath("unified_inventory") and not unified_inventory.sfinv_compat_layer then dofile(modpath .. "/gui_unified_inventory.lua") @@ -14,17 +15,6 @@ else dofile(modpath .. "/gui_plain.lua") end -upgrade_packs.register_pack("upgrade_packs:hp_10", "health", { - description = "+10 HP", - strength = 10, - image = "heart.png" -}) - -upgrade_packs.register_pack("upgrade_packs:breath_5", "breath", { - description = "+5 Breath", - strength = 5, - image = "bubble.png" -}) -- Cache items which are interesting for this mod minetest.after(0, function() @@ -49,11 +39,12 @@ end) -- Hacky: Set the hp_max and breath_max value first table.insert(minetest.registered_on_joinplayers, 1, function(player) - local inv = player:get_inventory() - inv:set_size("ugpacks", 4) + upgrade_packs.meta_to_inv(player) upgrade_packs.update_player(player) end) +minetest.register_on_leaveplayer(upgrade_packs.inv_to_meta) + minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user) if hp_change == 0 then return diff --git a/mod.conf b/mod.conf index 345020d..3a607dc 100644 --- a/mod.conf +++ b/mod.conf @@ -1,4 +1,4 @@ name = upgrade_packs description = Provides craftable packs to players to increase their health and breath depends = default -optional_depends = unified_inventory, sfinv \ No newline at end of file +optional_depends = unified_inventory, sfinv, vessels \ No newline at end of file diff --git a/packs.lua b/packs.lua new file mode 100644 index 0000000..1543b71 --- /dev/null +++ b/packs.lua @@ -0,0 +1,43 @@ +upgrade_packs.register_pack("upgrade_packs:hp_10", "health", { + description = "+10 HP", + strength = 10, + image = "heart.png" +}) + +local gb = "vessels:glass_bottle" +local ci = "default:copper_ingot" +minetest.register_craft({ + output = "upgrade_packs:hp_10", + recipe = { + {ci, gb, ci}, + {gb, "", gb}, + {ci, gb, ci} + } +}) + +upgrade_packs.register_pack("upgrade_packs:breath_5", "breath", { + description = "+5 Breath", + strength = 5, + image = "bubble.png" +}) + +local sb = "vessels:steel_bottle" +local ti = "default:tin_ingot" +minetest.register_craft({ + output = "upgrade_packs:breath_5", + recipe = { + {ti, sb, ti}, + {sb, "", sb}, + {ti, sb, ti} + } +}) + +-- Take something else from the player. BLOOD AND AIR +minetest.register_on_craft(function(itemstack, player) + local name = itemstack:get_name() + if name == "upgrade_packs:hp_10" then + player:set_hp(player:get_hp() - 5) + elseif name == "upgrade_packs:breath_5" then + player:set_breath(player:get_breath() - 10) + end +end) \ No newline at end of file