Getting all the online players usernames

Discussion in 'Plugin Development' started by BrushPainter, Mar 11, 2014.

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

    BrushPainter

    Hey everyone, I'm working on a social spy plugin. I'm trying to make it so if any online player types "/msg" it will show people with a permission node what they typed. But I can't figure out how to get all the online players usernames.

    Here is my current code:
    Code:java
    1. package me.BrushPainter.SpyChat;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin implements Listener{
    18.  
    19. public void onEnable() {
    20. FileConfiguration config = getConfig();
    21.  
    22. config.addDefault("SpyChat.OP_Only", "true");
    23.  
    24. config.options().copyDefaults(true);
    25. saveConfig();
    26.  
    27. getLogger().info("SpyChat Enabled");
    28.  
    29. this.getServer().getPluginManager().registerEvents(this, this);
    30. }
    31.  
    32. public void onDisable() {
    33.  
    34. getLogger().info("SpyChat Disabled");
    35.  
    36. }
    37.  
    38. ArrayList<String> players = new ArrayList<String>();
    39.  
    40. @EventHandler
    41. public void onPlayerJoin(PlayerJoinEvent e){
    42. Player player = e.getPlayer();
    43. players.add(e.getPlayer().getName());
    44. if(player.hasPermission("onjoin.autospy")) {
    45. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "spychat " + player);
    46. player.sendMessage(ChatColor.GOLD + "SpyChat enabled.");
    47.  
    48. }
    49. }
    50.  
    51. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    52.  
    53. if (sender instanceof Player)
    54. {
    55.  
    56. if (sender.hasPermission("brushpainter.spy"))
    57. {
    58.  
    59. if (cmd.getName().equalsIgnoreCase("spychat"))
    60. {
    61.  
    62. if (!players.contains(e.getPlayer().getName()))
    63.  
    64. players.getMessage().equalsIgnoreCase("/msg");
    65. {
    66.  
    67. }
    68. }
    69. }
    70. }
    71. return false;
    72. }
    73. }

    But on lines 62 and 64 I get errors:
    62: e cannot be resolved
    64: The method getMessage() is undefined for the type ArrayList<String>

    EDIT: I know that the config does not do anything at this point, I just added it for now.
     
  2. Offline

    ColaCraft

    How to get all online usernames:

    Code:java
    1. for(Player online : bukkit.getServer().getOnlinePlayers()){
    2.  
    3. //use online.getName(); to return a name
    4.  
    5. }
     
  3. Offline

    amhokies

    Just note that the 'b' in the Bukkit class name should be capitalized.
     
  4. Offline

    ColaCraft

    Oops. :rolleyes:
     
  5. Offline

    Aqua

  6. Offline

    ColaCraft

    Aqua

    Meh. My bad. Eclipse automatically capitalized it, wasn't paying attention.
     
  7. Offline

    BrushPainter

    ColaCraft amhokies Thanks guys, I still get an error "The method getMessage() is undefined for the type Player" on line 63:

    Code:java
    1. package me.BrushPainter.SpyChat;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin implements Listener{
    18.  
    19. public void onEnable() {
    20. FileConfiguration config = getConfig();
    21.  
    22. config.addDefault("SpyChat.OP_Only", "true");
    23.  
    24. config.options().copyDefaults(true);
    25. saveConfig();
    26.  
    27. getLogger().info("SpyChat Enabled");
    28.  
    29. this.getServer().getPluginManager().registerEvents(this, this);
    30. }
    31.  
    32. public void onDisable() {
    33.  
    34. getLogger().info("SpyChat Disabled");
    35.  
    36. }
    37.  
    38. ArrayList<String> players = new ArrayList<String>();
    39.  
    40. @EventHandler
    41. public void onPlayerJoin(PlayerJoinEvent e){
    42. Player player = e.getPlayer();
    43. players.add(e.getPlayer().getName());
    44. if(player.hasPermission("onjoin.autospy")) {
    45. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "spy " + player + "");
    46. player.sendMessage(ChatColor.GOLD + "Socialspy enabled.");
    47.  
    48. }
    49. }
    50.  
    51. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    52.  
    53. if (sender instanceof Player)
    54. {
    55.  
    56. if (sender.hasPermission("brushpainter.spy"))
    57. {
    58.  
    59. if (cmd.getName().equalsIgnoreCase("spy"))
    60. {
    61. for(Player online : Bukkit.getServer().getOnlinePlayers()){
    62.  
    63. online.getMessage().equalsIgnoreCase("/msg");
    64.  
    65.  
    66. }
    67.  
    68. }
    69. }
    70. }
    71. return false;
    72. }
    73. }
     
  8. Offline

    mrkirby153

    BrushPainter

    What you are doing right now (lines 61-66) does not accomplish anything. You can however, do something like this:
    Code:java
    1. public static ArrayList<String> playersSpying = new ArrayList<String>();
    2.  
    3. @EventHandler
    4. public void commandPreprocess(PlayerCommandPreprocessEvent event) {
    5. String msg = event.getMessage();
    6. String[] split = msg.split(" ");
    7. if (!split[0].startsWith("/"))
    8. return;
    9. String command = split[0];
    10. if (command.equalsIgnoreCase("tell") || command.equalsIgnoreCase("msg")) {
    11. StringBuilder sb = new StringBuilder();
    12. for(int i = 1; i < split.length; i++){
    13. sb.append(split[I]+" ");[/I]
    14. [I] }[/I]
    15. [I] String message = sb.toString().trim();[/I]
    16. [I] for(String playerName : playersSpying){[/I]
    17. [I] Player player = Bukkit.getPlayerExact(playerName);[/I]
    18. [I] player.sendMessage("[Private Msg] "+ event.getPlayer().getName()+": "+message);[/I]
    19. [I] }[/I]
    20. [I] }[/I]
    21. [I] }[/I]
    [/I]
    That basically checks if a command is /msg or /tell etc and then if it is, send the message to all the players who are in the arraylist playersSpying.
     
    BrushPainter likes this.
  9. Offline

    BrushPainter

    mrkirby153 Thank you sooo much man! I truly appreciate it. :D
     
  10. Offline

    mrkirby153

Thread Status:
Not open for further replies.

Share This Page