3d_armor/wieldview/init.lua

78 lines
2.1 KiB
Lua
Raw Normal View History

local time = 0
local update_time = tonumber(minetest.settings:get("wieldview_update_time"))
if not update_time then
update_time = 2
minetest.settings:set("wieldview_update_time", tostring(update_time))
end
2013-08-04 23:17:08 +02:00
wieldview = {
2013-06-22 20:31:57 +02:00
wielded_item = {},
transform = {},
}
dofile(minetest.get_modpath(minetest.get_current_modname()).."/get_texture.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/transform.lua")
2013-06-22 20:31:57 +02:00
wieldview.get_item_texture = function(self, item)
local texture = "3d_armor_trans.png"
2013-06-22 20:31:57 +02:00
if item ~= "" then
texture = armor.get_wield_image(item)
-- Get item image transformation, first from group, then from transform.lua
local transform = minetest.get_item_group(item, "wieldview_transform")
if transform == 0 then
transform = wieldview.transform[item]
end
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"..tostring(transform)
2013-08-04 23:17:08 +02:00
end
end
2013-08-04 23:17:08 +02:00
return texture
end
wieldview.update_wielded_item = function(self, player)
if not player then
return
end
local name = player:get_player_name()
local stack = player:get_wielded_item()
local item = stack:get_name()
if not item then
return
end
2013-06-22 20:31:57 +02:00
if self.wielded_item[name] then
if player:get_meta():get_int("show_wielded_item") == 2 then
item = ""
end
2013-06-22 20:31:57 +02:00
if self.wielded_item[name] == item then
return
end
armor.textures[name].wielditem = self:get_item_texture(item)
armor:update_player_visuals(player)
end
2013-06-22 20:31:57 +02:00
self.wielded_item[name] = item
end
minetest.register_on_joinplayer(function(player)
2013-06-22 20:31:57 +02:00
local name = player:get_player_name()
wieldview.wielded_item[name] = ""
minetest.after(0, function(pname)
local pplayer = minetest.get_player_by_name(pname)
if pplayer then
wieldview:update_wielded_item(pplayer)
end
end, name)
end)
minetest.register_globalstep(function(dtime)
time = time + dtime
if time > update_time then
for _,player in ipairs(minetest.get_connected_players()) do
wieldview:update_wielded_item(player)
end
time = 0
end
end)