Solved Can't make cooldown

Discussion in 'Plugin Development' started by MgMaor, Apr 13, 2015.

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

    MgMaor

    Code:
        @EventHandler
        public void onProjectileLaunchDemo(ProjectileLaunchEvent event) {
            Projectile p = event.getEntity();
            if (p instanceof Arrow && p.getShooter() instanceof Player) {
                Player pl = (Player) p.getShooter();
                Vector v = p.getVelocity();
                if (getKitForPlayer(pl) == demoman) {
                    List<Player> cooldown = new ArrayList<Player>();
                    if (cooldown.contains(pl)) {
                        pl.sendMessage("You are on cooldown..");
                        return;
                    }
                    p.remove();
                    TNTPrimed tnt = (TNTPrimed) pl.getWorld().spawnEntity(p.getLocation(), EntityType.PRIMED_TNT);
                    tnt.setVelocity(v.setY(v.getY() + 0.025));
                    tnt.setIsIncendiary(false);
                    tnt.setYield(10);
                    cooldown.add(pl);
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                       
                        @Override
                        public void run() {
                            cooldown.remove(pl);
                        }
                    }, 40);
                }
            }
        }
    The cooldown doesn't work, it allows me to keep spamming my bow..
     
  2. Offline

    mythbusterma

    @MgMaor

    I think you may want to go study some Java. Specifically the concept of scope.
     
    vhbob, KingFaris11 and ItsMattHogan like this.
  3. Offline

    ItsMattHogan

    @MgMaor you've declared your cooldown list inside of your onProjectileLaunchDemo method's scope, therefore, a new instance of the cooldown list will be created every time the onProjectileLaunchDemo method is called.

    @mythbusterma is right
     
  4. Since you seem to not be even intermediate at Java, (no offence), I recommend you don't store Player instances but you store their UUID.
     
  5. Offline

    MgMaor

  6. Offline

    nverdier

  7. Mine may not be relevant to solving yours, but I also would prevent memory leakage. Oh well, guess you want a terrible plugin with memory leakages, TPS lag, etc. I would never even use a scheduler, the more efficient way is storing the UUID and the time in millis into a Map, then checking against this within the delay check method.
     
Thread Status:
Not open for further replies.

Share This Page