Watch Plugin

Discussion in 'Plugin Development' started by Acar5999, Mar 10, 2014.

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

    Acar5999

    Hello​
    I'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
    1. package me.acer5999.plugins.watchplugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin{
    10.  
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    14. Player player = <player>;
    15. if(cmd.getName().equalsIgnoreCase("watch")){
    16. player.sendMessage(ChatColor.AQUA+"Usage: /watch <player> <color>");
    17. }
    18.  
    19.  
    20.  

     
  2. Offline

    simolus3

    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
    1. HashMap<Player, List<Player>> chat = new HashMap<Player, List<Player>>();
    2.  
    3. @Override
    4. public boolean onCommand...


    Now, the commands:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (sender instanceof Player) {
    3. //Player
    4. if (args.length != 1) {
    5. sender.sendMessage("Please enter a player");
    6. }
    7. else {
    8. Player p = (Player)sender; //The watcher
    9. Player w = Bukkit.getPlayer(args[0]); //The player which messages should be highlighted for the watcher
    10. if (w == null) {
    11. p.sendMessage("That player isn't online");
    12. }
    13. if (chat.get(p) == null) {
    14. chat.put(p, new ArrayList<Player>());
    15. }
    16. chat.get(p).add(w);
    17. w.sendMessage("You're beeing watched by " + p.getName());
    18. p.sendMessage("You're watching " + w.getName());
    19. }
    20. }
    21. else {
    22. //Console
    23. sender.sendMessage("Only players can do that.");
    24. }
    25. return true;
    26. }

    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
    1. public void onEnable() {
    2. Bukkit.getPluginManager().registerEvents(this, this);
    3. }

    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
    1. @EventHandler(priority = EventPriority.HIGHEST) //Other plugins might have change the message, so it's important that we are the last one
    2. public void chatMessage(AsyncPlayerChatEvent e) {
    3. e.setCancelled(true);
    4. for (Player p : Bukkit.getOnlinePlayers()) {
    5. if (chat.get(p) != null) {
    6. if (chat.get(p).contains(e.getPlayer())) {
    7. //Highlight
    8. p.sendMessage(String.format(ChatColor.RED + e.getFormat(), e.getPlayer().getDisplayName(), e.getMessage()));
    9. continue;
    10. }
    11. }
    12. //Don't highlight
    13. p.sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), e.getMessage()));
    14. }
    15. System.out.println(String.format(e.getFormat(), e.getPlayer().getDisplayName(), e.getMessage()));
    16. }
     
  3. Offline

    JasonDL13

    simolus3 You should never store the Player object. Store the username instead.
     
  4. Offline

    61352151511

    For good practice you shouldn't store the username either since they'll be changable soon, you should store the UUID
     
  5. Offline

    JasonDL13

    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.
     
Thread Status:
Not open for further replies.

Share This Page