Solved It Is possible to save and load a cooldown?

Discussion in 'Plugin Development' started by Gonmarte, Apr 20, 2016.

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

    Gonmarte

    Hi,
    I have a cooldown, when i do the command /hi it will run a cooldown of 10 seconds before i do /hi again.
    If i do /reload and the data of the cooldown will be gone. I tried to save my hashmap to a file, but it didnt make any difference. If i do /hi and then /reload , it wont be couting the cooldown, the cooldown will be gone, i have to do the command again. Im not sure if this is possible, so just reply below.

    Code:
    package me.gonmarte;
    
    import java.util.HashMap;
    import java.util.Map.Entry;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
      
        public void onEnable() {
            if(getConfig().contains("cd")){
                for(String key : getConfig().getConfigurationSection("cd.").getKeys(true)){
                    Long value = getConfig().getLong("cd." + key);
                    cd.put(key, value);
                }
            }
        }
      
        public void onDisable() {
            for(Entry<String, Long> e : cd.entrySet()){
                getConfig().set(e.getKey(), e.getValue());
            }
            saveConfig();
        }
      
        HashMap<String, Long> cd = new HashMap<>();
      
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player){
                Player player = (Player) sender;
                if(command.getName().equalsIgnoreCase("hi")){
                    if(cd.containsKey(player.getName()) && cd.get(player.getName()) > System.currentTimeMillis()){
                        long remainingTime = cd.get(player.getName()) - System.currentTimeMillis();
                        int remainingSeconds = (int) (remainingTime/1000);
                        player.sendMessage("Tens de esperar " + remainingSeconds + " para /hi");
                }else{
                    cd.put(player.getName(), System.currentTimeMillis() + (10*1000));
                    player.sendMessage("Hi");
                }
                }
                  
                }
            return false;
        }
      
    }
    
     
  2. Offline

    Liam Townsley

    @Gonmarte I do not think this is possible; And if it is just do not restart the server.
     
  3. Offline

    CraftCreeper6

    @Gonmarte
    Would you want this to count the cooldown time over reloads or not? If so, use date & time.
     
  4. Offline

    Gonmarte

    I do not make plugins for me. I make plugins to post online. So, if some1 do /reload in the mid of the cooldown, that will be a problem.
    What you have in mind? I do not have many experience in that.


    EDIT: I found a way to tell the player that the cooldown was broken. I just checked on the onDisable if the map was empty, if not, i loop through the keys and get the names of the players (could be using uuids but this is only a test) , then i get the player using his name and send the message. So all the payer that were in cooldown will get the meesage.

    But im still interesting in getting a solution for this.
     
    Last edited: Apr 20, 2016
  5. Offline

    I Al Istannen

    @Gonmarte
    You save just e.getKey() as key:
    Code:
            for(Entry<String, Long> e : cd.entrySet()){
                getConfig().set(e.getKey(), e.getValue());
            }
    
    but want to retrieve it as if it was saved with the prefix "cd.":
    Code:
                for(String key : getConfig().getConfigurationSection("cd.").getKeys(true)){
                    Long value = getConfig().getLong("cd." + key);
                    cd.put(key, value);
                }
    You could try adding "cd." before the e.getKey().
     
    Gonmarte likes this.
  6. Offline

    Gonmarte

    *slam my face into the wall*
    Thanks!! :3
     
    Liam Townsley and I Al Istannen like this.
Thread Status:
Not open for further replies.

Share This Page