Solved /reply is not working

Discussion in 'Plugin Development' started by random_username, Nov 20, 2013.

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

    random_username

    Hello, I am having trouble making a /reply command. The problem is that each time I use /reply it says "There is no one to reply to.", even after somone sent me a message. Here is my code:
    Code:java
    1. public class msg implements CommandExecutor{
    2. private Main main;
    3.  
    4. public msg(Main main)
    5. {
    6. this.main = main;
    7. }
    8.  
    9.  
    10. private Map<String, String> map = new HashMap<String, String>();
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    14. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    15. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    16. if(cmd.getName().equalsIgnoreCase("message")){
    17. if(Sender instanceof Player){
    18. Player player = (Player) Sender;
    19. if(player.hasPermission("pcommands.msg")){
    20. if(args.length >= 2){
    21. Player target = Bukkit.getPlayer(args[0]);
    22. if(target == null){
    23. player.sendMessage(prefix + "§cPlayer not found.");
    24. }else{
    25. StringBuilder sb = new StringBuilder();
    26. for(int i=1; i<args.length; i++){
    27. sb.append(args[i]).append(" ");
    28. }
    29. String message = sb.toString();
    30. String sendermsg = prefix + "§3" + " [me->" + target.getName() + "] " + ChatColor.WHITE + message.replace("&", "§");
    31. String targetmsg = prefix + "§3" + " [" + player.getName() + "->me] " + ChatColor.WHITE + message.replace("&", "§");
    32. target.sendMessage(targetmsg);
    33. player.sendMessage(sendermsg);
    34. this.map.put(target.getName(), player.getName());
    35. }
    36. }
    37. }else{
    38. player.sendMessage(prefix + noperm);
    39. }
    40. }
    41. }else if(cmd.getName().equalsIgnoreCase("reply")){
    42. if(Sender instanceof Player){
    43. Player player = (Player) Sender;
    44. if(player.hasPermission("pcommands.reply")){
    45. if(args.length >= 1){
    46. if(map.containsKey(player.getName())){
    47. Player sendto = Bukkit.getPlayer((String) this.map.get(player.getName()));
    48. if(sendto != null){
    49. if(sendto.isOnline()){
    50. StringBuilder sb = new StringBuilder();
    51. for(int i=1; i<args.length; i++){
    52. sb.append(args[i]).append(" ");
    53. }
    54. String message = sb.toString();
    55. String sendermsg = prefix + "§3" + "[me->" + sendto.getName() + "] " + ChatColor.WHITE + message.replace("&", "§");
    56. String targetmsg = prefix + "§3" + "[" + player.getName() + "->me] " + ChatColor.WHITE + message.replace("&", "§");
    57. sendto.sendMessage(targetmsg);
    58. player.sendMessage(sendermsg);
    59. }else{
    60. player.sendMessage(prefix + "§cPlayer is not online!");
    61. }
    62. player.sendMessage(prefix + "§cPlayer not found.");
    63. }else{
    64. player.sendMessage(prefix + "§cPlayer not found.");
    65. }
    66. }else{
    67. player.sendMessage(prefix + "§cThere is no one to reply to.");
    68. }
    69. }
    70. }
    71. }
    72. }
    73. return false;
    74. }
    75. }[/i][/i]

    Any ideas on how to solve this? Thanks for the help :)
     
  2. Offline

    afistofirony

    random_username Could you try putting the inverse into the HashMap as well? i.e.

    Code:
    this.map.put(target.getName(), player.getName());
    this.map.put(player.getName(), target.getName());
     
  3. Offline

    random_username

    Okay, I'm going to try it, thanks for the reply :)

    afistofirony
    Nope, It's not working :) could there be another way to solve it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    random_username

    Modified the code a bit, any ideas on how to solve it?
    code:
    Code:java
    1. public class msg implements CommandExecutor{
    2. private Main main;
    3.  
    4. public msg(Main main)
    5. {
    6. this.main = main;
    7. }
    8.  
    9. //for the ones who use /message
    10. private Map<String, String> map = new HashMap<String, String>();
    11.  
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    15. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    16. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    17. if(cmd.getName().equalsIgnoreCase("message")){
    18. if(Sender instanceof Player){
    19. Player player = (Player) Sender;
    20. if(player.hasPermission("pcommands.msg")){
    21. if(args.length >= 2){
    22. Player target = Bukkit.getPlayer(args[0]);
    23. if(target == null){
    24. player.sendMessage(prefix + "§cPlayer not found.");
    25. }else{
    26. StringBuilder sb = new StringBuilder();
    27. for(int i=1; i<args.length; i++){
    28. sb.append(args[i]).append(" ");
    29. }
    30. String message = sb.toString();
    31. String sendermsg = prefix + "§3" + " [me->" + target.getName() + "] " + ChatColor.WHITE + message.replace("&", "§");
    32. String targetmsg = prefix + "§3" + " [" + player.getName() + "->me] " + ChatColor.WHITE + message.replace("&", "§");
    33. target.sendMessage(targetmsg);
    34. player.sendMessage(sendermsg);
    35. this.map.put(target.getName(), player.getName());
    36. }
    37. }
    38. }else{
    39. player.sendMessage(prefix + noperm);
    40. }
    41. }
    42. }else if(cmd.getName().equalsIgnoreCase("reply")){
    43. if(Sender instanceof Player){
    44. Player player = (Player) Sender;
    45. if(player.hasPermission("pcommands.reply")){
    46. if(args.length >= 1){
    47. if(map.containsKey(player.getName())){
    48. Player target = Bukkit.getPlayer((String) this.map.get(player.getName()));
    49. if(target != null){
    50. if(target.isOnline()){
    51. StringBuilder sb = new StringBuilder();
    52. for(int i=1; i<args.length; i++){
    53. sb.append(args[i]).append(" ");
    54. }
    55. String message = sb.toString();
    56. String sendermsg = prefix + "§3" + "[me->" + target.getName() + "] " + ChatColor.WHITE + message.replace("&", "§");
    57. String targetmsg = prefix + "§3" + "[" + player.getName() + "->me] " + ChatColor.WHITE + message.replace("&", "§");
    58. target.sendMessage(targetmsg);
    59. player.sendMessage(sendermsg);
    60. this.map.remove(target.getName());
    61. this.map.remove(player.getName());
    62. this.map.put(target.getName(), player.getName());
    63.  
    64. }else{
    65. player.sendMessage(prefix + "§cPlayer is not online!");
    66. }
    67. player.sendMessage(prefix + "§cPlayer not found.");
    68. }else{
    69. player.sendMessage(prefix + "§cPlayer not found.");
    70. }
    71. }else{
    72. player.sendMessage(prefix + "§cThere is no one to reply to.");
    73. }
    74. }
    75. }
    76. }
    77. }
    78. return false;
    79. }
    80. }
    81. [/i][/i]
     
  5. Offline

    ajs333

  6. Offline

    random_username

    It isn't working at the moment :oops:
     
  7. Offline

    random_username

    bump, any ideas? :) The problem is that each time someone uses /reply, it says "There is no one to reply to.", even after receiving a message. Code:
    Code:java
    1. public class msg implements CommandExecutor{
    2. private Main main;
    3.  
    4. public msg(Main main)
    5. {
    6. this.main = main;
    7. }
    8.  
    9. //for the ones who use /message
    10. private Map<String, String> map = new HashMap<String, String>();
    11.  
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    15. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    16. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    17. if(cmd.getName().equalsIgnoreCase("message")){
    18. if(Sender instanceof Player){
    19. Player player = (Player) Sender;
    20. if(player.hasPermission("pcommands.msg")){
    21. if(args.length >= 2){
    22. Player target = Bukkit.getPlayer(args[0]);
    23. if(target == null){
    24. player.sendMessage(prefix + "§cPlayer not found.");
    25. }else{
    26. StringBuilder sb = new StringBuilder();
    27. for(int i=1; i<args.length; i++){
    28. sb.append(args[i]).append(" ");
    29. }
    30. String msg = sb.toString();
    31. String message = ChatColor.translateAlternateColorCodes('&', msg);
    32. String sendermsg = prefix + "§3" + " [me->" + target.getName() + "] " + ChatColor.WHITE + message.replace("&", "§");
    33. String targetmsg = prefix + "§3" + " [" + player.getName() + "->me] " + ChatColor.WHITE + message.replace("&", "§");
    34. target.sendMessage(targetmsg);
    35. player.sendMessage(sendermsg);
    36. this.map.put(target.getName(), player.getName());
    37. }
    38. }
    39. }else{
    40. player.sendMessage(prefix + noperm);
    41. }
    42. }
    43. }else if(cmd.getName().equalsIgnoreCase("reply")){
    44. if(Sender instanceof Player){
    45. Player player = (Player) Sender;
    46. if(player.hasPermission("pcommands.reply")){
    47. if(args.length >= 1){
    48. if(map.containsKey(player.getName())){
    49. Player target = Bukkit.getPlayer((String) this.map.get(player.getName()));
    50. if(target != null){
    51. if(target.isOnline()){
    52. StringBuilder sb = new StringBuilder();
    53. for(int i=0; i<args.length; i++){
    54. sb.append(args[i]).append(" ");
    55. }
    56. String msg = sb.toString();
    57. String message = ChatColor.translateAlternateColorCodes('&', msg);
    58. String sendermsg = prefix + "§3" + "[me->" + target.getName() + "] " + ChatColor.WHITE + message.replace("&", "§");
    59. String targetmsg = prefix + "§3" + "[" + player.getName() + "->me] " + ChatColor.WHITE + message.replace("&", "§");
    60. target.sendMessage(targetmsg);
    61. player.sendMessage(sendermsg);
    62. this.map.remove(target.getName());
    63. this.map.remove(player.getName());
    64. this.map.put(target.getName(), player.getName());
    65.  
    66. }else{
    67. player.sendMessage(prefix + "§cPlayer is not online!");
    68. }
    69. player.sendMessage(prefix + "§cPlayer not found.");
    70. }else{
    71. player.sendMessage(prefix + "§cPlayer not found.");
    72. }
    73. }else{
    74. player.sendMessage(prefix + "§cThere is no one to reply to.");
    75. }
    76. }
    77. }
    78. }
    79. }
    80. return false;
    81. }
    82. }
    83. [/i][/i]

    Thanks for the help ;)
     
  8. Offline

    ajs333

    random_username
    Try to do this under the line that says
    Code:
    this.map.put(target.getName(), player.getName());
    Put this under that ^
    Code:
    this.map.put(player.getName(), target.getName());
     
  9. Offline

    ajs333

  10. Offline

    random_username

    It still says "there is no one to reply to", is there any other way to fix this? :)
     
  11. Offline

    Plo124

    random_username
    Code:java
    1. map.containsKey(player.getName())
    2. // in the IF statement for reply
    3. // Make it
    4. map.containsValue(player.getName())
     
  12. Offline

    random_username

    Nope, that isn't working either, btw, thanks for the reply :) .
    Any other ideas?? :D
     
  13. Offline

    pope_on_dope

    are the messages being sent to one another and the replier has the correct permissions?

    if so, try adding your hashmap to your main class, like this:
    Code:
    public static HashMap<String, Stirng> map = new HashMap<String, String>();
    and access it from there, rather than in your command executor class
     
  14. Offline

    random_username

    while using /message yes, they are. But when I use /reply, it says "there is no one to reply to". :)
     
  15. Offline

    pope_on_dope

    read my edit :)
     
  16. Offline

    random_username

    What do you mean? Do I just set the Hashmap to:
    Code:java
    1. private static Map<String, String> map = new HashMap<String, String>();

    ?
     
  17. Offline

    pope_on_dope

    put that hashmap in your main class, the class that extends JavaPlugin. and use the hashmap their to put the targets of /reply
     
  18. Offline

    random_username

    so when I want to put the users, I should do:
    Code:java
    1. main.map.put(target.getName(), player.getName());

    ? Btw, this part is in the msg class, hashmap in the main class.
     
  19. Offline

    pope_on_dope

    yes! any luck?
     
  20. Offline

    random_username

    Sorry for late response, had to look for someone to help me test. Yes, that solved it, thanks so much! :D
     
    pope_on_dope likes this.
  21. Offline

    pope_on_dope

    BTW for future references: every time a command is typed with the correct syntax, like /msg or /reply, a new instance of your command executor class will be created resulting in the hash map to be reset every time the command is typed
     
  22. Offline

    ajs333

    random_username
    I have been trying to code something similar to what you just did and I only get one error on this line...

    Code:
     main.map.put(target.getName(), player.getName());
     
  23. Offline

    afistofirony


    Actually, this is not the case unless the code specifically says to do so. For example, if you use getCommand("msg").setExecutor(new MessageCommand()), that specific instance is stored as the instance to reference, not the class itself.

    The only case in which creating a new instance for every execution would be realistic is if the command's class was stored, not an instance of the class (i.e. getCommand("msg").setExecutor(MessageCommand.class)). However, this can lead to issues with <class>.newInstance() because the class may not have a default constructor (one with no parameters).
     
Thread Status:
Not open for further replies.

Share This Page