Tutorial Creating cooldown in event

Discussion in 'Resources' started by NortherKnight, Aug 1, 2015.

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

    NortherKnight

    I just want to help those who just started coding and dont know how to create cooldowns
    Code:
    public class EDelay implements Listener
    {
        public HashMap<String, Long> cooldown; // creating hashmap named cooldown
        Main plugin;
        public EDelay(Main passedPlugin) // This class isn't in the main class so connecting it
        {
            this.plugin = passedPlugin;
            cooldown = new HashMap<String, Long>(); // declaring cooldown so it can be used in event
        }
       
        @EventHandler
        public void onProjectileLaunch(ProjectileLaunchEvent e)
        {
            int seconds = 5; // how much cooldown we want
            Projectile pro = e.getEntity();
            if(pro.getShooter() instanceof Player)
            {
                if(pro.getType() == EntityType.ENDER_PEARL) // if player throws ender pearl
                {
                    Player player = (Player) pro.getShooter();
                    if(cooldown.containsKey(player.getName())) // if cooldown has players name in it (on first trow cooldown is empty)
                    {
                        long timer = (cooldown.get(player.getName())/1000 + seconds) - System.currentTimeMillis()/1000; // geting time in seconds
                        if(!(timer < 0)) // if timer is still more then 0 or 0
                        {
                            player.sendMessage("You have to wait for " + timer + " seconds!");
                            e.setCancelled(true);
                        }
                        else // if timer is done
                        {
                            cooldown.remove(player.getName()); // removing player from HashMap
                        }
                    }
                    else // if cooldown doesn't have players name in it
                    {
                        cooldown.put(player.getName(), System.currentTimeMillis()); // adding players name + current system time in miliseconds
                    }
                }
            }
        }
    }
    First we get what time we added player into HashMap then we divide it with 1000 so we get seconds and add how many seconds we want to have.
    Then we just subtract it with the current time in seconds(which is increasing over time)
    Code:
    long timer = (cooldown.get(player.getName())/1000 + seconds) - System.currentTimeMillis()/1000;
     
  2. Moved to Resources
     
  3. Offline

    mine-care

    Umm please fix the following:
    - access modifiers, don't forget the private modifier!
    - for efficiency purposes don't initialize the delay time in seconds each time the event executes instead have a constant field to hold this value.
    - isn't !(timer<0) the same as timer >= 0 ?
    Also consider using the TimeUnit enum to help yourself :- )
     
  4. Offline

    NortherKnight

    @mine-care tnx for suggestion im still learning
     
  5. Offline

    MCMatters

    @NortherKnight why are you making resources without good knowledge of the bukkit api
     
  6. Offline

    NortherKnight

    @MCMatters because so many cooldown tutorials are just way to complicated for new programmers
     
  7. Offline

    Eos

    This is as complicated as the other ones, whats the problem with just using a simple scheduler.

    Also why are you defining the hashmap in a constructor and why is your hashmap public?
    Also why are you defining the main class in a constructor if you aren't even using it?
     
  8. Offline

    mcdorli

    This creates unneccessary lagg on the server, just save the last time stamp the player used the item, and cancel the event, if a certain amount of time didn't passed away. Or connect it to a wither bossbar loading, that's what I do most of the time.
     
  9. Offline

    Mrs. bwfctower

    @mcdorli Where is that lag coming from? If statements? Subtracting numbers?
     
Thread Status:
Not open for further replies.

Share This Page