diff options
author | the lemons <citrons@mondecitronne.com> | 2022-02-13 23:47:21 -0600 |
---|---|---|
committer | the lemons <citrons@mondecitronne.com> | 2022-02-13 23:47:21 -0600 |
commit | 52e774c76269973cfb1fb79e73ba3d28e1db6e11 (patch) | |
tree | 0d41dfded63f91a0e23471f1c622c6e9141b169b | |
parent | 303d905d868c71fa02b4816e264ccb6265763717 (diff) |
allow item collection
-rw-r--r-- | mods/vzxv_inv/init.lua | 26 | ||||
-rw-r--r-- | mods/vzxv_itemstacks/container.lua | 30 | ||||
-rw-r--r-- | mods/vzxv_itemstacks/init.lua | 9 |
3 files changed, 49 insertions, 16 deletions
diff --git a/mods/vzxv_inv/init.lua b/mods/vzxv_inv/init.lua index c5bf6f0..8c7aff4 100644 --- a/mods/vzxv_inv/init.lua +++ b/mods/vzxv_inv/init.lua @@ -94,7 +94,7 @@ end -- item is given to player -- TODO: weight limit function vzxv.give_items(player, stack) - if stack:is_empty() then return 0 end + if stack:is_empty() then return stack end local inv = vzxv.get_inventory(player) local idx = vzxv.inventory_idx(player, stack) if idx then @@ -103,34 +103,35 @@ function vzxv.give_items(player, stack) table.insert(inv, ItemStack(stack)) end vzxv.update_inventory(player, inv) - return stack:get_count() + -- return leftover + return ItemStack "" end -- player actively collects item function vzxv.collect_items(player, stack) if stack:is_empty() then return 0 end - local count = vzxv.give_items(player, stack) + local leftover = vzxv.give_items(player, stack) local idx = vzxv.inventory_idx(player, stack) if idx then vzxv.add_to_hotbar(player, idx) end - return count + return leftover end -- set require_all to require that all that at least the amount of items being -- removed exist in the inventory function vzxv.remove_items(player, stack, require_all) - if stack:is_empty() then return 0 end + if stack:is_empty() then return ItemStack "" end local idx = vzxv.inventory_idx(player, stack) - if not idx then return 0 end + if not idx then return ItemStack "" end local inv = vzxv.get_inventory(player) if require_all and inv[idx]:get_count() < stack:get_count() then - return 0 + return ItemStack "" end local taken = inv[idx]:take_item(stack:get_count()) - vzxv.update_inventory(inv) + vzxv.update_inventory(player, inv) return taken end @@ -149,9 +150,9 @@ function minetest.handle_node_drops(pos, drops, digger) local rejected = {} for _, drop in ipairs(drops) do drop = ItemStack(drop) - local accepted = vzxv.collect_items(digger, drop) - if drop:get_count() - accepted ~= 0 then - table.insert(rejected, drop:peek_item(drop:get_count() - accepted)) + local leftover = vzxv.collect_items(digger, drop) + if not leftover:is_empty() then + table.insert(rejected, leftover) end end -- delay this until after the node has been set to air @@ -185,7 +186,8 @@ local function give_command(player_name, item, count) local stack = ItemStack(item) stack:set_count(count) - local count = vzxv.collect_items(player, stack) + local leftover = vzxv.collect_items(player, stack) + local count = count - leftover:get_count() return true, ("gave %s %s to %s"):format(count, item, player_name) end diff --git a/mods/vzxv_itemstacks/container.lua b/mods/vzxv_itemstacks/container.lua index dca02c8..e7cf9fc 100644 --- a/mods/vzxv_itemstacks/container.lua +++ b/mods/vzxv_itemstacks/container.lua @@ -56,7 +56,6 @@ end function vzxv.get_container_stack(pos) local meta = minetest.get_meta(pos) local stack = meta:get_string("stack") - if stack == "" then return false end return ItemStack(stack) end @@ -101,9 +100,32 @@ function vzxv.container_destruct(pos) remove_container_entity(pos) end --- if the itemstack can be placed in the container, do so and return true. --- otherwise return false. -function vzxv.add_container_items(pos, itemstack) +function vzxv.add_container_items(pos, stack) + local contents = vzxv.get_container_stack(pos) + local leftover = contents:add_item(stack) + vzxv.set_container_stack(pos, contents) + return leftover end +function vzxv.take_container_items(pos, n) + local contents = vzxv.get_container_stack(pos) + local removed = contents:take_item(n) + vzxv.set_container_stack(pos, contents) + return removed +end + +function vzxv.player_take_container_items(pos, _, player) + local items = vzxv.take_container_items(pos, 99) + local leftover = vzxv.collect_items(player, items) + vzxv.add_container_items(pos, leftover) +end + +function vzxv.player_add_container_items(pos, _, player, stack) + stack:set_count(math.min(stack:get_count(), stack:get_stack_max())) + local items = vzxv.remove_items(player, stack) + local leftover = vzxv.add_container_items(pos, items) + vzxv.give_items(player, leftover) + + return leftover +end diff --git a/mods/vzxv_itemstacks/init.lua b/mods/vzxv_itemstacks/init.lua index fcff59c..b87aa10 100644 --- a/mods/vzxv_itemstacks/init.lua +++ b/mods/vzxv_itemstacks/init.lua @@ -18,6 +18,15 @@ vzxv.mundane_block("vzxv_itemstacks:item","Item","apioform",{ {-0.30,-0.5,-0.30,0.30,0.10,0.30}, }, }, + on_destruct = vzxv.container_destruct, + on_rightclick = vzxv.player_add_container_items, + on_punch = function(pos, ...) + local s = vzxv.player_take_container_items(pos, ...) + if vzxv.get_container_stack(pos):is_empty() then + minetest.add_node(pos, {name = "air"}) + end + return s + end, }) -- TODO: handle dropping into nonair block correctly |