Fix wieldview in MT 5.6

This commit is contained in:
parent 60f3d4b74c
commit 71cfa67c7d
1 changed files with 42 additions and 32 deletions

View File

@ -1,43 +1,28 @@
minetest.register_on_joinplayer(function(player) minetest.register_entity("mcl_wieldview:wieldview", {
if not player or not player:is_player() then
return
end
local itementity = minetest.add_entity(player:get_pos(), "mcl_wieldview:wieldnode")
if not itementity then return end
itementity:set_attach(player, "Wield_Item")
--itementity:set_attach(player, "Hand_Right", vector.new(0, 1, 0), vector.new(90, 45, 90))
itementity:get_luaentity()._wielder = player
end)
minetest.register_entity("mcl_wieldview:wieldnode", {
initial_properties = { initial_properties = {
hp_max = 1, hp_max = 1,
visual = "wielditem", visual = "wielditem",
physical = false, physical = false,
textures = {""}, is_visible = false,
automatic_rotate = 1.5,
is_visible = true,
pointable = false, pointable = false,
collide_with_objects = false, collide_with_objects = false,
static_save = false, static_save = false,
collisionbox = {-0.21, -0.21, -0.21, 0.21, 0.21, 0.21}, collisionbox = {-0.21, -0.21, -0.21, 0.21, 0.21, 0.21},
selectionbox = {-0.21, -0.21, -0.21, 0.21, 0.21, 0.21}, selectionbox = {-0.21, -0.21, -0.21, 0.21, 0.21, 0.21},
visual_size = {x = 0.21, y = 0.21}, visual_size = {x = 0.21, y = 0.21},
}, }
})
on_step = function(self) local wieldview_luaentites = {}
local player = self._wielder
if not player or not player:is_player() then
self.object:remove()
return
end
local function update_wieldview_entity(player)
local luaentity = wieldview_luaentites[player]
if luaentity and luaentity.object:get_yaw() then
local item = player:get_wielded_item():get_name() local item = player:get_wielded_item():get_name()
if item == self._item then return end if item == luaentity._item then return end
self._item = item luaentity._item = item
local def = player:get_wielded_item():get_definition() local def = player:get_wielded_item():get_definition()
if def and def._mcl_wieldview_item then if def and def._mcl_wieldview_item then
@ -45,10 +30,35 @@ minetest.register_entity("mcl_wieldview:wieldnode", {
end end
local item_def = minetest.registered_items[item] local item_def = minetest.registered_items[item]
self.object:set_properties({ luaentity.object:set_properties({
glow = item_def and item_def.light_source or 0, glow = item_def and item_def.light_source or 0,
wield_item = item, wield_item = item,
is_visible = item ~= "" is_visible = item ~= ""
}) })
end, else
}) -- If the player is running through an unloaded area,
-- the wieldview entity will sometimes get unloaded.
-- This code path is also used to initalize the wieldview.
-- Creating entites from minetest.register_on_joinplayer
-- is unreliable as of Minetest 5.6
local obj_ref = minetest.add_entity(player:get_pos(), "mcl_wieldview:wieldview")
if not obj_ref then return end
obj_ref:set_attach(player, "Wield_Item")
--obj_ref:set_attach(player, "Hand_Right", vector.new(0, 1, 0), vector.new(90, 45, 90))
wieldview_luaentites[player] = obj_ref:get_luaentity()
end
end
minetest.register_on_leaveplayer(function(player)
if wieldview_luaentites[player] then
wieldview_luaentites[player].object:remove()
end
wieldview_luaentites[player] = nil
end)
minetest.register_globalstep(function(dtime)
local players = minetest.get_connected_players()
for i, player in pairs(players) do
update_wieldview_entity(player)
end
end)