Custom PM command!

Discussion in 'Plugin Development' started by ajs333, Nov 12, 2013.

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

    ajs333

    I have created a custom PM command so that players can private message someone. I don't understand why I don't say the actually message. When I typed /tell ajs333 hi... It won't tell ajs333 hi...

    Here is my coding...
    Code:
    if (commandLabel.equalsIgnoreCase("tell") || commandLabel.equalsIgnoreCase("msg") || commandLabel.equalsIgnoreCase("r")) {
                   
                    String message1 = "";
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                   
                    if(mute.contains(sender.getName())) {
                        return false;
                    } else {
         
                    for (int i = 1; i < args.length; i++) message += (i > 1 ? " " : "") + args[i];
         
                            sender.sendMessage(ChatColor.GRAY + "[Me -> " + target.getName() + "] " + message1);
         
                            target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);
         
                            return true;   
                    }
            }
     
  2. Offline

    Xacero

    ajs333


    Code:java
    1. if (commandLabel.equalsIgnoreCase("tell") || commandLabel.equalsIgnoreCase("msg") || commandLabel.equalsIgnoreCase("r")) {
    2.  
    3. String message1 = "";
    4. Player target = Bukkit.getServer().getPlayer(args[0]);
    5.  
    6. if(mute.contains(sender.getName())) {
    7. return false;
    8. } else {
    9.  
    10. for (int i = 1; i < args.length; i++)
    11. message1 += (i > 1 ? " " : "") + args[I]; // you had message not message1[/I]
    12.  
    13. [I] sender.sendMessage(ChatColor.GRAY + "[Me -> " + target.getName() + "] " + message1);[/I]
    14.  
    15. [I] target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);[/I]
    16.  
    17. [I] return true; [/I]
    18. [I] }[/I]
    19. [I] }[/I]


    Does that fix your problem? If not does it throw any errors? Also it'd be a good idea to check if (args.length > 2) before trying to parse everything. :)
     
  3. Offline

    MrSparkzz

    ajs333
    never use commandLabel use command.getName().equalsIgnoreCase("tell") or cmd.getName().equalsIgnoreCase("tell")
     
  4. Offline

    ajs333

  5. Offline

    MrSparkzz

    ajs333
    Try using
    Code:java
    1. args[ i ];


    Or you can use my method of converting the args to Strings:
    Code:java
    1. StringBuilder str = new StringBuilder();
    2.  
    3. for (int i = start; i < args.length; i++) {
    4. str.append(args[ i ] + " ");
    5. }
    6.  
    7. String message1 = str.toString();

    replace the "start" variable with a number where you want to start the string builder, which I'm guessing would be 2.
     
  6. Offline

    ajs333

    MrSparkzz
    I'm back to the first issue where it doesn't display the message.
     
  7. Offline

    Xacero

    ajs333
    Does it display anything at all to the user? [ex [TargetName] -> (no message here)) ?
     
  8. Offline

    MrSparkzz

    ajs333
    post your new code?

    PS: (Putting the I in the brackets was a huge pain, it kept thinking I wanted to italicize everything -.-)
     
  9. Offline

    ajs333

    Xacero
    When I type /tell ajs333 Hi

    This is what it displays!

    [me -> ajs333]

    MrSparkzz
    Here is my code:
    Code:
    if (command.getName().equalsIgnoreCase("tell") || command.getName().equalsIgnoreCase("msg")) {
               
               
                StringBuilder str1 = new StringBuilder();
                Player target = Bukkit.getServer().getPlayer(args[0]);
               
                if(mute.contains(sender.getName())) {
                return false;
                } else {
               
                    for (int i = 1; i < args.length; i++) {
                        str.append(args + " ");
                    }
                    String message1 = str1.toString();
               
                    sender.sendMessage(ChatColor.GRAY + "[Me -> " + target.getName() + "] " + message1);
               
                    target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);
               
                    return true;
                }
            }
     
  10. Offline

    MrSparkzz

    ajs333
    Oh str isn't a variable in your code, sorry. So replace that with message1. Then remove String from infront of message1 in the code I supplied.
     
  11. Offline

    ajs333

    MrSparkzz
    Woah, wait... I got a tone of errors...
     
  12. Offline

    Xacero

    ajs333
    Code:java
    1. if (command.getName().equalsIgnoreCase("tell") || command.getName().equalsIgnoreCase("msg")) {
    2.  
    3. Player target = Bukkit.getServer().getPlayer(args[0]);
    4.  
    5. if(mute.contains(sender.getName())) {
    6. return false;
    7. } else {
    8.  
    9. String message1 = "";
    10.  
    11. for (int i = 1; i < args.length; i++) {
    12. message1 = message1 + (args + " ");
    13. }
    14.  
    15. sender.sendMessage(ChatColor.GRAY + "[Me -> " + target.getName() + "] " + message1);
    16.  
    17. target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);
    18.  
    19. return true;
    20. }
    21. }
     
    MrSparkzz likes this.
  13. Offline

    MrSparkzz

    ajs333 Or you could do that ^
     
  14. Offline

    ajs333

  15. Offline

    Xacero

    ajs333
    Code:java
    1. if (command.getName().equalsIgnoreCase("tell") || command.getName().equalsIgnoreCase("msg")) {
    2.  
    3. Player target = Bukkit.getServer().getPlayer(args[0]);
    4.  
    5. if(mute.contains(sender.getName())) {
    6. return false;
    7. } else {
    8.  
    9. String message1 = "";
    10.  
    11. for (int i = 1; i < args.length; i++) {
    12. message1 = message1 + (args[I] + " ");[/I]
    13. [I]}[/I]
    14.  
    15. [I]sender.sendMessage(ChatColor.GRAY + "[Me -> " + target.getName() + "] " + message1);[/I]
    16.  
    17. [I]target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);[/I]
    18.  
    19. [I]return true;[/I]
    20. [I]}[/I]
    21. [I]}[/I]


    missed the index in the array.
    You should really familiarize yourself with how arrays work :)
    Easy error to fix if you knew what to look for, I'm writing this in the bukkit text editor so I sometimes miss things, sorry about that :)
     
  16. Offline

    ajs333

    Xacero
    Ahhh, yes that worked... Thank you to everyone who helped me! Xacero and MrSparkzz

    I don't want to overwhelm you two but now how could I do /r and that would reply to that message?
     
  17. Offline

    Xacero

    ajs333
    You're quite welcome :) Have a nice day!
     
  18. Offline

    ajs333

    Xacero
    I don't know if you saw my earlier post but I said that I didn't want to overwhelm you but how would I do /r to reply to that message?
     
  19. Offline

    Xacero

    ajs333
    Code:java
    1.  
    2. // put this outside of your command method
    3. HashMap<String, String> reply_list = new HashMap<String, String>();
    4.  
    5. //on your message command
    6. reply_list.put(target.getName(), sender.getName());
    7.  
    8. //on your reply command
    9. if (reply_list.containsKey(sender.getName())
    10. target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName());
    11. else
    12. sender.sendMessage(ChatColor.RED + "You have nobody to reply to!");
    13.  


    may need to rename some variables
     
  20. Offline

    ajs333

    Xacero
    Ok, I'll try this and see what happens!

    Xacero
    I quick question... I get an error on the "target"
     
  21. Offline

    Xacero

    ajs333
    Post your full command code please?
     
  22. Offline

    ajs333

    Xacero
    I'm sure I did something wrong because I get an internal error... :(

    Code:
    if (command.getName().equalsIgnoreCase("tell") || command.getName().equalsIgnoreCase("msg")) {
               
                Player target = Bukkit.getServer().getPlayer(args[0]);
               
                reply_list.put(target.getName(), sender.getName());
               
                if(mute.contains(sender.getName())) {
                    sender.sendMessage(ChatColor.GOLD + "You are muted!");
                return false;
                } else {
               
                String message1 = "";
               
                for (int i = 1; i < args.length; i++) {
                message1 = message1 + (args[i] + " ");
                }
               
                sender.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " ->" + target.getName() + "] " + message1);
               
                target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);
               
                return true;
                    }
                }
            if (command.getName().equalsIgnoreCase("r") || command.getName().equalsIgnoreCase("reply")) {
                Player target = null;
                String message1 = "";
               
                for (int i = 1; i < args.length; i++) {
                message1 = message1 + (args[i] + " ");
                }
     
                if (reply_list.containsKey(sender.getName()))
                        target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
                        sender.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " ->" + target.getName() + "] " + message1);
               
                        target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);
            } else {
                        sender.sendMessage(ChatColor.RED + "You have nobody to reply to!");
            }
            return false;
        }
    }
    
     
  23. Offline

    MrSparkzz

    ajs333
    target is returning null. In your "tell" command you need to add the target to the HashMap.

    EDIT: just realized you already did that... Well you should probably have it the other way around so that the sender is the first thing in the HashMap and target is the second.
     
  24. Offline

    ajs333

  25. Offline

    Xacero

    MrSparkzz

    The way it works :

    format : [player] [last person to message player]

    on message send : add to list [message target] [person that sent message]

    on reply send : get value of key [sender] (will return) [person that sent message]

    ajs333

    Got a stacktrace for me to look at?
     
  26. Offline

    MrSparkzz

    ajs333
    This is what I would do
    Code:java
    1. // your hashmap
    2. HashMap<Player, Player> reply_list = new HashMap<Player, Player>();
    3.  
    4. // adding to the hashmap
    5. reply_list.put(sender, target);
    6.  
    7. // getting the hashmap
    8. if (reply_list.containsKey(sender))
    9. target = Bukkit.getServer().getPlayer(reply_list.get(sender));


    Xacero when you do if (reply_list.containsKey(sender.getName)) you're looking for the first thing in the HashMap, which in the code you supplied would be the target's name. So basically you'd have to flip it around. and when you do reply_list.get(sender.getName()); you were still looking for the first thing in the HashMap and then get the second thing which would be the senders name in your case.

    Basically you just had your variables backwards. And I think it would be better to use Player variables instead of Strings in the HashMap. (just so you can get more information from them and it just looks cleaner.)
     
  27. Offline

    ajs333

    MrSparkzz
    Xacero
    I get this error

    20:47:25 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'r' in plugin sBan v1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
    at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
    at sBan.sBan.onCommand(sBan.java:166)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more

    >
     
  28. Offline

    Xacero

    MrSparkzz

    If I'm reading that correctly it won't reply to the last person that messaged you, it'll send the message to the last person you messaged. Not the same thing :p
     
  29. Offline

    MrSparkzz

    ajs333
    What's line 166 (located in your onCommand() method)?
     
  30. Offline

    Xacero

    ajs333

    Code:java
    1.  
    2. if (command.getName().equalsIgnoreCase("r") || command.getName().equalsIgnoreCase("reply")) {
    3. String message1 = "";
    4.  
    5. for (int i = 0; i < args.length; i++) {
    6. message1 = message1 + (args + " ");
    7. }
    8.  
    9. if (reply_list.containsKey(sender.getName()))
    10. Player target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
    11. if (target == null) {
    12. sender.sendMessage(ChatColor.RED + "That player is offline!");
    13. return true;
    14. }
    15. sender.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " ->" + target.getName() + "] " + message1);
    16.  
    17. target.sendMessage(ChatColor.GRAY + "[" + sender.getName() + " - > Me] " + message1);
    18. } else {
    19. sender.sendMessage(ChatColor.RED + "You have nobody to reply to!");
    20. }
    21. return false;[I][/I]


    Try this?
     
Thread Status:
Not open for further replies.

Share This Page