I have coded a reply command for my message command and i does not seem to be working correctly. In my message command when the message gets sent the player and the target player gets saved into a hash map and then will be used in the reply command like in the code. When i use /reply it just messages the player saying "You have no one to reply to" here is the code, thanks. Code:java //End of /msg command and the adding. lastRecieved.put(player, tarPlayer); return true; } } } if (commandLabel.equalsIgnoreCase("reply")){ if (sender instanceof Player) { Player player = (Player) sender; if (args.length == 0){ player.sendMessage(ChatColor.RED + "/reply <message>"); return true; } StringBuilder str = new StringBuilder(); for (int i = 0; i < args.length; i ++){ str.append(args[i] + " "); } String mes2 = str.toString(); Player target = lastRecieved.get(player); if (target == null){ player.sendMessage(ChatColor.RED + "You have no one to reply to."); return true; } else if(target != null) { target.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2); player.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2); } } } return true; }}[/i]
MCCoding Im not sure if this is an error, but on line 29, you do an else if target != null. Just do a normal else, because it will run if its not null, therefore you don't need the else part. Also, could you tell me (everyone also ;P) what doesn't work? Does the code even run? Does a the code run when target == null? Add more detail, and follow my change. Reply if you keep getting errors D:
JPG2000 Thanks for the reply, i changed it back to a normal else and still is displaying the "You have no one to reply to" message. As for errors i have not no errors at all and yes it runs when the target == null as its displaying that message. Thanks
MCCoding So the code still works, just with an else? Could you post the whole code (class), and every other map or what ever you use?
JPG2000 yes the code is doing the same thing as it did before and here is the whole messaging class, Code:java public class Messaging implements Listener { public Map<Player, Player> lastRecieved = new HashMap<Player, Player>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (sender instanceof Player) { final Player player = (Player) sender; if ((commandLabel.equalsIgnoreCase("m"))) { if(args.length == 0){ player.sendMessage("Message: Player argument missing."); return true; } Player tarPlayer = player.getServer().getPlayer(args[0]); if(args.length == 1){ if(tarPlayer != null) { player.sendMessage("Message: Message argument missing."); return true; } } if(tarPlayer == null){ player.sendMessage("Message: Cannot find player " + args[0]); return true; } if(args.length >= 2){ StringBuilder sb = new StringBuilder(); for(int i=1; i<args.length; i++){ sb.append(args[i]).append(" "); } String msg = sb.toString(); player.sendMessage(" -> "+ tarPlayer.getName() + msg); player.sendMessage(" <- "+ tarPlayer.getName() + msg); lastRecieved.put(player, tarPlayer); return true; } } } if (commandLabel.equalsIgnoreCase("reply")){ if (sender instanceof Player) { Player player = (Player) sender; if (args.length == 0){ player.sendMessage(ChatColor.RED + "/reply <message>"); return true; } StringBuilder str = new StringBuilder(); for (int i = 0; i < args.length; i ++){ str.append(args[i] + " "); } String mes2 = str.toString(); Player target = lastRecieved.get(player); if (target == null){ player.sendMessage(ChatColor.RED + "You have no one to reply to."); return true; } else { target.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2); player.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2); } } } return true; }}[/i][/i]
MCCoding Try this. Before you (Line 59) create the targetVariable, check if its null. Basicly, check if lastRecieved.get(player) == null. Im honostly not sure at all why this is erroring. It looks all good, and if the other things in this class are working, then I dont know.
JPG2000 When i added this Code:java if(lastRecieved.get(player) == null) { return true; } it stopped the whole /msg command.
I would build the HashMap with new HashMap<String, String>() because i'm not sure if the equals() method is overridden correctly by Bukkit. So just store the player.getName() and tarPlayer.getName() in the HashMap and receive it by using: Player player = <Plugin>.getServer().getPlayer(name);
5pHiNxX I tried adding the targetPlayer,getname() into a Array and tried the Player player = <Plugin>.getServer().getPlayer(name); but don't get how i get the target player i know you can use .contain(player.getName()) but that would get the command sender.
@MCCoding i edited your code. try this: Code:java public class Messaging implements Listener { public Map<String, String> lastRecieved = new HashMap<String, String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (sender instanceof Player) { final Player player = (Player) sender; if ((commandLabel.equalsIgnoreCase("m"))) { if(args.length == 0){ player.sendMessage("Message: Player argument missing."); return true; } Player tarPlayer = player.getServer().getPlayer(args[0]); if(args.length == 1){ if(tarPlayer != null) { player.sendMessage("Message: Message argument missing."); return true; } } if(tarPlayer == null){ player.sendMessage("Message: Cannot find player " + args[0]); return true; } if(args.length >= 2){ StringBuilder sb = new StringBuilder(); for(int i=1; i<args.length; i++){ sb.append(args[I]).append(" ");[/I][I] }[/I][I] String msg = sb.toString();[/I] [I] player.sendMessage(" -> "+ tarPlayer.getName() + msg);[/I][I] player.sendMessage(" <- "+ tarPlayer.getName() + msg);[/I][I] lastRecieved.put(player.getName(), tarPlayer.getName());[/I][I] return true;[/I][I] }[/I][I] }[/I][I] }[/I] [I] if (commandLabel.equalsIgnoreCase("reply")){[/I][I] if (sender instanceof Player) {[/I][I] Player player = (Player) sender;[/I] [I] if (args.length == 0){[/I][I] player.sendMessage(ChatColor.RED + "/reply <message>");[/I][I] return true;[/I][I] }[/I] [I] StringBuilder str = new StringBuilder();[/I][I] for (int i = 0; i < args.length; i ++){[/I][I] str.append(args + " ");[/I][I] }[/I] [I] String mes2 = str.toString();[/I][I] Player target = <plugin>.getServer().getPlayer(lastRecieved.get(player.getName()));[/I][I] if (target == null){[/I][I] player.sendMessage(ChatColor.RED + "You have no one to reply to.");[/I][I] return true;[/I] [I] } else {[/I] [I] target.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);[/I][I] player.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);[/I][I] }[/I][I] }[/I][I] }[/I][I] return true;[/I][I] }[/I][I]}[/I] just replace <plugin> with the instance of your plugin
@MCCoding just try to replace Code:java public Map<Player, Player> lastRecieved = new HashMap<Player, Player>(); with Code:java public Map<String, String> lastRecieved = new HashMap<String, String>(); and everytimes you use Code:java Player player = lastReceivet.get(<player>); use Code:java Player player = <plugin>.getServer().getPlayer(lastReceived.get(<player>.getName()); and also replace every occurance of Code:java lastReceived.put(<player>, <player>); with Code:java lastReceived.put(<player>.getName(), <player>.getName());