Advertisement
Jnk1296

IP-Check Scan Command

Feb 2nd, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package net.risenphoenix.jnk.ipcheck.commands;
  2.  
  3. import java.util.ArrayList;
  4. import net.risenphoenix.jnk.ipcheck.IPcheck;
  5. import net.risenphoenix.jnk.ipcheck.Objects.IPObject;
  6. import net.risenphoenix.jnk.ipcheck.Objects.UserObject;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.permissions.Permission;
  12.  
  13. public class CmdScan extends IpcCommand {
  14.    
  15.     @Override
  16.     public void execute(CommandSender sender, String commandLabel, String[] args) {
  17.         // Fetch online players and create a place to hold detected players
  18.         if (sender.hasPermission("ipcheck.scan") || sender.isOp()) {
  19.             Player[] online = Bukkit.getOnlinePlayers();
  20.             ArrayList<Player> detected = new ArrayList<Player>();
  21.  
  22.             // Iterate through the players
  23.             for (int i = 0; i < online.length; i++) {
  24.                 // Get the User-Object for this player
  25.                 UserObject user = IPcheck.Instance.Database.getIPs(online[i].getName());
  26.                 ArrayList<String> unique_names = new ArrayList<String>();
  27.  
  28.                 // If, for some unknown reason, no IPs were found for the user,
  29.                 // skip them and move on.
  30.                 if (user.getNumberOfIPs() == 0) {
  31.                     continue;
  32.                 }
  33.  
  34.                 // Fetch IP-Objects
  35.                 ArrayList<IPObject> ipos = new ArrayList<IPObject>();
  36.  
  37.                 for (String s:user.getIPs()) {
  38.                     // Get alt accounts for the IP Address
  39.                     ipos.add(IPcheck.Instance.Database.getAlts(s));
  40.                 }
  41.  
  42.                 // Get unique accounts from the IP-Objects
  43.                 for (IPObject ipo:ipos) {
  44.                     if (ipo.getNumberOfUsers() == 1) {
  45.                         if (ipo.getUsers().contains(online[i].getName().toLowerCase())) {
  46.                             if (!IPcheck.Instance.Configuration.shouldShowAllIPs) continue;
  47.                         }
  48.                     }
  49.  
  50.                     // Add each unique account found to arraylist
  51.                     for (String account:ipo.getUsers()) {
  52.                         if (!account.equalsIgnoreCase(online[i].getName())) {
  53.                             if (!unique_names.contains(account.toLowerCase())) {
  54.                                 unique_names.add(account.toLowerCase());
  55.                             }
  56.                         }
  57.                     }
  58.                 }
  59.  
  60.                 // If multiple accounts were found for the user, add them to
  61.                 // the detection queue.
  62.                 if (unique_names.size() > 0) detected.add(online[i]);
  63.             }
  64.  
  65.             // Display output to sender
  66.             if (detected.size() > 0) {
  67.                 Player[] convert = new Player[detected.size()];
  68.                 detected.toArray(convert);
  69.  
  70.                 sender.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------------");
  71.                 sender.sendMessage(ChatColor.GOLD + IPcheck.PLUG_NAME + ChatColor.RED + "Player Scan Results");
  72.                 sender.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------------");
  73.                 sender.sendMessage(ChatColor.GOLD + "The following players were found to have multiple accounts:");
  74.  
  75.                 // Build the list of players
  76.                 StringBuilder sb = new StringBuilder();
  77.  
  78.                 // List Formatting
  79.                 for (int i = 0; i < convert.length; i++) {
  80.                     if (convert.length == 1) {
  81.                         sb.append(convert[0].getName());
  82.                         break;
  83.                     } else if (convert.length == 2) {
  84.                         sb.append(convert[0].getName());
  85.                         sb.append(" and ");
  86.                         sb.append(convert[1].getName());
  87.                         break;
  88.                     } else if (convert.length > 2) {
  89.                         sb.append(convert[i].getName());
  90.  
  91.                         if (i == (convert.length - 2)) {
  92.                             sb.append(" and ");
  93.                         } else if (i == (convert.length - 1)) {
  94.                             sb.append(".");
  95.                         } else {
  96.                             sb.append(", ");
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.                 // Display detected users
  102.                 sender.sendMessage(ChatColor.YELLOW + sb.toString());
  103.                 sender.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------------");
  104.             } else {
  105.                 sender.sendMessage(ChatColor.GOLD + IPcheck.PLUG_NAME + ChatColor.YELLOW +
  106.                         IPcheck.Instance.Translation.getTranslation("SCAN_CLEAN"));
  107.             }
  108.         } else {
  109.             sender.sendMessage(ChatColor.GOLD + IPcheck.PLUG_NAME + ChatColor.YELLOW +
  110.                         IPcheck.Instance.Translation.getTranslation("NO_PERM_ERR"));
  111.         }
  112.     };
  113.  
  114.     @Override
  115.     public String getHelp(){
  116.         return "Scans all players currently logged in to check for any who " +
  117.                 "may possess multiple accounts.";
  118.     };
  119.  
  120.     @Override
  121.     public String getSyntax(){
  122.         return "scan";
  123.     };
  124.  
  125.     @Override
  126.     public Permission[] getPermissions(){
  127.         Permission perms[] = {
  128.             new Permission("ipcheck.scan")
  129.         };
  130.  
  131.         return perms;
  132.     };
  133.  
  134.     @Override
  135.     public String getName(){
  136.         return "Scan";
  137.     };
  138.    
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement