HashMap not working

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Aug 15, 2018.

  1. Everything works except the counter?
    Code (Text):
            public Map<UUID, Integer> offence = new HashMap<UUID, Integer>();
           
            // No swear
            @EventHandler
            public void onChatPlayer(AsyncPlayerChatEvent e) {
                for (String word : e.getMessage().toLowerCase().split(" ")) {
                    if (getConfig().getStringList("swearwords").contains(word)) {
                        if (getConfig().getBoolean("antiswear", true)) {
                        e.setCancelled(true);
                        for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                            if (p.hasPermission("sp.notify")) {
                                p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "[NoSwear]" + ChatColor.GRAY + " > " + ChatColor.RED + e.getPlayer().getName() + " tried to swear! " + ChatColor.GRAY + "(" + ChatColor.RED + word + ChatColor.GRAY + ")");
                            }
                        }
                        if (offence.containsKey(e.getPlayer().getUniqueId())) {
                            int mutecount = offence.get(e.getPlayer().getUniqueId());
                            offence.put(e.getPlayer().getUniqueId(), mutecount + 1);
                            switch (mutecount) {
                            case 2:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tempban " + e.getPlayer().getName() + "30m Please respect players! 2/3");
                                break;
                            case 3:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "gtfo " + e.getPlayer().getName() + "30m Disrespecting players! 3/3");
                            }
                        }else {
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "stfu " + e.getPlayer().getName() + " Please respect players! 1/3");
                            offence.put(e.getPlayer().getUniqueId(), 1);
                        }
                    }
                }
            }
        }
     
  2. What about it doesn’t work?
     
  3. Are Java 8 HashMaps bugged or I'm doing something wrong, this is my question. Anyways make sure you registered your event
     
    • Funny Funny x 1
  4. Yes it is. Its in main
     
  5. What about it isn't working with the counter? The switch statement? If so, you are checking the old mute count and not the updated one
     
  6. Choco

    Moderator

    A multi-billion dollar company producing one of the most widely used programming languages in the world does not release software if one of the essential types of "collections". If your HashMap is bugged, you're using the HashMap wrong.

    private and final. Encapsulation. Also, there's no need to repeat yourself with the type parameters when instantiating your HashMap. As of Java 7, they are inferred and empty diamond brackets may be used (<>).
    Code (Java):
    private final Map<UUID, Integer> offense = new HashMap<>();
    I would instead split by "\\s+" which instead splits by as many spaces as possible. If I'm not mistaken, splitting by a single space may result in some empty Strings in the returned array which is just a bit of unnecessary data.

    You should consider Map#merge(). Makes things a little cleaner by essentially saying "Set it to this value unless it already exists, then merge with the provided function and set it in the map"
    Code (Java):
    offence.merge(e.getPlayer().getUniqueId(), 1, Integer::sum);
    You're performing a switch based on the "mutecount" from before it was incremented. This is likely your issue

    If you're using Bukkit#dispatchCommand() to perform programmable functionality, you should just use command blocks instead. Use the API to ban and kick players. See Bukkit#getBanList()

    As a random note, you should also consider placing a "break" at the end of your for loop so that your players are not warned 3 times for having 3 swear words in a single message. You have one, but it's in a very specific situation. They should only be warned once for each message, but perhaps that's how you want it. Just a suggestion.
     
    #6 Choco, Aug 16, 2018
    Last edited: Aug 16, 2018
  7. While the use of a console command to ban the player in this instance is flawed, as this code seems to be for a punishment plugin. I would myself use a console command to ban players, perhaps as a secondary measure to retain compatibility with any form of punishment plugin present on a server without needing to hook into every single one. Sometimes these workarounds can be handy although lazy.
     
  8. Choco

    Moderator

    Really, there are no acceptable circumstances in which you should be hardcoding executions of Bukkit#dispatchCommand() (or Player#performCommand() for that matter). The parameter passed to that method should be configurable; i.e. "ban-command: 'ban'" or something similar. There's no reason whatsoever to hardcode that value.
     
    • Like Like x 1