Solved Cooldown without Scheduler

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

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

    Zettos

    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

    Code:
    package main;
    
    import java.io.File;
    
    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;
    import org.bukkit.plugin.java.JavaPlugin;
    
    /**
    * Class created by MsGamerHD on 20.05.2016
    */
    public class Main extends JavaPlugin{
    
        public static File confg;
        public static FileConfiguration cfg;
       
        @Override
        public void onEnable() {
            saveDefaultConfig();
           
            Main.confg = new File("plugins/Config", "config.yml");
            Main.cfg = YamlConfiguration.loadConfiguration(Main.confg);
           
        }
       
        @Override
        public void onDisable() {
            saveDefaultConfig();
        }
       
        /*
         * sek = ms/1000;
         * min = ms/1000/60;
         * std = ms/1000/60/60;
         * tag = ms/1000/60/60/24;
         */
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player){
                Player p = (Player) sender;
               
                if(cmd.getName().equalsIgnoreCase("cd")){
                    long jetzt = System.currentTimeMillis();
                   
                    if(cfg.contains("Cooldown."+p.getUniqueId()+"")){
                        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 {
                        cfg.save(confg);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    p.sendMessage("§aDiese Funktion wurde ausgeführt.");
                    return true;
                }
            }
            return false;
        }
       
    }
     
  2. Offline

    KarimAKL

    Copy and pasting is not recommended.

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

    Kars

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

    gochi9

    @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
     
Thread Status:
Not open for further replies.

Share This Page