From e03bd4e6000a1863f5f1941ca95032b883f4a3bd Mon Sep 17 00:00:00 2001 From: stujones11 Date: Tue, 9 Apr 2019 20:02:15 +0100 Subject: [PATCH] Simplify attachment updates Refreshes the wield attachment on every update step. This should ensure that other players joining after the initial attachment do not need to wait until the next rotation change to see the object. Note: This will not work well in 3rd-person view mode without Minetest PR: #8342 --- init.lua | 62 +++++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/init.lua b/init.lua index ddcf4da..56c3a65 100644 --- a/init.lua +++ b/init.lua @@ -38,12 +38,10 @@ update_time = update_time and tonumber(update_time) or 1 verify_time = verify_time and tonumber(verify_time) or 10 wield_scale = wield_scale and tonumber(wield_scale) or 0.25 -- default scale -local location = { - "Arm_Right", -- default bone - {x=0, y=5.5, z=3}, -- default position - {x=-90, y=225, z=90}, -- default rotation - {x=wield_scale, y=wield_scale}, -} +local default_bone = "Arm_Right" +local default_position = {x=0, y=5.5, z=3} +local default_rotation = {x=-90, y=225, z=90} +local default_scale = {x=wield_scale, y=wield_scale} local function add_wield_entity(player) if not player or not player:is_player() then @@ -55,12 +53,13 @@ local function add_wield_entity(player) pos.y = pos.y + 0.5 local object = minetest.add_entity(pos, "wield3d:wield_entity", name) if object then - object:set_attach(player, location[1], location[2], location[3]) + object:set_attach(player, default_bone, default_position, + default_rotation) object:set_properties({ textures = {"wield3d:hand"}, - visual_size = location[4], + visual_size = default_scale, }) - player_wielding[name] = {item="", location=location} + player_wielding[name] = true end end end @@ -104,32 +103,27 @@ function wield_entity:on_step(dtime) self.object:remove() return end - local wield = player_wielding[self.wielder] local stack = player:get_wielded_item() local item = stack:get_name() or "" - if wield and item ~= wield.item then - if has_wieldview then - local def = minetest.registered_items[item] or {} - if def.inventory_image ~= "" then - item = "" - end + if has_wieldview then + local def = minetest.registered_items[item] or {} + if def.inventory_image ~= "" then + item = "" end - wield.item = item - if item == "" then - item = "wield3d:hand" - end - local loc = wield3d.location[item] or location - if loc[1] ~= wield.location[1] or - not vector.equals(loc[2], wield.location[2]) or - not vector.equals(loc[3], wield.location[3]) then - self.object:set_attach(player, loc[1], loc[2], loc[3]) - wield.location = {loc[1], loc[2], loc[3]} - end - self.object:set_properties({ - textures = {item}, - visual_size = loc[4], - }) end + if item == "" then + item = "wield3d:hand" + end + local location = wield3d.location[item] or {} + local bone = location[1] or default_bone + local position = location[2] or default_position + local rotation = location[3] or default_rotation + local scale = location[4] or default_scale + self.object:set_attach(player, bone, position, rotation) + self.object:set_properties({ + textures = {item}, + visual_size = scale, + }) self.timer = 0 end @@ -159,8 +153,8 @@ local function verify_wielditems() end player_iter = table_iter(names) -- clean-up player_wielding table - for name, wield in pairs(player_wielding) do - player_wielding[name] = tmp[name] and wield + for name, _ in pairs(player_wielding) do + player_wielding[name] = tmp[name] end end -- only deal with one player per server step @@ -172,7 +166,7 @@ local function verify_wielditems() pos.y = pos.y + 0.5 local wielding = false local objects = minetest.get_objects_inside_radius(pos, 1) - for _, object in pairs(objects) do + for object in table_iter(objects) do local entity = object:get_luaentity() if entity and entity.wielder == name then if wielding then