How do i make a command cooldown for my plugin?

Discussion in 'Plugin Development' started by IcyRelic, Sep 23, 2012.

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

    IcyRelic

    Exactly what the title says
     
  2. Offline

    nala3

    Have you even tried?..
     
  3. Offline

    IcyRelic

    yea the only thing i can think of is log it in a list and have a repeating task checking and editing a list but how would i split the playername from the time left on cooldown
     
  4. Offline

    nala3

    Code:
    //pseudo code
    int cooldownTime = 30;
    Map<String, Long> cooldowns; //Player name, Time in milliseconds when the player ran cooldown starts
     
    if(cooldowns.get(playerName) - System.currentTimeInMillis() * 1000 < cooldownTime) {
    //the player is still cooling down.
    } 
    
     
    Orange Tabby likes this.
  5. Offline

    IcyRelic

    ok so
    i can take the hash map and use that to check in the repeating task every 20 ticks?
     
  6. Offline

    MrFigg

    Have a HashMap<String, Long> save the player name, and the last time they used the command. Before you run the command check if the HashMap.containsKey(player.getName()) and if so check if HashMap.get(player.getName()) is <= the current time minus your cool down time.

    No need for a repeating task. Just put it at the start of the command.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  7. Offline

    IcyRelic

    its not working its giving nullpointer when i try to do anything in the code with the hashmap

    Code:
    HashMap<String, Long> cooldowns;
    Long left = cooldowns.get(p.getName());
                int cooldownTime = getConfig().getInt("SpawnX.Cooldown.Cooldown");
                if(cooldown){
                    if(cooldowns.get(p.getName()) - System.currentTimeMillis() * 1000 < cooldownTime) {
                        //the player is still cooling down.
                            p.sendMessage("You cant use that commands for another "+left+"? seconds!");
                   
                       
                }else{
    my code here
    }
    but i always get this

    00:00:40 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spaw
    n' in plugin SpawnX Reborn v3.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    92)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:878)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)

    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
    at me.icyrelic.com.SpawnX.onCommand(SpawnX.java:89)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 15 more
    >
     
  8. Offline

    MrFigg

    You didn't initialize cooldowns, along with some other mistakes. Here's an example;
    Code:
    public class CooldownExample extends JavaPlugin {
        public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
     
        public void onEnable() {
            // Do whatever
        }
     
        public void onDisable() {
            // Do whatever
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            int cooldownTime = 60; // Get number of seconds from wherever you want
            if(cooldowns.containsKey(sender.getName())) {
                long secondsLeft = ((cooldowns.get(sender.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
                if(secondsLeft>0) {
                    // Still cooling down
                    sender.sendMessage("You cant use that commands for another "+ secondsLeft +" seconds!");
                    return true;
                }
            }
            // No cooldown found or cooldown has expired, save new cooldown
            cooldowns.put(sender.getName(), System.currentTimeMillis());
            // Do Command Here
            return true;
        }
    }
     
    Ragnarok_ and pcgamers123 like this.
Thread Status:
Not open for further replies.

Share This Page