Private Message Command - How to Code /r

Discussion in 'Plugin Help/Development/Requests' started by HexLazer205, Dec 28, 2014.

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

    HexLazer205

    Hi, I am working on a private messaging system for the players on my server to private message each other. Currently I have a successfully working /msg command. Here is the code:

    Code:
    if(cmd.getName().equalsIgnoreCase("msg")) {
    
            
    
                Player target = Bukkit.getPlayer(args[0]);
    
            
    
                if(target != null) {
    
                
    
                    String message = "";
    
                
    
                    for(int i = 1; i != args.length; i++)
    
                        message += args[I] + " ";
    
                 
    
                    target.sendMessage("§6§l" + sender.getName() + " §e§l>>> §6§l" + target.getName() + " §7§l" + message);
    
                    sender.sendMessage("§6§l" + sender.getName() + " §e§l>>> §6§l" + target.getName() + " §7§l" + message);
    
                }
    
                else if(!(target != null)) {
    
                 
    
                    sender.sendMessage("§4That player is not currently online!");
    
                 
    
                }
    
                return true;
    
            }
    
    I now need to add a /r command to enable convenience for my users. Instead of doing /msg (username) (message) every time they need to private message a friend, they can just do /r (message). I have attempted to code this in myself, however I could not figure it out. I have also searched on the internet for this. How would I go about making a /r command for my users to conveniently be able to reply to one another's private message?
     
  2. Offline

    TheCodingCat

  3. Offline

    1Rogue Retired Staff

    Keep a cache of the previous message recipient using a Map.
     
  4. Offline

    HexLazer205

    OK, thanks! I will try this out. How would I make it so the argument to specify a username is not needed, and just a message is needed?
     
  5. Offline

    WarmakerT

    Code:
    HashMap<UUID, UUID> lastSentMessages = new HashMap<UUID, UUID>();
    The key (the first entry) is the sender, the value (the second entry) is the receiver.

    So, for example:
    HexLazer205: /msg WarmakerT Hello, how are you?
    lastSentMessages.put(HexLazer205's UUID, WarmakerT's UUID);
    lastSentMessages.put(WarmakerT's UUID, HexLazer205's UUID);
    WarmakerT: /r I'm fine. How are you?
    lastSentMessages.put(HexLazer205's UUID, WarmakerT's UUID);
    TheCodingCat: /msg WarmakerT hey bro wanna smoke some weed
    lastSentMessages.put(TheCodingCat's UUID, WarmakerT's UUID);
    lastSentMessages.put(WarmakerT's UUID, HexLazer205's UUID);
    HexLazer205: /r I'm very happy today, actually! :)
    lastSentMessages.put(WarmakerT's UUID, HexLazer205's UUID);
     
  6. Offline

    ThatGamingRiot

    I would check if the player using /r had any recent messages directed toward him, and if so, get the username that sent it. Then just make the player using /r send a message to that player.
     
  7. Offline

    HexLazer205

    @WarmakerT I am currently trying out the code you suggested, and I do not know what exactly I put in the parentheses in lastSentMessages.put(key, value); where would I place this in the code and how would I use it? Thanks!
     
  8. Offline

    WarmakerT

    @HexLazer205 I know I said UUID, but you can use the actual players since they have to relog to change username.

    The key is one Player object, the value is another Player object.
    To get HexLazer205's Player, you'd use getServer().getPlayer("HexLazer205"). If HexLazer205 isn't online, that code will return null. Knowing this, you can insert a safeguard in case the player is offline.

    Code:
    Player playerA = getServer().getPlayer("HexLazer205");
    Player playerB = getServer().getPlayer("WarmakerT");
    
    if(playerB == null) {
       playerA.sendMessage("§cThat player is offline at the moment.");
    }
     
  9. Offline

    HexLazer205

    Hi, @WarmakerT is it possible I can use getServer().getPlayer(); in any way in order to let anyone private message each other, so I don't need to manually type in everyone's username to allow them successful access to /r when someone private messages them?
     
  10. Offline

    1Rogue Retired Staff

    What are you talking about? When someone uses /msg, you would add the player they are messaging to a map linking their uuid to the sender's uuid.

    Code:
    #get
    Target UUID => Sender UUID
    Then when you use /r, you retrieve the mapped value using the command executor's UUID.
     
  11. Offline

    HexLazer205

    @1Rogue WarmakerT showed me this part of code:

    Code:
    Player playerA = getServer().getPlayer("HexLazer205");
    Player playerB = getServer().getPlayer("WarmakerT");
    
    It seems as though I would have to type the player username in the code...
    How would I use a HashMap to retrieve data, such as player names who just sent you a private message, instead of typing the player names in the code?
     
  12. Offline

    1Rogue Retired Staff

    When people made /msg and /r commands they didn't take into account every single player in the universe.

    Read up on The Map Interface if you want to become better at programming by understanding how they work. You map a key to a value using .put(), and retrieve that value by using the key with .get(). You only need to use getPlayer(String) if you are looking up the player for the /msg command.
     
  13. Offline

    HexLazer205

    Hi, I am now attempting to do this. I get an error where it says "put".

    Code:
    lastSentMessages.put(sender.getName(), target.getName());
    
    I get the following error:
    The method put(UUID, UUID) in the type HashMap<UUID,UUID> is not applicable for the arguments (String,
    String). How do I fix this error?
     
  14. Offline

    mythbusterma

    @HexLazer205

    Stop trying to put Strings into a Map that expects a UUID.
     
    Captain Dory likes this.
  15. Offline

    HexLazer205

    @mythbusterma I need to know what code line to get a player UUID, so I can put the command sender and the target player into the key and value of the code line. Thanks!
     
  16. Offline

    teej107

    Read the Bukkit code documentation for Player (Entity to be more specific). It has the method you need to get the UUID. I would also recommend picking up a Java for beginners book.
     
  17. Offline

    HexLazer205

    @teej107 I know all of the Java basics and a few other things in Java, just enough to get me to my prerequisites for Bukkit. I understand how all the code I learn is used, and for now I am just learning. Thanks for your help :)

    Also, could I please have a link to the page you recommended on the Bukkit Documentation? Thanks!
     
  18. Offline

    teej107

  19. Offline

    1Rogue Retired Staff

    Your solution would be valid if it was a Map<String, String>, however you should not use player's name as indexes anymore since name-changing is going to be introduced. Instead, using their unique id is preferred. A player's unique id is of the type UUID, whereas their name is a String. So instead of using names, use Player#getUniqueId()
     
  20. Offline

    HexLazer205

    @1Rogue this would be a messaging plugin that isn't saving any player data, such as rank or money. Therefore it wouldn't need to be UUID, right? Anyway, here I have an error where HashMap is supposed to be <UUID, UUID> and it is apparently <CommandSender, UUID> which is giving me this error. How can I get past this?

    Code:
    lastSentMessages.put(sender.getUniqueId(), target.getUniqueId());
    
     
  21. Offline

    mythbusterma

    @HexLazer205

    Doesn't really matter either way, honestly.
     
  22. Offline

    1Rogue Retired Staff

    Change it to <UUID, UUID> then. As for CommandSender, you need to verify it's a Player and cast them to player in order to get their UUID.

    As for using UUID vs. their name, it's just more consistent to index based on UUID. That and since Bukkit indexes based on that, it's quicker to do lookups on things using UUID (such as Bukkit.getPlayer())
     
    WarmakerT likes this.
  23. Offline

    WarmakerT

    HashMap<UUID, UUID> a = new HashMap<UUID, UUID>();
    That means you'll need to use a.put(UUID here, UUID here);

    HashMap<UUID, String> a = new HashMap<UUID here, String here>();
    That means you'll need to use a.put(UUID here, String here);

    HashMap<String, string> a = new HashMap<String here, String here>();
    That means you'll need to use a.put(String here, String here);

    I'd recommend watching this.
    Basically, UUID is like a Tuna class. You just can't see the code for UUID (unless you go and look for it online).
     
  24. Offline

    1Rogue Retired Staff

    I would recommend not watching that at all seeing the name of the class within the first five seconds.

    Generics (or rather, their syntax) are essentially explained by page 2 of oracle's tutorial for Collections. Also unless he is using Java 6, then it would just be:

    Code:java
    1. Map<?, ?> example = new HashMap<>();
     
  25. Offline

    HexLazer205

    Hi, I have been doing other things recently, sorry for the late response. I tried getting the sender and target's names in the HashMap in the /msg command after it was executed, in order to get the target's name. Then, in the /r command, I get the target player and send the message to him/her. Here is my command method code:

    Code:
    @SuppressWarnings("deprecation")
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
           
    
            HashMap<String, String> lastSentMessages = new HashMap<String, String>();
    
            Player player = (Player) sender;
    
           
    
            if(cmd.getName().equalsIgnoreCase("gm")) {
    
                   
    
                if(args.length == 0) {
    
                    returnfalse;
    
                }
    
               
    
                if(args[0].equalsIgnoreCase("survival") || 
    
                        args[0].equalsIgnoreCase("0")) {
    
                       
    
                    if(player.getGameMode() != GameMode.SURVIVAL) {
    
                           
    
                        player.setGameMode(GameMode.SURVIVAL);
    
                        player.sendMessage("§aYou are now in survival mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.SURVIVAL) {
    
                           
    
                        player.sendMessage("§cYou are already in survival mode!");
    
                           
    
                    }
    
                }
    
                else if(args[0].equalsIgnoreCase("creative") || 
    
                        args[0].equalsIgnoreCase("1")) {
    
                       
    
                    if(player.getGameMode() != GameMode.CREATIVE) {
    
                           
    
                        player.setGameMode(GameMode.CREATIVE);
    
                        player.sendMessage("§aYou are now in creative mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.CREATIVE) {
    
                           
    
                        player.sendMessage("§cYou are already in creative mode!");
    
                           
    
                    }
    
                }
    
                else if(args[0].equalsIgnoreCase("adventure") || 
    
                        args[0].equalsIgnoreCase("2")) {
    
                       
    
                    if(player.getGameMode() != GameMode.ADVENTURE) {
    
                           
    
                        player.setGameMode(GameMode.ADVENTURE);
    
                        player.sendMessage("§aYou are now in adventure mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.ADVENTURE) {
    
                           
    
                        player.sendMessage("§cYou are already in adventure mode!");
    
                           
    
                    }
    
                }
    
                else if(args[0].equalsIgnoreCase("spectator") || 
    
                        args[0].equalsIgnoreCase("3")) {
    
                       
    
                    if(player.getGameMode() != GameMode.SPECTATOR) {
    
                           
    
                        player.setGameMode(GameMode.SPECTATOR);
    
                        player.sendMessage("§aYou are now in spectator mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.SPECTATOR) {
    
                           
    
                        player.sendMessage("§cYou are already in spectator mode!");
    
                           
    
                    }
    
                }
    
                returntrue;
    
            }
    
            if(cmd.getName().equalsIgnoreCase("fly")) {
    
               
    
                if(player.getAllowFlight() == false) {
    
                   
    
                    player.setAllowFlight(true);
    
                    player.sendMessage("§aYou are now able to fly!");
    
                   
    
                }
    
                else if(player.getAllowFlight() == true) {
    
                   
    
                    player.setAllowFlight(false);
    
                    player.sendMessage("§cYou are no longer able to fly!");
    
                   
    
                }
    
                returntrue;
    
            }
    
            if(cmd.getName().equalsIgnoreCase("msg")) {
    
               
    
                Player target = Bukkit.getPlayer(args[0]);
    
               
    
                if(target != null) {
    
                   
    
                    String message = "";
    
                   
    
                    for(int i = 1; i != args.length; i++) {
    
                        message += args[i] + " ";
    
                    }
    
                   
    
                    target.sendMessage("§6" + player.getName() + " §e--> §6" + target.getName() + " §7" + message);
    
                    player.sendMessage("§6" + player.getName() + " §e--> §6" + target.getName() + " §7" + message);
    
                   
    
                    lastSentMessages.put(player.getName(), target.getName());
    
                   
    
                }
    
               
    
                else if(target == null) {
    
                   
    
                    player.sendMessage("§cThat player is not currently online!");
    
                   
    
                }
    
                returntrue;
    
            }
    
            if(cmd.getName().equalsIgnoreCase("r")) {
    
               
    
                Player target = Bukkit.getPlayer(args[0]);
    
               
    
                if(target != null) {
    
                   
    
                    String message = "";
    
                   
    
                    for(int i = 1; i != args.length; i++) {
    
                        message += args[i] + " ";
    
                    }
    
                   
    
                    lastSentMessages.get(target.getName()).sendMessage("§6" + player.getName() + " §e--> §6" + lastSentMessages.get(target.getName()) + " §7" + message);
    
                    player.sendMessage("§6" + player.getName() + " §e--> §6" + lastSentMessages.get(target.getName()) + " §7" + message);
    
                   
    
                    lastSentMessages.put(player.getName(), target.getName());
    
                   
    
                }
    
               
    
                else if(target == null) {
    
                   
    
                    player.sendMessage("§cThat player is not currently online!");
    
                   
    
                }
    
                returntrue;
    
            }
    
            returnfalse;
    
        }
    
    The error highlights over "sendMessage()" in the /r command. The error is:
    The method sendMessage(String) is undefined for the type String.

    I don't understand this error. How can I fix this? Thanks!
     
  26. Offline

    Konato_K

    @HexLazer205 You're trying to use "sendMessage" in a String object, try to do it in a Player object
     
  27. Offline

    HexLazer205

    What do you mean? I'm using player objects, such as "player.getName()" and "target.getName()" where I get the error on "sendMessage" is when I send the private message to the other player. Is the hashmap "lastSentMessages.put()" and "lastSentMessages.get()" in the correct places, and are correctly used? I don't understand what you mean, Konato_K. Can you explain how to do what you mentioned?
     
  28. Offline

    Konato_K

    @HexLazer205

    Code:
    lastSentMessages.get(target.getName()).sendMessage...
    lastSentMessages is a Map<String, String> map, therefore calling get will return a String, you're trying to use sendMessage in your String.

    Anyway, there are also other really bad issues with your code :/
     
  29. Offline

    HexLazer205

    I'm glad you said there were bad issues with my code, however didn't tell me how to resolve them :)
    I know, I just copied the /msg into the /r for now, until I can get what I want, working, then I will edit the code to the necessary edits.

    Should I use UUID, and it will work? Or do I need to use specific things such as CommandSender?

    Hi, I have worked on the command, and now I have no errors with strings, etc.
    My commands code:

    Code:
    @SuppressWarnings("deprecation")
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
           
    
            Player player = (Player) sender;
    
            Player target = Bukkit.getPlayer(args[0]);
    
            HashMap<Player, Player> lastSentMessages = new HashMap<Player, Player>();
    
           
    
            if(cmd.getName().equalsIgnoreCase("gm")) {
    
                   
    
                if(args.length == 0) {
    
                    returnfalse;
    
                }
    
               
    
                if(args[0].equalsIgnoreCase("survival") || 
    
                        args[0].equalsIgnoreCase("0")) {
    
                       
    
                    if(player.getGameMode() != GameMode.SURVIVAL) {
    
                           
    
                        player.setGameMode(GameMode.SURVIVAL);
    
                        player.sendMessage("§aYou are now in survival mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.SURVIVAL) {
    
                           
    
                        player.sendMessage("§cYou are already in survival mode!");
    
                           
    
                    }
    
                }
    
                else if(args[0].equalsIgnoreCase("creative") || 
    
                        args[0].equalsIgnoreCase("1")) {
    
                       
    
                    if(player.getGameMode() != GameMode.CREATIVE) {
    
                           
    
                        player.setGameMode(GameMode.CREATIVE);
    
                        player.sendMessage("§aYou are now in creative mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.CREATIVE) {
    
                           
    
                        player.sendMessage("§cYou are already in creative mode!");
    
                           
    
                    }
    
                }
    
                else if(args[0].equalsIgnoreCase("adventure") || 
    
                        args[0].equalsIgnoreCase("2")) {
    
                       
    
                    if(player.getGameMode() != GameMode.ADVENTURE) {
    
                           
    
                        player.setGameMode(GameMode.ADVENTURE);
    
                        player.sendMessage("§aYou are now in adventure mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.ADVENTURE) {
    
                           
    
                        player.sendMessage("§cYou are already in adventure mode!");
    
                           
    
                    }
    
                }
    
                else if(args[0].equalsIgnoreCase("spectator") || 
    
                        args[0].equalsIgnoreCase("3")) {
    
                       
    
                    if(player.getGameMode() != GameMode.SPECTATOR) {
    
                           
    
                        player.setGameMode(GameMode.SPECTATOR);
    
                        player.sendMessage("§aYou are now in spectator mode!");
    
                           
    
                    }
    
                    else if(player.getGameMode() == GameMode.SPECTATOR) {
    
                           
    
                        player.sendMessage("§cYou are already in spectator mode!");
    
                           
    
                    }
    
                }
    
                returntrue;
    
            }
    
            if(cmd.getName().equalsIgnoreCase("fly")) {
    
               
    
                if(player.getAllowFlight() == false) {
    
                   
    
                    player.setAllowFlight(true);
    
                    player.sendMessage("§aYou are now able to fly!");
    
                   
    
                }
    
                else if(player.getAllowFlight() == true) {
    
                   
    
                    player.setAllowFlight(false);
    
                    player.sendMessage("§cYou are no longer able to fly!");
    
                   
    
                }
    
                returntrue;
    
            }
    
            if(cmd.getName().equalsIgnoreCase("msg")) {
    
               
    
                if(target != null) {
    
                   
    
                    String message = "";
    
                   
    
                    for(int i = 1; i != args.length; i++) {
    
                        message += args[i] + " ";
    
                    }
    
                   
    
                    target.sendMessage("§6" + player.getName() + " §e--> §6" + target.getName() + " §7" + message);
    
                    player.sendMessage("§6" + player.getName() + " §e--> §6" + target.getName() + " §7" + message);
    
                   
    
                    lastSentMessages.put(player, target);
    
                   
    
                }
    
               
    
                else if(target == null) {
    
                   
    
                    player.sendMessage("§cThat player is not currently online!");
    
                   
    
                }
    
                returntrue;
    
            }
    
            if(cmd.getName().equalsIgnoreCase("r")) {
    
               
    
                String message = "";
    
                   
    
                lastSentMessages.get(target).sendMessage("§6" + player.getName() + " §e--> §6" + lastSentMessages.get(target) + " §7" + message);
    
                player.sendMessage("§6" + player.getName() + " §e--> §6" + lastSentMessages.get(target) + " §7" + message);
    
                   
    
                lastSentMessages.put(player, target);
    
                   
    
                returntrue;
    
            }
    
            returnfalse;
    
        }
    
    When I /msg another player, the /msg command works fine, but when i do /r (message) it gives me an internal error. I checked the console to see what the error was and here it is:

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'r' in plugin HexPlugin v1.0.0
    
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_25]
    
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_25]
    
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_25]
    
    Caused by: java.lang.NullPointerException
    
        at io.github.edragon205.hexplugin.HexPlugin.onCommand(HexPlugin.java:182) ~[?:?]
    
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.jar:git-Spigot-5ffe41c-306b233]
    
        ... 14 more
    
    
    I'm sure there's something wrong with my code (again), please help?

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

    mrCookieSlime Retired Staff

    Moved to Alternatives Section.
     
Thread Status:
Not open for further replies.

Share This Page