Solved ArrayList failing to remove a player

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 13, 2016.

Thread Status:
Not open for further replies.
  1. Hey!

    In my /CmdSpy command. When doing /CmdSpy On it works perfectly fine and enables it for the player. However, when doing /CmdSpy off it is NOT removing the player. In effect, it's not triggering the event.

    CODE;

    Code (Text):
    package ChatFilter;

    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import me.DecisionsYT.Main;

    public class CmdSpy implements Listener, CommandExecutor {
       
    Main plugin;
       
        public CmdSpy(Main passedPlugin) {
            this.plugin = passedPlugin;
        }

          public ArrayList<String> spy = new ArrayList<String>();
       
        @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
            Player player = e.getPlayer();
            for (Player p : Bukkit.getOnlinePlayers())
              if (this.spy.contains(p.getName()))
                  player.sendMessage(" ");
                  player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Format").replace("{player}", e.getPlayer().getName()).replace("{command}", e.getMessage())));
            }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
       
         
            if (args.length == 0) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Usage")));
                return true;
                }
            }
           
           
            else if (args.length == 1) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                    if (args[0].equalsIgnoreCase("On")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Enabled")));
                        this.spy.add(sender.getName());
                        return true;
                    }
                }
            }
            if (args.length == 1) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                    if (args[0].equalsIgnoreCase("Off")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Disabled")));
                        this.spy.remove(sender.getName());
                        return true;
                    }
                }
            }
           
           
            if (!sender.hasPermission("ChatX.CmdSpy")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
                return true;
            }
       
       
       
        return true;
            }
         }
     
  2. Make this code:
    Code (Text):

            else if (args.length == 1) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                    if (args[0].equalsIgnoreCase("On")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Enabled")));
                        this.spy.add(sender.getName());
                        return true;
                    }
                }
            }
            if (args.length == 1) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                    if (args[0].equalsIgnoreCase("Off")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Disabled")));
                        this.spy.remove(sender.getName());
                        return true;
                    }
                }
            }
     
    This:
    Code (Text):

            else if (args.length == 1) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                    if (args[0].equalsIgnoreCase("On")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Enabled")));
                        this.spy.add(sender.getName());
                        return true;
                    }
                    else if (args[0].equalsIgnoreCase("Off")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Disabled")));
                        this.spy.remove(sender.getName());
                        return true;
                    }
                }
            }
     
     
  3. Still being told the commands even when /cmdspy off
     
  4. The problem is that it's failing to remove the player from the ArrayList

    EDIT:

    Current CODE:

    Code (Text):
    package ChatFilter;

    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import me.DecisionsYT.Main;

    public class CmdSpy implements Listener, CommandExecutor {
       
    Main plugin;
       
        public CmdSpy(Main passedPlugin) {
            this.plugin = passedPlugin;
        }

          public ArrayList<String> spy = new ArrayList<String>();
       
        @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
            Player player = e.getPlayer();
            for (Player p : Bukkit.getOnlinePlayers())
              if (this.spy.contains(p.getName()))
                  player.sendMessage(" ");
                  player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Format").replace("{player}", e.getPlayer().getName()).replace("{command}", e.getMessage())));
            }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
       
         
            if (args.length == 0) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Usage")));
                return true;
                }
           }
           
           
             else if (args.length == 1) {
                 if (sender.hasPermission("ChatX.CmdSpy")) {
                     if (args[0].equalsIgnoreCase("On")) {
                         sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Enabled")));
                         this.spy.add(sender.getName());
                         return true;
                     }
                     else if (args[0].equalsIgnoreCase("Off")) {
                         sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Disabled")));
                         this.spy.remove(sender.getName());
                         return true;
                     }
                 }
             }
           
           
            if (!sender.hasPermission("ChatX.CmdSpy")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
                return true;
            }
       
       
       
        return true;
            }
          }
     
  5. Any exceptions in the console? Add some debug messages to be sure args[0] is indeed off, and print the boolean returned by the call to add/remove methods.
     
  6. Done so, not getting any more information from it as I've already got.
     
  7. if (args.length == 1) {
    if (args[0].equalsIgnoreCase("whatever")) {
    do whatever
    }

    if (args[0].equalsIgnoreCase("whatever")) {
    do whatever
    }
    }
     
  8. The value returned by the call to remove(String) is true?
     
  9. Yea, I'm pretty sure, if read correctly
     
  10. Just to be sure try something like Bukkit.broadcastMessage("Was Removed?: " + this.spy.remove(sender.getName()));
     
  11. Wait... Put that where?
     
  12. Just comment out your current call to this.spy.remove(sender.getName()) and put that in its place, then in game it should broadcast the result to the chat. It should print true if it was removed and false if it wasn't.
     
  13. It tells me false
     
  14. Okay so that remove method returns true if the specified element was found in the collection. So somewhere, somehow, the senders name is not the same one added to the list.
     
  15. Hmm, okay, got any suggestions?
     
  16. printing out the senders name for both the add and remove might give a clue as to where the problem is.
     
  17. Doing now


    Sent from my iPod touch using Tapatalk
     
    • Funny Funny x 1
  18. It tells me this: http://prntscr.com/b3pqgl

    CODE for that is:

    Code (Text):
     else if (args.length == 1) {
                 if (sender.hasPermission("ChatX.CmdSpy")) {
                     if (args[0].equalsIgnoreCase("On")) {
                         sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Enabled")));
                         //this.spy.add(sender.getName());
                         Bukkit.broadcastMessage("Was Added?: " + this.spy.add(sender.getName()));
                         return true;
                     }
                     else if (args[0].equalsIgnoreCase("Off")) {
                         sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Disabled")));
                         //this.spy.remove(sender.getName());
                         Bukkit.broadcastMessage("Was Removed?: " + this.spy.remove(sender.getName()));
                         return true;
                     }
                 }
             }
     
  19. Looks like its working there?
     
  20. Yes it is, and always has, it's just the removing, which I truely find difficult to understand, as it's doing the exact same thing as /cmdspy ON, just changing "this.spy.ADD" to "this.spy.REMOVE"
     
Thread Status:
Not open for further replies.