2020-06-21 04:26:38 -07:00
local function start_cooldown ( ) end
2020-06-21 08:13:28 -07:00
-- on_cooldown non viene creato in automatico al login, bensì al primo utilizzo di un oggetto con cooldown.
-- Questo perché si potrebbero avere zero oggetti con cooldown, creando tabelle inutili
2020-06-21 04:26:38 -07:00
local on_cooldown = { } -- KEY: player name; VALUE: {items, on, cooldown}
2020-06-21 08:13:28 -07:00
local S = minetest.get_translator ( " magic_compass " )
2020-06-21 04:26:38 -07:00
2020-06-17 08:32:01 -07:00
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
if formname ~= " magic_compass:GUI " then return end
if fields.EMPTY or fields.quit then return end
2020-07-23 03:44:51 -07:00
local ID = string.match ( dump ( fields ) , " %d+ " )
2020-06-21 04:26:38 -07:00
local item = magic_compass.items [ tonumber ( ID ) ]
local p_name = player : get_player_name ( )
2020-07-23 06:28:38 -07:00
minetest.log ( " action " , " ID = " .. ID )
2020-06-21 08:13:28 -07:00
-- se non ha i permessi, annullo
if item.privs and not minetest.check_player_privs ( p_name , minetest.string_to_privs ( item.privs , " , " ) ) then
2020-07-20 12:07:23 -07:00
minetest.chat_send_player ( p_name , minetest.colorize ( " #e6482e " , S ( " [!] This location is not available for you at the moment! " ) ) )
minetest.sound_play ( " magiccompass_teleport_deny " , {
to_player = p_name
} )
2020-06-21 08:13:28 -07:00
return end
2020-06-21 04:26:38 -07:00
-- se è in cooldown, annullo
if item.cooldown and on_cooldown [ p_name ] and on_cooldown [ p_name ] [ ID ] then
2020-07-20 12:07:23 -07:00
minetest.chat_send_player ( p_name , minetest.colorize ( " #e6482e " , S ( " [!] You can't reteleport to this location so quickly! (seconds remaining: @1) " , on_cooldown [ p_name ] [ ID ] ) ) )
minetest.sound_play ( " magiccompass_teleport_deny " , {
to_player = p_name
} )
2020-06-21 04:26:38 -07:00
return end
2020-06-17 08:32:01 -07:00
2020-07-27 07:25:11 -07:00
-- se non passa gli eventuali callback, annullo
for _ , callback in ipairs ( magic_compass.registered_on_use ) do
if not callback ( player , ID , item.desc , item.pos ) then
minetest.sound_play ( " magiccompass_teleport_deny " , {
to_player = p_name
} )
return
end
end
2020-06-21 04:26:38 -07:00
-- teletrasporto
player : set_pos ( minetest.string_to_pos ( item.pos ) )
2020-07-20 12:07:23 -07:00
minetest.sound_play ( " magiccompass_teleport " , {
to_player = p_name
} )
2020-06-17 08:32:01 -07:00
2020-07-27 07:25:11 -07:00
-- eventuali callback dopo l'uso
for _ , callback in ipairs ( magic_compass.registered_on_after_use ) do
callback ( player , ID , item.desc , item.pos )
end
2020-06-21 04:26:38 -07:00
-- eventuale cooldown
if item.cooldown then
2020-06-17 08:32:01 -07:00
2020-06-21 04:26:38 -07:00
if not on_cooldown [ p_name ] then
on_cooldown [ p_name ] = { }
end
2020-06-21 08:13:28 -07:00
-- lo imposto
2020-06-21 04:26:38 -07:00
on_cooldown [ p_name ] [ ID ] = item.cooldown
2020-06-21 08:13:28 -07:00
-- e lo avvio
run_cooldown ( p_name , ID )
2020-06-21 04:26:38 -07:00
end
2020-06-17 08:32:01 -07:00
end )
2020-06-21 04:26:38 -07:00
2020-06-21 08:13:28 -07:00
function run_cooldown ( p_name , ID )
2020-06-21 04:26:38 -07:00
on_cooldown [ p_name ] [ ID ] = on_cooldown [ p_name ] [ ID ] - 1
if on_cooldown [ p_name ] [ ID ] == 0 then
on_cooldown [ p_name ] [ ID ] = nil
else
minetest.after ( 1 , function ( )
2020-06-21 08:13:28 -07:00
run_cooldown ( p_name , ID )
2020-06-21 04:26:38 -07:00
end )
end
end