[Tutorial] Simple Cooldown Timer - Newb Friendly!

Discussion in 'Resources' started by guangong4, Jun 28, 2013.

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

    guangong4

    This is my first tutorial, and I'm still beginning with Java, however, a lot of the cooldown tutorials on here were too complicated for a dummy like me, so I thought of this system that's very easy to understand.

    Cooldown.java
    Code:java
    1. // Don't forget your Package
    2. package your.package.name;
    3. // Import your goodies
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scheduler.BukkitRunnable;
    7.  
    8. //Extend BukkitRunnable
    9. public class Cooldown extends BukkitRunnable {
    10. // Constructor to fetch variables
    11. public MyMainClass plugin;
    12.  
    13. public Cooldown(MyMainClass plugin){
    14. this.plugin = plugin;
    15. }
    16. // This is what happens when you call in this task
    17. public void run() {
    18. //for every person online
    19. for(Player player : Bukkit.getServer().getOnlinePlayers()) {
    20. // Set a cooldown hashmap for the player down by one
    21. plugin.cooldown1.put(player.getName(), plugin.cooldown1.get(player.getName()) - 1);
    22. }
    23. }
    24. }


    MyMainClass.java
    Code:java
    1. package your.package.name;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6. import org.bukkit.scheduler.BukkitTask;
    7.  
    8. public final class MyMainClass extends JavaPlugin {
    9. public Cooldown cooldown;
    10. HashMap<String, Integer> cooldown1;
    11. // create as many "cooldown1", 2, 3, 4, etc as you need.
    12. // Just remember to add them into the cooldown class.
    13. @Override
    14. public void onEnable(){
    15. // All of your enable stuff goes here.
    16. // The run method from your cooldown class will be run every 20 ticks, AKA 1 second
    17. BukkitTask Cooldown = new Cooldown(this).runTaskTimer(this, 20, 20);
    18. // Creates a new hashmap, for a player and the number of seconds left in the cooldown.
    19. cooldown1 = new HashMap<String, Integer>();
    20.  
    21. }
    22. // and your disable stuff.
    23. @Override
    24. public void onDisable() {
    25. }
    26.  


    When you want to put something on a cooldown, do this:
    Code:java
    1.  
    2. // Remember to have a constructor for the main class where this hashmap is stored.
    3. // This below fetches the amount of seconds left and checks if its lower than 0.
    4. if (plugin.cooldown1.get(p.getName()) <= 0) {
    5. // This will then set the cooldown to 10 seconds
    6. plugin.cooldown1.put(p.getName(), 10);
    7. // Do all of your stuff here
    8. }
    9. else {
    10. // Cooldown notifier.
    11. p.sendMessage("You must wait " + plugin.cooldown1.get(p.getName()) + " seconds.");
    12. }
    13.  


    So basically, what this does is create a variable that represents the number of seconds left, and you decrease it by one every second. Then, you can test this variable with other stuff.
     
    TheMintyMate likes this.
  2. Offline

    Ivan

    Code:java
    1.  
    2. HashMap<String,Long> cooldowns = new HashMap<String,Long>();
    3.  
    4. Final int seconds = 60;
    5.  
    6. public boolean hasCooldown(Player player){
    7. if(cooldowns.get(player.getName()) < (System.getCurrentTimeMillis() - seconds*1000))
    8. return false;
    9. else
    10. return true;
    11. }
    12.  
    13. public void activateCooldown(Player player){
    14. cooldowns.put(player.getName(), System.getCurrentTimeMillis());
    15. }
    16.  

    Wrote that under a minute :).
    I think this method is a better way to do it, since it doesn't use a ScheduledTask, is more dynamic and is in less lines of code. I guess you could call it preference though.
     
  3. Offline

    Ultimate_n00b

    Was just about to put something like this :p I wrote a more in-depth class for cooldown handling a while ago
     
  4. Offline

    guangong4

    I would agree that your method is more compact, but I've never been able to wrap my head around getting the time and using it for anything.
     
  5. Offline

    Ultimate_n00b

    Code:java
    1. //hashmap storing the data
    2. public HashMap<String, Date> cooldowns = new HashMap<String, Date>();
    3.  
    4. public static boolean cooldown(final Long mil, final Player p) {
    5. //If the player is registered in the hashmap.
    6. if (cooldowns.containsKey(p.getName())) {
    7. //Check where I put the info in the hashmap, but basically I set the cooldown to current time + extra time. So if the current time catches up to the stored time, then the cooldown is over.
    8. if (cooldowns.get(p.getName()).getTime() > new Date().getTime()) {
    9. return false;
    10. } else {
    11. //Cooldown is over
    12. cooldowns.remove(p.getName());
    13. return true;
    14. }
    15. } else {
    16. //Cooldown is not set, so we will set the cooldown then return false.
    17. cooldowns.put(p.getName() , new Date(new Date().getTime() + mil));
    18. return false;
    19. }
    20. }
    21.  


    Thats my custom cooldown explained (not the same as Ivan, but similar).
     
  6. Offline

    mrgreen33gamer

    I am getting an error right here in the cooldown.class

    Code:
    plugin.cooldown1.put(player.getName(), plugin.cooldown2.get(player.getName()) - 1);
    This part,
    Code:
    plugin.cooldown2
    is giving me an error saying there is not such thing as cooldown2
     
Thread Status:
Not open for further replies.

Share This Page