Fix issue #55 (tubelib_addons2:doorblock works incorrectly)

This commit is contained in:
Joachim Stolberg 2020-09-24 20:19:10 +02:00
parent 677c1f4148
commit 51fdab3c8a
3 changed files with 61 additions and 55 deletions

@ -64,13 +64,16 @@ local Version = minetest.deserialize(storage:get_string("Version")) or 1
local Number2Pos
local Number2Data
local TemporaryRemovedNodes
if Version == 2 then -- legacy data base
Number2Pos = minetest.deserialize(storage:get_string("Number2Pos")) or {}
Number2Data = extract_data(Number2Pos)
TemporaryRemovedNodes = {}
Version = 3
else
Number2Pos = deserialize(storage:get_string("Number2Pos"))
Number2Data = minetest.deserialize(storage:get_string("Number2Data")) or {}
TemporaryRemovedNodes = minetest.deserialize(storage:get_string("TemporaryRemovedNodes")) or {}
end
@ -82,6 +85,7 @@ local function update_mod_storage()
storage:set_string("Version", minetest.serialize(Version))
storage:set_string("Number2Pos", serialize(Number2Pos))
storage:set_string("Number2Data", minetest.serialize(Number2Data))
storage:set_string("TemporaryRemovedNodes", minetest.serialize(TemporaryRemovedNodes))
-- store data each hour
minetest.after(60*59, update_mod_storage)
t = minetest.get_us_time() - t
@ -276,7 +280,8 @@ end
-- param name: name of the data (string)
-- param data: any data (number, string, table)
function tubelib.set_data(number, name, data)
if Number2Data[number] and type(name) == "string" then
Number2Data[number] = Number2Data[number] or {}
if type(name) == "string" then
Number2Data[number]["u_"..name] = data
end
end
@ -591,6 +596,22 @@ function tubelib.get_inv_state(meta, listname)
return state
end
-- Mainly used for door/gate nodes
-- To delete an entry, provide nil as number. The stored data will be returned.
function tubelib.temporary_remove_node(pos, number, name, add_data)
local key = get_key_str(pos)
if number then
add_data = add_data or {}
add_data.pos = pos
add_data.number = number
add_data.name = name
TemporaryRemovedNodes[key] = add_data
else
local data = table.copy(TemporaryRemovedNodes[key])
TemporaryRemovedNodes[key] = nil
return data
end
end
-------------------------------------------------------------------------------
-- Data Maintenance
@ -622,41 +643,6 @@ local function get_node_number(pos)
return 0
end
local NodesWithoutNumber = {
["tubelib_addons2:doorblock1"] = true,
["tubelib_addons2:doorblock2"] = true,
["tubelib_addons2:doorblock3"] = true,
["tubelib_addons2:doorblock4"] = true,
["tubelib_addons2:doorblock5"] = true,
["tubelib_addons2:doorblock6"] = true,
["tubelib_addons2:doorblock7"] = true,
["tubelib_addons2:doorblock8"] = true,
["tubelib_addons2:doorblock9"] = true,
["tubelib_addons2:doorblock10"] = true,
["tubelib_addons2:doorblock11"] = true,
["tubelib_addons2:doorblock12"] = true,
["tubelib_addons2:doorblock13"] = true,
["tubelib_addons2:doorblock14"] = true,
["tubelib_addons2:gateblock1"] = true,
["tubelib_addons2:gateblock2"] = true,
["tubelib_addons2:gateblock3"] = true,
["tubelib_addons2:gateblock4"] = true,
["tubelib_addons2:gateblock5"] = true,
["tubelib_addons2:gateblock6"] = true,
["tubelib_addons2:gateblock7"] = true,
["tubelib_addons2:gateblock8"] = true,
["tubelib_addons2:gateblock9"] = true,
["tubelib_addons2:gateblock10"] = true,
["tubelib_addons2:gateblock11"] = true,
["tubelib_addons2:gateblock12"] = true,
["tubelib_addons2:gateblock13"] = true,
["tubelib_addons2:gateblock14"] = true,
["tubelib_addons2:gateblock15"] = true,
["tubelib_addons2:gateblock16"] = true,
["tubelib_addons2:gateblock17"] = true,
["tubelib_addons2:gateblock18"] = true,
}
local function data_maintenance()
minetest.log("info", "[Tubelib] Data maintenance started")
@ -670,14 +656,28 @@ local function data_maintenance()
cnt1 = cnt1 + 1
-- Is there a tubelib node?
if tubelib_NodeDef[name] then
local nnum = get_node_number(item.pos)
-- Does the number match?
if nnum == num or NodesWithoutNumber[name] then
local nnum = get_node_number(item.pos)
if nnum == num then
cnt2 = cnt2 + 1
-- Store again
Number2Pos[num] = item
-- Add node names which are not stored as file
Number2Pos[num].name = name
--print("added", num, name)
else
--print("wrong number", num, name)
end
else
local key = get_key_str(item.pos)
local data = TemporaryRemovedNodes[key]
if data then
cnt2 = cnt2 + 1
-- Store again
Number2Pos[data.number] = data
--print("restored", data.number, data.name)
else
--print("no data", num)
end
end
end

