Reply command wont work

Discussion in 'Plugin Development' started by TerThesz, Apr 7, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    TerThesz

    Hello,
    I`m working on plugin that contains msg and replyes but I can`t get the /r command working.

    Here is /msg and /r
    Code:
            else if (cmd.getName().equalsIgnoreCase("msg")) {
                if (sender.hasPermission("chatManagement.*") || sender.hasPermission("chatManagement.msg")) {
                    if (args.length > 1) {
                       
                        Player reciever = Bukkit.getPlayerExact(args[0]);
                       
                        if (reciever == null) {
                            //errorPlayerNotOnline
                            String noPlayer = formatNoPlayer(plugin.getConfig().getString("messages.errorPlayerNotOnline"), (Player)sender, plugin, args[0]);
                            sender.sendMessage(noPlayer);
                            return true;
                           
                        } else {
                            //String str, Player sender, Plugin plugin, String message, String senderReciever
                            String message = "";
                            for (int i = 1; i != args.length; message = message + " " + args[i], i++);
                            String recieverMessage = formatMsg(plugin.getConfig().getString("messages.msgReciever"), (Player)sender, plugin, message, Bukkit.getPlayer(args[0]).getName());
                            Bukkit.getPlayer(args[0]).sendMessage(recieverMessage);
                            String senderMessage = formatMsg(plugin.getConfig().getString("messages.msgSender"), (Player)sender, plugin, message, ((Player)sender).getName());
                            sender.sendMessage(senderMessage);
                            reciever.playSound(reciever.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP , 1, 1);
                           
                            reply.put((Player)sender, reciever);
                            reply.put(reciever, (Player)sender);
                           
                        }
                       
                    } else {
                        String error = format(plugin.getConfig().getString("messages.errorFewArguments"), (Player)sender, plugin);
                        sender.sendMessage(error);
                       
                    }
                   
                } else {
                    sender.sendMessage(errorMessage);
                   
                }
               
            }
            else if (cmd.getName().equalsIgnoreCase("reply")) {
                if (sender.hasPermission("chatManagement.*") || sender.hasPermission("chatManagement.msg")) {
                    if (args.length > 1) {
                       
                        Player reciever = (Player) reply.get(sender);
                       
                        if (reciever == null) {
                            //errorPlayerNotOnline
                            String noPlayer = formatNoPlayer(plugin.getConfig().getString("messages.errorPlayerNotOnline"), (Player)sender, plugin, reciever.getName());
                            sender.sendMessage(noPlayer);
                            return true;
                           
                        } else {
                            //String str, Player sender, Plugin plugin, String message, String senderReciever
                            String message = "";
                            for (int i = 0; i != args.length; message = message + " " + args[i], i++);
                            String recieverMessage = formatMsg(plugin.getConfig().getString("messages.msgReciever"), (Player)sender, plugin, message, Bukkit.getPlayer(args[0]).getName());
                            Bukkit.getPlayer(args[0]).sendMessage(recieverMessage);
                            String senderMessage = formatMsg(plugin.getConfig().getString("messages.msgSender"), (Player)sender, plugin, message, ((Player)sender).getName());
                            sender.sendMessage(senderMessage);
                            reciever.playSound(reciever.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP , 1, 1);
                           
                        }
                       
                    } else {
                        String error = format(plugin.getConfig().getString("messages.errorFewArguments"), (Player)sender, plugin);
                        sender.sendMessage(error);
                       
                    }
                   
                } else {
                    sender.sendMessage(errorMessage);
                   
                }
               
            }
    Any idea?
     
  2. Offline

    Strahan

    1. I'd recommend using a switch block instead of a million if/else for the command checking, though that's just personal preference
    2. You don't need to check for wildcard permissions; that's the responsibility of the permissions manager. Just check what is required (i.e. chatManagement.msg not chatManagement.*)
    3. I'd also recommend negative check and return, it'd save a lot of unnecessary indentation
    4. I don't see you checking if sender is a Player before casting it thus, though that may be in the section above what was displayed. If it wasn't, you should be doing that.
    5. Instead of the loop you have to build the msg, I'd do StringUtils.join. It won't result in an erroneously padded String like your for loop is doing
    6. Args check on the reply should be > 0 not > 1 as they may reply with just a one word answer
    7. In your reply you are using args[0] as the recipient when it should be pulling from the map, so that will throw an NPE. You should be using reciever (though that's really spelled receiver)
     
  3. Offline

    TerThesz

    Ok I'll send the edited code. Btw args check on the reply is on 1 because the first argument is players na
    Edit: nvm I see what did you ment
     
Thread Status:
Not open for further replies.

Share This Page