Solved Stop and Continue Events

Discussion in 'Spigot Plugin Development' started by FK7, Jul 7, 2021.

  1. FK7

    FK7

    Hello, I have a problem, i have a PlayerMoveEvent (version 1.8) that i would like to be able to obtain the coordinates of a player but only at certain times. How could I do to be able to cancel this event, and when I want from another function to re-activate this event? I know that i can only put a if(...) but i want to totally "pause" the event to dont cause lag.
     
  2. Well, you can use a boolean and check it before getting coords in the event..

    Or another way is (but i have never did it) to unregister the listener and re-add it so the event is not listened when you don't need it
    Like this:
    Code (Java):
    HandlerList.unregisterAll(listener);
     
  3. You really should describe your problem better, but you probably need something like
    Code (Java):
    public class MovementListener implements Listener {
        public ArrayList<PlayerMoveEvent> savedEvents;
        public MovementListener() {
            savedEvents = new ArrayList<PlayerMoveEvent>();
        }
     
        @EventHandler
        public void onMovement(PlayerMoveEvent e) {
            if (/*smth*/) {
                e.setCancelled(true);
                savedEvents.add(e);
            }
            else {
                /*smth*/
            }
        }
    }
    In Main.OnLoad() you would write
    Code (Java):
            MovementListener movementListener = new MovementListener();
            getServer().getPluginManager().registerEvents(movementListener, this);
    Now you can access all canceled events trough movementListener.savedEvents;
     
  4. FK7

    FK7

    And how can i do to load it again without restarting the server?
     
  5. https://xyproblem.info/
     
    • Like Like x 1
    • Agree Agree x 1
  6. As simple as registering it again, like you would do on your onEnable, getServer().getPluginManager().registerEvent(listener, plugin) then when you don't want to listen to it any more just HandlerList.unregisterAll(listener (the same instance as the one registered before))
     
  7. Strahan

    Benefactor

    No, no, no. Don't fiddle with the registration that's a silly way to approach this issue. Simply check if the conditions merit performing the functionality, and if not return (i.e. guard clause).
     
  8. I don't really know what OP is going to use this for, but asking for me: in some cases isn't it better to register and unregister at need? For example if I want to prevent players that are about to teleport to some place from interacting or sending commands, should I have one listener always registered with a list of player that should be listened for or register a new listener every time a player is going to teleport and unregistering it when the player is teleported?
     
  9. FK7

    FK7

    Thanks you very much to all of you, now i know how to do it
     
  10. Strahan

    Benefactor

    IMO, no. There is no need to fiddle with registering/unregistering listeners. If I wanted to prevent players from interacting when teleporting for example, I'd do something like:
    Code (Text):
    main class {
      private Set<UUID> teleportPending = new HashSet<>();

      public void teleporting(UUID u) {
        if (teleportPending.contains(u)) return;

        teleportPending.add(u);
      }

      public boolean isTeleporting(UUID u) {
        return teleportPending.contains(u);
      }
    }

    command class {
      plugin.teleporting(p.getUniqueId());
    }

    public void onPlayerInteract(PlayerInteractEvent e) {
      if (!plugin.isTeleporting(e.getPlayer().getUniqueId())) return;

      e.getPlayer().sendMessage("Can't do that when queued for teleport!");
      e.setCancelled(true);
    }
    Obviously I skipped a lot of detail there but that's basically how I'd handle it.
     
    • Like Like x 1
  11. Got the idea, will take it into consideration, thanks
     
  12. You can always use a controller system with your registered listener. For example, have an interface with methods like onMove, and extend them in a child class along with an empty extension class. In your actual listener class you can reference the implemented interface method and whenever you want to stop listening, just change the child class to the empty one.