-1

I have no idea why my code is not keeping players muted after they relog even though I added them to the config.

This is there the muted players get saved:

private static ArrayList <Player> mutedPlayers = new ArrayList<Player>();

This is the event that handles the muted player and that should check if the player is muted or not:

@EventHandler
public void handlePlayerChat(AsyncPlayerChatEvent e){
    Player p = e.getPlayer();
    if (mutedPlayers.contains(p)) {
        p.sendMessage(ChatColor.DARK_RED + "You've been muted!");
        e.setCancelled(true);
    }
}

This is the command:

    if(command.getName().equals("mute")) {
        if (sender instanceof Player) {
            Player p = (Player) sender;
            if (p.hasPermission("shxr.mute")) {
                if (args.length == 1) {

                    Player target = Bukkit.getPlayer(args[0]);
                    if (target != null) {
                        if (!mutedPlayers.contains(target)) {
                            mutedPlayers.add(target);
                            p.sendMessage(ChatColor.GREEN + "You have successfully muted " + target.getName() + ChatColor.GREEN + "!");
                            target.sendMessage(ChatColor.DARK_RED + "You are muted!");
                            getConfig().set("mutedPlayers.Players", mutedPlayers);
                                saveConfig();
                        } else {
                            mutedPlayers.remove(target);
                            p.sendMessage(ChatColor.GREEN + target.getName() + ChatColor.GREEN + " has been unmuted!");
                            target.sendMessage(ChatColor.DARK_RED + "You have been unmuted!");
                                saveConfig();
                        }
                    } else {
                        p.sendMessage(ChatColor.DARK_RED + "Cannot find the player.");
                    }
                } else {
                    p.sendMessage(ChatColor.DARK_RED + "Proper usage of this command is: /mute <player>");
                }
            } else {
                p.sendMessage(ChatColor.DARK_RED + "You do not have the permissions to mute players!");
            }

        }
    }
Martijn
  • 7
  • 2

1 Answers1

0

Two issues:

  1. You aren't saving this list to disk, so when the server restarts, you're going to lose it all.

  2. You are storing references to the Player object, which gets recreated any time a user logs in or changes dimensions (Player is just an Entity class and is not a permanent reference). You need to store the user's UUID.

  • I think the muted players are actually saved to disk, but into the config file. In the code snippet above they are just not loaded again. Probably because it won't work if you try to load a Player entity object instead of a UUID as described in (2). – SteffenJacobs Dec 10 '19 at 16:48
  • 1
    @SteffenJacobs The code as shown in the question does not include *how* they're written to the config file, so I'm assuming either (a) no such code exists or (b) it isn't done correctly or (c) a combination of factors (including no code to read values out of the config file). – Draco18s no longer trusts SE Dec 10 '19 at 17:55
  • This is correct. The code indicates only _that_ the config is saved, not how. In addition, it does not provide any hint that the config is loaded whatsoever. Martijn, could you provide a bit more context in regards to saving/loading the players? Or did you already resolve the issue? BTW: Storing this kind of data into the config can be considered a bad practice since it violates [Separation of Concerns](https://en.wikipedia.org/wiki/Separation_of_concerns). It might be cleaner to store the muted players at least in a separate file. – SteffenJacobs Dec 11 '19 at 09:18