2021-06-11 20:47:29 +02:00
|
|
|
-- Localize globals
|
|
|
|
local minetest, modlib, pairs, table = minetest, modlib, pairs, table
|
|
|
|
|
2021-06-17 19:45:08 +02:00
|
|
|
-- Set environment
|
|
|
|
local _ENV = ...
|
|
|
|
setfenv(1, _ENV)
|
|
|
|
|
2021-03-25 17:46:04 +01:00
|
|
|
players = {}
|
|
|
|
|
|
|
|
registered_on_wielditem_changes = {function(...)
|
2021-03-27 20:10:49 +01:00
|
|
|
local _, previous_item, _, item = ...
|
|
|
|
if previous_item then
|
|
|
|
((previous_item:get_definition()._modlib or {}).un_wield or modlib.func.no_op)(...)
|
|
|
|
end
|
|
|
|
if item then
|
|
|
|
((item:get_definition()._modlib or {}).on_wield or modlib.func.no_op)(...)
|
|
|
|
end
|
2021-03-25 17:46:04 +01:00
|
|
|
end}
|
|
|
|
|
|
|
|
--+ Registers an on_wielditem_change callback: function(player, previous_item, previous_index, item)
|
|
|
|
--+ Will be called once with player, nil, index, item on join
|
|
|
|
register_on_wielditem_change = modlib.func.curry(table.insert, registered_on_wielditem_changes)
|
|
|
|
|
2021-08-19 15:05:57 +02:00
|
|
|
local function register_callbacks()
|
2021-03-27 20:10:49 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local item, index = player:get_wielded_item(), player:get_wield_index()
|
|
|
|
players[player:get_player_name()] = {
|
|
|
|
wield = {
|
|
|
|
item = item,
|
|
|
|
index = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
modlib.table.icall(registered_on_wielditem_changes, player, nil, index, item)
|
|
|
|
end)
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
players[player:get_player_name()] = nil
|
|
|
|
end)
|
2021-08-19 15:05:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Other on_joinplayer / on_leaveplayer callbacks should execute first
|
|
|
|
if minetest.get_current_modname() then
|
|
|
|
-- Loaded during load time, register callbacks after load time
|
|
|
|
minetest.register_on_mods_loaded(register_callbacks)
|
|
|
|
else
|
|
|
|
-- Loaded after load time, register callbacks immediately
|
|
|
|
register_callbacks()
|
|
|
|
end
|
2021-03-25 17:46:04 +01:00
|
|
|
|
|
|
|
minetest.register_globalstep(function()
|
2021-03-27 20:10:49 +01:00
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
local item, index = player:get_wielded_item(), player:get_wield_index()
|
|
|
|
local playerdata = players[player:get_player_name()]
|
|
|
|
if not playerdata then return end
|
|
|
|
local previous_item, previous_index = playerdata.wield.item, playerdata.wield.index
|
|
|
|
if item:get_name() ~= previous_item or index ~= previous_index then
|
|
|
|
playerdata.wield.item = item
|
|
|
|
playerdata.wield.index = index
|
|
|
|
modlib.table.icall(registered_on_wielditem_changes, player, previous_item, previous_index, item)
|
|
|
|
end
|
|
|
|
end
|
2021-03-25 17:46:04 +01:00
|
|
|
end)
|