1
0
Fork 0
MineClone2/mods/PLAYER/mcl_wieldview/init.lua

127 lines
3.1 KiB
Lua
Raw Normal View History

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)
2022-03-15 14:58:27 +00:00
if itemname == "" or minetest.get_item_group(itemname, "no_wieldview") ~= 0 then
2021-04-14 13:46:52 +00:00
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
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()
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)
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))
itementity:get_luaentity().wielder = player
2021-04-14 13:46:52 +00:00
end)
end)
minetest.register_on_leaveplayer(function(player)
mcl_wieldview.players[player] = nil
2021-04-14 13:46:52 +00:00
end)
minetest.register_globalstep(function()
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)
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
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
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
2022-03-15 14:58:27 +00:00
if minetest.get_item_group(itemstring, "no_wieldview") ~= 0 then
self.object:set_properties({textures = {""}})
end
2021-04-14 13:46:52 +00:00
self.itemstring = itemstring
end
else
self.object:remove()
end
end,
})