Guest User

DeepNoCheatMain.java

a guest
Jun 13th, 2016
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.99 KB | None | 0 0
  1. package com.ObnoxiousHacker.DeepNoCheat;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.nio.charset.Charset;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.Comparator;
  12. import java.util.HashMap;
  13. import java.util.LinkedHashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Timer;
  17.  
  18. import org.bukkit.ChatColor;
  19. import org.bukkit.command.Command;
  20. import org.bukkit.command.CommandSender;
  21. import org.bukkit.entity.Entity;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.plugin.java.JavaPlugin;
  24. import org.bukkit.potion.PotionEffect;
  25. import org.bukkit.potion.PotionEffectType;
  26. import org.bukkit.util.Vector;
  27.  
  28. import com.comphenix.protocol.PacketType;
  29. import com.comphenix.protocol.ProtocolLibrary;
  30. import com.comphenix.protocol.ProtocolManager;
  31. import com.comphenix.protocol.events.ListenerPriority;
  32. import com.comphenix.protocol.events.PacketAdapter;
  33. import com.comphenix.protocol.events.PacketEvent;
  34. import com.google.gson.Gson;
  35. import com.google.gson.JsonArray;
  36. import com.google.gson.JsonElement;
  37. import com.google.gson.JsonParser;
  38. import com.google.gson.reflect.TypeToken;
  39.  
  40. public class DeepNoCheatMain extends JavaPlugin {
  41.     ProtocolManager protocolManager;
  42.  
  43.     public Timer timer;
  44.     private ScannerTask scannerTask;
  45.     public LinkedHashMap<String, Pair<Long, ArrayList<String>>> data;
  46.     public HashMap<String, String> UUIDs;
  47.     public Map<String, Cheater> cheaters;
  48.  
  49.     public boolean Analyzing = true;
  50.  
  51.     public ArrayList<String> DedicatedList;
  52.     public LinkedHashMap<String, Pair<Long, ArrayList<String>>> DedicatedData;
  53.    
  54.     public long TotalRequests = 0;
  55.     public long TotalTime = 0;
  56.    
  57.     public Entity getEntityFromPacket(PacketEvent event) {
  58.         return event.getPacket()
  59.                 .getEntityModifier(event.getPlayer().getWorld()).read(0);
  60.     }
  61.  
  62.     @Override
  63.     public void onEnable() {
  64.         BuildConfig();
  65.         data = new LinkedHashMap<String, Pair<Long, ArrayList<String>>>();
  66.         DedicatedData = new LinkedHashMap<String, Pair<Long, ArrayList<String>>>();
  67.         DedicatedList = new ArrayList<String>();
  68.         UUIDs = new HashMap<String, String>();
  69.         cheaters = new HashMap<String, Cheater>();
  70.        
  71.         LoadCheaters();
  72.  
  73.         protocolManager = ProtocolLibrary.getProtocolManager();
  74.         protocolManager.addPacketListener(new PacketAdapter(this,
  75.                 ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY) {
  76.             @Override
  77.             public void onPacketReceiving(PacketEvent event) {
  78.                 if (event.getPacketType() == PacketType.Play.Client.USE_ENTITY) {
  79.                     if (Analyzing) {
  80.                         Player p = event.getPlayer();
  81.                         Entity target = getEntityFromPacket(event);
  82.                         double distance = target.getLocation().distance(
  83.                                 p.getLocation());
  84.                         Vector a = target.getLocation().toVector()
  85.                                 .subtract(p.getLocation().toVector()).normalize();
  86.                         Vector b = p.getLocation().getDirection();
  87.                         double angle = Math.acos(a.dot(b));
  88.                         angle = Math.toDegrees(angle);
  89.                         String uuid = p.getUniqueId().toString();
  90.  
  91.                         // Add to the database.
  92.                         if (data.containsKey(uuid)) {
  93.                             // Feature vector is hidden
  94.                         } else {
  95.                             Pair<Long, ArrayList<String>> nP = new Pair<Long, ArrayList<String>>(
  96.                                     System.currentTimeMillis(),
  97.                                     new ArrayList<String>());
  98.                             data.put(uuid, nP);
  99.                             UUIDs.put(uuid, p.getName());
  100.                         }  
  101.                     }
  102.                 }
  103.             }
  104.         });
  105.  
  106.         timer = new Timer();
  107.         scannerTask = new ScannerTask(this);
  108.         timer.scheduleAtFixedRate(scannerTask, DELAY, DELAY);
  109.     }
  110.  
  111.     @Override
  112.     public void onDisable() {
  113.  
  114.     }
  115.  
  116.     @Override
  117.     public boolean onCommand(CommandSender sender, Command cmd, String label,
  118.             String[] args) {
  119.         if (cmd.getName().equalsIgnoreCase("dnc")) { // If the player typed
  120.                                                         // /basic then do the
  121.                                                         // following...
  122.             if (args[0].equals("regen")) {
  123.                 ((Player) sender).addPotionEffect(new PotionEffect(
  124.                         PotionEffectType.REGENERATION, 100000, 10));
  125.             }
  126.             if (args[0].equals("quota") || args[0].equals("credit") || args[0].equals("credits") || args[0].equals("q")) {
  127.                 Runnable request = new SimpleAPIThread(API + "getcredit?key=" + KEY, (Player) sender, "[" + PLUGIN_NAME + "] Credits Left: ");
  128.                 new Thread(request).start();
  129.             }
  130.             if (args[0].equals("usage")) {
  131.                 String message = "";
  132.                 if (TotalTime == 0) {
  133.                     message = "[" + PLUGIN_NAME + "] You haven't used the plugin yet.";
  134.                 }
  135.                 else {
  136.                     message = "[" + PLUGIN_NAME + "]" + ChatColor.YELLOW + " Requests per Hour: " + ChatColor.WHITE + String.format("%.2f", TotalRequests / TotalTime * 60 * 60);
  137.                 }
  138.                 sender.sendMessage(message);
  139.             }
  140.             if (args[0].equals("list") || args[0].equals("l")) {
  141.                 List<Cheater> cheats = new ArrayList<Cheater>(cheaters.values());
  142.                 Collections.sort(cheats, new Comparator<Cheater>() {
  143.                     public int compare(Cheater o1, Cheater o2) {
  144.                         double a1 = average(o1.offences);
  145.                         double a2 = average(o2.offences);
  146.                        
  147.                         if (a1 > a2) return 1;
  148.                         if (a1 < a2) return -1;
  149.                         return 0;
  150.                     }
  151.                 });
  152.                 String message = "";
  153.                 int topX = 10;
  154.                 if (args.length > 1) {
  155.                     try {
  156.                         topX = Integer.parseInt(args[1]);
  157.                     }
  158.                     catch (NumberFormatException e) {
  159.                         (sender).sendMessage("[" + PLUGIN_NAME + "] Usage, /dnc list <Optional: Top n results>");
  160.                     }
  161.                 }
  162.                 if (topX > cheats.size()) {
  163.                     topX = cheats.size();
  164.                 }
  165.                 message = "Showing Top " + Integer.toString(topX) + " results: \n";
  166.                 StringBuilder ch = new StringBuilder();
  167.                 for (Cheater cc : cheats) {
  168.                     ch.append(ChatColor.YELLOW + cc.name + ": " + ChatColor.WHITE + String.format("%.2f", average(cc.offences)) + "%\n");
  169.                 }
  170.                
  171.                 (sender).sendMessage(message + ch.toString());
  172.             }
  173.             if (args[0].equals("ban")) {
  174.                 List<Cheater> cheats = new ArrayList<Cheater>(cheaters.values());
  175.                 Collections.sort(cheats, new Comparator<Cheater>() {
  176.                     public int compare(Cheater o1, Cheater o2) {
  177.                         double a1 = average(o1.offences);
  178.                         double a2 = average(o2.offences);
  179.                        
  180.                         if (a1 > a2) return 1;
  181.                         if (a1 < a2) return -1;
  182.                         return 0;
  183.                     }
  184.                 });
  185.                 String message = "";
  186.                 int topX = 10;
  187.                 if (args.length > 1) {
  188.                     try {
  189.                         topX = Integer.parseInt(args[1]);
  190.                     }
  191.                     catch (NumberFormatException e) {
  192.                         if (args[1].equals("*")) {
  193.                             topX = cheaters.size();
  194.                         }
  195.                         else {
  196.                             (sender).sendMessage("[" + PLUGIN_NAME + "] Usage, /dnc ban <Top n or *>");
  197.                             return false;
  198.                         }
  199.                     }
  200.                    
  201.                     if (topX > cheats.size()) {
  202.                         topX = cheats.size();
  203.                     }
  204.                     message = "Banned " + ChatColor.RED + Integer.toString(topX) + ChatColor.WHITE + " players.";
  205.  
  206.                     for (Cheater cc : cheats) {
  207.                         this.getServer().dispatchCommand(sender, BAN_COMMAND.replace("<Player>", cc.name));
  208.                         cheaters.remove(cc.name);
  209.                     }
  210.                     SaveCheaters();
  211.                     (sender).sendMessage(message);
  212.                 }
  213.                 else {
  214.                     (sender).sendMessage("[" + PLUGIN_NAME + "] Usage, /dnc ban <Top n or *>");
  215.                 }
  216.                
  217.             }
  218.             if (args[0].equals("stop")) {
  219.                 if (Analyzing == true) {
  220.                     Analyzing = false;
  221.                     (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis has been stopped.");
  222.                 }
  223.                 else {
  224.                     (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis is already stopped.");
  225.                 }
  226.             }
  227.             if (args[0].equals("start")) {
  228.                 if (Analyzing == false) {
  229.                     Analyzing = true;
  230.                     (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis has been started.");
  231.                 }
  232.                 else {
  233.                     (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis is already running.");
  234.                 }
  235.             }
  236.             if (args[0].equals("a")) {
  237.                 if (args.length < 2) {
  238.                     (sender).sendMessage("Usage /dnc a <PlayerName>");
  239.                     return true;
  240.                 }
  241.                 String playerName = args[1];
  242.                 Player playerTo = this.getServer().getPlayer(playerName);
  243.                 if (playerTo.isOnline() && playerTo != null) {
  244.                     ArrayList<String> packets = data.get(playerTo.getUniqueId().toString()).second;
  245.                     if (packets.size() >= NUMBER_PER_DEDICATED) {
  246.                         (sender).sendMessage("[" + PLUGIN_NAME + "] Analyzing " + playerName);
  247.                         Runnable request = new ScannerThread(this, sender, packets, NUMBER_PER_DEDICATED, playerTo.getName());
  248.                         new Thread(request).start();
  249.                     }
  250.                     else {
  251.                         (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis needs at least " + Integer.toString(NUMBER_PER_DEDICATED) + " samples, player has " + Integer.toString(packets.size()) + " samples right now.");
  252.                     }
  253.                 }
  254.                 else {
  255.                     (sender).sendMessage("[" + PLUGIN_NAME + "] Invalid player name: " + playerName);
  256.                 }
  257.             }
  258.             return true;
  259.         }
  260.         return false;
  261.     }
  262.  
  263.     public String API;
  264.     public String KEY;
  265.     public long DELAY = 2000;
  266.     public int PACKETS_PER_REQUEST = 20;
  267.     public int NUMBER_SECOND = 3;
  268.     public double THRESHOLD_FIRST = 95;
  269.     public float THRESHOLD_SECOND = 95;
  270.     public int NUMBER_PER_DEDICATED = 1 * PACKETS_PER_REQUEST;
  271.     public String PLUGIN_NAME = "DeepNoCheat";
  272.     public String JSON_NAME = "cheaters.json";
  273.     public boolean AUTO_KICK = false;
  274.     public boolean AUTO_BAN = false;
  275.     public String BAN_COMMAND = "/ban <Player>";
  276.     public String KICK_MESSAGE = "You were kicked for cheating.";
  277.  
  278.     public void BuildConfig() {
  279.         this.saveDefaultConfig();
  280.         API = "http://" + this.getConfig().getString("server") + ":"
  281.                 + this.getConfig().getString("port") + "/";
  282.         KEY = this.getConfig().getString("key");
  283.         THRESHOLD_FIRST = this.getConfig().getDouble("threshold");
  284.         DELAY = this.getConfig().getLong("delay");
  285.         AUTO_KICK = this.getConfig().getBoolean("autokick");
  286.         AUTO_BAN = this.getConfig().getBoolean("autoban");
  287.         BAN_COMMAND = this.getConfig().getString("bancommand");
  288.         BAN_COMMAND = BAN_COMMAND.replace("/", "");
  289.         JSON_NAME = this.getConfig().getString("dbname");
  290.         KICK_MESSAGE = this.getConfig().getString("kickmessage");
  291.     }
  292.    
  293.     public void SaveCheaters() {
  294.         Gson gson = new Gson();
  295.         String json = gson.toJson(cheaters);
  296.         File dataFolder = getDataFolder();
  297.         if(!dataFolder.exists())
  298.         {
  299.             dataFolder.mkdir();
  300.         }
  301.         File saveTo = new File(getDataFolder(), JSON_NAME);
  302.         if(!saveTo.exists())
  303.         {
  304.             try {
  305.                 saveTo.createNewFile();
  306.             } catch (IOException e) {
  307.                 e.printStackTrace();
  308.             }
  309.         }
  310.         try {
  311.             FileWriter fw = new FileWriter(saveTo, false);
  312.             fw.write(json);
  313.             fw.flush();
  314.             fw.close();
  315.         } catch (IOException e) {
  316.             // TODO Auto-generated catch block
  317.             e.printStackTrace();
  318.         }
  319.     }
  320.    
  321.     public void LoadCheaters() {
  322.         File dataFolder = getDataFolder();
  323.         if(dataFolder.exists())
  324.         {
  325.             File saveTo = new File(getDataFolder(), JSON_NAME);
  326.             if(saveTo.exists())
  327.             {
  328.                 try {
  329.                     String json = readFile(saveTo.getAbsolutePath(), Charset.defaultCharset());
  330.                     Gson gson = new Gson();
  331.                     cheaters = gson.fromJson(json, new TypeToken<Map<String, Cheater>>(){}.getType());
  332.                 } catch (Exception e) {
  333.                     // TODO Auto-generated catch block
  334.                     e.printStackTrace();
  335.                 }
  336.             }
  337.         }
  338.     }
  339.    
  340.     static String readFile(String path, Charset encoding)
  341.               throws IOException
  342.             {
  343.               byte[] encoded = Files.readAllBytes(Paths.get(path));
  344.               return new String(encoded, encoding);
  345.             }
  346.     // Better use a `List`. It is more generic and it also receives an `ArrayList`.
  347.     public static double average(List<Double> list) {
  348.         // 'average' is undefined if there are no elements in the list.
  349.         if (list == null || list.isEmpty())
  350.             return 0.0;
  351.         // Calculate the summation of the elements in the list
  352.         double sum = 0;
  353.         int n = list.size();
  354.         // Iterating manually is faster than using an enhanced for loop.
  355.         for (int i = 0; i < n; i++)
  356.             sum += list.get(i);
  357.         // We don't want to perform an integer division, so the cast is mandatory.
  358.         return ((double) sum) / n;
  359.     }
  360. }
Add Comment
Please, Sign In to add comment