Solved Cooldown without Scheduler

Discussion in 'Plugin Development' started by Zettos, Oct 3, 2020.

  1. Offline


    How do I integrate hours here. I want to make a Kit plugin and i have this already done, only a cooldown is missing. So i want something like this, but the hours missing. Please help

    package main;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    * Class created by MsGamerHD on 20.05.2016
    public class Main extends JavaPlugin{
        public static File confg;
        public static FileConfiguration cfg;
        public void onEnable() {
            Main.confg = new File("plugins/Config", "config.yml");
            Main.cfg = YamlConfiguration.loadConfiguration(Main.confg);
        public void onDisable() {
         * sek = ms/1000;
         * min = ms/1000/60;
         * std = ms/1000/60/60;
         * tag = ms/1000/60/60/24;
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player){
                Player p = (Player) sender;
                    long jetzt = System.currentTimeMillis();
                        long be = cfg.getLong("Cooldown."+p.getUniqueId()+"");
                        int rest = (int) ((be + (5*1000*60)) - jetzt);
                        if(rest > 0){
                            int minute = rest/1000/60;
                            rest = rest-(minute*1000*60);
                            int sekunde = rest/1000;
                            p.sendMessage("§cDu musst noch warten: "+minute+(minute == 1 ? " Minute" : " Minuten") + " und "
                                    +sekunde+(sekunde == 1 ? " Sekunde" : " Sekunden") + " warten.");
                            return true;
                    cfg.set("Cooldown."+p.getUniqueId()+"", jetzt);
                    try {
                    } catch (Exception e) {
                        // TODO: handle exception
                    p.sendMessage("§aDiese Funktion wurde ausgeführt.");
                    return true;
            return false;
  2. Offline


    Copy and pasting is not recommended.

    Anyway, you can create a BukkitRunnable for this task.
  3. Offline


    You can just compare the milliseconds and discern the time difference with provided Java libraries. No need to overcomplicate.
  4. Offline


    @Kars answer is definitely what i would choose for a simple cooldown.:I'll try to explain how to use it a little bit.
    So to add a cooldown to a player just store his UUID in a hashmap along with a Long (System.currentTimeMillis()).Then to check the cooldown you can calculate the cooldown that you want + the Long from the map / 1000 - System.currentTimeMillis() / 1000.This is for seconds,you can update it to hours if you want to

Share This Page