Search code examples
javaipbukkit

Gather online players with same IP to a list


I run a bunch of alt/bot accounts on my server and I'm trying to make a command that gets all the (online)players IP addresses and if it matches mine, then it adds them to a player list to print to whoever executed the command. Here's what I have so far.

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  if (label.equalsIgnoreCase("bots")) {
    if (!sender.hasPermission("lbp.bots")) {
      sender.sendMessage(ChatColor.RED + "You cannot run this command.");
      return true;
    }
    if (args.length == 0) {
      //Player p = (Player) sender;
      List < Player > onlineBots = new ArrayList < Player > ();
      for (Player players: Bukkit.getOnlinePlayers()) {
        if (players.getAddress().getAddress().getHostAddress() == "redacted ip address") {
          onlineBots.add(players);
        }
      }
      sender.sendMessage(ChatColor.AQUA + "Luke's Bot List: " + ChatColor.BOLD + "" + onlineBots);
      return true;
    }
  }
  return false;
}

I've plugged in my IP address to "redacted ip address", joined my portforwarded local server so i dont join with host IP(double checked my IP registered correctly with essentials /whois) and the command returns an empty list. No errors in console. [1]: https://i.sstatic.net/ykTAX.png Any ideas?


Solution

  • Got it working using this code:

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      if (label.equalsIgnoreCase("bots")) {
        if (!sender.hasPermission("lbp.bots")) {
          sender.sendMessage(ChatColor.RED + "You cannot run this command.");
          return true;
        }
        if (args.length == 0) {
          List < String > onlineBots = new ArrayList < > ();
          for (Player players: Bukkit.getOnlinePlayers()) {
            if (players.getAddress().getAddress().getHostAddress().equals("insert IP here")) {
              onlineBots.add(players.getName());
            }
          }
          sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Luke's Bot List: " + ChatColor.RESET + "" + ChatColor.AQUA + "" + onlineBots.toString());
          return true;
        }
      }
      return false;
    }
    

    The list was returning players in the CraftPlayer format because of the List<Player>, so I changed the list type to List<String> and used onlineBots.add(players.getName()); and that seems to have done the trick.