2021-05-29 14:12:33 +00:00
|
|
|
local get_connected_players = minetest.get_connected_players
|
|
|
|
local get_item_group = minetest.get_item_group
|
|
|
|
|
2021-04-14 13:46:52 +00:00
|
|
|
mcl_wieldview = {
|
|
|
|
players = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mcl_wieldview.get_item_texture(itemname)
|
|
|
|
if itemname == "" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local def = minetest.registered_items[itemname]
|
|
|
|
if not def then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local inv_image = def.inventory_image
|
|
|
|
if inv_image == "" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local texture = inv_image
|
|
|
|
|
2021-05-29 14:12:33 +00:00
|
|
|
local transform = get_item_group(itemname, "wieldview_transform")
|
2021-04-14 13:46:52 +00:00
|
|
|
if transform then
|
|
|
|
-- This actually works with groups ratings because transform1, transform2, etc.
|
|
|
|
-- have meaning and transform0 is used for identidy, so it can be ignored
|
|
|
|
texture = texture .. "^[transform" .. transform
|
|
|
|
end
|
|
|
|
|
|
|
|
return texture
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_wieldview.update_wielded_item(player)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local itemstack = player:get_wielded_item()
|
|
|
|
local itemname = itemstack:get_name()
|
|
|
|
|
2021-04-17 11:24:30 +00:00
|
|
|
local def = mcl_wieldview.players[player]
|
2021-04-14 13:46:52 +00:00
|
|
|
|
|
|
|
if def.item == itemname then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
def.item = itemname
|
|
|
|
def.texture = mcl_wieldview.get_item_texture(itemname) or "blank.png"
|
|
|
|
|
|
|
|
mcl_player.player_set_wielditem(player, def.texture)
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2021-04-17 11:24:30 +00:00
|
|
|
mcl_wieldview.players[player] = {item = "", texture = "blank.png"}
|
2021-04-14 13:46:52 +00:00
|
|
|
|
|
|
|
minetest.after(0, function()
|
|
|
|
if not player:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
mcl_wieldview.update_wielded_item(player)
|
|
|
|
|
|
|
|
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))
|
2021-04-17 11:24:30 +00:00
|
|
|
itementity:get_luaentity().wielder = player
|
2021-04-14 13:46:52 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2021-04-17 11:24:30 +00:00
|
|
|
mcl_wieldview.players[player] = nil
|
2021-04-14 13:46:52 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_globalstep(function()
|
2021-05-29 14:12:33 +00:00
|
|
|
local players = get_connected_players()
|
|
|
|
for i = 1, #players do
|
|
|
|
mcl_wieldview.update_wielded_item(players[i])
|
2021-04-14 13:46:52 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_entity("mcl_wieldview:wieldnode", {
|
|
|
|
initial_properties = {
|
|
|
|
hp_max = 1,
|
|
|
|
visual = "wielditem",
|
|
|
|
physical = false,
|
|
|
|
textures = {""},
|
|
|
|
automatic_rotate = 1.5,
|
|
|
|
is_visible = true,
|
|
|
|
pointable = false,
|
|
|
|
collide_with_objects = false,
|
|
|
|
static_save = false,
|
|
|
|
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},
|
|
|
|
visual_size = {x = 0.21, y = 0.21},
|
|
|
|
},
|
|
|
|
|
|
|
|
itemstring = "",
|
|
|
|
|
|
|
|
on_step = function(self)
|
2021-04-17 11:24:30 +00:00
|
|
|
if self.wielder:is_player() then
|
|
|
|
local def = mcl_wieldview.players[self.wielder]
|
|
|
|
local itemstring = def.item
|
2021-04-14 13:46:52 +00:00
|
|
|
|
|
|
|
if self.itemstring ~= itemstring then
|
2021-04-17 11:24:30 +00:00
|
|
|
local itemdef = minetest.registered_items[itemstring]
|
|
|
|
self.object:set_properties({glow = itemdef and itemdef.light_source or 0})
|
2021-04-14 13:46:52 +00:00
|
|
|
|
|
|
|
-- wield item as cubic
|
2021-04-17 11:24:30 +00:00
|
|
|
if def.texture == "blank.png" then
|
2021-04-14 13:46:52 +00:00
|
|
|
self.object:set_properties({textures = {itemstring}})
|
|
|
|
-- wield item as flat
|
|
|
|
else
|
|
|
|
self.object:set_properties({textures = {""}})
|
|
|
|
end
|
|
|
|
|
|
|
|
self.itemstring = itemstring
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.object:remove()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|