Bones: Iterate player inventory lists dynamically (#1229)
authorTim <t4im@users.noreply.github.com>
Tue, 20 Feb 2018 18:15:03 +0000 (19:15 +0100)
committerSmallJoker <SmallJoker@users.noreply.github.com>
Tue, 20 Feb 2018 18:15:03 +0000 (19:15 +0100)
Avoid hard-coded player inventory lists.
Expose `bones.player_inventory_lists` for mods to look up or change,
which player inventory lists are being dropped or placed into bones.

game_api.txt
mods/bones/init.lua

index f7bcc1ebf0fbfb6066ec0e784c87892271a9c47d..bf342674ad517f1138c1141369d3bdf1611093d2 100644 (file)
@@ -67,6 +67,15 @@ Beds API
                }
        }
 
+Bones API
+---------
+
+An ordered list of listnames (default: "main", "craft") of the player inventory,
+that will be placed into bones or dropped on player death can be looked up or changed
+in `bones.player_inventory_lists`.
+
+e.g. `table.insert(bones.player_inventory_lists, "backpack")`
+
 Creative API
 ------------
 
index 8688fa1ef92dc3cf60aa1423de45e3b1697e4eb1..429451822e5f2a5b627a56a942ca2bd76e65a56f 100644 (file)
@@ -1,6 +1,8 @@
 -- Minetest 0.4 mod: bones
 -- See README.txt for licensing and other information.
 
+bones = {}
+
 local function is_owner(pos, name)
        local owner = minetest.get_meta(pos):get_string("owner")
        if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then
@@ -165,6 +167,18 @@ local drop = function(pos, itemstack)
        end
 end
 
+local player_inventory_lists = { "main", "craft" }
+bones.player_inventory_lists = player_inventory_lists
+
+local function is_all_empty(player_inv)
+       for _, list_name in ipairs(player_inventory_lists) do
+               if not player_inv:is_empty(list_name) then
+                       return false
+               end
+       end
+       return true
+end
+
 minetest.register_on_dieplayer(function(player)
 
        local bones_mode = minetest.settings:get("bones_mode") or "bones"
@@ -179,8 +193,7 @@ minetest.register_on_dieplayer(function(player)
        end
 
        local player_inv = player:get_inventory()
-       if player_inv:is_empty("main") and
-               player_inv:is_empty("craft") then
+       if is_all_empty(player_inv) then
                return
        end
 
@@ -198,19 +211,12 @@ minetest.register_on_dieplayer(function(player)
        end
 
        if bones_mode == "drop" then
-
-               -- drop inventory items
-               for i = 1, player_inv:get_size("main") do
-                       drop(pos, player_inv:get_stack("main", i))
-               end
-               player_inv:set_list("main", {})
-
-               -- drop crafting grid items
-               for i = 1, player_inv:get_size("craft") do
-                       drop(pos, player_inv:get_stack("craft", i))
+               for _, list_name in ipairs(player_inventory_lists) do
+                       for i = 1, player_inv:get_size(list_name) do
+                               drop(pos, player_inv:get_stack(list_name, i))
+                       end
+                       player_inv:set_list(list_name, {})
                end
-               player_inv:set_list("craft", {})
-
                drop(pos, ItemStack("bones:bones"))
                return
        end
@@ -221,21 +227,19 @@ minetest.register_on_dieplayer(function(player)
        local meta = minetest.get_meta(pos)
        local inv = meta:get_inventory()
        inv:set_size("main", 8 * 4)
-       inv:set_list("main", player_inv:get_list("main"))
 
-       for i = 1, player_inv:get_size("craft") do
-               local stack = player_inv:get_stack("craft", i)
-               if inv:room_for_item("main", stack) then
-                       inv:add_item("main", stack)
-               else
-                       --drop if no space left
-                       drop(pos, stack)
+       for _, list_name in ipairs(player_inventory_lists) do
+               for i = 1, player_inv:get_size(list_name) do
+                       local stack = player_inv:get_stack(list_name, i)
+                       if inv:room_for_item("main", stack) then
+                               inv:add_item("main", stack)
+                       else -- no space left
+                               drop(pos, stack)
+                       end
                end
+               player_inv:set_list(list_name, {})
        end
 
-       player_inv:set_list("main", {})
-       player_inv:set_list("craft", {})
-
        meta:set_string("formspec", bones_formspec)
        meta:set_string("owner", player_name)