Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ObnoxiousHacker.DeepNoCheat;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Timer;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import org.bukkit.util.Vector;
- import com.comphenix.protocol.PacketType;
- import com.comphenix.protocol.ProtocolLibrary;
- import com.comphenix.protocol.ProtocolManager;
- import com.comphenix.protocol.events.ListenerPriority;
- import com.comphenix.protocol.events.PacketAdapter;
- import com.comphenix.protocol.events.PacketEvent;
- import com.google.gson.Gson;
- import com.google.gson.JsonArray;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonParser;
- import com.google.gson.reflect.TypeToken;
- public class DeepNoCheatMain extends JavaPlugin {
- ProtocolManager protocolManager;
- public Timer timer;
- private ScannerTask scannerTask;
- public LinkedHashMap<String, Pair<Long, ArrayList<String>>> data;
- public HashMap<String, String> UUIDs;
- public Map<String, Cheater> cheaters;
- public boolean Analyzing = true;
- public ArrayList<String> DedicatedList;
- public LinkedHashMap<String, Pair<Long, ArrayList<String>>> DedicatedData;
- public long TotalRequests = 0;
- public long TotalTime = 0;
- public Entity getEntityFromPacket(PacketEvent event) {
- return event.getPacket()
- .getEntityModifier(event.getPlayer().getWorld()).read(0);
- }
- @Override
- public void onEnable() {
- BuildConfig();
- data = new LinkedHashMap<String, Pair<Long, ArrayList<String>>>();
- DedicatedData = new LinkedHashMap<String, Pair<Long, ArrayList<String>>>();
- DedicatedList = new ArrayList<String>();
- UUIDs = new HashMap<String, String>();
- cheaters = new HashMap<String, Cheater>();
- LoadCheaters();
- protocolManager = ProtocolLibrary.getProtocolManager();
- protocolManager.addPacketListener(new PacketAdapter(this,
- ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY) {
- @Override
- public void onPacketReceiving(PacketEvent event) {
- if (event.getPacketType() == PacketType.Play.Client.USE_ENTITY) {
- if (Analyzing) {
- Player p = event.getPlayer();
- Entity target = getEntityFromPacket(event);
- double distance = target.getLocation().distance(
- p.getLocation());
- Vector a = target.getLocation().toVector()
- .subtract(p.getLocation().toVector()).normalize();
- Vector b = p.getLocation().getDirection();
- double angle = Math.acos(a.dot(b));
- angle = Math.toDegrees(angle);
- String uuid = p.getUniqueId().toString();
- // Add to the database.
- if (data.containsKey(uuid)) {
- // Feature vector is hidden
- } else {
- Pair<Long, ArrayList<String>> nP = new Pair<Long, ArrayList<String>>(
- System.currentTimeMillis(),
- new ArrayList<String>());
- data.put(uuid, nP);
- UUIDs.put(uuid, p.getName());
- }
- }
- }
- }
- });
- timer = new Timer();
- scannerTask = new ScannerTask(this);
- timer.scheduleAtFixedRate(scannerTask, DELAY, DELAY);
- }
- @Override
- public void onDisable() {
- }
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label,
- String[] args) {
- if (cmd.getName().equalsIgnoreCase("dnc")) { // If the player typed
- // /basic then do the
- // following...
- if (args[0].equals("regen")) {
- ((Player) sender).addPotionEffect(new PotionEffect(
- PotionEffectType.REGENERATION, 100000, 10));
- }
- if (args[0].equals("quota") || args[0].equals("credit") || args[0].equals("credits") || args[0].equals("q")) {
- Runnable request = new SimpleAPIThread(API + "getcredit?key=" + KEY, (Player) sender, "[" + PLUGIN_NAME + "] Credits Left: ");
- new Thread(request).start();
- }
- if (args[0].equals("usage")) {
- String message = "";
- if (TotalTime == 0) {
- message = "[" + PLUGIN_NAME + "] You haven't used the plugin yet.";
- }
- else {
- message = "[" + PLUGIN_NAME + "]" + ChatColor.YELLOW + " Requests per Hour: " + ChatColor.WHITE + String.format("%.2f", TotalRequests / TotalTime * 60 * 60);
- }
- sender.sendMessage(message);
- }
- if (args[0].equals("list") || args[0].equals("l")) {
- List<Cheater> cheats = new ArrayList<Cheater>(cheaters.values());
- Collections.sort(cheats, new Comparator<Cheater>() {
- public int compare(Cheater o1, Cheater o2) {
- double a1 = average(o1.offences);
- double a2 = average(o2.offences);
- if (a1 > a2) return 1;
- if (a1 < a2) return -1;
- return 0;
- }
- });
- String message = "";
- int topX = 10;
- if (args.length > 1) {
- try {
- topX = Integer.parseInt(args[1]);
- }
- catch (NumberFormatException e) {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Usage, /dnc list <Optional: Top n results>");
- }
- }
- if (topX > cheats.size()) {
- topX = cheats.size();
- }
- message = "Showing Top " + Integer.toString(topX) + " results: \n";
- StringBuilder ch = new StringBuilder();
- for (Cheater cc : cheats) {
- ch.append(ChatColor.YELLOW + cc.name + ": " + ChatColor.WHITE + String.format("%.2f", average(cc.offences)) + "%\n");
- }
- (sender).sendMessage(message + ch.toString());
- }
- if (args[0].equals("ban")) {
- List<Cheater> cheats = new ArrayList<Cheater>(cheaters.values());
- Collections.sort(cheats, new Comparator<Cheater>() {
- public int compare(Cheater o1, Cheater o2) {
- double a1 = average(o1.offences);
- double a2 = average(o2.offences);
- if (a1 > a2) return 1;
- if (a1 < a2) return -1;
- return 0;
- }
- });
- String message = "";
- int topX = 10;
- if (args.length > 1) {
- try {
- topX = Integer.parseInt(args[1]);
- }
- catch (NumberFormatException e) {
- if (args[1].equals("*")) {
- topX = cheaters.size();
- }
- else {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Usage, /dnc ban <Top n or *>");
- return false;
- }
- }
- if (topX > cheats.size()) {
- topX = cheats.size();
- }
- message = "Banned " + ChatColor.RED + Integer.toString(topX) + ChatColor.WHITE + " players.";
- for (Cheater cc : cheats) {
- this.getServer().dispatchCommand(sender, BAN_COMMAND.replace("<Player>", cc.name));
- cheaters.remove(cc.name);
- }
- SaveCheaters();
- (sender).sendMessage(message);
- }
- else {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Usage, /dnc ban <Top n or *>");
- }
- }
- if (args[0].equals("stop")) {
- if (Analyzing == true) {
- Analyzing = false;
- (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis has been stopped.");
- }
- else {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis is already stopped.");
- }
- }
- if (args[0].equals("start")) {
- if (Analyzing == false) {
- Analyzing = true;
- (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis has been started.");
- }
- else {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis is already running.");
- }
- }
- if (args[0].equals("a")) {
- if (args.length < 2) {
- (sender).sendMessage("Usage /dnc a <PlayerName>");
- return true;
- }
- String playerName = args[1];
- Player playerTo = this.getServer().getPlayer(playerName);
- if (playerTo.isOnline() && playerTo != null) {
- ArrayList<String> packets = data.get(playerTo.getUniqueId().toString()).second;
- if (packets.size() >= NUMBER_PER_DEDICATED) {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Analyzing " + playerName);
- Runnable request = new ScannerThread(this, sender, packets, NUMBER_PER_DEDICATED, playerTo.getName());
- new Thread(request).start();
- }
- else {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Analysis needs at least " + Integer.toString(NUMBER_PER_DEDICATED) + " samples, player has " + Integer.toString(packets.size()) + " samples right now.");
- }
- }
- else {
- (sender).sendMessage("[" + PLUGIN_NAME + "] Invalid player name: " + playerName);
- }
- }
- return true;
- }
- return false;
- }
- public String API;
- public String KEY;
- public long DELAY = 2000;
- public int PACKETS_PER_REQUEST = 20;
- public int NUMBER_SECOND = 3;
- public double THRESHOLD_FIRST = 95;
- public float THRESHOLD_SECOND = 95;
- public int NUMBER_PER_DEDICATED = 1 * PACKETS_PER_REQUEST;
- public String PLUGIN_NAME = "DeepNoCheat";
- public String JSON_NAME = "cheaters.json";
- public boolean AUTO_KICK = false;
- public boolean AUTO_BAN = false;
- public String BAN_COMMAND = "/ban <Player>";
- public String KICK_MESSAGE = "You were kicked for cheating.";
- public void BuildConfig() {
- this.saveDefaultConfig();
- API = "http://" + this.getConfig().getString("server") + ":"
- + this.getConfig().getString("port") + "/";
- KEY = this.getConfig().getString("key");
- THRESHOLD_FIRST = this.getConfig().getDouble("threshold");
- DELAY = this.getConfig().getLong("delay");
- AUTO_KICK = this.getConfig().getBoolean("autokick");
- AUTO_BAN = this.getConfig().getBoolean("autoban");
- BAN_COMMAND = this.getConfig().getString("bancommand");
- BAN_COMMAND = BAN_COMMAND.replace("/", "");
- JSON_NAME = this.getConfig().getString("dbname");
- KICK_MESSAGE = this.getConfig().getString("kickmessage");
- }
- public void SaveCheaters() {
- Gson gson = new Gson();
- String json = gson.toJson(cheaters);
- File dataFolder = getDataFolder();
- if(!dataFolder.exists())
- {
- dataFolder.mkdir();
- }
- File saveTo = new File(getDataFolder(), JSON_NAME);
- if(!saveTo.exists())
- {
- try {
- saveTo.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try {
- FileWriter fw = new FileWriter(saveTo, false);
- fw.write(json);
- fw.flush();
- fw.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void LoadCheaters() {
- File dataFolder = getDataFolder();
- if(dataFolder.exists())
- {
- File saveTo = new File(getDataFolder(), JSON_NAME);
- if(saveTo.exists())
- {
- try {
- String json = readFile(saveTo.getAbsolutePath(), Charset.defaultCharset());
- Gson gson = new Gson();
- cheaters = gson.fromJson(json, new TypeToken<Map<String, Cheater>>(){}.getType());
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- static String readFile(String path, Charset encoding)
- throws IOException
- {
- byte[] encoded = Files.readAllBytes(Paths.get(path));
- return new String(encoded, encoding);
- }
- // Better use a `List`. It is more generic and it also receives an `ArrayList`.
- public static double average(List<Double> list) {
- // 'average' is undefined if there are no elements in the list.
- if (list == null || list.isEmpty())
- return 0.0;
- // Calculate the summation of the elements in the list
- double sum = 0;
- int n = list.size();
- // Iterating manually is faster than using an enhanced for loop.
- for (int i = 0; i < n; i++)
- sum += list.get(i);
- // We don't want to perform an integer division, so the cast is mandatory.
- return ((double) sum) / n;
- }
- }
Add Comment
Please, Sign In to add comment