reply command

Discussion in 'Plugin Development' started by MCCoding, Oct 15, 2013.

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

    MCCoding

    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
    1. //End of /msg command and the adding.
    2.  
    3. lastRecieved.put(player, tarPlayer);
    4. return true;
    5. }
    6. }
    7. }
    8.  
    9. if (commandLabel.equalsIgnoreCase("reply")){
    10. if (sender instanceof Player) {
    11. Player player = (Player) sender;
    12.  
    13. if (args.length == 0){
    14. player.sendMessage(ChatColor.RED + "/reply <message>");
    15. return true;
    16. }
    17.  
    18. StringBuilder str = new StringBuilder();
    19. for (int i = 0; i < args.length; i ++){
    20. str.append(args[i] + " ");
    21. }
    22.  
    23. String mes2 = str.toString();
    24. Player target = lastRecieved.get(player);
    25. if (target == null){
    26. player.sendMessage(ChatColor.RED + "You have no one to reply to.");
    27. return true;
    28.  
    29. } else if(target != null) {
    30.  
    31. target.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);
    32. player.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);
    33. }
    34. }
    35. }
    36. return true;
    37. }
    38. }[/i]
     
    JPG2000 likes this.
  2. Offline

    JPG2000

    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:
     
  3. Offline

    MCCoding

    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
     
    JPG2000 likes this.
  4. Offline

    JPG2000

    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?
     
  5. Offline

    MCCoding

    JPG2000
    yes the code is doing the same thing as it did before and here is the whole messaging class,

    Code:java
    1. public class Messaging implements Listener {
    2.  
    3. public Map<Player, Player> lastRecieved = new HashMap<Player, Player>();
    4.  
    5. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    6. if (sender instanceof Player) {
    7. final Player player = (Player) sender;
    8. if ((commandLabel.equalsIgnoreCase("m"))) {
    9.  
    10. if(args.length == 0){
    11. player.sendMessage("Message: Player argument missing.");
    12. return true;
    13. }
    14. Player tarPlayer = player.getServer().getPlayer(args[0]);
    15. if(args.length == 1){
    16. if(tarPlayer != null) {
    17. player.sendMessage("Message: Message argument missing.");
    18. return true;
    19. }
    20. }
    21.  
    22. if(tarPlayer == null){
    23. player.sendMessage("Message: Cannot find player " + args[0]);
    24. return true;
    25. }
    26.  
    27.  
    28.  
    29. if(args.length >= 2){
    30. StringBuilder sb = new StringBuilder();
    31. for(int i=1; i<args.length; i++){
    32. sb.append(args[i]).append(" ");
    33. }
    34. String msg = sb.toString();
    35.  
    36. player.sendMessage(" -> "+ tarPlayer.getName() + msg);
    37. player.sendMessage(" <- "+ tarPlayer.getName() + msg);
    38. lastRecieved.put(player, tarPlayer);
    39. return true;
    40. }
    41. }
    42. }
    43.  
    44. if (commandLabel.equalsIgnoreCase("reply")){
    45. if (sender instanceof Player) {
    46. Player player = (Player) sender;
    47.  
    48. if (args.length == 0){
    49. player.sendMessage(ChatColor.RED + "/reply <message>");
    50. return true;
    51. }
    52.  
    53. StringBuilder str = new StringBuilder();
    54. for (int i = 0; i < args.length; i ++){
    55. str.append(args[i] + " ");
    56. }
    57.  
    58. String mes2 = str.toString();
    59. Player target = lastRecieved.get(player);
    60. if (target == null){
    61. player.sendMessage(ChatColor.RED + "You have no one to reply to.");
    62. return true;
    63.  
    64. } else {
    65.  
    66. target.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);
    67. player.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);
    68. }
    69. }
    70. }
    71. return true;
    72. }
    73. }[/i][/i]
     
  6. Offline

    JPG2000

    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.
     
    MCCoding likes this.
  7. Offline

    MCCoding

    JPG2000

    When i added this
    Code:java
    1. if(lastRecieved.get(player) == null) {
    2. return true;
    3. }


    it stopped the whole /msg command.
     
    JPG2000 likes this.
  8. Offline

    MCCoding

    Anyone?
     
  9. Offline

    5pHiNxX

    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);
     
  10. Offline

    MCCoding

    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.
     
  11. Offline

    5pHiNxX

    @MCCoding i edited your code. try this:

    Code:java
    1. public class Messaging implements Listener {
    2.  
    3. public Map<String, String> lastRecieved = new HashMap<String, String>();
    4.  
    5. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    6. if (sender instanceof Player) {
    7. final Player player = (Player) sender;
    8. if ((commandLabel.equalsIgnoreCase("m"))) {
    9.  
    10. if(args.length == 0){
    11. player.sendMessage("Message: Player argument missing.");
    12. return true;
    13. }
    14. Player tarPlayer = player.getServer().getPlayer(args[0]);
    15. if(args.length == 1){
    16. if(tarPlayer != null) {
    17. player.sendMessage("Message: Message argument missing.");
    18. return true;
    19. }
    20. }
    21.  
    22. if(tarPlayer == null){
    23. player.sendMessage("Message: Cannot find player " + args[0]);
    24. return true;
    25. }
    26.  
    27.  
    28.  
    29. if(args.length >= 2){
    30. StringBuilder sb = new StringBuilder();
    31. for(int i=1; i<args.length; i++){
    32. sb.append(args[I]).append(" ");[/I]
    33. [I] }[/I]
    34. [I] String msg = sb.toString();[/I]
    35.  
    36. [I] player.sendMessage(" -> "+ tarPlayer.getName() + msg);[/I]
    37. [I] player.sendMessage(" <- "+ tarPlayer.getName() + msg);[/I]
    38. [I] lastRecieved.put(player.getName(), tarPlayer.getName());[/I]
    39. [I] return true;[/I]
    40. [I] }[/I]
    41. [I] }[/I]
    42. [I] }[/I]
    43.  
    44. [I] if (commandLabel.equalsIgnoreCase("reply")){[/I]
    45. [I] if (sender instanceof Player) {[/I]
    46. [I] Player player = (Player) sender;[/I]
    47.  
    48. [I] if (args.length == 0){[/I]
    49. [I] player.sendMessage(ChatColor.RED + "/reply <message>");[/I]
    50. [I] return true;[/I]
    51. [I] }[/I]
    52.  
    53. [I] StringBuilder str = new StringBuilder();[/I]
    54. [I] for (int i = 0; i < args.length; i ++){[/I]
    55. [I] str.append(args + " ");[/I]
    56. [I] }[/I]
    57.  
    58. [I] String mes2 = str.toString();[/I]
    59. [I] Player target = <plugin>.getServer().getPlayer(lastRecieved.get(player.getName()));[/I]
    60. [I] if (target == null){[/I]
    61. [I] player.sendMessage(ChatColor.RED + "You have no one to reply to.");[/I]
    62. [I] return true;[/I]
    63.  
    64. [I] } else {[/I]
    65.  
    66. [I] target.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);[/I]
    67. [I] player.sendMessage(ChatColor.AQUA + "<" + player.getDisplayName() + ChatColor.AQUA + " -> " + target.getDisplayName() + ChatColor.AQUA + "> " + ChatColor.GRAY + mes2);[/I]
    68. [I] }[/I]
    69. [I] }[/I]
    70. [I] }[/I]
    71. [I] return true;[/I]
    72. [I] }[/I]
    73. [I]}[/I]


    just replace <plugin> with the instance of your plugin
     
  12. Offline

    MCCoding

    5pHiNxX
    The messaging is fine the reply is not working.
     
  13. Offline

    5pHiNxX

    @MCCoding
    just try to replace
    Code:java
    1. public Map<Player, Player> lastRecieved = new HashMap<Player, Player>();

    with
    Code:java
    1. public Map<String, String> lastRecieved = new HashMap<String, String>();


    and everytimes you use
    Code:java
    1. Player player = lastReceivet.get(<player>);

    use
    Code:java
    1. Player player = <plugin>.getServer().getPlayer(lastReceived.get(<player>.getName());


    and also replace every occurance of
    Code:java
    1. lastReceived.put(<player>, <player>);

    with
    Code:java
    1. lastReceived.put(<player>.getName(), <player>.getName());
     
Thread Status:
Not open for further replies.

Share This Page