From b34224736770a8c5202b40c4822ba2978123323d Mon Sep 17 00:00:00 2001 From: flux <25628292+fluxionary@users.noreply.github.com> Date: Fri, 23 Sep 2022 09:57:17 -0700 Subject: [PATCH] disallow putting stacks w/metadata into stations (currently, metadata is lost) --- stairsplus/api/station.lua | 5 ++ stairsplus/util.lua | 145 ++++++++++++++++++++----------------- 2 files changed, 82 insertions(+), 68 deletions(-) diff --git a/stairsplus/api/station.lua b/stairsplus/api/station.lua index 1d1b622..1ac0881 100644 --- a/stairsplus/api/station.lua +++ b/stairsplus/api/station.lua @@ -2,6 +2,7 @@ local api = stairsplus.api +local has_metadata = stairsplus.util.has_metadata local resolve_aliases = stairsplus.util.resolve_aliases local default_stack_max = tonumber(minetest.settings:get("default_stack_max")) or 99 @@ -171,6 +172,10 @@ function station.allow_inventory_put(meta, inv, listname, index, stack, player) return 0 end + if has_metadata(stack) then + return 0 + end + local to_put_node = resolve_aliases(stack:get_name()) local node = api.get_node_of_shaped_node(to_put_node) local shape = api.get_shape_of_shaped_node(to_put_node) diff --git a/stairsplus/util.lua b/stairsplus/util.lua index 4859558..62697e0 100644 --- a/stairsplus/util.lua +++ b/stairsplus/util.lua @@ -1,82 +1,91 @@ -stairsplus.util = { - table_set_all = function(t, other_table) - for key, value in pairs(other_table) do - t[key] = value - end - return t - end, +local util = {} - table_is_empty = function(t) - return next(t) == nil - end, +function util.table_set_all(t, other_table) + for key, value in pairs(other_table) do + t[key] = value + end + return t +end - table_sort_keys = function(t, sort_function) - local sorted = {} - for key in pairs(t) do - table.insert(sorted, key) - end - if sort_function then - table.sort(sorted, sort_function) - else - table.sort(sorted) - end - return sorted - end, +function util.table_is_empty(t) + return next(t) == nil +end - table_equals = function(t1, t2) - if t1 == t2 then - return true - end +function util.table_sort_keys(t, sort_function) + local sorted = {} + for key in pairs(t) do + table.insert(sorted, key) + end + if sort_function then + table.sort(sorted, sort_function) + else + table.sort(sorted) + end + return sorted +end - local tt1 = type(t1) - local tt2 = type(t2) +function util.table_equals(t1, t2) + if t1 == t2 then + return true + end - if tt1 ~= tt2 then + local tt1 = type(t1) + local tt2 = type(t2) + + if tt1 ~= tt2 then + return false + end + + if tt1 ~= "table" or tt2 ~= "table" then + return t1 == t2 + end + + for k1, v1 in pairs(t1) do + if v1 ~= t2[k1] then return false end + end - if tt1 ~= "table" or tt2 ~= "table" then - return t1 == t2 + for k2, v2 in pairs(t2) do + if t1[k2] ~= v2 then + return false end + end - for k1, v1 in pairs(t1) do - if v1 ~= t2[k1] then - return false - end - end + return true +end - for k2, v2 in pairs(t2) do - if t1[k2] ~= v2 then - return false - end - end +function util.get_location_string(inv) + local location = inv:get_location() + if location.type == "node" then + return ("nodemeta:%i,%i,%i"):format(location.pos.x, location.pos.y, location.pos.z) + elseif location.type == "player" then + return ("player:%s"):format(location.name) + elseif location.type == "detached" then + return ("detached:%s"):format(location.name) + else + error(("unexpected location? %s"):format(dump(location))) + end +end - return true - end, +function util.resolve_aliases(node, seen) + seen = seen or {} + if seen[node] then + error(("alias loop on %s"):format(node)) + end + local aliased_to = minetest.registered_aliases[node] + if aliased_to then + seen[node] = true + return util.resolve_aliases(aliased_to, seen) + end + return node +end - get_location_string = function(inv) - local location = inv:get_location() - if location.type == "node" then - return ("nodemeta:%i,%i,%i"):format(location.pos.x, location.pos.y, location.pos.z) - elseif location.type == "player" then - return ("player:%s"):format(location.name) - elseif location.type == "detached" then - return ("detached:%s"):format(location.name) - else - error(("unexpected location? %s"):format(dump(location))) - end - end, +function util.has_metadata(item) + item = type(item) == "userdata" and item or ItemStack(item) + local meta = item:get_meta() - resolve_aliases = function(node, seen) - seen = seen or {} - if seen[node] then - error(("alias loop on %s"):format(node)) - end - local aliased_to = minetest.registered_aliases[node] - if aliased_to then - seen[node] = true - return stairsplus.util.resolve_aliases(aliased_to, seen) - end - return node - end, -} + return not util.table_is_empty(meta:to_table().fields) +end + +stairsplus.util = util