Reset the repeating task

Discussion in 'Plugin Development' started by Uhlala, Feb 26, 2017.

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

    Uhlala

    How do I reset the repeating task without having to reload the server?

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        void onHitWW(EntityDamageByEntityEvent e) {
            final Player p = (Player) e.getDamager();
            if (p.getItemInHand().hasItemMeta()) {
                if (p.getItemInHand().getItemMeta().hasDisplayName()) {
                    if (p.getItemInHand().getItemMeta().getDisplayName()
                            .equals("Werewolf Sword")) {
                        if (p.hasPotionEffect(PotionEffectType.SPEED))
                            this.getServer()
                                    .getScheduler()
                                    .scheduleAsyncRepeatingTask(this,
                                            new Runnable() {
    
                                                public void run() {
                                                    if (number != -1) {
                                                        if (number != 0) {
                                                            p.setHealth(p
                                                                    .getHealth() + 1);
                                                            number--;
                                                        } else
                                                            p.sendMessage(ChatColor.RED
                                                                    + "You are no longer healing");
                                                        number--;
                                                    }
                                                }
                                            }, 0L, 20L);
                    }
                }
    
            }
        }
     
  2. Offline

    Zombie_Striker

    @Uhlala
    What do you mean by reset? Do you mean cancel the task?

    Also Never use an async task to access the BukkitAPI. The Bukkit API does not support async tasks, so your setHealth and sendMessage lines may not get triggered. Change it to a Sync repeating task.
     
  3. Online

    timtower Administrator Administrator Moderator

    @Uhlala This looks like a scheduler that gets used too much.
    Every time the player damages something then the runnable gets started, hit something 5 times and it runs very very fast.
    You assume that the damager is a player, check for that before you cast please.
    It looks like number is a global variable, having multiple players will break that effect.
    You never stop the task.


    My suggestion is using 1 runnable that you start in the onEnable and stop in the onDisable.
    With that you have a HashMap<UUID,Long> for players that should get regen.
     
Thread Status:
Not open for further replies.

Share This Page