Camas/mods/wielditem/init.lua

83 lines
1.9 KiB
Lua
Raw Normal View History

2015-10-20 19:59:21 +02:00
2015-09-01 17:15:24 +02:00
--
-- Wielditem mod
-- By Kaadmy, for Pixture
--
2021-08-08 01:16:49 +02:00
local wielditem = {}
2015-10-20 19:59:21 +02:00
2015-09-01 17:15:24 +02:00
local update_time = 1
2015-10-20 19:59:21 +02:00
local timer = 10 -- needs to be more than update_time
2021-08-08 01:31:30 +02:00
local function attach_wielditem(player)
local name = player:get_player_name()
local pos = player:get_pos()
wielditem[name] = minetest.add_entity(pos, "wielditem:wielditem", name)
wielditem[name]:set_attach(player, "right_arm", {x = -1.5, y = 5.7, z = 2.5}, {x = 90, y = -45, z = 270})
wielditem[name]:get_luaentity()._wielder = player
end
2021-08-08 01:16:49 +02:00
minetest.register_entity("wielditem:wielditem", {
is_visible = false,
visual = "wielditem",
visual_size = {x = 0.17, y = 0.17},
hp_max = 1,
physical = false,
pointable = false,
collide_with_objects = false,
makes_footstep_sounds = false,
static_save = false,
_wielder = nil,
on_activate = function(self, staticdata)
2021-08-08 01:31:30 +02:00
self.object:set_armor_groups({immortal=1})
end,
on_deactivate = function(self)
-- Respawn wielditem entity if neccessary
local wielder = self._wielder
2021-08-08 01:16:49 +02:00
if wielder and wielder:is_player() then
2021-08-08 01:31:30 +02:00
minetest.after(3, function(player)
if player and player:is_player() then
attach_wielditem(player)
end
end, wielder)
2015-10-20 19:59:21 +02:00
end
2021-08-08 01:16:49 +02:00
end,
on_step = function(self, dtime)
2021-08-08 01:31:30 +02:00
local player = self._wielder
-- Remove orphan wielditem
if player == nil or (minetest.get_player_by_name(player:get_player_name()) == nil) then
minetest.log("info", "[wielditem] Removed orphan wielditem!")
2021-08-08 01:16:49 +02:00
self.object:remove()
return
2021-08-08 01:31:30 +02:00
end
2021-08-08 01:16:49 +02:00
2021-08-08 01:31:30 +02:00
local itemname = player:get_wielded_item():get_name()
2021-08-08 01:16:49 +02:00
2021-08-08 01:31:30 +02:00
if itemname ~= "" then
self.object:set_properties({wield_item = itemname, is_visible=true})
else
2021-08-08 01:16:49 +02:00
self.object:set_properties({is_visible=false})
2021-08-08 01:31:30 +02:00
end
2021-08-08 01:16:49 +02:00
end
})
2015-10-20 19:59:21 +02:00
2019-09-15 13:12:13 +02:00
minetest.register_on_joinplayer(function(player)
2021-08-08 01:31:30 +02:00
minetest.after(3, function(player)
2019-09-15 13:12:13 +02:00
if player and player:is_player() then
attach_wielditem(player)
end
end, player)
end)
2021-08-08 01:16:49 +02:00
minetest.register_on_leaveplayer(function(player)
wielditem[name] = nil
end)
2019-08-29 05:46:44 +02:00
default.log("mod:wielditem", "loaded")