persist stashed items in player meta

fixes #42
master
BuckarooBanzay 2021-10-29 08:19:51 +02:00
parent 905d0ba165
commit 93171d12c4
2 changed files with 14 additions and 5 deletions

View File

@ -45,7 +45,6 @@ minetest.register_node("epic:stash_inv", {
epic = {
on_enter = function(pos, meta, player, ctx)
ctx.data.stashed_items = ctx.data.stashed_items or {}
local filter_map = {}
local inv = meta:get_inventory()
local filter_items = inv:get_list("main")
@ -63,6 +62,7 @@ minetest.register_node("epic:stash_inv", {
local i = 1
local stashed_string = ""
local items_stashed = false
local stashed_items = {}
while i <= player_inv:get_size("main") do
local stack = player_inv:get_stack("main", i)
if not stack:is_empty() and filter_all or filter_map[stack:get_name()] then
@ -70,12 +70,15 @@ minetest.register_node("epic:stash_inv", {
local stack_str = stack:to_string()
stashed_string = stashed_string .. stack_str .. ", "
items_stashed = true
table.insert(ctx.data.stashed_items, stack_str)
table.insert(stashed_items, stack_str)
end
i = i + 1
end
-- save stash in player meta
player:get_meta():set_string("epic_stash", minetest.serialize(stashed_items))
if items_stashed then
minetest.log("action", ("[epic::stash_inventory@%s] %s's inventory has had items stashed: { %s }")
:format(minetest.pos_to_string(pos), player:get_player_name(), stashed_string:sub(1, -3)))

View File

@ -9,18 +9,24 @@ minetest.register_node("epic:unstash_inv", {
epic = {
on_enter = function(pos, _, player, ctx)
ctx.data.stashed_items = ctx.data.stashed_items or {}
-- restore stash from player meta
local player_meta = player:get_meta()
local serialized_stash = player_meta:get_string("epic_stash")
local stashed_items = {}
if serialized_stash and serialized_stash ~= "" then
stashed_items = minetest.deserialize(serialized_stash)
player_meta:set_string("epic_stash", "")
end
local player_inv = player:get_inventory()
local unstashed_string = ""
local items_unstashed = false
for i, itemstr in ipairs(ctx.data.stashed_items) do
for _, itemstr in ipairs(stashed_items) do
local stack = ItemStack(itemstr)
if player_inv:room_for_item("main", stack) then
unstashed_string = unstashed_string..itemstr..", "
items_unstashed = true
player_inv:add_item("main", stack)
ctx.data.stashed_items[i] = nil
end
end
if items_unstashed then