Delay not working

Discussion in 'Plugin Development' started by Reflxction, Aug 14, 2017.

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

    Reflxction

    Hello,
    I've been encountering a little problem while trying to make a delay.
    EventHandler:
    Code:
        @EventHandler
        public void onFish(PlayerFishEvent event) {
            ItemStack rod = new ItemStack(Material.FISHING_ROD);
            rod.addEnchantment(Enchantment.DURABILITY, 2);
            final Player p = event.getPlayer();
            if (cooldown.containsKey(p)) {
                if (p.getItemInHand() == rod) {
                    p.sendMessage(
                            org.bukkit.ChatColor.translateAlternateColorCodes('&', "&3[&eSoupPvP&3] &7You have to wait &d"
                                    + delay.get(p) + " &7seconds until you can use your ability again!"));
                    event.setCancelled(true);
                }
            }
            cooldown.put(p, 30);
            delay.put(p, 30);
            task.put(p, new BukkitRunnable() {
                public void run() {
                    delay.put(p, delay.get(p) - 1);
                    if (delay.get(p) == 0) {
                        delay.remove(p);
                        task.remove(p);
                        cooldown.remove(p);
                        cancel();
                    }
                }
            });
    
            task.get(p).runTaskTimer(this, 20, 20);
    
        }
    HashMaps:
    Code:
    HashMap<Player, Integer> cooldown = new HashMap();
        private HashMap<Player, Integer> delay;
        private HashMap<Player, BukkitRunnable> task;
    
        @Override
        public void onEnable() {
            delay = new HashMap<Player, Integer>();
            task = new HashMap<Player, BukkitRunnable>();
                     getServer().getPluginManager().registerEvents(this, this);
    }
    I used this method before in a command and it worked. However, it's not now. Any help would be much appreciated. Thanks.

    //EDIT: On stacktraces, it says the error line is "delay.put(p, delay.get(p) - 1);". It says something is null here.
     
  2. Offline

    Zombie_Striker

    @xTechno_
    Do not create the itemstack multiple times. Create it once and store it as a field.

    This will almost never be the case, as == compares memory space. Use .isSimilar to compare itemstack types.

    Main issue: Most likely, there are two instances of the runnable running at the same time, since the event is called for both the main and off hand. Make sure the event is only for the main hand.

    Also, you are not checking if a delayed task is already running. Make sure that when you create a new cooldown, that the old runnable is canceled.

    [Edit] You are also not returning after you cancel the event. Currently, you're creating a new cooldown even when they are in the cooldown.
     
    Reflxction likes this.
Thread Status:
Not open for further replies.

Share This Page