From 05a831d643f1dab5129b8a225dbc9ddef115bc62 Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Sun, 19 Apr 2020 13:50:54 -0400 Subject: [PATCH] Add basic validation for moving items into the table's slots --- table.lua | 61 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/table.lua b/table.lua index 2c8fab4..0b993c4 100644 --- a/table.lua +++ b/table.lua @@ -459,16 +459,52 @@ minetest.register_on_player_receive_fields(function(player, name, fields) end 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); + 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); - meta:set_int("size", SIZE_SMALL); - meta:set_int("scale", SCALE_SMALL); - meta:set_int("detail", 0); + meta:set_int("size", SIZE_SMALL); + meta:set_int("scale", SCALE_SMALL); + meta:set_int("detail", 0); +end + +-- Called when the player tries to put an item into one of the table's +-- inventories. +-- listname: The name of the inventory the item is being placed in. +-- stack: The itemstack +-- +-- Returns 0 if the place is invalid; otherwise, returns the number of items +-- that can be placed. +local function table_can_put(_, listname, _, stack, _) + if listname == "copy_output" then + return 0; + end + + if stack:get_name() ~= "cartographer:map" and (listname == "output" or listname == "copy_input") then + return 0; + end + + return stack:get_count(); +end + +-- Called when the player tries to move an item between two of the table's +-- inventories. +-- to_list: The name of the inventory the item is being placed in. +-- count: The number of items being moved +-- +-- Returns 0 if the move is invalid; otherwise, returns the number of items +-- that can be moved. +local function table_can_move(_, _, _, to_list, _, count, _) + if to_list == "copy_output" then + return 0; + end + + return count; end minetest.register_node("cartographer:simple_table", { @@ -501,6 +537,9 @@ minetest.register_node("cartographer:simple_table", { end, after_place_node = setup_table_node, + + allow_metadata_inventory_move = table_can_move, + allow_metadata_inventory_put = table_can_put, }); minetest.register_node("cartographer:standard_table", { @@ -533,6 +572,9 @@ minetest.register_node("cartographer:standard_table", { end, after_place_node = setup_table_node, + + allow_metadata_inventory_move = table_can_move, + allow_metadata_inventory_put = table_can_put, }); minetest.register_node("cartographer:advanced_table", { @@ -565,6 +607,9 @@ minetest.register_node("cartographer:advanced_table", { end, after_place_node = setup_table_node, + + allow_metadata_inventory_move = table_can_move, + allow_metadata_inventory_put = table_can_put, }); function cartographer.register_map_material_name(name, material, value)