Solved Command cooldown

Discussion in 'Plugin Development' started by Spark61, Oct 17, 2018.

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

    Spark61

    Hello,
    I'm currently programming a plugin with the / head command, but I do not know how to program a 30 day cooldown for the players. Can you help me? Gladly with code samples. :)

    My Code:

    Code:
    package de.spark61.commands;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import de.spark61.utils.KopfManager;
    import de.spark61.utils.Utils;
    
    
    public class HeadCMD implements CommandExecutor{
         
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if(sender instanceof Player) {
              
                Player p = (Player) sender;
              
                if(p.hasPermission("citybuild.head.admin")) {
                  
                        if(args.length == 1) {
                                          
                        p.getInventory().addItem(new ItemStack[] { KopfManager.playerSkullForName(args[0])});
    
                        p.sendMessage(Utils.prefix + "§aYou have received the head of §e" + KopfManager.getName(args[0]));
    
    //                    without cooldown
                      
                    }else
                        p.sendMessage(Utils.prefix + "§aPlease use §e/head <Player>");
                  
                } else if(p.hasPermission("citybuild.head")) {
                  
                    if(args.length == 1) {
                      
    //                    with cooldown for players
                      
                    }
                  
                } else
                    p.hasPermission(Utils.perms);
              
            }
          
            return false;
        }
      
      
    
    
      
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @Spark61 Store the System.currentMs() or currentMillis (you will figure it out) + 30 days in millis in the config.
    Then check the config value and see if the current System time is bigger then the config one, if it is then the cooldown has passed.
     
  3. Offline

    Spark61

    and how does it work?
     
  4. Online

    timtower Administrator Administrator Moderator

    @Spark61 Start by saving the time to the config after adding 30 days.
     
  5. Offline

    Spark61

    Could you please give me some code examples this is my first plugin and I just have no idea how to do that
     
  6. Online

    timtower Administrator Administrator Moderator

    I don't spoonfeed.
     
  7. Offline

    Spark61

    is that 30 days? 2,592e + 9
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    Spark61

    2,592,000,000 is right?
     
  10. Online

    timtower Administrator Administrator Moderator

    Probably.
     
  11. Offline

    Spark61

    So how can I run the time now?
    Code:
    package de.spark61.commands;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    import de.spark61.CityBuild;
    import de.spark61.utils.KopfManager;
    import de.spark61.utils.Utils;
    
    
    public class HeadCMD implements CommandExecutor{
       
        HashMap<UUID, Long> cooldown = new HashMap<>();
         
        int time = CityBuild.getInstance().getConfig().getInt("time");
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
       
            if(sender instanceof Player) {
           
                Player p = (Player) sender;
           
                if(p.hasPermission("citybuild.head.admin")) {
               
                        if(args.length == 1) {
                                       
                        p.getInventory().addItem(new ItemStack[] { KopfManager.playerSkullForName(args[0])});
    
                        p.sendMessage(Utils.prefix + "§aYou have received the head of §e" + KopfManager.getName(args[0]));
    
    //                    without cooldown
                   
                    }else
                        p.sendMessage(Utils.prefix + "§aPlease use §e/head <Player>");
               
                } else if(p.hasPermission("citybuild.head")) {
               
                    if(args.length == 1) {
                   
    //                    with cooldown for players
                     
                        if(CityBuild.getInstance().getConfig().isSet("cooldown." + p.getName())) {
                         
                            p.sendMessage (Utils.prefix + "§aYou still need to" + CityBuild.getInstance().getConfig().getInt("cooldown." + p.getName ()) + "Days to wait");
                         
                        } else {
                         
                            p.getInventory().addItem(new ItemStack[] { KopfManager.playerSkullForName(args[0])});
    
                            p.sendMessage(Utils.prefix + "§aYou have received the head of §e" + KopfManager.getName(args[0]));
                         
                            CityBuild.getInstance().getConfig().set("cooldown." + p.getName(), time);
                            CityBuild.getInstance().saveConfig();
                        }
                     
                     
                     
                    }
               
                } else
                    p.hasPermission(Utils.perms);
           
            }
       
            return false;
        }
    
    
    }
    
    or is this code wrong

    @timtower

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 17, 2018
  12. Online

    timtower Administrator Administrator Moderator

    @Spark61 That time value doesn't change though.
     
  13. Offline

    Spark61

    how can I make time change with while it's probably bad
     
  14. Online

    timtower Administrator Administrator Moderator

    @Spark61 Calculate the value when you need to save it.
     
  15. Offline

    Spark61

  16. Online

    timtower Administrator Administrator Moderator

    @Spark61 You need to save when the cooldown finishes. When that is changes every tick, so you need to calculate it just before you save it.
     
  17. Offline

    Spark61

    How can I save it when the cooldown is over? @timtower
     
  18. Online

    timtower Administrator Administrator Moderator

    Why do you need to save that?
     
  19. Offline

    Spark61

    You wrote that yesterday
     
  20. Online

    timtower Administrator Administrator Moderator

    Then I probably misunderstood it.

    You need to set it in the config and save the config.
     
  21. Offline

    Spark61

    what do I have to set in the config?
     
  22. Online

    timtower Administrator Administrator Moderator

    The time when the cooldown finishes.
     
  23. Offline

    Spark61

    How can I do that?
     
  24. Online

    timtower Administrator Administrator Moderator

    You already saved things in the config, not gonna spoonfeed this.
     
  25. Offline

    Spark61

    So far I have only stored in the config how long the timer should run, but not yet when it ends
     
  26. Online

    timtower Administrator Administrator Moderator

    @Spark61 Then save that.
    *ahem from your own code*
    Code:
    CityBuild.getInstance().getConfig().set("cooldown." + p.getName(), time);
    CityBuild.getInstance().saveConfig();
     
  27. Offline

    Spark61

    how can I query the time from now and then add 30 days?

    @timtower

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 18, 2018
  28. Offline

    KarimAKL

    @Spark61 I think you should just add 2,592,000,000 to the current time millis and then save it.
     
  29. Offline

    ForbiddenSoul

    Last edited by a moderator: Oct 24, 2018
  30. Offline

    Spark61

    How can I ask if the time is over? That's what it looks like in my config
    Code:
    head:
      Spark61: 2018/11/23 19:01:12
     
     
Thread Status:
Not open for further replies.

Share This Page