Solved Bukkit Scheduler can't be canceled on death.

Discussion in 'Spigot Plugin Development' started by Nosmakos, May 14, 2016.

  1. i can't cancel the task when the player is dead anyone know why? if a player die before effects happend i want the task to be stopped but it's not working :/

    Code (Text):
       
        @EventHandler
        public void onZombieHitPlayer(final EntityDamageByEntityEvent e) {
            if (((e.getDamager() instanceof Zombie)) && ((e.getEntity() instanceof Player))) {
                final Player player = (Player) e.getEntity();
                if (!this.effects.contains(player.getPlayer().getUniqueId())){
                    effects.add(player.getPlayer().getUniqueId());
                 
                    e.getEntity().sendMessage(ChatColor.RED + "You just got bited! You have to find a cure before you die by infection. Use the cure when you get infected!");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                        @Override
                        public void run(){
                            if (player.getHealth() > 0){
                            ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999, 1));
                            ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 999999, 5));
                            ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 999999, 1));
                            }else return;
                        }
                    }, 300L);
                }
            }
        }
     
     
  2. Remove them from the arraylist in a death event then in the task check if they are still inside the list
     
  3. what about this? it seems like it's working at the time.

    Code (Text):
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            Player player = e.getEntity();
            effects.remove(player.getPlayer().getUniqueId());
        }
       
        @EventHandler
        public void onZombieHitPlayer(final EntityDamageByEntityEvent e) {
            if (((e.getDamager() instanceof Zombie)) && ((e.getEntity() instanceof Player))) {
                Player player = (Player) e.getEntity();
                if (!this.effects.contains(player.getPlayer().getUniqueId())){
                    effects.add(player.getPlayer().getUniqueId());
                 
                    e.getEntity().sendMessage(ChatColor.RED + "You just got bited! You have to find a cure before you die by infection. Use the cure when you get infected!");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                        @Override
                        public void run(){
                            Player player = (Player) e.getEntity();
                            if (!effects.contains(player.getPlayer().getUniqueId())){
                                return;
                            }else{
                            ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999, 1));
                            ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 999999, 5));
                            ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 999999, 1));
                            }
                        }
                    }, 300L);
                }
            }
        }
     
    • Like Like x 1
  4. Code (Text):
    package me.tut;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener{
       
        ArrayList<String> effects = new ArrayList<String>();
       
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler public void onDeath(PlayerDeathEvent event) {
            // They died from poison/zombie damage
            effects.remove(event.getEntity().getName());
        }
       
         @EventHandler
            public void onZombieHitPlayer(final EntityDamageByEntityEvent e) {
                if (((e.getDamager() instanceof Zombie)) && ((e.getEntity() instanceof Player))) {
                    final Player player = (Player) e.getEntity();
                    if (!this.effects.contains(player.getPlayer().getUniqueId())){
                        effects.add(player.getPlayer().getUniqueId());
                     
                        e.getEntity().sendMessage(ChatColor.RED + "You just got bited! You have to find a cure before you die by infection. Use the cure when you get infected!");
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                            public void run(){
                                if (effects.contains(e.getEntity().getName())){
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999, 1));
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 999999, 5));
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 999999, 1));
                                }
                            }
                        }, 300L);
                    } else {
                        return;
                    }
                }
            }
    }
     

    Thinking something like this. Also, sorry I used an ArrayList. I noticed you used a hashmap but it should still be the same idea.
     
  5. i used arraylist i never use hashmap :D and cool thanks mine works too and i made an event on joining tho
    Code (Text):
        @EventHandler
        public void onJoinnn(final PlayerJoinEvent e) {
            Player player = e.getPlayer();
            if (effects.contains(player.getPlayer().getUniqueId())){
               
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    @Override
                    public void run(){
                        Player player = e.getPlayer();
                        player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 999999, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 999999, 5));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 999999, 1));
                    }
                }, 100L);
            }
        }
     
    • Like Like x 1