Solved Error with cooldowns in ban plugin

Discussion in 'Spigot Plugin Development' started by SrHiti, Jun 29, 2021.

  1. I am writing a plugin for bans for my server and I ran into a problem when I call the hasCdw method in the BanCommand class, it gives the following error to the console:

    https://hastebin.com/irajihicor.properties

    My friend and a good coder told me that I have no problems in the code, but for some reason console writes the value "null" when I use command "/ban"

    Code (cooldownmanager (Unknown Language)):

    package hiti.cooldown;

    import com.google.common.collect.*;

    public class CooldownManager {

        public static TreeBasedTable<String, String, Cooldown> accs;

        public static void createCooldown(final String playerName, final String name, final long time) {
            final Cooldown c = new Cooldown(playerName.toLowerCase(), name, time);
            CooldownManager.accs.put(playerName.toLowerCase(), name, c);
        }

        public static boolean hasCdw(String playerName, String name) {
            final Cooldown c = CooldownManager.accs.get(playerName.toLowerCase(), name);
            if (c == null) {
                return true;
            }
            if (c.isLeft()) {
                CooldownManager.accs.remove(playerName.toLowerCase(), name);
                return false;
            }
            return true;
        }

        public static long getLeftTime(final String playerName, final String name) {
            return ((Cooldown) CooldownManager.accs.get((Object) playerName.toLowerCase(), (Object) name)).getLeftTime();
        }
    }
     
    Code (bancommand (Unknown Language)):

    package hiti.commands;

    import org.bukkit.command.*;
    import hiti.cooldown.*;
    import hiti.main.*;
    import hiti.api.*;
    import org.bukkit.*;
    import hiti.utils.*;

    public class BanCommand implements CommandExecutor {

        public boolean onCommand(final CommandSender sender, final Command cmd, final String l, final String[] args) {
            if (!sender.hasPermission("hiti.ban")) {
                sender.sendMessage(ConfigUtil.getMSG("no_permission"));
                return false;
            }
            if (CooldownManager.hasCdw(sender.getName(), "ban")) {
                sender.sendMessage(ConfigUtil.getMSG("cooldown_ban_command").replace("{time}", TimeUtils.getTimeŠ”(CooldownManager.getLeftTime(sender.getName(), "ban"))));
                return false;
            }
            if (args.length < 3) {
                sender.sendMessage(ConfigUtil.getMSG("usage_ban_command"));
                return false;
            }
            if (args[0].toLowerCase().equalsIgnoreCase(sender.getName().toLowerCase())) {
                sender.sendMessage(ConfigUtil.getMSG("sender_is_target_ban"));
                return false;
            }
            if (BanManager.isBan(args[0])) {
                sender.sendMessage(ConfigUtil.getMSG("player_is_banned").replace("{target}", args[0]));
                return false;
            }
            if (BetterPunishments.rules.getString("rules." + args[1].replace(".", "|")) == null) {
                sender.sendMessage(ConfigUtil.getMSG("rules_is_null").replace("{rules}", args[1]));
                return false;
            }
            if (!BetterPunishments.rules.getStringList("rules." + args[1].replace(".", "|") + ".type").contains("ban")) {
                sender.sendMessage(ConfigUtil.getMSG("corresponds_not_paragraph"));
                return false;
            }
            final StringBuffer sb = new StringBuffer();
            for (int i = 2; i < args.length; ++i) {
                sb.append(args[i]).append(" ");
            }
            if (sb.substring(0, sb.length() - 1).toString().length() < 3) {
                sender.sendMessage(ConfigUtil.getMSG("comment_little"));
                return false;
            }
            BanManager.createBan(args[0], sender.getName(), args[1], 0L, sb.substring(0, sb.length() - 1).toString(), BanType.BAN);
            sender.sendMessage(ConfigUtil.getMSG("successfully_ban").replace("{target}", args[0]));
            HoverChat.sendHover(ConfigUtil.getMSG("banned_broadcast").replace("{admin}", sender.getName()).replace("{target}", args[0]), ConfigUtil.getMSGHover("ban_text").replace("{number-rules}", args[1]).replace("{rules}", ChatColor.stripColor(BetterPunishments.rules.getString("rules." + args[1].replace(".", "|") + ".text").replace("&", "Ā§"))).replace("{comment}", sb.substring(0, sb.length() - 1).toString()));
            if (Bukkit.getPlayerExact(args[0]) != null) {
                Bukkit.getPlayerExact(args[0]).kickPlayer(ConfigUtil.getMSGInfo("ban_text").replace("{admin}", sender.getName()).replace("{comment}", sb.substring(0, sb.length() - 1).toString()).replace("{number-rules}", args[1]).replace("{rules}", BetterPunishments.rules.getString("rules." + args[1].replace(".", "|") + ".text")));
            }
            if (!sender.hasPermission("Bans.ban-cooldown.bypass") && BetterPunishments.config.getString("Cooldown-Manager.ban." + VaultPermission.getGroupName(Bukkit.getPlayerExact(sender.getName()))) != null) {
                CooldownManager.createCooldown(sender.getName(), "ban", TimeUtils.longTime(BetterPunishments.config.getString("Cooldown-Manager.ban." + VaultPermission.getGroupName(Bukkit.getPlayer(sender.getName())))));
            }
            return false;
        }
    }

     
     
  2. Hey, something on line 15 is null in the cooldown manager, could you please post your cooldown manager in a pastebin? And do not edit it, so the error still matches the code.
     
  3. https://pastebin.com/pVBz9BjA
     
  4. Code (Text):
    final Cooldown c = CooldownManager.accs.get(playerName.toLowerCase(), name);
    I think "playerName" is null.
     
  5. How I can fix it?
     
  6. I don't actually think so, because I changed it to "name" and it gave nothing.

    upload_2021-6-29_19-4-54.png
     
  7. Make sure there is a cooldown for that player as well, so maybe check "if CooldownManager....get(...)is not null"
     
  8. The issue is that the object below is never initialized, hence why it will throw an error
    Code (Text):
    public static TreeBasedTable<String, String, Cooldown> accs;
     
    • Like Like x 2
  9. Thanks!