/reply not working

Discussion in 'Spigot Plugin Development' started by GCNT, May 26, 2018.

  1. Hey spigot.

    I just coded a reply command.
    But it's not working...
    Don't know why, cause I think i have done it very well.

    Here is the /msg command class:
    Code (Java):

    public Map<Player, Player> MessageMap = new HashMap<Player, Player>();
       
        private static Msg instance;
        public static Msg getInstance() {
            return instance;
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("msg")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "You must enter a player and a message");
                    return true;
                }
               
                if (args.length == 1) {
                    sender.sendMessage(ChatColor.RED + "You must enter a message");
                    return true;
                }
               
                if (args.length >= 2) {
                   
                    Player target = Bukkit.getPlayer(args[0]);
                   
                    if (sender instanceof Player) {
                        String language = AdvancedMultiLanguageAPI.getLanguageOfUuid(sender.getServer().getPlayer(sender.getName()).getUniqueId().toString());
                        if (target == null) {
                            if (language.equalsIgnoreCase("EN")) {
                                sender.sendMessage(ChatColor.RED + "That player is not online");
                                return true;
                            }
                            if (language.equalsIgnoreCase("DE")) {
                                sender.sendMessage(ChatColor.RED + "Dieser Spieler ist nicht online");
                                return true;
                            }
                            if (language.equalsIgnoreCase("NL")) {
                                sender.sendMessage(ChatColor.RED + "Die speler is niet online");
                                return true;
                            }
                        }
                        if (target == sender) {
                            if (language.equalsIgnoreCase("EN")) {
                                sender.sendMessage(ChatColor.RED + "You cannot message yourself");
                                return true;
                            }
                            if (language.equalsIgnoreCase("DE")) {
                                sender.sendMessage(ChatColor.RED + "Sie können sich keine Nachricht senden");
                                return true;
                            }
                            if (language.equalsIgnoreCase("NL")) {
                                sender.sendMessage(ChatColor.RED + "Je kan jezelf geen bericht sturen");
                                return true;
                            }
                        }
                        else {
                            Player p = (Player) sender;
                            StringBuilder str = new StringBuilder();
                            for (int i = 1; i < args.length; i++) {
                                str.append(ChatColor.translateAlternateColorCodes('&', args[i])).append(" ");
                            }
                            MessageMap.put(target, p);
                            MessageMap.put(p, target);
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages.general.msgToSender").replaceAll("%sender%", sender.getName()).replaceAll("%receiver%", args[0]).replaceAll("%message%", str.toString())));
                            target.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages.general.msgToTarget").replaceAll("%sender%", sender.getName()).replaceAll("%receiver%", args[0]).replaceAll("%message%", str.toString())));
                            return true;
                        }
                    }
                    else {
                        if (args[0] == "console") {
                            sender.sendMessage(ChatColor.RED + "You cannot message yourself");
                            return true;
                        }
                        if (target == null) {
                            sender.sendMessage(ChatColor.RED + "That player is not online");
                            return true;
                        }
                       
                        else {
                            StringBuilder str = new StringBuilder();
                            for (int i = 1; i < args.length; i++) {
                                str.append(ChatColor.translateAlternateColorCodes('&', args[i])).append(" ");
                            }
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages.general.msgToSender").replaceAll("%sender%", sender.getName()).replaceAll("%receiver%", args[0]).replaceAll("%message%", str.toString())));
                            target.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages.general.msgToTarget").replaceAll("%sender%", sender.getName()).replaceAll("%receiver%", args[0]).replaceAll("%message%", str.toString())));
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    And here is the /reply command class:
    Code (Java):

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("reply")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "You must add a message!");
                    return true;
                }
                if (args.length >= 1) {
                    Player target = (Player) Msg.getInstance().MessageMap.get(sender);
                    String language = AdvancedMultiLanguageAPI.getLanguageOfUuid(sender.getServer().getPlayer(sender.getName()).getUniqueId().toString());
                   
                    if (target == null) {
                        if (language.equalsIgnoreCase("EN")) {
                            sender.sendMessage(ChatColor.RED + "That player is not online");
                            return true;
                        }
                        if (language.equalsIgnoreCase("DE")) {
                            sender.sendMessage(ChatColor.RED + "Dieser Spieler ist nicht online");
                            return true;
                        }
                        if (language.equalsIgnoreCase("NL")) {
                            sender.sendMessage(ChatColor.RED + "Die speler is niet online");
                            return true;
                        }
                    }
                    if (target == sender) {
                        if (language.equalsIgnoreCase("EN")) {
                            sender.sendMessage(ChatColor.RED + "You cannot message yourself");
                            return true;
                        }
                        if (language.equalsIgnoreCase("DE")) {
                            sender.sendMessage(ChatColor.RED + "Sie können sich keine Nachricht senden");
                            return true;
                        }
                        if (language.equalsIgnoreCase("NL")) {
                            sender.sendMessage(ChatColor.RED + "Je kan jezelf geen bericht sturen");
                            return true;
                        }
                    }
                    else {
                        StringBuilder str = new StringBuilder();
                        for (int i = 1; i < args.length; i++) {
                            str.append(ChatColor.translateAlternateColorCodes('&', args[i])).append(" ");
                        }
                       
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages.general.msgToSender").replaceAll("%sender%", sender.getName()).replaceAll("%receiver%", args[0]).replaceAll("%message%", str.toString())));
                        target.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages.general.msgToTarget").replaceAll("%sender%", sender.getName()).replaceAll("%receiver%", args[0]).replaceAll("%message%", str.toString())));
                        return true;
                    }

                }
           
            }
            return false;
        }
     
  2. The incorrect conventions are extremely annoying, as well as the incorrect use of static.

    Also, pretty sure that would be the issue. Use object oriented programming (OOP) correctly. You're calling the Msg instance without setting it to that class, which by the way doesn't follow the single use per class rule, store instances of the class (which you would need to use the 'new' keyword for) in another class. Learn Java dude.
     
  3. How is it incorrect usage of static ?
    If he is using the get instance in the context of a singleton why is static a bad thing ?

    OP

    Where you have

    Code (Text):
     private static Msg instance = null;
        public static Msg getInstance() {
            return instance;
        }
    If the instance is not initialised elsewhere you will need to do in the getInstance method.
    Try somthing like

    Code (Text):
     private static Msg instance = null;
        public static Msg getInstance() {
             if(instance = null){
               instance = new Msg();
             }
            return instance;
        }
    Look at singletons here:
    https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples
     
  4. Incorrect use as it makes no sense to make it follow the singleton design, it's a message class for a command, don't make it static and let it store values like maps inside of it. Instantiate objects like you should in proper Java. Store objects in a seperate class, easier and better to handle.
     
  5. I made it this and it works fine:
    Code (Java):
    static Msg instance = new Msg();

        public static Msg getInstance() {
            return instance;
        }
    But there is another problem now...
    If i use /reply it says that the player is not online
    Code (Java):
    if (target == null) {
    //do stuff
    }
    But i don't know why it's doing that because i messaged a player, than I use /reply and the message comes up.
     
  6. You're going to have to learn to debug.
    Check if the value is null when you grab the player from the map in reply, check if your instance is null before that, check if what arguments you think you're sending you're actually sending, check if the player actually gets added to the map in the first place, debug.
     
    • Agree Agree x 1