Only handle event after a certain event has happened

Discussion in 'Plugin Development' started by xX4w3s0m3Xx, Jun 2, 2017.

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

    xX4w3s0m3Xx

    I don't really know how to word the title, but this is my problem.
    I am trying to make a plugin, where you throw an enderpearl and it first waits 3 seconds before you teleport and you don't get fall damage.
    The problem is that I don't know how I can disable the fall damage only after the projectile launch event has happened. This is the code I have right now:
    Code:
    package me.Ruud.UltimateEnderpearl;
    
    import org.bukkit.Location;
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileLaunchEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    /**
    * @author Ruud.
    */
    public class UltimateEnderpearl extends JavaPlugin implements Listener{
    
        int time;
        int taskID;
    
        @Override
        public void onEnable() {
            PluginManager pluginManager = getServer().getPluginManager();
            pluginManager.registerEvents(this, this);
        }
    
        @Override
        public void onDisable() {
    
        }
    
        @EventHandler
        public void onThrow(ProjectileLaunchEvent e) {
            if (e.getEntity() instanceof EnderPearl) {
                Projectile enderPearl = e.getEntity();
                if (enderPearl.getShooter() instanceof Player) {
                    Player player = (Player) enderPearl.getShooter();
                    e.setCancelled(true);
                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
                        Location location = enderPearl.getLocation();
                        location.add(0, 10, 0);
                        player.teleport(location);
                    }, 60L);
                }
            }
        }
    
    }
    
     
  2. Offline

    Caderape2

    @xX4w3s0m3Xx Listen to PlayerTeleportEvent
    if the cause it's TeleportCause.enderpearl, cancel the event, and teleport the player to the location event.getTo() with a scheduler.
     
  3. Offline

    YoloSanta

    Code:
    @EventHandler
    public void onCancelFallDamage(EntityDamageEvent e) {
        if(e.getEntity() instanceof Player) { //Checks to see if the entity that is taking damage is a player
            if(e.getCause() == DamageCause.FALL) { //if the cause of damage is fall damage
                e.setCancelled(true); //you cancel the event.
            }
        }
        }
     
  4. Offline

    xX4w3s0m3Xx

    Ah, that's a good idea, thank you very much!

    Wouldn't this disable all the fall damage?
     
  5. Offline

    Horsey

    If you don't mind losing particle effects and such, use Caderape2's method. Otherwise, you can create a Boolean, and set the Boolean to true if someone teleports with an enderpearl. Then, listen for an entity damage event, if the entity is a player, and the damage is fall damage AND the Boolean we set earlier is true, cancel the event, and set the Boolean to false again.
     
  6. Offline

    xX4w3s0m3Xx

    Oh, Just like yolosanta suggested, but with the if statement and Boolean?


    Verzonden vanaf mijn iPhone met Tapatalk
     
  7. Offline

    YoloSanta

    Yes, this would listen for when a player is about to take fall damage and cancels the action so they don't
     
  8. Offline

    Horsey

    @xX4w3s0m3Xx Pretty much, except I forgot that you need a hashmap containing uuids and booleans, not just one boolean, to make sure it's the same person who used the enderpearl.
     
  9. Offline

    xX4w3s0m3Xx

    Thank you for helping, I fixed it with another way, because the person wanted to make the player invincible for 5 seconds, so that's what I did.
    Here are the events I used and what I added to them:
    Code:
    @EventHandler
        public void onThrow(ProjectileLaunchEvent e) {
            if (e.getEntity() instanceof EnderPearl) {
                Projectile enderPearl = e.getEntity();
                if (enderPearl.getShooter() instanceof Player) {
                    Player player = (Player) enderPearl.getShooter();
                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
                        Location location = enderPearl.getLocation();
                        location.add(0, 10, 0);
                        player.setNoDamageTicks(5 * 20);
                        player.teleport(location);
    
                    }, 60L);
                }
            }
        }
    
        @EventHandler
        public void onTeleport(PlayerTeleportEvent e) {
            if (e.getCause() == PlayerTeleportEvent.TeleportCause.ENDER_PEARL) {
                e.setCancelled(true);
            }
        }
    UPDATE:
    I did it with only one eventhandler:
    Code:
    @EventHandler
        public void onTeleport(PlayerTeleportEvent e) {
            Player player = e.getPlayer();
            if (e.getCause() == PlayerTeleportEvent.TeleportCause.ENDER_PEARL) {
                this.getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
                    Location location = e.getTo();
                    location.add(0, 10, 0);
                    PotionEffect invincibility = new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 5 * 20, 4);
                    player.addPotionEffect(invincibility);
                    player.teleport(location);
                }, 3 * 20);
                e.setCancelled(true);
            }
        }
     
    Last edited: Jun 5, 2017
Thread Status:
Not open for further replies.

Share This Page