Having an issue with cooldown logic. (NOT using delayed tasks.)

Discussion in 'Spigot Plugin Development' started by Sirenum, May 22, 2015.

  1. As the tittle would suggest, I am in a little bit of a rut when it comes to implementing a simple cool down system into my command. This is the first time I have experimented with such a feature, so it's rather new to me.

    PHP:
     public HashMap<String, Long> cooldowns = new HashMap<>();
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[])
        {
            int cooldownTime = this.main.getConfig().getInt("Time-Delay");

            if(cooldowns.containsKey(sender.getName()))
            {
                long secondsLeft = ((cooldowns.get(sender.getName()) / 1000) + cooldownTime) - (System.currentTimeMillis() / 1000);
                if (secondsLeft > 0)
                {
                    sender.sendMessage(util.color("&cYou still have &b" + secondsLeft + " &cseconds left!"));
                }else if(secondsLeft == 0)
                {
                    cooldowns.remove(sender.getName());
                }
            }

               if(cmd.getName().equalsIgnoreCase("report") && !(sender.hasPermission("playerreport.report")))
                {
                    sender.sendMessage(util.permission("&cYou need the permission, &bplayerreport.report &cto use this!"));
                }else if(args.length < 1)
                {
                    sender.sendMessage(util.color(util.getPrefix() +"&6Usage: &a/report <&cuser&a> <&creason&a>"));
                }else if(args.length == 1)
                {
                    sender.sendMessage(util.color(util.getPrefix()+"&6Usage: &a/report <user> <&creason&a>"));
                }else if(args.length > 1)
                {
                    Player user = Bukkit.getServer().getPlayer(args[0]);
                    if(user == null)
                    {
                        sender.sendMessage(util.color(util.getPrefix()+"&6The Player, &a&o" + args[0] + " &cIs invalid."));
                    }else
                    {
                        StringBuilder str = new StringBuilder();
                        for (String arg : args) {
                            str.append(arg + " ");
                        }
                        String report = str.toString().replaceAll(args[0], "");
                        String prefix = this.main.getConfig().getString(util.color("Report.prefix"));
                        String config = this.main.getConfig().getString("Report.format");
                       
                        config = config.replaceAll("%username%", user.getName());
                        config = config.replaceAll("%sendername%", sender.getName());
                        config = config.replaceAll("%reportmsg%", report);
                        config = config.replaceAll("%prefix%", prefix);

                        String filter = this.main.getConfig().getString("Filter");

                        /**
                         * Add user to cool down.
                         */

                        cooldowns.put(sender.getName(), System.currentTimeMillis());
                        util.reportBroadcast(main.getConfig().getBoolean("Broadcast.public"), util.color(config));
                    }
                }
            return true;
        }
    Currently, everything works OK with this code. However, After the cool down is finished, and the user attempts to execute the command, nothing happens. Console registers the command has been executed, but nothing appears.

    Any advice, or insight to what I might be missing or doing wrong is appreciated.

    Thanks,

    ES
     
  2. @ES359 secondsLeft can be negative if the cooldown has passed already. You currently only remove them if secondLeft is 0.

    Also, use replace rather than replaceAll if you are replacing String literals and program against interfaces rather than implementations ;) (use Map rather than HashMap for fields, then you can switch to other implementations without issue - unless you are using specific methods which are only available for that class (which is not the case with HashMap))
     
    • Like Like x 1
  3. Thanks dude, I am still learning about HashMaps and Maps in general, so my usage is understandably a little bit shaky. Thanks for the info, I'll go fix the arguments check.