From 41482b830ededf0d69d65a7fa2d184275d542977 Mon Sep 17 00:00:00 2001 From: Kimapr Date: Wed, 10 Jan 2024 14:44:40 +0500 Subject: [PATCH] Metadata in item entity visuals Send item metadata in item entities using the `wield_item` property, replacing the deprecated since at least 5.1 `textures[1]` which does not allow metadata in the itemstring. Only metadata fields known to affect visuals are sent to the client, count and wear are reset. This allows item entities to display color and (since 5.8) texture overrides properly. --- mods/nc_api_ents/api.lua | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/mods/nc_api_ents/api.lua b/mods/nc_api_ents/api.lua index c0b34316..5274c154 100644 --- a/mods/nc_api_ents/api.lua +++ b/mods/nc_api_ents/api.lua @@ -21,6 +21,31 @@ function minetest.spawn_falling_node(pos, node, meta) return false end +local visible_stackmeta = { + inventory_image = true, + inventory_overlay = true, + wield_image = true, + wield_overlay = true, + wield_scale = true, + color = true, + palette_index = true, +} + +local function stack_tostring_clean(stack) + -- Do not send metadata that does not affect stack visuals to the client + stack = ItemStack(stack) + local meta = stack:get_meta():to_table() + for key, _ in pairs(meta.fields) do + if not visible_stackmeta[key] then + meta.fields[key] = nil + end + end + stack:get_meta():from_table(meta) + stack:set_count(1) + stack:set_wear(0) + return stack:to_string() +end + function nodecore.stackentprops(stack, yaw, rotate, ss) local props = { hp_max = 1, @@ -38,7 +63,7 @@ function nodecore.stackentprops(stack, yaw, rotate, ss) if stack then if type(stack) == "string" then stack = ItemStack(stack) end props.is_visible = not stack:is_empty() - props.textures[1] = stack:get_name() + props.wield_item = stack_tostring_clean(stack) local ratio = stack:get_count() / stack:get_stack_max() if ratio > 1 then ratio = 1 end