Preserve items & materials when breaking a cartography table

This commit is contained in:
Hugues Ross 2024-10-06 10:21:33 -04:00
parent 54c7fb5123
commit 664a61bd7f
2 changed files with 65 additions and 9 deletions

View File

@ -280,6 +280,9 @@ return {
-- The texture of the height toggle button when inactive
flat_button_texture = "cartographer_flat_button",
-- The color to use for less important details in long item descriptions
minor_desc_color = "#bbbbbb",
player_inv_columns = 8,
player_inv_rows = 4,
}

View File

@ -847,16 +847,25 @@ end)
-- Called after a table is placed. Sets up the table's inventory and metadata.
--
-- pos: The node's position
local function setup_table_node(pos)
local meta = minetest.get_meta(pos)
meta:get_inventory():set_size("input", 1)
meta:get_inventory():set_size("output", 1)
meta:get_inventory():set_size("copy_input", 1)
meta:get_inventory():set_size("copy_output", 1)
-- stack: The item stack being placed
local function setup_table_node(pos, _, stack, _)
local meta = stack:get_meta():to_table();
local node_meta = minetest.get_meta(pos)
if meta then
node_meta:from_table(meta);
else
node_meta:set_int("size", settings.default_size)
node_meta:set_int("scale", SCALE_SMALL)
node_meta:set_int("detail", 0)
end
meta:set_int("size", settings.default_size)
meta:set_int("scale", SCALE_SMALL)
meta:set_int("detail", 0)
-- Clear the item description if we have one; we don't want to carry it over
node_meta:set_string("description", "")
node_meta:get_inventory():set_size("input", 1)
node_meta:get_inventory():set_size("output", 1)
node_meta:get_inventory():set_size("copy_input", 1)
node_meta:get_inventory():set_size("copy_output", 1)
end
-- Called when the player tries to put an item into one of the table's
@ -910,6 +919,47 @@ local function table_on_items_changed(pos, listname, _, _, _)
end
end
-- Called when a table is being broken by the player, in order to preserve its metadata
--
-- pos: The node's position
-- oldnode: The name of the old node
-- oldmeta: The metadata table corresponding to the old node
-- drops: The list of items to drop from breaking the table
local function table_on_break(pos, oldnode, oldmeta, drops)
for _,item in ipairs(drops) do
if item:get_name() == oldnode.name then
item:get_meta():from_table({fields=oldmeta});
if oldmeta.paper + oldmeta.pigment > 0 then
item:get_meta():set_string("description", item:get_short_description()
.. minetest.colorize(gui_skin.minor_desc_color, string.format("\nPaper: %d", oldmeta.paper))
.. minetest.colorize(gui_skin.minor_desc_color, string.format("\nPigment: %d", oldmeta.pigment))
)
end
end
end
local node_meta = minetest.get_meta(pos)
local old_inventory = node_meta:get_inventory()
local input_item = old_inventory:get_stack("input", 1)
if input_item then
table.insert(drops, input_item)
end
local output_item = old_inventory:get_stack("output", 1)
if output_item then
table.insert(drops, output_item)
end
local copy_input_item = old_inventory:get_stack("copy_input", 1)
if copy_input_item then
table.insert(drops, copy_input_item)
end
local copy_output_item = old_inventory:get_stack("copy_output", 1)
if copy_output_item then
table.insert(drops, copy_output_item)
end
-- TODO: Drop the maps
end
-- The table node definitions
minetest.register_node("cartographer:simple_table", {
description = "Shabby Cartographer's Table",
@ -949,6 +999,7 @@ minetest.register_node("cartographer:simple_table", {
allow_metadata_inventory_put = table_can_put,
on_metadata_inventory_put = table_on_items_changed,
on_metadata_inventory_take = table_on_items_changed,
preserve_metadata = table_on_break,
})
minetest.register_node("cartographer:standard_table", {
@ -989,6 +1040,7 @@ minetest.register_node("cartographer:standard_table", {
allow_metadata_inventory_put = table_can_put,
on_metadata_inventory_put = table_on_items_changed,
on_metadata_inventory_take = table_on_items_changed,
preserve_metadata = table_on_break,
})
minetest.register_node("cartographer:advanced_table", {
@ -1029,4 +1081,5 @@ minetest.register_node("cartographer:advanced_table", {
allow_metadata_inventory_put = table_can_put,
on_metadata_inventory_put = table_on_items_changed,
on_metadata_inventory_take = table_on_items_changed,
preserve_metadata = table_on_break,
})