How to toggle recieving msg

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

  1. Hi!
    I wrote a private message class and was wondering how to make a /msgtoggle function to not recieve msg's.
    This is my code so far.
    Code (Text):
    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;
                    }
                   
                    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 someone would give me a helping hand in the right direction then that would be great!
     
  2. @Aurorion
    Make an array list, and add the player to it once they disable private messaging. If the target is in the list, then that player has message turned off
     
  3. Weer een nederlander :p
    • Create a ArrayList with String (playername) and add them if they toggle it, remove them if they toggle again.
    • In your command msg check if the player is not in the arraylist, if so do nothing.
    • Add a else statement so if he is in the arraylist do stuff, so for example send a message like : This player doesnt want to receive private messages!
     
  4. @BlazingBroGamer @Svenna use a Set for add/remove/contains are important, and use a List when you need random access. In this case, as we focus on add/remove/contains, a Set is faster.

    @Aurorion semi-unrelated sidenotes:
    • I can't see if you added it, but make sure you check if the sender is an instanceof Player before casting to a Player. It could be the console, a command block, etc. after all.
    • Use a StringBuilder rather than String concatenation, it's faster.
    • Use ChatColor rather than section signs (§).
      • They have actual meaning (section signs have no special meaning in the code, whereas the ChatColor enum has a pretty obvious meaning).
      • If Bukkit decides to use another character, code using section signs will no longer work, whereas code that uses the ChatColor enum continues to work fine.
      • No need to remember which colour it was! You can instantly see which colour it is without any extra effort.
     
  5. Hi!
    Thnx for all your replies, and sorry to not respond faster (had a buzzy week)
    I now have this but does not seem to work. Can anyone Look trough it and clear things up for me?
    This is the code
    Code (Text):
    package nl.Aurorion.DHDCreative;

    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;
        }

    }
     
    Players still recieve msg's

    Thanks in advance:D