Update attachment locations and code refactor

This commit is contained in:
stujones11 2016-11-26 19:50:11 +00:00
parent 3800812c0a
commit 52f2b73f64
4 changed files with 106 additions and 129 deletions

@ -11,5 +11,7 @@ Depends: default
Makes hand wielded items visible to other players.
See wield3d.conf.example for config options (save as wield3d.conf)
By default the wielded object is updated at one second intervals,
you can override this by adding wield3d_update_time = 1 (seconds)
to your minetest.conf

165
init.lua

@ -1,41 +1,30 @@
WIELD3D_INIT_DELAY = 1
WIELD3D_RETRY_TIME = 10
WIELD3D_UPDATE_TIME = 1
wield3d = {}
local modpath = minetest.get_modpath(minetest.get_current_modname())
local input = io.open(modpath.."/wield3d.conf", "r")
if input then
dofile(modpath.."/wield3d.conf")
input:close()
input = nil
end
dofile(modpath.."/location.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/location.lua")
local location = {
"Arm_Right", -- default bone
{x=0.2, y=6.5, z=3}, -- default position
{x=-100, y=225, z=90}, -- default rotation
}
local player_wielding = {}
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 = {}
local location = {
"Arm_Right", -- default bone
{x=0, y=5.5, z=3}, -- default position
{x=-90, y=225, z=90}, -- default rotation
}
local function add_wield_entity(player)
local name = player:get_player_name()
local pos = player:getpos()
local inv = player:get_inventory()
if name and pos and inv then
local offset = {x=pos.x, y=pos.y + 0.5, z=pos.z}
local object = minetest.add_entity(offset, "wield3d:wield_entity")
if name and pos then
pos.y = pos.y + 0.5
local object = minetest.add_entity(pos, "wield3d:wield_entity")
if object then
object:set_properties({collisionbox={0,0,0, 0,0,0}})
object:set_attach(player, location[1], location[2], location[3])
local entity = object:get_luaentity()
if entity then
entity.player = player
player_wielding[name] = 1
else
object:remove()
end
player_wielding[name] = {}
player_wielding[name].item = ""
player_wielding[name].object = object
player_wielding[name].location = location
end
end
end
@ -51,82 +40,76 @@ minetest.register_entity("wield3d:wield_entity", {
visual = "wielditem",
visual_size = {x=0.25, y=0.25},
textures = {"wield3d:hand"},
player = nil,
item = nil,
timer = 0,
location = {location[1], location[2], location[3]},
on_step = function(self, dtime)
self.timer = self.timer + dtime
if self.timer < WIELD3D_UPDATE_TIME then
return
end
self.timer = 0
local player = self.player
if player then
local name = player:get_player_name()
local p1 = player:getpos()
local p2 = self.object:getpos()
if p1 and p2 then
if vector.equals(p1, p2) then
local stack = player:get_wielded_item()
local item = stack:get_name() or ""
if item == self.item then
return
end
if minetest.get_modpath("wieldview") then
local def = minetest.registered_items[item] or {}
if def.inventory_image ~= "" then
item = ""
end
end
self.item = item
if item == "" then
item = "wield3d:hand"
end
local loc = wield3d_location[item] or location
if loc[1] ~= self.location[1]
or vector.equals(loc[2], self.location[2]) == false
or vector.equals(loc[3], self.location[3]) == false then
self.object:set_detach()
self.object:set_attach(player, loc[1], loc[2], loc[3])
self.location = {loc[1], loc[2], loc[3]}
end
self.object:set_properties({textures={item}})
return
end
end
player_wielding[name] = 0
on_activate = function(self, staticdata)
if staticdata == "expired" then
self.object:remove()
end
end,
on_punch = function(self)
self.object:remove()
end,
get_staticdata = function(self)
return "expired"
end,
})
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer > WIELD3D_RETRY_TIME then
for name, state in pairs(player_wielding) do
if state == 0 then
local player = minetest.get_player_by_name(name)
if player then
add_wield_entity(player)
else
player_wielding[name] = nil
end
end
end
timer = 0
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]}
end
wield.object:set_properties({textures={item}})
end
else
add_wield_entity(player)
end
active_players[name] = true
end
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
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
if name then
local wield = player_wielding[name] or {}
if wield.object then
wield.object:remove()
end
player_wielding[name] = nil
end
end)
minetest.register_on_joinplayer(function(player)
player_wielding[player:get_player_name()] = 0
minetest.after(WIELD3D_INIT_DELAY, add_wield_entity, player)
end)

