Solved I am trying to add a potioneffect in a timer

Discussion in 'Spigot Plugin Development' started by Alexalien, Jun 1, 2016.

  1. Okay im making a plugin for someone and they want potion effects to stack up so they get a new one every 50 seconds or something this code dosn't work

    Code (Text):
        public static void infected(final Player player) {
         
         
            if(!(Disease.Infected.contains(player))) {
                Disease.Infected.add(player);
         
            player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING,Integer.MAX_VALUE, 0));
            player.sendMessage(ChatColor.RED + "You have been infected");
         
         
         
            final Player p = player;
            Timer timer = new Timer();
            TimerTask task = new TimerTask(){
                public void run(){
                    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,Integer.MAX_VALUE, 0));
                 
                }
            };
         
            timer.schedule(task, 60000);
         
            Timer timer1 = new Timer();
            TimerTask task1 = new TimerTask(){
                public void run(){
                    p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS,Integer.MAX_VALUE, 0));
                }
            };
         
            timer1.schedule(task1, 110000);
            Timer timer2 = new Timer();
            TimerTask task2 = new TimerTask(){
                public void run(){
                    p.addPotionEffect(new PotionEffect(PotionEffectType.POISON,Integer.MAX_VALUE, 4));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER,Integer.MAX_VALUE, 4));
                 
                 
                 
                }
            };
         
            timer2.schedule(task2, 290000);
            }


        }

    and error code

    Code (Text):
    [13:28:14 WARN]: Exception in thread "Timer-0"
    [13:28:14 WARN]: java.lang.IllegalStateException: Asynchronous effect add!
    [13:28:14 WARN]:        at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14)
    [13:28:14 WARN]:        at net.minecraft.server.v1_8_R3.EntityLiving.addEffect(EntityLiving.java:582)
    [13:28:14 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity.addPotionEffect(CraftLivingEntity.java:303)
    [13:28:14 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity.addPotionEffect(CraftLivingEntity.java:293)
    [13:28:14 WARN]:        at me.alexalien.Flox$1.run(Flox.java:43)
    [13:28:14 WARN]:        at java.util.TimerThread.mainLoop(Unknown Source)
    [13:28:14 WARN]:        at java.util.TimerThread.run(Unknown Source)
     
  2. Choco

    Moderator

    It literally says it in the stacktrace. You're adding a potion effect asynchronously.
     
  3. But how can i get around this?
     
  4. Choco

    Moderator

    Run it synchronously?
    All the code surrounding p.addPotionEffect() can just go. There's no need for it. Is there any reason you needed to put this in a TimerTask (Which is part of the Java library, and not Bukkit. Use BukkitRunnable's instead for future reference)
     
  5. But i need the timer to add the potion effect at different times can you show me how to do that with bukkitrunnables please?
     
  6. So where would i put my code and stuff and what does the seconds * 20 mean? because i get an error also what does the "plugin" mean?
     
  7. Dont worry i understand the seconds thing but what about plugin?
     
  8. plugin is the instance of your main class. Since runnables can only run inside the main class you have 2 options, or pass the instance through a constructor or use the runnable inside your main class.
     
  9. Thanks loads its all fixed
     
  10. plugin is your plugin, if you are using this on main class you can use 'this' or use JavaPlugin.getProvidingPlugin(YourMainClass.class)

    * 20 is because 1 second is 20 ticks
     
    • Optimistic Optimistic x 1
  11. I also want to cancel on the tasks on the players death and when they are cured how could i do this?
    Code (Text):
        public static void infected(final Player player) {
           
           
            if(!(Disease.Infected.contains(player))) {
                Disease.Infected.add(player);
           
            player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING,Integer.MAX_VALUE, 0));
            player.sendMessage(ChatColor.RED + "You have been infected");
           
           
           
            new BukkitRunnable() {
                  @Override
                  public void run() {
                      player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,Integer.MAX_VALUE, 0));
                  }
                }.runTaskLater(plugin, 60 * 20);
               
               
               
            new BukkitRunnable() {
                  @Override
                  public void run() {
                       player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS,Integer.MAX_VALUE, 0));
                   }
                }.runTaskLater(plugin, 110 * 20);
               
               
            new BukkitRunnable() {
                @Override
                public void run() {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.POISON,Integer.MAX_VALUE, 4));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER,Integer.MAX_VALUE, 4));
                   
                }
              }.runTaskLater(plugin, 290 * 20);      
            }
    }
     
  12. Code (Text):
    // ...
    public void run() {
       if (!Disease.Infected.contains(player)) {
           cancel();
       }
    // ....
    }
     
  13. What if they get infected after they die wont it just run through it is there any way i can cancel the task outside the task?
    so cancel it before it does the task?
     
  14. is there any way i can make it public and cancel it from the other class?
     
  15. You need to save the instance of task, then use instance.cancel();
     
  16. I have no idea how to do that to be honest XD im such a noob at this i hardly ever use timers or anything of the sort could you please expalin how i could do that?
     
  17. If you want to cancel all task when player die you can store it in an Map<UUID, List<BukkitRunnable>>

    then when player dies you just need to use an foreach to cancel each BukkitRunnable.
     
  18. You need cancel all tasks when player die ?