Tutorial Cooldowns - Prevent users from executing commands

Discussion in 'Resources' started by Matthewenderle, Mar 17, 2018.

Thread Status:
Not open for further replies.
  1. Here is a way that I've been keeping a tab for cooldowns. It's actually simpler than one would think.

    PHP:
    public static HashMap<PlayerLongcooldownUsers = new HashMap<PlayerLong>(); // This is where the users will be stored if they have a cooldown hold

    /**
    * Starts a scheduler in Bukkit to monitor the cooldown
    *
    * @author MatthewEnderle
    * @param delay Amount in seconds to wait to run
    * @param cooldown Amount in seconds to place user in cooldown list
    */
    public void cooldownSchedule(Integer delayInteger cooldown) { // Delay is
        
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("Your plugin"), new Runnable() {
       
            @
    Override
            
    public void run() {
               for (
    Player p cooldownUsers.keySet()) { // searching for the player in the cooldownUsers Hashmap
                   
    if (cooldown >= ((System.currentTimeMillis() / 1000L) - cooldownUsers.get(p))) { // Simple math
                       
    cooldownUsers.remove(p); // Removing the player because they are past the cooldown period
                   
    // User is not out of cooldown period
               
    // Could not find user in table, prob never ran command
            
    }

        }, 
    1L , (longdelay 20);
    }
    You will need to start the task where ever you want. In most cases this is done in onEnable. It doesn't have to be. You will also want to clear the cooldown just to be safe when the server stops or the plugin is reloaded.
    PHP:
    public void onEnable() {
        
    cooldownSchedule(3300); // Run the task
    }
        public 
    void onDisable() {
        
    cooldownUsers null// Empty the HashMap
    }

    To actually block someone from running a command until their cooldown completes place this:
    PHP:
    cooldownUsers.put(playerSystem.currentTimeMillis() / 1000L);
    Multiple Cooldown Guide (open)

    Create a HashMap per cooldown
    PHP:
    public static HashMap<PlayerLongcooldownKit = new HashMap<PlayerLong>();
    public static 
    HashMap<PlayerLongcooldownTeleport = new HashMap<PlayerLong>();
    Replace the one for loop inside the run() function with multiple ones like so:
    PHP:
    public void run() {
       for (
    Player p cooldownKit.keySet()) { // searching for the player in the cooldownUsers Hashmap
           
    if (cooldown >= ((System.currentTimeMillis() / 1000L) - cooldownKit.get(p))) { // Simple math
               
    cooldownKit.remove(p); // Removing the player because they are past the cooldown period
           
    // User is not out of cooldown period
       
    // Could not find user in table, prob never ran command
       
    for (Player p cooldownTeleport.keySet()) { // searching for the player in the cooldownUsers Hashmap
           
    if (cooldown >= ((System.currentTimeMillis() / 1000L) - cooldownTeleport.get(p))) { // Simple math
               
    cooldownTeleport.remove(p); // Removing the player because they are past the cooldown period
           
    // User is not out of cooldown period
       
    // Could not find user in table, prob never ran command
    }
    Add the new HashMaps to the onDisable:
    PHP:
    public void onDisable() {
        
    cooldownKit null// Empty the HashMap
        
    cooldownTeleport null// Empty the HashMap
    }
    Finally manipulate the users via this
    PHP:
    cooldownTeleport.put(playerSystem.currentTimeMillis() / 1000L);
    cooldownKit.put(playerSystem.currentTimeMillis() / 1000L);
     
    Zombie_Striker likes this.
  2. Offline

    Zombie_Striker

    @Matthewenderle
    Neat, however there is one problem; you never put the cooldown time in the hashmap.

    Also, so that each version of the plugin is not plugin-name-dependant, you could require a JavaPlugin instance.
     
  3. Offline

    timtower Administrator Administrator Moderator

    @Matthewenderle Might also want to use UUID'S instead kd player objects.
     
  4. Offline

    MightyOne

    Arent BukkitRunnables the newer way to do this?
     
  5. Offline

    Dai_Kunai

    The only problem I could see is that the cooldowns would disappear when the server is turned off.
    Right?

    The hashmap would not keep its values.
     
  6. Offline

    KarimAKL

    @Eccentric_Gamer Then save the hashmap entries in a file onDisable and load it onEnable.
     
    Dai_Kunai likes this.
  7. Offline

    Dai_Kunai

    @KarimAKL Yeah that's how I do it (cooldowns.yml), but I don't see that here is all I'm saying.

    EDIT: Is there a better way, by any chance. I know essentials and other big plugins and big minecraft servers don't have files for it. Do they save cooldowns in something like mySQL. The essentials /heal, /feed, and kits all have cooldowns which continue even when the server is turned offline. How do they work?
     
    KarimAKL likes this.
  8. Offline

    CraftCreeper6

    @Eccentric_Gamer
    Whatever way they use it's likely time consuming and potentially expensive. If it's only for a small problem that you don't plan on making money from, then just use a file.
     
Thread Status:
Not open for further replies.

Share This Page