Improve wielditem performance and fix wielditems sometimes showing duplicate

This commit is contained in:
Elias Fleckenstein 2021-04-17 13:24:30 +02:00
parent 4d515e95c6
commit d952423dda

View file

@ -33,11 +33,10 @@ function mcl_wieldview.update_wielded_item(player)
if not player then if not player then
return return
end end
local name = player:get_player_name()
local itemstack = player:get_wielded_item() local itemstack = player:get_wielded_item()
local itemname = itemstack:get_name() local itemname = itemstack:get_name()
local def = mcl_wieldview.players[name] local def = mcl_wieldview.players[player]
if def.item == itemname then if def.item == itemname then
return return
@ -50,8 +49,7 @@ function mcl_wieldview.update_wielded_item(player)
end end
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name() mcl_wieldview.players[player] = {item = "", texture = "blank.png"}
mcl_wieldview.players[name] = {item = "", texture = "blank.png"}
minetest.after(0, function() minetest.after(0, function()
if not player:is_player() then if not player:is_player() then
@ -62,13 +60,12 @@ minetest.register_on_joinplayer(function(player)
local itementity = minetest.add_entity(player:get_pos(), "mcl_wieldview:wieldnode") local itementity = minetest.add_entity(player:get_pos(), "mcl_wieldview:wieldnode")
itementity:set_attach(player, "Hand_Right", vector.new(0, 1, 0), vector.new(90, 0, 45)) itementity:set_attach(player, "Hand_Right", vector.new(0, 1, 0), vector.new(90, 0, 45))
itementity:get_luaentity().wielder = name itementity:get_luaentity().wielder = player
end) end)
end) end)
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name() mcl_wieldview.players[player] = nil
mcl_wieldview.players[name] = nil
end) end)
minetest.register_globalstep(function() minetest.register_globalstep(function()
@ -96,17 +93,16 @@ minetest.register_entity("mcl_wieldview:wieldnode", {
itemstring = "", itemstring = "",
on_step = function(self) on_step = function(self)
local player = minetest.get_player_by_name(self.wielder) if self.wielder:is_player() then
if player then local def = mcl_wieldview.players[self.wielder]
local wielded = player:get_wielded_item() local itemstring = def.item
local itemstring = wielded:get_name()
if self.itemstring ~= itemstring then if self.itemstring ~= itemstring then
local def = minetest.registered_items[itemstring] local itemdef = minetest.registered_items[itemstring]
self.object:set_properties({glow = def and def.light_source or 0}) self.object:set_properties({glow = itemdef and itemdef.light_source or 0})
-- wield item as cubic -- wield item as cubic
if mcl_wieldview.players[self.wielder].texture == "blank.png" then if def.texture == "blank.png" then
self.object:set_properties({textures = {itemstring}}) self.object:set_properties({textures = {itemstring}})
-- wield item as flat -- wield item as flat
else else