2016-11-26 20:50:11 +01:00
|
|
|
wield3d = {}
|
2014-05-15 22:14:00 +02:00
|
|
|
|
2016-11-26 20:50:11 +01:00
|
|
|
dofile(minetest.get_modpath(minetest.get_current_modname()).."/location.lua")
|
2013-11-06 21:33:52 +01:00
|
|
|
|
2016-11-26 20:50:11 +01:00
|
|
|
local has_wieldview = minetest.get_modpath("wieldview")
|
|
|
|
local update_time_conf = minetest.setting_get("wield3d_update_time") or 1
|
|
|
|
local update_time = tonumber(update_time_conf) or 1
|
|
|
|
local timer = 0
|
|
|
|
local player_wielding = {}
|
2013-11-06 21:33:52 +01:00
|
|
|
local location = {
|
2016-11-26 20:50:11 +01:00
|
|
|
"Arm_Right", -- default bone
|
|
|
|
{x=0, y=5.5, z=3}, -- default position
|
|
|
|
{x=-90, y=225, z=90}, -- default rotation
|
2013-11-06 21:33:52 +01:00
|
|
|
}
|
2013-06-30 22:17:16 +02:00
|
|
|
|
2013-11-06 21:33:52 +01:00
|
|
|
local function add_wield_entity(player)
|
2014-05-13 21:54:53 +02:00
|
|
|
local name = player:get_player_name()
|
2013-11-06 21:33:52 +01:00
|
|
|
local pos = player:getpos()
|
2016-11-26 20:50:11 +01:00
|
|
|
if name and pos then
|
|
|
|
pos.y = pos.y + 0.5
|
|
|
|
local object = minetest.add_entity(pos, "wield3d:wield_entity")
|
2013-11-06 21:33:52 +01:00
|
|
|
if object then
|
|
|
|
object:set_attach(player, location[1], location[2], location[3])
|
2016-11-26 20:50:11 +01:00
|
|
|
player_wielding[name] = {}
|
|
|
|
player_wielding[name].item = ""
|
|
|
|
player_wielding[name].object = object
|
|
|
|
player_wielding[name].location = location
|
2013-11-06 21:33:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-06-30 22:17:16 +02:00
|
|
|
|
|
|
|
minetest.register_item("wield3d:hand", {
|
|
|
|
type = "none",
|
|
|
|
wield_image = "wield3d_trans.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_entity("wield3d:wield_entity", {
|
2013-11-06 21:33:52 +01:00
|
|
|
physical = false,
|
2014-05-15 22:14:00 +02:00
|
|
|
collisionbox = {-0.125,-0.125,-0.125, 0.125,0.125,0.125},
|
2013-11-06 21:33:52 +01:00
|
|
|
visual = "wielditem",
|
|
|
|
visual_size = {x=0.25, y=0.25},
|
|
|
|
textures = {"wield3d:hand"},
|
2016-11-26 20:50:11 +01:00
|
|
|
on_activate = function(self, staticdata)
|
|
|
|
if staticdata == "expired" then
|
|
|
|
self.object:remove()
|
2013-06-30 22:17:16 +02:00
|
|
|
end
|
2016-11-26 20:50:11 +01:00
|
|
|
end,
|
|
|
|
on_punch = function(self)
|
2013-11-06 21:33:52 +01:00
|
|
|
self.object:remove()
|
2013-06-30 22:17:16 +02:00
|
|
|
end,
|
2016-11-26 20:50:11 +01:00
|
|
|
get_staticdata = function(self)
|
|
|
|
return "expired"
|
|
|
|
end,
|
2013-06-30 22:17:16 +02:00
|
|
|
})
|
|
|
|
|
2013-11-06 21:33:52 +01:00
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
timer = timer + dtime
|
2016-11-26 20:50:11 +01:00
|
|
|
if timer < update_time then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local active_players = {}
|
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local wield = player_wielding[name]
|
|
|
|
if wield and wield.object then
|
|
|
|
local stack = player:get_wielded_item()
|
|
|
|
local item = stack:get_name() or ""
|
|
|
|
if item ~= wield.item then
|
|
|
|
if has_wieldview then
|
|
|
|
local def = minetest.registered_items[item] or {}
|
|
|
|
if def.inventory_image ~= "" then
|
|
|
|
item = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
wield.item = item
|
|
|
|
if item == "" then
|
|
|
|
item = "wield3d:hand"
|
|
|
|
end
|
|
|
|
local loc = wield3d.location[item] or location
|
|
|
|
if loc[1] ~= wield.location[1] or
|
|
|
|
not vector.equals(loc[2], wield.location[2]) or
|
|
|
|
not vector.equals(loc[3], wield.location[3]) then
|
|
|
|
wield.object:set_detach()
|
|
|
|
wield.object:set_attach(player, loc[1], loc[2], loc[3])
|
|
|
|
wield.location = {loc[1], loc[2], loc[3]}
|
2013-11-06 21:33:52 +01:00
|
|
|
end
|
2016-11-26 20:50:11 +01:00
|
|
|
wield.object:set_properties({textures={item}})
|
2013-10-02 23:37:49 +02:00
|
|
|
end
|
2016-11-26 20:50:11 +01:00
|
|
|
else
|
|
|
|
add_wield_entity(player)
|
2013-06-30 22:17:16 +02:00
|
|
|
end
|
2016-11-26 20:50:11 +01:00
|
|
|
active_players[name] = true
|
2013-11-06 21:33:52 +01:00
|
|
|
end
|
2016-11-26 20:50:11 +01:00
|
|
|
for name, wield in pairs(player_wielding) do
|
|
|
|
if not active_players[name] then
|
|
|
|
if wield.object then
|
|
|
|
wield.object:remove()
|
|
|
|
end
|
|
|
|
player_wielding[name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
timer = 0
|
2013-11-06 21:33:52 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if name then
|
2016-11-26 20:50:11 +01:00
|
|
|
local wield = player_wielding[name] or {}
|
|
|
|
if wield.object then
|
|
|
|
wield.object:remove()
|
|
|
|
end
|
2013-11-06 21:33:52 +01:00
|
|
|
player_wielding[name] = nil
|
|
|
|
end
|
2013-06-30 22:17:16 +02:00
|
|
|
end)
|
|
|
|
|