1
0
Fork 0

Create inv only when actually used (and remove after)

This commit is contained in:
cora 2022-09-25 22:20:05 +02:00
parent e56c589a14
commit a3466a1796
1 changed files with 34 additions and 7 deletions

View File

@ -33,8 +33,25 @@ local function load_inv(ent,size)
return inv return inv
end end
local function save_inv(ent)
if ent._inv then
ent._items = {}
for i,it in ipairs(ent._inv:get_list("main")) do
ent._items[i] = it:to_string()
end
end
minetest.remove_detached_inventory(ent._inv_id)
end
local open_invs = {}
local function show_form(ent,player,show_name,size) local function show_form(ent,player,show_name,size)
if not ent._inv_id then return end if not ent._inv_id then return end
ent._inv = load_inv(ent,size)
if not open_invs[ent] then
open_invs[ent] = {}
end
table.insert(open_invs[ent],player:get_player_name())
local playername = player:get_player_name() local playername = player:get_player_name()
local rows = 3 local rows = 3
local cols = (math.ceil(size/rows)) local cols = (math.ceil(size/rows))
@ -57,12 +74,23 @@ end
local function drop_inv(ent) local function drop_inv(ent)
local pos = ent.object:get_pos() local pos = ent.object:get_pos()
for i,it in pairs(ent._inv:get_list("main")) do for i,it in pairs(ent._items) do
local p = vector.add(pos,vector.new(math.random() - 0.5, math.random()-0.5, math.random()-0.5)) local p = vector.add(pos,vector.new(math.random() - 0.5, math.random()-0.5, math.random()-0.5))
minetest.add_item(p,it:to_string()) minetest.add_item(p,it)
end end
end end
minetest.register_on_player_receive_fields(function(player, formname, fields)
for k,v in pairs(open_invs) do
if formname == k._inv_id then
if #v < 2 then
save_inv(k)
open_invs[k] = nil
end
end
end
end)
function mcl_entity_invs.register_inv(entity_name,show_name,size) function mcl_entity_invs.register_inv(entity_name,show_name,size)
assert(minetest.registered_entities[entity_name],"mcl_entity_invs.register_inv called with invalid entity: "..tostring(entity_name)) assert(minetest.registered_entities[entity_name],"mcl_entity_invs.register_inv called with invalid entity: "..tostring(entity_name))
local old_oa = minetest.registered_entities[entity_name].on_activate local old_oa = minetest.registered_entities[entity_name].on_activate
@ -77,9 +105,6 @@ function mcl_entity_invs.register_inv(entity_name,show_name,size)
self._inv_id="entity_inv_"..minetest.sha1(minetest.get_gametime()..minetest.pos_to_string(self.object:get_pos())..tostring(math.random())) self._inv_id="entity_inv_"..minetest.sha1(minetest.get_gametime()..minetest.pos_to_string(self.object:get_pos())..tostring(math.random()))
--gametime and position for collision safety and math.random salt to protect against position brute-force --gametime and position for collision safety and math.random salt to protect against position brute-force
end end
if self._inv_id then
self._inv = load_inv(self,size)
end
return r return r
end end
@ -95,8 +120,10 @@ function mcl_entity_invs.register_inv(entity_name,show_name,size)
assert(type(d) == "table","mcl_entity_invs currently only works with entities that return a (serialized) table in get_staticdata. "..tostring(self.name).." returned: "..tostring(old_sd)) assert(type(d) == "table","mcl_entity_invs currently only works with entities that return a (serialized) table in get_staticdata. "..tostring(self.name).." returned: "..tostring(old_sd))
d._inv_id = self._inv_id d._inv_id = self._inv_id
d._items = {} d._items = {}
for i,it in pairs(self._inv:get_list("main")) do if self._items then
d._items[i] = it:to_string() for i,it in ipairs(self._items) do
d._items[i] = it
end
end end
return minetest.serialize(d) return minetest.serialize(d)
end end