player_manager.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. local function start_cooldown() end
  2. -- on_cooldown non viene creato in automatico al login, bensì al primo utilizzo di un oggetto con cooldown.
  3. -- Questo perché si potrebbero avere zero oggetti con cooldown, creando tabelle inutili
  4. local on_cooldown = {} -- KEY: player name; VALUE: {items, on, cooldown}
  5. local S = minetest.get_translator("magic_compass")
  6. minetest.register_on_player_receive_fields(function(player, formname, fields)
  7. if formname ~= "magic_compass:GUI" then return end
  8. if fields.EMPTY or fields.quit or fields.key_up or fields.key_down then return end
  9. local ID = string.match(dump(fields), "%d+")
  10. local item = magic_compass.items[tonumber(ID)]
  11. local p_name = player:get_player_name()
  12. -- se non ha i permessi, annullo
  13. if item.privs and not minetest.check_player_privs(p_name, minetest.string_to_privs(item.privs, ", ")) then
  14. minetest.chat_send_player(p_name, minetest.colorize("#e6482e", S("[!] This location is not available for you at the moment!")))
  15. minetest.sound_play("magiccompass_teleport_deny", {
  16. to_player = p_name
  17. })
  18. return end
  19. -- se è in cooldown, annullo
  20. if item.cooldown and on_cooldown[p_name] and on_cooldown[p_name][ID] then
  21. 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])))
  22. minetest.sound_play("magiccompass_teleport_deny", {
  23. to_player = p_name
  24. })
  25. return end
  26. -- se non passa gli eventuali callback, annullo
  27. for _, callback in ipairs(magic_compass.registered_on_use) do
  28. if not callback(player, ID, item.desc, item.pos) then
  29. minetest.sound_play("magiccompass_teleport_deny", {
  30. to_player = p_name
  31. })
  32. return
  33. end
  34. end
  35. -- teletrasporto
  36. player:set_pos(minetest.string_to_pos(item.pos))
  37. minetest.sound_play("magiccompass_teleport", {
  38. to_player = p_name
  39. })
  40. -- eventuali callback dopo l'uso
  41. for _, callback in ipairs(magic_compass.registered_on_after_use) do
  42. callback(player, ID, item.desc, item.pos)
  43. end
  44. -- eventuale cooldown
  45. if item.cooldown then
  46. if not on_cooldown[p_name] then
  47. on_cooldown[p_name] = {}
  48. end
  49. -- lo imposto
  50. on_cooldown[p_name][ID] = item.cooldown
  51. -- e lo avvio
  52. run_cooldown(p_name, ID)
  53. end
  54. end)
  55. function run_cooldown(p_name, ID)
  56. on_cooldown[p_name][ID] = on_cooldown[p_name][ID] -1
  57. if on_cooldown[p_name][ID] == 0 then
  58. on_cooldown[p_name][ID] = nil
  59. else
  60. minetest.after(1, function()
  61. run_cooldown(p_name, ID)
  62. end)
  63. end
  64. end