Fix workbench and drops

This commit is contained in:
stujones11 2019-05-26 17:56:22 +01:00
parent 43625aafee
commit 508c998590
2 changed files with 37 additions and 15 deletions

View File

@ -2,9 +2,20 @@ local age = 0.5 --How old an item has to be before collecting
local radius_magnet = 2 --Radius of item magnet local radius_magnet = 2 --Radius of item magnet
local player_collect_height = 1.3 --Added to their pos y value local player_collect_height = 1.3 --Added to their pos y value
local function valid_pos()
if pos then
for _, v in pairs({"x", "y", "z"}) do
if not pos[v] or pos[v] < -32000 or pos[v] > 32000 then
return
end
end
return true
end
end
local function collect_items(player) local function collect_items(player)
local pos = player:get_pos() local pos = player:get_pos()
if not pos then if not valid_pos(pos) then
return return
end end
local col_pos = vector.add(pos, {x=0, y=player_collect_height, z=0}) local col_pos = vector.add(pos, {x=0, y=player_collect_height, z=0})

View File

@ -166,11 +166,34 @@ function workbench.construct(pos)
workbench:set_formspec(meta, 1) workbench:set_formspec(meta, 1)
end end
function workbench.fields(pos, _, fields) function workbench.fields(pos, _, fields, sender)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if fields.back then workbench:set_formspec(meta, 1) if fields.back then workbench:set_formspec(meta, 1)
elseif fields.creating then workbench:set_formspec(meta, 2) elseif fields.creating then workbench:set_formspec(meta, 2)
elseif fields.anvil then workbench:set_formspec(meta, 3) end elseif fields.anvil then workbench:set_formspec(meta, 3)
elseif fields.quit and pos and sender then
local inv = sender:get_inventory()
if inv then
for i, stack in ipairs(inv:get_list("craft")) do
minetest.item_drop(stack, nil, pos)
stack:clear()
inv:set_stack("craft", i, stack)
end
end
inv = meta:get_inventory()
if inv then
for _, name in pairs({"input", "tool", "hammer"}) do
local stack = inv:get_stack(name, 1)
minetest.item_drop(stack, nil, pos)
stack:clear()
inv:set_stack(name, 1, stack)
end
for i, stack in ipairs(inv:get_list("forms")) do
stack:clear()
inv:set_stack("forms", i, stack)
end
end
end
end end
function workbench.dig(pos) function workbench.dig(pos)
@ -270,18 +293,6 @@ minetest.register_node("workbench:workbench", {
on_metadata_inventory_take = workbench.on_take, on_metadata_inventory_take = workbench.on_take,
allow_metadata_inventory_put = workbench.put, allow_metadata_inventory_put = workbench.put,
allow_metadata_inventory_move = workbench.move, allow_metadata_inventory_move = workbench.move,
on_receive_fields = function(pos, formname, fields, sender)
if fields.quit and pos and sender then
local inv = sender:get_inventory()
if inv then
for i, stack in ipairs(inv:get_list("craft")) do
minetest.item_drop(stack, nil, pos)
stack:clear()
inv:set_stack("craft", i, stack)
end
end
end
end,
}) })
minetest.register_tool("workbench:hammer", { minetest.register_tool("workbench:hammer", {