mirror of
https://github.com/stujones11/wield3d.git
synced 2024-08-20 13:34:06 +02:00
Code refactor for minetest 5.0.x
This commit is contained in:
parent
fbffa818ab
commit
a11586e718
10
.luacheckrc
Normal file
10
.luacheckrc
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
allow_defined_top = true
|
||||||
|
|
||||||
|
read_globals = {
|
||||||
|
"vector",
|
||||||
|
table = {fields = {"getn"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
globals = {
|
||||||
|
"minetest",
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
[mod] 3d wielded items [wield3d]
|
[mod] 3d wielded items [wield3d]
|
||||||
================================
|
================================
|
||||||
|
|
||||||
Copyright (C) 2013 Stuart Jones - WTFPL
|
Copyright (C) 2019 Stuart Jones - MIT
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
[mod] 3d wielded items [wield3d]
|
[mod] 3d wielded items [wield3d]
|
||||||
================================
|
================================
|
||||||
|
|
||||||
Mod Version: 0.4.0
|
Mod Version: 0.5
|
||||||
|
|
||||||
Minetest Version: 0.4.12 or later
|
Minetest Version: 5.0.0 or later
|
||||||
|
|
||||||
Decription: Visible 3d wielded items for Minetest
|
Decription: Visible 3d wielded items for Minetest
|
||||||
|
|
||||||
@ -12,6 +12,8 @@ Depends: default
|
|||||||
Makes hand wielded items visible to other players.
|
Makes hand wielded items visible to other players.
|
||||||
|
|
||||||
By default the wielded object is updated at one second intervals,
|
By default the wielded object is updated at one second intervals,
|
||||||
you can override this by adding wield3d_update_time = 1 (seconds)
|
you can override this by adding `wield3d_update_time = 1` (seconds)
|
||||||
to your minetest.conf
|
to your minetest.conf
|
||||||
|
|
||||||
|
Servers can also control how often to verify the wield item of each
|
||||||
|
individual player by setting `wield3d_update_time = 10` (seconds)
|
||||||
|
@ -1,2 +1 @@
|
|||||||
default
|
default
|
||||||
|
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
Visible 3d wielded items for Minetest.
|
|
||||||
Adds 3d wield-items that are visible in third person view and to other players.
|
|
172
init.lua
172
init.lua
@ -1,12 +1,39 @@
|
|||||||
|
--[[
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2019 stujones11, Stuart Jones
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
]]--
|
||||||
|
|
||||||
wield3d = {}
|
wield3d = {}
|
||||||
|
|
||||||
dofile(minetest.get_modpath(minetest.get_current_modname()).."/location.lua")
|
dofile(minetest.get_modpath(minetest.get_current_modname()).."/location.lua")
|
||||||
|
|
||||||
local has_wieldview = minetest.get_modpath("wieldview")
|
local has_wieldview = minetest.get_modpath("wieldview")
|
||||||
local update_time_conf = minetest.settings:get("wield3d_update_time") or 1
|
local update_time = minetest.settings:get("wield3d_update_time")
|
||||||
local update_time = tonumber(update_time_conf) or 1
|
local verify_time = minetest.settings:get("wield3d_verify_time")
|
||||||
local timer = 0
|
|
||||||
local player_wielding = {}
|
local player_wielding = {}
|
||||||
|
update_time = update_time and tonumber(update_time) or 1
|
||||||
|
verify_time = verify_time and tonumber(verify_time) or 10
|
||||||
local location = {
|
local location = {
|
||||||
"Arm_Right", -- default bone
|
"Arm_Right", -- default bone
|
||||||
{x=0, y=5.5, z=3}, -- default position
|
{x=0, y=5.5, z=3}, -- default position
|
||||||
@ -14,12 +41,16 @@ local location = {
|
|||||||
{x=0.25, y=0.25}, -- default scale
|
{x=0.25, y=0.25}, -- default scale
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
local function add_wield_entity(player)
|
local function add_wield_entity(player)
|
||||||
|
if not player or not player:is_player() then
|
||||||
|
return
|
||||||
|
end
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local pos = player:get_pos()
|
local pos = player:get_pos()
|
||||||
if name and pos then
|
if name and pos and not player_wielding[name] then
|
||||||
pos.y = pos.y + 0.5
|
pos.y = pos.y + 0.5
|
||||||
local object = minetest.add_entity(pos, "wield3d:wield_entity")
|
local object = minetest.add_entity(pos, "wield3d:wield_entity", name)
|
||||||
if object then
|
if object then
|
||||||
object:set_attach(player, location[1], location[2], location[3])
|
object:set_attach(player, location[1], location[2], location[3])
|
||||||
object:set_properties({
|
object:set_properties({
|
||||||
@ -28,47 +59,54 @@ local function add_wield_entity(player)
|
|||||||
})
|
})
|
||||||
player_wielding[name] = {}
|
player_wielding[name] = {}
|
||||||
player_wielding[name].item = ""
|
player_wielding[name].item = ""
|
||||||
player_wielding[name].object = object
|
|
||||||
player_wielding[name].location = location
|
player_wielding[name].location = location
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_item("wield3d:hand", {
|
local function sq_dist(a, b)
|
||||||
type = "none",
|
local x = a.x - b.x
|
||||||
wield_image = "blank.png",
|
local y = a.y - b.y
|
||||||
})
|
local z = a.z - b.z
|
||||||
|
return x * x + y * y + z * z
|
||||||
|
end
|
||||||
|
|
||||||
minetest.register_entity("wield3d:wield_entity", {
|
local wield_entity = {
|
||||||
physical = false,
|
physical = false,
|
||||||
collisionbox = {-0.125,-0.125,-0.125, 0.125,0.125,0.125},
|
collisionbox = {-0.125,-0.125,-0.125, 0.125,0.125,0.125},
|
||||||
visual = "wielditem",
|
visual = "wielditem",
|
||||||
on_activate = function(self, staticdata)
|
textures = {"wield3d:hand"},
|
||||||
if staticdata == "expired" then
|
wielder = nil,
|
||||||
self.object:remove()
|
timer = 0,
|
||||||
end
|
}
|
||||||
end,
|
|
||||||
on_punch = function(self)
|
|
||||||
self.object:remove()
|
|
||||||
end,
|
|
||||||
get_staticdata = function(self)
|
|
||||||
return "expired"
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
function wield_entity:on_activate(staticdata)
|
||||||
timer = timer + dtime
|
if staticdata then
|
||||||
if timer < update_time then
|
self.wielder = staticdata
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local active_players = {}
|
self.object:remove()
|
||||||
for _, player in pairs(minetest.get_connected_players()) do
|
end
|
||||||
local name = player:get_player_name()
|
|
||||||
local wield = player_wielding[name]
|
function wield_entity:on_step(dtime)
|
||||||
if wield and wield.object then
|
if self.wielder == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
self.timer = self.timer + dtime
|
||||||
|
if self.timer < update_time then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local player = minetest.get_player_by_name(self.wielder)
|
||||||
|
if player == nil or
|
||||||
|
sq_dist(player:get_pos(), self.object:get_pos()) > 3 then
|
||||||
|
player_wielding[self.wielder] = nil
|
||||||
|
self.object:remove()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local wield = player_wielding[self.wielder]
|
||||||
local stack = player:get_wielded_item()
|
local stack = player:get_wielded_item()
|
||||||
local item = stack:get_name() or ""
|
local item = stack:get_name() or ""
|
||||||
if item ~= wield.item then
|
if wield and item ~= wield.item then
|
||||||
if has_wieldview then
|
if has_wieldview then
|
||||||
local def = minetest.registered_items[item] or {}
|
local def = minetest.registered_items[item] or {}
|
||||||
if def.inventory_image ~= "" then
|
if def.inventory_image ~= "" then
|
||||||
@ -83,38 +121,74 @@ minetest.register_globalstep(function(dtime)
|
|||||||
if loc[1] ~= wield.location[1] or
|
if loc[1] ~= wield.location[1] or
|
||||||
not vector.equals(loc[2], wield.location[2]) or
|
not vector.equals(loc[2], wield.location[2]) or
|
||||||
not vector.equals(loc[3], wield.location[3]) then
|
not vector.equals(loc[3], wield.location[3]) then
|
||||||
wield.object:set_attach(player, loc[1], loc[2], loc[3])
|
self.object:set_attach(player, loc[1], loc[2], loc[3])
|
||||||
wield.location = {loc[1], loc[2], loc[3]}
|
wield.location = {loc[1], loc[2], loc[3]}
|
||||||
end
|
end
|
||||||
wield.object:set_properties({
|
self.object:set_properties({
|
||||||
textures = {item},
|
textures = {item},
|
||||||
visual_size = loc[4],
|
visual_size = loc[4],
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
else
|
self.timer = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
local function table_iter(t)
|
||||||
|
local i = 0
|
||||||
|
local n = table.getn(t)
|
||||||
|
return function ()
|
||||||
|
i = i + 1
|
||||||
|
if i <= n then
|
||||||
|
return t[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local player_iter = nil
|
||||||
|
|
||||||
|
local function verify_wielditems()
|
||||||
|
player_iter = player_iter or table_iter(minetest.get_connected_players())
|
||||||
|
-- only deal with one player per server step
|
||||||
|
local player = player_iter()
|
||||||
|
if player and player:is_player() then
|
||||||
|
local name = player:get_player_name()
|
||||||
|
local pos = name and player:get_pos()
|
||||||
|
if pos then
|
||||||
|
pos.y = pos.y + 0.5
|
||||||
|
local wielding = false
|
||||||
|
local objects = minetest.get_objects_inside_radius(pos, 0.5)
|
||||||
|
for _, object in pairs(objects) do
|
||||||
|
local entity = object:get_luaentity()
|
||||||
|
if entity and entity.wielder == name then
|
||||||
|
wielding = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not wielding then
|
||||||
add_wield_entity(player)
|
add_wield_entity(player)
|
||||||
end
|
end
|
||||||
active_players[name] = true
|
|
||||||
end
|
end
|
||||||
for name, wield in pairs(player_wielding) do
|
return minetest.after(0, verify_wielditems)
|
||||||
if not active_players[name] then
|
|
||||||
if wield.object then
|
|
||||||
wield.object:remove()
|
|
||||||
end
|
end
|
||||||
player_wielding[name] = nil
|
player_iter = nil
|
||||||
end
|
minetest.after(verify_time, verify_wielditems)
|
||||||
end
|
end
|
||||||
timer = 0
|
|
||||||
|
minetest.after(verify_time, verify_wielditems)
|
||||||
|
|
||||||
|
minetest.register_entity("wield3d:wield_entity", wield_entity)
|
||||||
|
|
||||||
|
minetest.register_item("wield3d:hand", {
|
||||||
|
type = "none",
|
||||||
|
wield_image = "blank.png",
|
||||||
|
})
|
||||||
|
|
||||||
|
minetest.register_on_joinplayer(function(player)
|
||||||
|
minetest.after(2, add_wield_entity, player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
if name then
|
if name then
|
||||||
local wield = player_wielding[name] or {}
|
|
||||||
if wield.object then
|
|
||||||
wield.object:remove()
|
|
||||||
end
|
|
||||||
player_wielding[name] = nil
|
player_wielding[name] = nil
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
1
mod.conf
1
mod.conf
@ -1 +1,2 @@
|
|||||||
name = wield3d
|
name = wield3d
|
||||||
|
description = Adds 3d wield-items that are visible in third person view and to other players.
|
||||||
|
Loading…
Reference in New Issue
Block a user