HelloI'm making a plugin that uses two commands, /watch <playername> <chatcolor> and /unwatch <playername> that pretty much highlights one or multiple people for a specific player in a certain color but I can't figure out how to get the person who gives the command to have the other player highlighted for just them, also how to check if the player being watched says anything and maybe getting a "You are now being watched by <watchingplayername>" sent to the player heres my current code I know its not much but...P.S. don't know what player player = player means since I used a completely useless tutorial THANKS!P.S.S. I would also like to have the console print a message that says <Watcherplayer> is watching <watchee> Code:java package me.acer5999.plugins.watchplugin; import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin{ @Overridepublic boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){Player player = <player>;if(cmd.getName().equalsIgnoreCase("watch")){player.sendMessage(ChatColor.AQUA+"Usage: /watch <player> <color>");}
Step-by-step solution: We want to save which player listens to which player(s), so we'll use a HashMap with a player(the watcher) as key and a list of player(the players who are beeing watched by the key). So: Code:java HashMap<Player, List<Player>> chat = new HashMap<Player, List<Player>>(); @Overridepublic boolean onCommand... Now, the commands: Code:java public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {if (sender instanceof Player) {//Playerif (args.length != 1) {sender.sendMessage("Please enter a player");}else {Player p = (Player)sender; //The watcherPlayer w = Bukkit.getPlayer(args[0]); //The player which messages should be highlighted for the watcherif (w == null) {p.sendMessage("That player isn't online");}if (chat.get(p) == null) {chat.put(p, new ArrayList<Player>());}chat.get(p).add(w);w.sendMessage("You're beeing watched by " + p.getName());p.sendMessage("You're watching " + w.getName());}}else {//Consolesender.sendMessage("Only players can do that.");}return true;} Now players have a command to "subscribe" to others. The only thing missing is the highlighting. To do that, we'll have to set up a listener, so create a method (void) called "onEnable()" and put this inside. Code:java public void onEnable() {Bukkit.getPluginManager().registerEvents(this, this);} Eclipse will give you an error because your plugin isn't yet a listener. Let's do that by adding " implements Listener" after "extends JavaPlugin". Now: the highlighting. We'll do that like this: whenever a player writes something, it get's displayed normally to all the others, but the watchers will see it highlighted. Bukkit doesn't have a single function for this, so let's first cancell the event: Code:java @EventHandler(priority = EventPriority.HIGHEST) //Other plugins might have change the message, so it's important that we are the last onepublic void chatMessage(AsyncPlayerChatEvent e) {e.setCancelled(true);for (Player p : Bukkit.getOnlinePlayers()) {if (chat.get(p) != null) {if (chat.get(p).contains(e.getPlayer())) {//Highlightp.sendMessage(String.format(ChatColor.RED + e.getFormat(), e.getPlayer().getDisplayName(), e.getMessage()));continue;}}//Don't highlightp.sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), e.getMessage()));}System.out.println(String.format(e.getFormat(), e.getPlayer().getDisplayName(), e.getMessage()));}
For good practice you shouldn't store the username either since they'll be changable soon, you should store the UUID
61352151511 That also depends if they can log out. If they can't log out why store UUIDs to make it more complex if you can store player names and they can't change there name? But I agree with you if they can log out or if you don't remove them from a list. Ex: Stats tracker.