Toggle for msg problem

Discussion in 'Spigot Plugin Development' started by Aurorion, May 16, 2015.

  1. Hi!
    So i have this class for msg and added a toggle function to disable receiving msg's.
    But it doesn't seem to work.
    Could anyone help me a bit out?
    Code (Text):
    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class PrivateMessage implements CommandExecutor {
       
        Main plugin;
        public PrivateMessage(Main instance){
            plugin = instance;
        }
       
        ArrayList<String> msgtoggle = new ArrayList<String>();
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)){
                sender.sendMessage(plugin.noplayer);
                return true;
            }
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("msg")){
                if(args.length == 0){
                    player.sendMessage(plugin.prefix + "Je moet wel een spelernaam invoeren!");
                    return true;
                }
                if(args.length >= 2){
                    Player target = Bukkit.getServer().getPlayerExact(args[0]);
                    if(target == null){
                        player.sendMessage(plugin.prefix + "§cSpeler niet online!");
                        return true;
                    }
                    if(msgtoggle.contains(target.getName())){
                        player.sendMessage(plugin.prefix + "§cDeze speler heeft zijn/haar msg uitstaan!");
                        return true;
                    }else{
                        String message = "";
                        for (int i = 1; i < args.length; i++) {
                        message = message + args[i] + " ";
                        }
                       
                        target.sendMessage("§9---------- §ePrive Bericht §9----------");
                        target.sendMessage("§6Van: §f" + player.getName());
                        target.sendMessage("§6Bericht: §f" + message);
                        target.sendMessage("§9--------------------------------");
                        player.sendMessage("§9---------- §ePrive Bericht §9----------");
                        player.sendMessage("§6Aan: §f" + target.getName());
                        player.sendMessage("§6Bericht: §f" + message);
                        player.sendMessage("§9--------------------------------");
                        return true;
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("msgtoggle")){
                if(!player.hasPermission("dhd.mod")){
                    player.sendMessage(plugin.noperm);
                    return true;
                }
                if(args.length == 0){
                    if(!msgtoggle.contains(player.getName())){
                        msgtoggle.add(player.getName());
                        player.sendMessage(plugin.prefix + "§cOntvangst van msg uitgeschakeld");
                        return true;
                    }else{
                        msgtoggle.remove(player.getName());
                        player.sendMessage(plugin.prefix + "§aOntvangst van msg ingeschakeld");
                        return true;
                    }
                }
            }
            return false;
        }

    }
     
    Thnx in advance!
     
  2. @Aurorion the code looks fine, what does it do instead of the intended behaviour? And what is that intended behaviour? (Also, use a HashSet rather than a ArrayList ;), it's faster)
     
    • Agree Agree x 1
  3. Where you have the for loop, it needs to be i < args.length - 1. If not, it will return an OutOfBoundsException since the entry won't exist. Also, why not just use getPlayer() instead of getPlayerExact()?
     
  4. You might have forgotten to register a listener.
     
  5. Please provide any errors if possible. What is supposed to happen and what is happening?
     
  6. @OP
    I have absolutely no idea why it doesn't work. From looking at your code, it seems like it should. I can only recommend you use a Set instead of a list for this though. Set would be the proper type of collection for what you're trying to achieve.

    Actually that's wrong, and let me explain why. Before I do, just know I'm doing this as a way to help, so don't take offence to it.

    "[].length" returns the length an array is as we would count it, 0 being empty. An array will start at index 0 for the first element in it, and it if has no elements, it essentially has no indexes available. Since it starts at 0, you start at 0. If you start at 0 and use < [].length, you'll never have any issues indexing it. The same can't be said for <= though. Another way to iterate through an array without any issues is to go backwards, but of course you can only do this if the order of events doesn't count for it.
    Code (Text):
    for (int i = [].length - 1; i >= 0; i--) {
    //dostuff
    }
    Also, it's ArrayIndexOutOfBoundsException which extends IndexOutOfBoundsException; OutOfBoundsException doesn't even exist.

    https://bukkit.org/threads/getplayer-exact.26696/

    Again, just hoping I can help with the learning process, not trying to be rude at all.
     
  7. I wasn't quoting the EXACT exception. Also that isn't how he used it at all. His loop is (int i = 1; i < args.length; i++). I just noticed it didn't say '<=' so that was an error on my part.
     
  8. Also, make sure you have register the command executor in your main class. I noticed you are using 2 commands in one command execution class. You need to register the executor TWICE. Once for the msg command and once for the msgtoggle. What the game is doing is only running the class when it sees /msg since it might only be registered to that command.