use node inv instead of detached inv

This commit is contained in:
Thomas Rudin 2018-07-26 09:41:10 +02:00
parent 444407a5b6
commit b56b52048c
5 changed files with 90 additions and 91 deletions

View File

@ -25,6 +25,45 @@ minetest.register_node("missions:mission", {
inv:set_size("main", 8)
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:get_stack(listname, index):get_count() == 0 then
-- target inv empty
local step = missions.get_selected_step(pos)
local spec = missions.get_step_spec_by_type(step.type)
if spec.allow_inv_stack_put then
-- delegate to spec check
if spec.allow_inv_stack_put(listname, index, stack) then
return stack:get_count()
else
return 0
end
end
return stack:get_count()
else
-- target inv not empty, disallow swapping
return 0
end
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
--copy items from player to local inv
local inv = player:get_inventory()
inv:add_item("main", stack)
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
--remove items if taken from inv
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_stack(listname, index, ItemStack(""))
return 0
end,
can_dig = missions.only_owner_can_dig,
on_construct = function(pos)

View File

@ -54,6 +54,14 @@ missions.set_steps = function(pos, steps)
meta:set_string("steps", minetest.serialize(steps))
end
missions.get_selected_step = function(pos)
local step = missions.get_steps(pos)
local meta = minetest.get_meta(pos)
local selected_step = meta:get_int("selected_step")
return step[selected_step]
end
-- node register helper
missions.only_owner_can_dig = function(pos, player)

View File

@ -17,9 +17,17 @@ end
local FORMNAME = "mission_block_editstep"
missions.show_step_editor = function(pos, node, player, stepnumber, step, stepdata)
-- clear inv before showing step editor
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
for i=1,inv:get_size("main") do
inv:set_stack("main", i, ItemStack(""))
end
for i,spec in ipairs(missions.steps) do
if spec.type == step.type then
local formspec = spec.edit_formspec(pos, node, player, stepnumber, step, stepdata)
local formspec = spec.edit_formspec(pos, node, player, stepnumber, step, stepdata, inv)
minetest.show_formspec(player:get_player_name(),
FORMNAME .. ";" .. minetest.pos_to_string(pos) .. ";" .. stepnumber .. ";" .. spec.type,
@ -37,6 +45,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
local pos = minetest.string_to_pos(parts[2])
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local node = minetest.get_node(pos)
local stepnumber = tonumber(parts[3])
local spectype = parts[4]
@ -60,7 +70,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
missions.form.missionblock(pos, node, player)
end
spec.update(fields, player, step, stepdata, show_editor, show_mission)
spec.update(fields, player, step, stepdata, show_editor, show_mission, inv)
-- write back data
missions.set_steps(pos, steps)

View File

@ -1,49 +1,7 @@
local get_inv_name = function(player)
return "mission_chestput_" .. player:get_player_name()
end
local get_inv = function(player)
return minetest.get_inventory({type="detached",name=get_inv_name(player)})
end
local hud = {} -- playerName -> {}
local remainingItems = {} -- playerName -> ItemStack
-- setup detached inv
minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name()
local inv = minetest.create_detached_inventory(get_inv_name(player), {
allow_put = function(inv, listname, index, stack, player)
if not inv:is_empty(listname) then
return 0
end
if listname == "target" and stack:get_name() == "missions:wand_chest" then
return stack:get_count()
end
if listname == "main" then
return stack:get_count()
end
return 0
end,
allow_take = function(inv, listname, index, stack, player)
-- remove from det inv
inv:remove_item(listname, stack)
-- give player nothing
return 0
end,
on_put = function(inv, listname, index, stack, player)
-- copy stack
local playerInv = player:get_inventory()
playerInv:add_item("main", stack)
end,
})
inv:set_size("main", 1)
inv:set_size("target", 1)
end)
missions.register_step({
@ -58,7 +16,6 @@ missions.register_step({
local str = remainingItems[player:get_player_name()]
if str then
local stack = ItemStack(str)
--TODO: prettier item name
return "Put " .. stack:get_count() .. " x " .. stack:get_name() .. " into the chest"
else
return ""
@ -92,8 +49,22 @@ missions.register_step({
end,
edit_formspec = function(pos, node, player, stepnumber, step, stepdata)
local inv = get_inv(player)
allow_inv_stack_put = function(listname, index, stack)
-- allow position wand on pos 1 of main inv
if listname == "main" then
if index == 2 and stack:get_name() == "missions:wand_chest" then
return true
end
if index == 1 then
return true
end
end
return false
end,
edit_formspec = function(pos, node, player, stepnumber, step, stepdata, inv)
inv:set_stack("main", 1, ItemStack(stepdata.stack))
local name = ""
@ -121,10 +92,10 @@ missions.register_step({
"label[0,0;Put items in chest]" ..
"label[0,1;Items]" ..
"list[detached:" .. get_inv_name(player) .. ";main;2,1;1,1;]" ..
"list[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";main;2,1;1,1;0]" ..
"label[3,1;Target]" ..
"list[detached:" .. get_inv_name(player) .. ";target;4,1;1,1;]" ..
"list[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";main;4,1;1,1;1]" ..
"label[0,2;" .. name .. "]" ..
@ -136,7 +107,7 @@ missions.register_step({
return formspec;
end,
update = function(fields, player, step, stepdata, show_editor, show_mission)
update = function(fields, player, step, stepdata, show_editor, show_mission, inv)
if fields.togglevisible then
if stepdata.visible == 1 then
@ -149,14 +120,13 @@ missions.register_step({
end
if fields.save then
local inv = get_inv(player)
local stack = inv:get_stack("main", 1)
if not stack:is_empty() then
stepdata.stack = stack:to_string()
end
stack = inv:get_stack("target", 1)
stack = inv:get_stack("main", 2)
if not stack:is_empty() then
local meta = stack:get_meta()

View File

@ -1,39 +1,4 @@
local get_inv_name = function(player)
return "mission_waypoint_" .. player:get_player_name()
end
local get_inv = function(player)
return minetest.get_inventory({type="detached",name=get_inv_name(player)})
end
local hud = {} -- playerName -> {}
-- setup detached inv for wand placement
minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name()
local inv = minetest.create_detached_inventory(get_inv_name(player), {
allow_put = function(inv, listname, index, stack, player)
if stack:get_name() == "missions:wand_position" and inv:is_empty("main") then
return 1
end
return 0
end,
on_put = function(inv, listname, index, stack, player)
-- copy stack
local playerInv = player:get_inventory()
playerInv:add_item("main", stack)
end,
allow_take = function(inv, listname, index, stack, player)
-- remove from det inv
inv:remove_item("main", stack)
-- give player nothing
return 0
end
})
inv:set_size("main", 1)
end)
missions.register_step({
@ -60,6 +25,15 @@ missions.register_step({
end
end,
allow_inv_stack_put = function(listname, index, stack)
-- allow position wand on pos 1 of main inv
if listname == "main" and index == 1 and stack:get_name() == "missions:wand_position" then
return true
end
return false
end,
edit_formspec = function(pos, node, player, stepnumber, step, stepdata)
local name = ""
@ -86,9 +60,8 @@ missions.register_step({
local formspec = "size[8,8;]" ..
"label[0,0;Walk to (Step #" .. stepnumber .. ")]" ..
"list[detached:" .. get_inv_name(player) .. ";main;0,1;1,1;]" ..
"list[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";main;0,1;1,1;0]" ..
--TODO: escape
"label[0,2;" .. name .. "]" ..
"field[0,3;8,1;description;Description;" .. stepdata.description .. "]" ..
@ -102,7 +75,7 @@ missions.register_step({
return formspec;
end,
update = function(fields, player, step, stepdata, show_editor, show_mission)
update = function(fields, player, step, stepdata, show_editor, show_mission, inv)
if fields.radius then
local radius = tonumber(fields.radius)
@ -126,7 +99,6 @@ missions.register_step({
end
if fields.save then
local inv = get_inv(player)
local stack = inv:get_stack("main", 1)
if not stack:is_empty() then