@ -1,32 +1,35 @@
-- Wielded Item Location Overrides - [item_name] = {bone, position, rotation}
local bone = "Arm_Right"
local pos = {x=0, y=5.5, z=3}
local rx = -90
local rz = 90
wield3d_location = {
["default:torch"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["default:sapling"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["flowers:dandelion_white"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["flowers:dandelion_yellow"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["flowers:geranium"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["flowers:rose"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["flowers:tulip"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["flowers:viola"] = {bone, {x=0.2, y=6.5, z=3}, {x=-100, y=182, z=83}},
["default:shovel_wood"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_stone"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_steel"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_bronze"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_mese"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["default:shovel_diamond"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["bucket:bucket_empty"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["bucket:bucket_water"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["bucket:bucket_lava"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver1"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver2"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver3"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["screwdriver:screwdriver4"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["vessels:glass_bottle"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["vessels:drinking_glass"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
["vessels:steel_bottle"] = {bone, {x=0, y=6.5, z=3}, {x=-90, y=137, z=83}},
wield3d.location = {
["default:torch"] = {bone, pos, {x=rx, y=180, z=rz}},
["default:sapling"] = {bone, pos, {x=rx, y=180, z=rz}},
["flowers:dandelion_white"] = {bone, pos, {x=rx, y=180, z=rz}},
["flowers:dandelion_yellow"] = {bone, pos, {x=rx, y=180, z=rz}},
["flowers:geranium"] = {bone, pos, {x=rx, y=180, z=rz}},
["flowers:rose"] = {bone, pos, {x=rx, y=180, z=rz}},
["flowers:tulip"] = {bone, pos, {x=rx, y=180, z=rz}},
["flowers:viola"] = {bone, pos, {x=rx, y=180, z=rz}},
["default:shovel_wood"] = {bone, pos, {x=rx, y=135, z=rz}},
["default:shovel_stone"] = {bone, pos, {x=rx, y=135, z=rz}},
["default:shovel_steel"] = {bone, pos, {x=rx, y=135, z=rz}},
["default:shovel_bronze"] = {bone, pos, {x=rx, y=135, z=rz}},
["default:shovel_mese"] = {bone, pos, {x=rx, y=135, z=rz}},
["default:shovel_diamond"] = {bone, pos, {x=rx, y=135, z=rz}},
["bucket:bucket_empty"] = {bone, pos, {x=rx, y=135, z=rz}},
["bucket:bucket_water"] = {bone, pos, {x=rx, y=135, z=rz}},
["bucket:bucket_lava"] = {bone, pos, {x=rx, y=135, z=rz}},
["screwdriver:screwdriver"] = {bone, pos, {x=rx, y=135, z=rz}},
["screwdriver:screwdriver1"] = {bone, pos, {x=rx, y=135, z=rz}},
["screwdriver:screwdriver2"] = {bone, pos, {x=rx, y=135, z=rz}},
["screwdriver:screwdriver3"] = {bone, pos, {x=rx, y=135, z=rz}},
["screwdriver:screwdriver4"] = {bone, pos, {x=rx, y=135, z=rz}},
["vessels:glass_bottle"] = {bone, pos, {x=rx, y=135, z=rz}},
["vessels:drinking_glass"] = {bone, pos, {x=rx, y=135, z=rz}},
["vessels:steel_bottle"] = {bone, pos, {x=rx, y=135, z=rz}},
}

@ -1,11 +0,0 @@
-- Wield3d Configuration (defaults)
-- Increase this if you get non-attachment glitches when a player first joins.
WIELD3D_INIT_DELAY = 1
-- Number of seconds before retry if initialization fails.
WIELD3D_RETRY_TIME = 10
-- How often player wield items are updated.
WIELD3D_UPDATE_TIME = 1