@ -52,8 +52,7 @@ for idx,pgn in ipairs(tPgns) do
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local number = tubelib.add_node(pos, "tubelib_addons2:doorblock"..idx)
tubelib.set_data(number, "facedir", node.param2)
local number = tubelib.add_node(pos, node.name)
meta:set_string("number", number)
meta:set_string("infotext", "Tubelib Door Block "..number)
meta:set_string("formspec", "size[3,2]"..
@ -65,11 +64,10 @@ for idx,pgn in ipairs(tPgns) do
on_receive_fields = function(pos, formname, fields, player)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local number = meta:get_string("number")
if fields.type then
node.name = "tubelib_addons2:doorblock"..tTextures[fields.type]
minetest.swap_node(pos, node)
tubelib.set_data(number, "texture", node.name)
tubelib.add_node(pos, node.name)
end
if fields.exit then
meta:set_string("formspec", nil)
@ -96,14 +94,16 @@ for idx,pgn in ipairs(tPgns) do
on_recv_message = function(pos, topic, payload)
local node = minetest.get_node(pos)
if topic == "on" then
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
minetest.remove_node(pos)
tubelib.temporary_remove_node(pos, number, node.name, {param2 = node.param2})
elseif topic == "off" then
local num = tubelib.get_node_number(pos)
local name = tubelib.get_data(num, "texture") or "tubelib_addons2:doorblock1"
if name then
minetest.add_node(pos, {name=name,
paramtype2="facedir",
param2=tubelib.get_data(num, "facedir")})
local data = tubelib.temporary_remove_node(pos)
if data then
minetest.add_node(pos, {name = data.name, param2 = data.param2})
local meta = minetest.get_meta(pos)
meta:set_string("number", data.number)
end
end
end,

@ -34,6 +34,7 @@ local tPgns = {"default_wood.png", "default_aspen_wood.png", "default_junglewood
"default_copper_block.png", "default_steel_block.png", "default_tin_block.png", "default_coral_skeleton.png",
"default_glass.png", "default_obsidian_glass.png", "default_ice.png", "tubelib_addon2_gate.png"}
local not_in_inventory=nil
for idx,pgn in ipairs(tPgns) do
minetest.register_node("tubelib_addons2:gateblock"..idx, {
description = "Tubelib Gate Block",
@ -41,7 +42,8 @@ for idx,pgn in ipairs(tPgns) do
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local number = tubelib.add_node(pos, "tubelib_addons2:gateblock"..idx)
local number = tubelib.add_node(pos, node.name)
meta:set_string("number", number)
meta:set_string("infotext", "Tubelib Gate Block "..number)
meta:set_string("formspec", "size[3,2]"..
"label[0,0;Select texture]"..
@ -62,7 +64,7 @@ for idx,pgn in ipairs(tPgns) do
end
end,
after_dig_node = function(pos)
after_dig_node = function(pos, oldnode, oldmetadata)
tubelib.remove_node(pos)
end,
@ -71,7 +73,7 @@ for idx,pgn in ipairs(tPgns) do
paramtype2 = "facedir",
sunlight_propagates = true,
sounds = default.node_sound_stone_defaults(),
groups = {cracky=2, choppy=2, crumbly=2, not_in_creative_inventory = idx==NUM_TEXTURES and 0 or 1},
groups = {cracky=2, choppy=2, crumbly=2, not_in_creative_inventory=not_in_inventory},
is_ground_content = false,
drop = "tubelib_addons2:gateblock1",
})
@ -82,12 +84,16 @@ for idx,pgn in ipairs(tPgns) do
on_recv_message = function(pos, topic, payload)
local node = minetest.get_node(pos)
if topic == "on" then
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
minetest.remove_node(pos)
tubelib.temporary_remove_node(pos, number, node.name, {param2 = node.param2})
elseif topic == "off" then
local num = tubelib.get_node_number(pos)
local info = tubelib.get_node_info(num)
if info then
minetest.add_node(pos, {name=info.name})
local data = tubelib.temporary_remove_node(pos)
if data then
minetest.add_node(pos, {name = data.name, param2 = data.param2})
local meta = minetest.get_meta(pos)
meta:set_string("number", data.number)
end
end
end,