diff options
author | 4Evergreen4 <everett@mcjohnso.com> | 2016-01-12 15:14:33 -0500 |
---|---|---|
committer | 4Evergreen4 <everett@mcjohnso.com> | 2016-01-12 15:14:33 -0500 |
commit | b8555fb71dcfea26a4d18cf23af8b248df086072 (patch) | |
tree | af84a1bff1cb875b70beb93596a4fd3589df87c5 | |
parent | b8df65405fb4bfc8dc32eec45c22a65f0af663f4 (diff) |
Add items to trash can when thrown instead0.2.0
-rw-r--r-- | init.lua | 70 |
1 files changed, 43 insertions, 27 deletions
@@ -13,9 +13,6 @@ local function checkwall(pos) local second_node_x = pos.x + fdir_to_front[fdir + 1].x local second_node_z = pos.z + fdir_to_front[fdir + 1].z local second_node_pos = {x=second_node_x, y=pos.y, z=second_node_z} - print("x: "..second_node_pos.x) - print("y: "..second_node_pos.y) - print("z: "..second_node_pos.z) local second_node = minetest.get_node(second_node_pos) if not second_node or not minetest.registered_nodes[second_node.name] or not minetest.registered_nodes[second_node.name].buildable_to then @@ -91,40 +88,43 @@ minetest.register_node("trash_can:trash_can_wooden",{ on_construct = function(pos) local meta = minetest.get_meta(pos) meta:set_string("formspec", - "size[8,9]".. - "button[0,0;2,1;empty;Empty Trash]".. - "list[current_name;main;3,1;2,3;]".. + "size[8,9]" .. + "button[0,0;2,1;empty;Empty Trash]" .. + "list[context;trashlist;3,1;2,3;]" .. "list[current_player;main;0,5;8,4;]" ) meta:set_string("infotext", "Trash Can") local inv = meta:get_inventory() inv:set_size("main", 8*4) + inv:set_size("trashlist", 2*3) end, can_dig = function(pos,player) local meta = minetest.get_meta(pos); local inv = meta:get_inventory() return inv:is_empty("main") - end, - on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) - minetest.log("action", player:get_player_name().. - " moves stuff in trash can at "..minetest.pos_to_string(pos)) + end, + on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) + minetest.log("action", player:get_player_name() .. + " moves stuff in trash can at " .. minetest.pos_to_string(pos)) end, on_metadata_inventory_put = function(pos, listname, index, stack, player) - minetest.log("action", player:get_player_name().. - " moves stuff to trash can at "..minetest.pos_to_string(pos)) + minetest.log("action", player:get_player_name() .. + " moves stuff to trash can at " .. minetest.pos_to_string(pos)) end, on_metadata_inventory_take = function(pos, listname, index, stack, player) - minetest.log("action", player:get_player_name().. - " takes stuff from trash can at "..minetest.pos_to_string(pos)) + minetest.log("action", player:get_player_name() .. + " takes stuff from trash can at " .. minetest.pos_to_string(pos)) end, on_receive_fields = function(pos, formname, fields, sender) if fields.empty then local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - for i = 1, inv:get_size("main") do - inv:set_stack("main", i, nil) + for i = 1, inv:get_size("trashlist") do + inv:set_stack("trashlist", i, nil) end minetest.sound_play("trash", {to_player=sender:get_player_name(), gain = 1.0}) + minetest.log("action", sender:get_player_name() .. + " empties trash can at " .. minetest.pos_to_string(pos)) end end, }) @@ -162,9 +162,9 @@ minetest.register_node("trash_can:dumpster", { on_construct = function(pos) local meta = minetest.get_meta(pos) meta:set_string("formspec", - "size[8,9]".. - "button[0,0;2,1;empty;Empty Trash]".. - "list[current_name;main;1,1;6,3;]".. + "size[8,9]" .. + "button[0,0;2,1;empty;Empty Trash]" .. + "list[context;main;1,1;6,3;]" .. "list[current_player;main;0,5;8,4;]" ) meta:set_string("infotext", "Dumpster") @@ -183,16 +183,16 @@ minetest.register_node("trash_can:dumpster", { return inv:is_empty("main") end, on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) - minetest.log("action", player:get_player_name().. - " moves stuff in dumpster at "..minetest.pos_to_string(pos)) + minetest.log("action", player:get_player_name() .. + " moves stuff in dumpster at " .. minetest.pos_to_string(pos)) end, on_metadata_inventory_put = function(pos, listname, index, stack, player) - minetest.log("action", player:get_player_name().. - " moves stuff to dumpster at "..minetest.pos_to_string(pos)) + minetest.log("action", player:get_player_name() .. + " moves stuff to dumpster at " .. minetest.pos_to_string(pos)) end, on_metadata_inventory_take = function(pos, listname, index, stack, player) - minetest.log("action", player:get_player_name().. - " takes stuff from dumpster at "..minetest.pos_to_string(pos)) + minetest.log("action", player:get_player_name() .. + " takes stuff from dumpster at " .. minetest.pos_to_string(pos)) end, on_receive_fields = function(pos, formname, fields, sender) if fields.empty then @@ -237,8 +237,24 @@ minetest.register_craft({ -- Remove any items thrown in trash can. local old_on_step = minetest.registered_entities["__builtin:item"].on_step minetest.registered_entities["__builtin:item"].on_step = function(self, dtime) - if minetest.get_node(self.object:getpos()).name == "trash_can:trash_can_wooden" then - self.object:remove() + local item_pos = self.object:getpos() + -- Round the values. Not essential, but makes logging look nicer. + for key, value in pairs(item_pos) do item_pos[key] = math.floor(value + 0.5) end + if minetest.get_node(item_pos).name == "trash_can:trash_can_wooden" then + local item_stack = ItemStack(self.itemstring) + local inv = minetest.get_inventory({type="node", pos=item_pos}) + local leftover = inv:add_item("trashlist", item_stack) + if leftover:get_count() == 0 then + self.object:remove() + minetest.log("action", item_stack:to_string() .. + " added to trash can at " .. minetest.pos_to_string(item_pos)) + elseif item_stack:get_count() - leftover:get_count() ~= 0 then + self.set_item(self, leftover:to_string()) + minetest.log("action", item_stack:to_string() .. + " added to trash can at " .. minetest.pos_to_string(item_pos) .. + " with " .. leftover:to_string() .. " left over" + ) + end return end old_on_step(self, dtime) |