mirror of
https://github.com/minetest-mods/digtron.git
synced 2024-10-27 02:39:23 +01:00
change metadata naming standard, add some corrupted data checks
This commit is contained in:
parent
e16035b037
commit
1c732ae1cd
@ -4,6 +4,11 @@ digtron.layout = {}
|
|||||||
digtron.adjacent = {}
|
digtron.adjacent = {}
|
||||||
digtron.bounding_box = {}
|
digtron.bounding_box = {}
|
||||||
|
|
||||||
|
-- Wipes mod_meta
|
||||||
|
--for field, value in pairs(mod_meta:to_table().fields) do
|
||||||
|
-- mod_meta:set_string(field, "")
|
||||||
|
--end
|
||||||
|
|
||||||
------------------------------------------------------------------------------------
|
------------------------------------------------------------------------------------
|
||||||
-- Inventory
|
-- Inventory
|
||||||
|
|
||||||
@ -69,7 +74,7 @@ digtron.retrieve_inventory = function(digtron_id)
|
|||||||
local inv = minetest.get_inventory({type="detached", name=digtron_id})
|
local inv = minetest.get_inventory({type="detached", name=digtron_id})
|
||||||
if inv == nil then
|
if inv == nil then
|
||||||
inv = minetest.create_detached_inventory(digtron_id, detached_inventory_callbacks)
|
inv = minetest.create_detached_inventory(digtron_id, detached_inventory_callbacks)
|
||||||
local inv_string = mod_meta:get_string("inv_"..digtron_id)
|
local inv_string = mod_meta:get_string(digtron_id..":inv")
|
||||||
if inv_string ~= "" then
|
if inv_string ~= "" then
|
||||||
local inventory_table = minetest.deserialize(inv_string)
|
local inventory_table = minetest.deserialize(inv_string)
|
||||||
for listname, invlist in pairs(inventory_table) do
|
for listname, invlist in pairs(inventory_table) do
|
||||||
@ -100,7 +105,7 @@ local persist_inventory = function(digtron_id)
|
|||||||
persist[listname] = inventory
|
persist[listname] = inventory
|
||||||
end
|
end
|
||||||
|
|
||||||
mod_meta:set_string("inv_"..digtron_id, minetest.serialize(persist))
|
mod_meta:set_string(digtron_id..":inv", minetest.serialize(persist))
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(function(dtime)
|
||||||
@ -117,7 +122,7 @@ local create_new_id = function()
|
|||||||
local new_id = last_id + 1
|
local new_id = last_id + 1
|
||||||
mod_meta:set_int("last_id", new_id) -- ensure each call to this method gets a unique number
|
mod_meta:set_int("last_id", new_id) -- ensure each call to this method gets a unique number
|
||||||
|
|
||||||
local digtron_id = "digtron_id_" .. tostring(new_id)
|
local digtron_id = "digtron" .. tostring(new_id)
|
||||||
local inv = minetest.create_detached_inventory(digtron_id, detached_inventory_callbacks)
|
local inv = minetest.create_detached_inventory(digtron_id, detached_inventory_callbacks)
|
||||||
|
|
||||||
return digtron_id, inv
|
return digtron_id, inv
|
||||||
@ -128,22 +133,22 @@ local dispose_id = function(digtron_id)
|
|||||||
minetest.remove_detached_inventory(digtron_id)
|
minetest.remove_detached_inventory(digtron_id)
|
||||||
digtron.layout[digtron_id] = nil
|
digtron.layout[digtron_id] = nil
|
||||||
digtron.adjacent[digtron_id] = nil
|
digtron.adjacent[digtron_id] = nil
|
||||||
mod_meta:set_string("inv_"..digtron_id, "")
|
mod_meta:set_string(digtron_id..":inv", "")
|
||||||
mod_meta:set_string("layout_"..digtron_id, "")
|
mod_meta:set_string(digtron_id..":layout", "")
|
||||||
mod_meta:set_string("adjacent_"..digtron_id, "")
|
mod_meta:set_string(digtron_id..":adjacent", "")
|
||||||
mod_meta:set_string("name_"..digtron_id, "")
|
mod_meta:set_string(digtron_id..":name", "")
|
||||||
mod_meta:set_string("bounding_box_"..digtron_id, "")
|
mod_meta:set_string(digtron_id..":bounding_box", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------------------
|
||||||
-- Name
|
-- Name
|
||||||
|
|
||||||
digtron.get_name = function(digtron_id)
|
digtron.get_name = function(digtron_id)
|
||||||
return mod_meta:get_string("name_"..digtron_id)
|
return mod_meta:get_string(digtron_id..":name")
|
||||||
end
|
end
|
||||||
|
|
||||||
digtron.set_name = function(digtron_id, digtron_name)
|
digtron.set_name = function(digtron_id, digtron_name)
|
||||||
mod_meta:set_string("name_"..digtron_id, digtron_name)
|
mod_meta:set_string(digtron_id..":name", digtron_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------
|
||||||
@ -151,7 +156,7 @@ end
|
|||||||
|
|
||||||
local get_persist_table_function = function(identifier)
|
local get_persist_table_function = function(identifier)
|
||||||
return function(digtron_id, tbl)
|
return function(digtron_id, tbl)
|
||||||
mod_meta:set_string(identifier .."_"..digtron_id, minetest.serialize(tbl))
|
mod_meta:set_string(digtron_id..":"..identifier, minetest.serialize(tbl))
|
||||||
digtron[identifier][digtron_id] = tbl
|
digtron[identifier][digtron_id] = tbl
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -162,7 +167,7 @@ local get_retrieve_table_function = function(identifier)
|
|||||||
if current then
|
if current then
|
||||||
return current
|
return current
|
||||||
end
|
end
|
||||||
local tbl_string = mod_meta:get_string(identifier.."_"..digtron_id)
|
local tbl_string = mod_meta:get_string(digtron_id..":"..identifier)
|
||||||
if tbl_string ~= "" then
|
if tbl_string ~= "" then
|
||||||
current = minetest.deserialize(tbl_string)
|
current = minetest.deserialize(tbl_string)
|
||||||
if current then
|
if current then
|
||||||
@ -359,6 +364,14 @@ digtron.deconstruct = function(digtron_id, root_pos, player_name)
|
|||||||
|
|
||||||
local layout = retrieve_layout(digtron_id)
|
local layout = retrieve_layout(digtron_id)
|
||||||
local inv = digtron.retrieve_inventory(digtron_id)
|
local inv = digtron.retrieve_inventory(digtron_id)
|
||||||
|
|
||||||
|
if not (layout and inv) then
|
||||||
|
minetest.log("error", "Unable to find layout or inventory record for " .. digtron_id
|
||||||
|
.. ", wiping any remaining metadata for this id to prevent corruption. Sorry!")
|
||||||
|
dispose_id(digtron_id)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local root_hash = minetest.hash_node_position(root_pos)
|
local root_hash = minetest.hash_node_position(root_pos)
|
||||||
|
|
||||||
-- Write metadata and inventory to in-world node at this location
|
-- Write metadata and inventory to in-world node at this location
|
||||||
@ -394,6 +407,16 @@ end
|
|||||||
-- Does not destroy its layout info
|
-- Does not destroy its layout info
|
||||||
digtron.remove_from_world = function(digtron_id, root_pos, player_name)
|
digtron.remove_from_world = function(digtron_id, root_pos, player_name)
|
||||||
local layout = retrieve_layout(digtron_id)
|
local layout = retrieve_layout(digtron_id)
|
||||||
|
|
||||||
|
if not layout then
|
||||||
|
minetest.log("error", "Unable to find layout record for " .. digtron_id
|
||||||
|
.. ", wiping any remaining metadata for this id to prevent corruption. Sorry!")
|
||||||
|
local meta = minetest.get_meta(root_pos)
|
||||||
|
meta:set_string("digtron_id", "")
|
||||||
|
dispose_id(digtron_id)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local root_hash = minetest.hash_node_position(root_pos)
|
local root_hash = minetest.hash_node_position(root_pos)
|
||||||
local nodes_to_destroy = {}
|
local nodes_to_destroy = {}
|
||||||
for hash, data in pairs(layout) do
|
for hash, data in pairs(layout) do
|
||||||
@ -451,7 +474,8 @@ end
|
|||||||
|
|
||||||
digtron.can_dig = function(pos, digger)
|
digtron.can_dig = function(pos, digger)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
if meta:get_string("digtron_id") ~= "" then
|
local digtron_id = meta:get_string("digtron_id")
|
||||||
|
if mod_meta:contains(digtron_id..":layout") then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
|
@ -51,9 +51,8 @@ minetest.register_node("digtron:inventory", {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
can_dig = function(pos,player)
|
can_dig = function(pos,player)
|
||||||
local meta = minetest.get_meta(pos)
|
local inv = minetest.get_inventory({type="node", pos=pos})
|
||||||
local inv = meta:get_inventory()
|
return inv:is_empty("main") and digtron.can_dig(pos,player)
|
||||||
return inv:is_empty("main") and meta:get_string("digtron_id") == ""
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
@ -151,9 +150,8 @@ minetest.register_node("digtron:fuelstore", {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
can_dig = function(pos,player)
|
can_dig = function(pos,player)
|
||||||
local meta = minetest.get_meta(pos)
|
local inv = minetest.get_inventory({type="node", pos=pos})
|
||||||
local inv = meta:get_inventory()
|
return inv:is_empty("fuel") and digtron.can_dig(pos,player)
|
||||||
return inv:is_empty("fuel") and meta:get_string("digtron_id") == ""
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
@ -273,9 +271,8 @@ minetest.register_node("digtron:combined_storage", {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
can_dig = function(pos,player)
|
can_dig = function(pos,player)
|
||||||
local meta = minetest.get_meta(pos)
|
local inv = minetest.get_inventory({type="node", pos=pos})
|
||||||
local inv = meta:get_inventory()
|
return inv:is_empty("fuel") and inv:is_empty("main") and digtron.can_dig(pos,player)
|
||||||
return inv:is_empty("fuel") and inv:is_empty("main") and meta:get_string("digtron_id") == ""
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
|
Loading…
Reference in New Issue
Block a user