Reply Messages

Discussion in 'Plugin Development' started by MaxNatural, Feb 8, 2015.

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

    MaxNatural

    So I am trying to let players message and reply. But when I do /reply it says player not online after he sent me the message.

    Code:
    package me.max.msg;
    
    import java.util.HashMap;
    
    import me.max.MainCore;
    
    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;
    
    public class CommandMSG implements CommandExecutor{
    
        public CommandMSG(MainCore mainCore) {
            // TODO Auto-generated constructor stub
        }
        public HashMap<Player, Player> reply = new HashMap<Player, Player>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (!(sender instanceof Player)) {
                sender.sendMessage("Players only bud");
                return true;
            }
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("message") || cmd.getName().equalsIgnoreCase("msg")) {
                if(args.length < 2) {
                    player.sendMessage(MainCore.TAG + "/message [username] [message]");
                    return true;
                }
    
                if (args.length > 1) {
                    Player online = Bukkit.getPlayer(args[0]);
                    if (online == null || !online.isOnline()) {
                        player.sendMessage(MainCore.TAG + "Player is not online");
    
                    } else {
                        StringBuilder x = new StringBuilder();
                        for (int i = 1; i < args.length; i++) {
                            x.append(args[i] + " ");
                        }
                        reply.put(player, online);
                        reply.put(online, player);
                        player.sendMessage(ChatColor.GRAY + "<" + sender.getName() + "->" + online.getName() + "> " + x.toString().trim());
                        online.sendMessage(ChatColor.GRAY + "<" + sender.getName() + "->" + online.getName() + "> " + x.toString().trim());
    
                    }
    
    
                }
            }
            if(cmd.getName().equalsIgnoreCase("reply")) {
                Player online = reply.get(player);
                if(args.length < 1) {
                    player.sendMessage(MainCore.TAG + "/reply [message]");
                    return true;
                }
                if(args.length > 0) {
                    if(online == null || !online.isOnline()) {
                        player.sendMessage(MainCore.TAG + "Player is not online");
                    } else {
                        StringBuilder x = new StringBuilder();
                        for(int i = 0; i < args.length; i++) {
                            x.append(args[i] + " ");
                        }
                        reply.put(player, online);
                        reply.put(online, player);
                        player.sendMessage(ChatColor.GRAY + "<" + sender.getName() + "->" + online.getName() + "> " + x.toString().trim());
                        online.sendMessage(ChatColor.GRAY + "<" + sender.getName() + "->" + online.getName() + "> " + x.toString().trim());
    
                       
                    }
                }
            }
            return false;
    
        }
    
    }
    
     
  2. Offline

    mine-care

    Avoid storing player objects in memory, prefer names or uuids (chose appropriately) I will look your issue up when I get to a pc since iPhone won't allow me to scroll down on your code.
    Sorry for that.
     
  3. Offline

    BaconStripzMan

    I would help you, but I have never attempted to do this before. But I do know there is a Conversation method, whether that will work or not
     
  4. Offline

    hexaan

    @MaxNatural
    It could be that the Player object that you are getting form the onCommand does not reference the same object that has been stored into the HashMap the first time. If you use UUID's and get the player with that ID you won't have this problem I think.
     
  5. Offline

    nj2miami

    Your problem is "Player online = Bukkit.getPlayer(args[0]);"

    args[0] "message", you want args[1] which should be the player name.

    I agree about not storing the Player object. Simply save the UUID's and check for online that way / reply to the message that way.

    Actually, look at all of your 'args' checks as they all reference a different index. In '/message; you do a check for 'length > 1', however, you probably want > 2 since the 1st args is "message" the 2nd is the PlayerName and the 3rd+ would be your message. Allowing for 2 would allow blank messages to be sent.

    Edit2: You will also have an issue in your /reply command at
    Code:
    for(int i = 0; i < args.length; i++) {
       x.append(args[i] + " ");
    }
    You need to start at int = 1 or you will include "reply" to the returned message. Same as above, check your referencing.
     
    Last edited: Feb 8, 2015
  6. @nj2miami Arguments starts at 0.... The line "Player target = Bukkit.getPlayer(args[0]);" is completely fine, and so is "if (args.length) Also, this would be a way to include his message:
    Code:
                            String message = "";
                            for (int i = 1; i < args.length; i++) {
                                message = message + args[i] + " ";
                            }
                            player.sendMessage(ChatColor.AQUA + "You"
                                    + ChatColor.GRAY + " >> " + ChatColor.GREEN
                                    + target.getName() + ChatColor.GRAY + ": "
                                    + ChatColor.WHITE + message);
    @MaxNatural You need to check if the player's arguments are greater than or equal to 2 because the player could be saying multiple words.
     
    Last edited: Feb 8, 2015
  7. Offline

    nj2miami

    I am completely aware they start at 0, but you obviously missed the point here and are leading him to believe that args[0] is "completely fine" to do.

    He is checking "Player target = Bukkit.getPlayer(args[0])" which is asking Bukkit to find a player named "message" (args[0]).

    In HIS case, args[1] will contain the PLAYER NAME. not args[0]. He later makes the same mistake again in his /reply code. Why add to the conversation if you are just going to confuse the OP?
     
Thread Status:
Not open for further replies.

Share This Page