Advertisement
acuddlyheadcrab

CmdCooldownPlugin

Apr 18th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package com.acuddlyheadcrab.testplugin;
  2.  
  3. import java.util.Date;
  4. import java.util.HashMap;
  5.  
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandSender;
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. public class TestPlugin extends JavaPlugin{
  11.    
  12.     public static HashMap<CommandSender, Date> lastcmdtime;
  13.     private int cmdcooldown = 3;
  14.    
  15.     public void onEnable() {
  16. //      make sure to load the hash map here or else you'll get an NPE
  17.         lastcmdtime = new HashMap<CommandSender, Date>();
  18.     }
  19.    
  20.     public void onDisable() {}
  21.    
  22.     @Override
  23.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
  24. //      this is just a test command
  25.         if(cmd.getName().equalsIgnoreCase("test")){
  26.             if(checkLastCmdTime(sender, cmdcooldown*1000)){
  27.                
  28.             } else sender.sendMessage("You must wait to do this!");
  29. //          disadvantage here is that it doesn't return the seconds you have left to do it
  30.         }
  31.         return true;
  32.     }
  33.    
  34. //  this is used to get when they will be able to use the cmd again
  35.     public final static Date getCooldownTime(Date date, int cooldownmillis){
  36.         return new Date(date.getTime()+cooldownmillis);
  37.     }
  38.    
  39. //  as it says, this gets the current time via the java.util.Date class
  40.     public final static Date getCurrentDate(){
  41.         return new Date(System.currentTimeMillis());
  42.     }
  43.    
  44.     /* returns true if the current time is after their required cooldown time
  45.     * OR if they are not in the hash map yet
  46.     */
  47.     public static boolean checkLastCmdTime(CommandSender sender, int cooldownmillis){
  48.         if(lastcmdtime.containsKey(sender)){
  49.             Date cooldowntime = getCooldownTime(lastcmdtime.get(sender), cooldownmillis);
  50.             return getCurrentDate().after(cooldowntime);
  51.         } else
  52.             lastcmdtime.put(sender, getCurrentDate()); return true;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement