/r command for /msg

Discussion in 'Spigot Plugin Development' started by Oahu, Apr 16, 2017.

  1. I have been working on a custom essentials recently and I have ran into one issue. I am trying to make the /r command and I can't seem to figure out how I would do it. Would anyone know how I could make the command? Here is my code for /msg.

    Code (Text):
    package us.alarha.essentials.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import us.alarha.essentials.API;
    import us.alarha.essentials.Main;

    public class Msg extends API implements CommandExecutor {

        public Msg(Main plugin) {
            super(plugin);
        }

        @Override
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
            if (s.equalsIgnoreCase("msg")) {
                Player p = (Player) commandSender;
                if (args.length == 0 || args.length == 1) {
                    if (!p.hasPermission("alarhaessentials.msg")) {
                        p.sendMessage(noperm);
                    } else {
                       message(p, "&cUsage: /msg <player> <message>");
                    }
                }
                if (args.length >= 2) {
                    if (!p.hasPermission("alarhaessentials.msg")) {
                        p.sendMessage(noperm);
                    } else {
                        if (args[0] == null) {
                            message(p, "&cThe player specified is not online.");
                        } else {
                            Player target = Bukkit.getPlayer(args[0]);

                            String msg = "";
                            for (int i = 1; i < args.length; i++) {
                                msg = msg + args[i] + " ";
                            }
                            message(p, "&7(To &c" + target.getName() + "&7) " + msg);
                            message(target, "&7(From &c" + p.getName() + "&7) " + msg);
                        }
                    }
                }
            }
           
            return false;
        }
    }
     
     
  2. You could create a HashMap<Player, Player> and when someone messages you do hashmap.put(target, sender);
     
    • Agree Agree x 1
  3. You could have a class containing a map that links UUID to UUID.
    So then when a player messages another player you can set a Key Value Pair in the map linking the two UUIDS.
    Then on /r do: someClass.getLastMessaged(p,p2).sendMessage or whatever.
    Essentially just find a way to store who the last person messaged was then on /r send a message to that person. A map is a good place to store that sort of data.
    This was really unclear I'm sorry I'm sloshed :/
     
  4. Yea I have tried that already but couldn't figure out how I would do it, could you put an example of that implemented into my code?
     
  5. Rewrote your onCommand part and added lastmsg Map
    Code (Text):

        HashMap<UUID, UUID> lastmsg = new HashMap<>();
        @Override
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
           
            if (s.equalsIgnoreCase("msg")) {
                Player p = (Player) commandSender;
                if (args.length == 0 || args.length == 1) {
                    if (!p.hasPermission("alarhaessentials.msg")) {
                        p.sendMessage(noperm);
                    } else {
                       message(p, "&cUsage: /msg <player> <message>");
                    }
                }
                if (args.length >= 2) {
                    if (!p.hasPermission("alarhaessentials.msg")) {
                        p.sendMessage(noperm);
                    } else {
                        if (Bukkit.getPlayer(args[0]) == null) {
                            message(p, "&cThe player specified is not online.");
                        } else {
                            Player target = Bukkit.getPlayer(args[0]);
                           
                            //Here is the Hashmap Part
                            lastmsg.put(target.getUniqueId(), p.getUniqueId());
                           
                            StringBuilder sb = new StringBuilder();
                            for (int i = 2; i < args.length; i++) {
                                sb.append(args[i] + " ");
                            }
                            String msg = sb.toString();
                            message(p, "&7(To &c" + target.getName() + "&7) " + msg);
                            message(target, "&7(From &c" + p.getName() + "&7) " + msg);
                        }
                    }
                }
               
            }
            if (s.equalsIgnoreCase("r")) {
                Player p = (Player) commandSender;
                if (! lastmsg.containsKey(p.getUniqueId())) {
                    message(p, "&cNobody messaged you!");
                    return true;
                }
                Player target = Bukkit.getPlayer(lastmsg.get(p.getUniqueId()));
                if (target == null) {
                    message(p, "&cThe last player you messaged is offline.");
                    lastmsg.remove(target);
                    return true;
                }
               
                StringBuilder sb = new StringBuilder();
                for (int i = 2; i < args.length; i++) {
                    sb.append(args[i] + " ");
                }
                String msg = sb.toString();
                message(p, "&7(To &c" + target.getName() + "&7) " + msg);
                message(target, "&7(From &c" + p.getName() + "&7) " + msg);
               
                lastmsg.put(target.getUniqueId(), p.getUniqueId());

               
            }
            return false;
        }
     
     
  6. Use guava cache builders for the last replied map, therefore it doesn't stay in the map for 10 years. Like a timeout system.
     
  7. I tried this, whenever I try to /r no matter what I say it says "Nobody messaged you!"
     
  8. You need to initialise the hashmap/map like this:
    Code (Text):
    HashMap<UUID, UUID> map0 = new HashMap<UUID, UUID>();
    // Or
    Map<UUID, UUID> map1 = new HashMap<UUID, UUID();
    // You can also get a synchronised hashmap/hashset/hashtable (so many implementations!)
    Map<UUID, UUID> map2 = Collections.synchronizedMap(new HashMap<UUID, UUID>());
     
  9. Weird that it isn't working, when I run my code It works

    Sidenote: I made the stringbuilder from the 3rd argument so you should probably fix that.
     
  10. And you need to lower the for loop starting point from 2 to 0. You need to make a task that will delete all the previous players' messages and you need to add at line 25, right after the comment,
    Code (Text):
    lastmsg.put(p.getUniqueId(), target.getUniqueId());
    . The overall code should be:

    Code (Text):


        HashMap<UUID, UUID> lastmsg = Collections.synchronizedMap(new HashMap<UUID, UUID>());
        @Override
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
       
            if (s.equalsIgnoreCase("msg")) {
                Player p = (Player) commandSender;
                if (args.length == 0 || args.length == 1) {
                    if (!p.hasPermission("alarhaessentials.msg")) {
                        p.sendMessage(noperm);
                    } else {
                       message(p, "&cUsage: /msg <player> <message>");
                    }
                }
                if (args.length >= 2) {
                    if (!p.hasPermission("alarhaessentials.msg")) {
                        p.sendMessage(noperm);
                    } else {
                        if (Bukkit.getPlayer(args[0]) == null) {
                            message(p, "&cThe player specified is not online.");
                        } else {
                            Player target = Bukkit.getPlayer(args[0]);
                       
                            //Here is the Hashmap Part
                            lastmsg.put(target.getUniqueId(), p.getUniqueId());
                           // The added part
                           lastmsg.put(p.getUniqueId(), target.getUniqueId());
                       
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < args.length; i++) {
                                sb.append(args[i] + " ");
                            }
                            String msg = sb.toString();
                            message(p, "&7(To &c" + target.getName() + "&7) " + msg);
                            message(target, "&7(From &c" + p.getName() + "&7) " + msg);
                        }
                    }
                }
           
            }
            if (s.equalsIgnoreCase("r")) {
                Player p = (Player) commandSender;
                if (! lastmsg.containsKey(p.getUniqueId())) {
                    message(p, "&cNobody messaged you!");
                    return true;
                }
                Player target = Bukkit.getPlayer(lastmsg.get(p.getUniqueId()));
                if (target == null) {
                    message(p, "&cThe last player you messaged is offline.");
                    lastmsg.remove(target);
                    return true;
                }
           
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < args.length; i++) {
                    sb.append(args[i] + " ");
                }
                String msg = sb.toString();
                message(p, "&7(To &c" + target.getName() + "&7) " + msg);
                message(target, "&7(From &c" + p.getName() + "&7) " + msg);
           
                lastmsg.put(target.getUniqueId(), p.getUniqueId());

           
            }
            // Return true if you don't want to send them the usage
            return true;
        }
     
    EDITED
     
    #10 ramidzk, Apr 17, 2017
    Last edited: Apr 17, 2017
  11. Well you posted that seconds before this one :/
     
  12. Tried @ramidzk method, did not work.
     
  13. You are definitely doing something wrong because when I'm testing the codes it works
     
  14. Hello ! this usage is simple:
    Code (Text):

    // MAIN
    public Map<Player, Player> msg = new HashMap<>();
    // ON COMMAND
    onCommand(Message){
    msg.put((Player)sender, target);
    }
     
    Then, on the /R command
    Code (Text):

    if(plugin.msg.containsKey((Player)sender){
    if(plugin.msg.get((Player) sender ) != null){
       //send message
       }
    }

     
    Here you go ;)
     
  15. That's just begging for memory leaks.
     
  16. Remove the player when they leave the server
     
    • Agree Agree x 1
  17. Yea using UUID's is a good idea- if you only want /r to work for player-to-player. Storing names of the sender would work better because, first, you can have a conversation with the console, and second, the names aren't really subject to change when the player is on the server.
     
  18. And if the relogs you won't be able to reply to them.
     
  19. (FeelsBadMan) Put a timer on it, idk do something to remove him