Cancel Event

Discussion in 'Plugin Development' started by ewrs, Jul 14, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    ewrs

    Hello. I have my own system of murders on the server. When the damage is greater than the health of the player, it teleports to spawn.

    The problem is that when the player teleports, PlayerTeleportEvent is triggered. I have a question. Can I cancel it at the right time?

    I say to those who do not understand, teleportation create particles, I need to cancel them if the player teleports to spawn. After that he should work, as before(PlayerTeleportEvent).

    Sorry for my english, he's not the best. :(
     
  2. Offline

    Zombie_Striker

    @ewrs
    There is no good way to do this. You can try canceling and undoing the cancel the event for the priorities above and below the one the other plugin uses, but that also risks other plugins breaking if they also listen for that event.
     
  3. Offline

    ewrs

    Did not quite understand your idea ...
     
  4. Offline

    Zombie_Striker

    @ewrs
    Basically, you should not be doing this unless you are okay with potentially breaking other plugins.

    If you are okay with that, using different event priorities, cancel the event in a higher priority so that other plugin does not run its code, while also un-canceling it in a lower priority so the player will teleport.
     
  5. Offline

    ewrs

    but how can I cancel it? ..
     
  6. Offline

    Zombie_Striker

    @ewrs
    Event#setCanceled(true);
     
  7. Offline

    ewrs

    I need to cancel not a given event, but a close one. I.e

    TeleportEvent {
    ...
    }

    EntityDamageByEntityEvent {
    Cancel TeleportEvent
    p.teleport (loc);
    }
     
  8. Offline

    Zombie_Striker

    @ewrs
    Alright, then create a List for players where the telepotation should be canceled, add the player to the list when you need, and only cancel the event if the player is in the List.
     
  9. Offline

    ewrs

    Okay, but how do I make this list for two plugins?
    Teleportation is one plugin, death is another.
     
  10. Offline

    Zombie_Striker

    @ewrs
    What is this other 'death' plugin? If it is the plugin you are working on, the list only needs to be for your plugin.
     
  11. Offline

    ewrs

    1 plugin is responsible for the partys at the output, input and teleportation
    2 plugin for death (teleport to
    spawn and playing effects)
     
  12. Offline

    SavageAvocado

    Kinda like this. All of the code would go in your 'death' plugin.

    Code:
    public class DeathPlugin extends JavaPlugin {
        private ArrayList<Player> deadPlayers;
    
        @Override
        public void onEnable() {
            this.deadPlayers = new ArrayList();
            this.loadListeners();
        }
    
        private void loadListeners() {
            PluginManager pluginManager = this.getServer().getPluginManager();
            pluginManager.registerEvents(new TeleportE (this), this);
            pluginManager.registerEvents(new DeathE(this), this);
        }
    
        public void addDeadPlayer(Player user) {
            this.deadPlayers.add(user);
        }
    
        public void removeDeadPlayer(Player user) {
            this.deadPlayers.remove(user);
        }
    
        public ArrayList<Player> getDeadPlayers() {
            return this.deadPlayers;
        }
    }
    
    Code:
    public class DeathE implements Listener {
        private DeathPlugin plugin;
    
        public DeathE(DeathPlugin plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void onDeathE(EntityDamageByEntityEvent e) {
            Player user = e.getEntity();
    
            if ((user.getHealth() - e.getDamage()) <= 0) {
                this.plugin.addDeadPlayer(user);
                user.teleport(spawnLocation);
            }
        }
    }
    
    Code:
    public class TeleportE implements Listener {
        private DeathPlugin plugin;
    
        public TeleportE (DeathPlugin plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onTeleportE(PlayerTeleportEvent e) {
            Player user = e.getPlayer();
    
            if (!this.plugin.getDeadPlayers().contains(user))
                return;
    
            e.setCancelled(true);
            this.plugin.removeDeadPlayer(user);
        }
    }
    
    Sent from my SM-G920P using Tapatalk
     
  13. Offline

    ewrs

    How do I do this between two plugins ?!
     
  14. Offline

    ewrs

    I need help...
     
  15. Offline

    SavageAvocado

    What more could you need help with? I gave you all of the code.

    Sent from my SM-G920P using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page