disallow putting stacks w/metadata into stations (currently, metadata is lost)

This commit is contained in:
flux 2022-09-23 09:57:17 -07:00
parent 16c72b1aa3
commit b342247367
No known key found for this signature in database
GPG Key ID: 9333B27816848A15
2 changed files with 82 additions and 68 deletions

View File

@ -2,6 +2,7 @@
local api = stairsplus.api local api = stairsplus.api
local has_metadata = stairsplus.util.has_metadata
local resolve_aliases = stairsplus.util.resolve_aliases local resolve_aliases = stairsplus.util.resolve_aliases
local default_stack_max = tonumber(minetest.settings:get("default_stack_max")) or 99 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 return 0
end end
if has_metadata(stack) then
return 0
end
local to_put_node = resolve_aliases(stack:get_name()) local to_put_node = resolve_aliases(stack:get_name())
local node = api.get_node_of_shaped_node(to_put_node) local node = api.get_node_of_shaped_node(to_put_node)
local shape = api.get_shape_of_shaped_node(to_put_node) local shape = api.get_shape_of_shaped_node(to_put_node)

View File

@ -1,82 +1,91 @@
stairsplus.util = { local util = {}
table_set_all = function(t, other_table)
for key, value in pairs(other_table) do
t[key] = value
end
return t
end,
table_is_empty = function(t) function util.table_set_all(t, other_table)
return next(t) == nil for key, value in pairs(other_table) do
end, t[key] = value
end
return t
end
table_sort_keys = function(t, sort_function) function util.table_is_empty(t)
local sorted = {} return next(t) == nil
for key in pairs(t) do end
table.insert(sorted, key)
end
if sort_function then
table.sort(sorted, sort_function)
else
table.sort(sorted)
end
return sorted
end,
table_equals = function(t1, t2) function util.table_sort_keys(t, sort_function)
if t1 == t2 then local sorted = {}
return true for key in pairs(t) do
end 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) function util.table_equals(t1, t2)
local tt2 = type(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 return false
end end
end
if tt1 ~= "table" or tt2 ~= "table" then for k2, v2 in pairs(t2) do
return t1 == t2 if t1[k2] ~= v2 then
return false
end end
end
for k1, v1 in pairs(t1) do return true
if v1 ~= t2[k1] then end
return false
end
end
for k2, v2 in pairs(t2) do function util.get_location_string(inv)
if t1[k2] ~= v2 then local location = inv:get_location()
return false if location.type == "node" then
end return ("nodemeta:%i,%i,%i"):format(location.pos.x, location.pos.y, location.pos.z)
end 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 function util.resolve_aliases(node, seen)
end, 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) function util.has_metadata(item)
local location = inv:get_location() item = type(item) == "userdata" and item or ItemStack(item)
if location.type == "node" then local meta = item:get_meta()
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,
resolve_aliases = function(node, seen) return not util.table_is_empty(meta:to_table().fields)
seen = seen or {} end
if seen[node] then
error(("alias loop on %s"):format(node)) stairsplus.util = util
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,
}