Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.acuddlyheadcrab.testplugin;
- import java.util.Date;
- import java.util.HashMap;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.plugin.java.JavaPlugin;
- public class TestPlugin extends JavaPlugin{
- public static HashMap<CommandSender, Date> lastcmdtime;
- private int cmdcooldown = 3;
- public void onEnable() {
- // make sure to load the hash map here or else you'll get an NPE
- lastcmdtime = new HashMap<CommandSender, Date>();
- }
- public void onDisable() {}
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
- // this is just a test command
- if(cmd.getName().equalsIgnoreCase("test")){
- if(checkLastCmdTime(sender, cmdcooldown*1000)){
- } else sender.sendMessage("You must wait to do this!");
- // disadvantage here is that it doesn't return the seconds you have left to do it
- }
- return true;
- }
- // this is used to get when they will be able to use the cmd again
- public final static Date getCooldownTime(Date date, int cooldownmillis){
- return new Date(date.getTime()+cooldownmillis);
- }
- // as it says, this gets the current time via the java.util.Date class
- public final static Date getCurrentDate(){
- return new Date(System.currentTimeMillis());
- }
- /* returns true if the current time is after their required cooldown time
- * OR if they are not in the hash map yet
- */
- public static boolean checkLastCmdTime(CommandSender sender, int cooldownmillis){
- if(lastcmdtime.containsKey(sender)){
- Date cooldowntime = getCooldownTime(lastcmdtime.get(sender), cooldownmillis);
- return getCurrentDate().after(cooldowntime);
- } else
- lastcmdtime.put(sender, getCurrentDate()); return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement