Sanity commit. Had a nice break.

This commit is contained in:
h31ix committed 2013-02-20 22:03:12 -05:00
1 parent 77645da2b2
commit e7e5c996f3
10 files changed
+178 -178

No files matched your search

+160 -159
View File
@@ -55,210 +55,211 @@ public class Anticheat extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
manager = new AnticheatManager(this, getLogger()); manager = new AnticheatManager(this, getLogger());
eventList.add(new PlayerListener()); eventList.add(new PlayerListener());
eventList.add(new BlockListener()); eventList.add(new BlockListener());
eventList.add(new EntityListener()); eventList.add(new EntityListener());
eventList.add(new VehicleListener()); eventList.add(new VehicleListener());
eventList.add(new InventoryListener()); eventList.add(new InventoryListener());
// Order is important in some cases, don't screw with these unless needed, especially config // Order is important in some cases, don't screw with these unless needed, especially config
setupConfig(); setupConfig();
// Xray must come before events // Xray must come before events
setupXray(); setupXray();
setupEvents(); setupEvents();
setupCommands(); setupCommands();
setupUpdater(); setupUpdater();
setupMetrics(); setupMetrics();
restoreLevels(); restoreLevels();
// Test for compatibility for 1.4.5-R1.0 // Test for compatibility for 1.4.5-R1.0
try { try {
Material.AIR.isSolid(); Material.AIR.isSolid();
} catch (NoSuchMethodError error) { } catch (NoSuchMethodError error) {
getLogger().severe("You are using an outdated version of CraftBukkit that does not contain the API required for AntiCheat to run properly."); getLogger().severe("You are using an outdated version of CraftBukkit that does not contain the API required for AntiCheat to run properly.");
getLogger().severe("Please update to CraftBukkit 1.4.5-R1.0 or higher by visiting dl.bukkit.org and hitting the green download button."); getLogger().severe("Please update to CraftBukkit 1.4.5-R1.0 or higher by visiting dl.bukkit.org and hitting the green download button.");
getLogger().severe("AntiCheat will now be disabled, and will not protect your server until it is updated."); getLogger().severe("AntiCheat will now be disabled, and will not protect your server until it is updated.");
getPluginLoader().disablePlugin(this); getPluginLoader().disablePlugin(this);
return; return;
} }
// End test // End test
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Finished loading."); getLogger().log(Level.INFO, "Finished loading.");
} }
} }
@Override @Override
public void onDisable() { public void onDisable() {
AnticheatManager.close(); AnticheatManager.close();
for (User user : manager.getUserManager().getUsers()) { for (User user : manager.getUserManager().getUsers()) {
config.saveLevel(user.getName(), user.getLevel()); config.saveLevel(user.getName(), user.getLevel());
} }
config.saveLevels(); config.saveLevels();
getServer().getScheduler().cancelTasks(this); getServer().getScheduler().cancelTasks(this);
cleanup(); cleanup();
} }
private boolean save(InputStream in, File file) { private boolean save(InputStream in, File file) {
if (!file.exists()) { if (!file.exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
try { try {
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
while ((len = in.read(buf)) > 0) { while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len); out.write(buf, 0, len);
} }
out.close(); out.close();
in.close(); in.close();
} catch (Exception e) { } catch (Exception e) {
getLogger().severe("Unable to save files. Is the directory writeable?"); getLogger().severe("Unable to save files. Is the directory writeable?");
} }
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, file.getName() + " created."); getLogger().log(Level.INFO, file.getName() + " created.");
} }
return true; return true;
} }
return false; return false;
} }
private void setupXray() { private void setupXray() {
final XRayTracker xtracker = manager.getXRayTracker(); final XRayTracker xtracker = manager.getXRayTracker();
if (config.logXRay()) { if (config.logXRay()) {
eventList.add(new XRayListener()); eventList.add(new XRayListener());
if (config.alertXRay()) { if (config.alertXRay()) {
getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() { getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() {
@Override @Override
public void run() { public void run() {
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
String name = player.getName(); String name = player.getName();
if (!xtracker.hasAlerted(name) && xtracker.sufficientData(name) && xtracker.hasAbnormal(name)) { if (!xtracker.hasAlerted(name) && xtracker.sufficientData(name) && xtracker.hasAbnormal(name)) {
List<String> alert = new ArrayList<String>(); List<String> alert = new ArrayList<String>();
alert.add(ChatColor.YELLOW + "[ALERT] " + ChatColor.WHITE + name + ChatColor.YELLOW + " might be using xray."); alert.add(ChatColor.YELLOW + "[ALERT] " + ChatColor.WHITE + name + ChatColor.YELLOW + " might be using xray.");
alert.add(ChatColor.YELLOW + "[ALERT] Please check their xray stats using " + ChatColor.WHITE + "/anticheat xray " + name + ChatColor.YELLOW + "."); alert.add(ChatColor.YELLOW + "[ALERT] Please check their xray stats using " + ChatColor.WHITE + "/anticheat xray " + name + ChatColor.YELLOW + ".");
Utilities.alert(alert); Utilities.alert(alert);
xtracker.logAlert(name); xtracker.logAlert(name);
} }
} }
} }
}, XRAY_TIME, XRAY_TIME); }, XRAY_TIME, XRAY_TIME);
if (verbose) {
getLogger().log(Level.INFO, "Scheduled the XRay checker."); if (verbose) {
} getLogger().log(Level.INFO, "Scheduled the XRay checker.");
} }
} }
}
} }
private void setupEvents() { private void setupEvents() {
for (Listener listener : eventList) { for (Listener listener : eventList) {
getServer().getPluginManager().registerEvents(listener, this); getServer().getPluginManager().registerEvents(listener, this);
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Registered events for ".concat(listener.toString().split("@")[0].split(".anticheat.")[1])); getLogger().log(Level.INFO, "Registered events for ".concat(listener.toString().split("@")[0].split(".anticheat.")[1]));
} }
} }
} }
private void setupCommands() { private void setupCommands() {
getCommand("anticheat").setExecutor(new CommandHandler()); getCommand("anticheat").setExecutor(new CommandHandler());
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Registered commands."); getLogger().log(Level.INFO, "Registered commands.");
} }
} }
private void setupUpdater() { private void setupUpdater() {
if (config.autoUpdate()) { if (config.autoUpdate()) {
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Checking for a new update..."); getLogger().log(Level.INFO, "Checking for a new update...");
} }
Updater updater = new Updater(this, "anticheat", this.getFile(), Updater.UpdateType.DEFAULT, false); Updater updater = new Updater(this, "anticheat", this.getFile(), Updater.UpdateType.DEFAULT, false);
update = updater.getResult() != Updater.UpdateResult.NO_UPDATE; update = updater.getResult() != Updater.UpdateResult.NO_UPDATE;
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Update available: " + update); getLogger().log(Level.INFO, "Update available: " + update);
} }
} }
} }
private void setupMetrics() { private void setupMetrics() {
try { try {
metrics = new Metrics(this); metrics = new Metrics(this);
final EventListener listener = new EventListener(); final EventListener listener = new EventListener();
Graph hacksGraph = metrics.createGraph("Hacks blocked"); Graph hacksGraph = metrics.createGraph("Hacks blocked");
for (final CheckType type : CheckType.values()) { for (final CheckType type : CheckType.values()) {
hacksGraph.addPlotter(new Metrics.Plotter(CheckType.getName(type)) { hacksGraph.addPlotter(new Metrics.Plotter(CheckType.getName(type)) {
@Override @Override
public int getValue() { public int getValue() {
return EventListener.getCheats(type); return EventListener.getCheats(type);
} }
}); });
listener.resetCheck(type); listener.resetCheck(type);
} }
Graph apiGraph = metrics.createGraph("API Usage"); Graph apiGraph = metrics.createGraph("API Usage");
apiGraph.addPlotter(new Metrics.Plotter("Checks disabled") { apiGraph.addPlotter(new Metrics.Plotter("Checks disabled") {
@Override @Override
public int getValue() { public int getValue() {
return manager.getCheckManager().getDisabled(); return manager.getCheckManager().getDisabled();
} }
}); });
apiGraph.addPlotter(new Metrics.Plotter("Players exempted") { apiGraph.addPlotter(new Metrics.Plotter("Players exempted") {
@Override @Override
public int getValue() { public int getValue() {
return manager.getCheckManager().getExempt(); return manager.getCheckManager().getExempt();
} }
}); });
metrics.start(); metrics.start();
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Metrics started."); getLogger().log(Level.INFO, "Metrics started.");
} }
} catch (IOException ex) {} } catch (IOException ex) {}
} }
private void setupConfig() { private void setupConfig() {
config = manager.getConfiguration(); config = manager.getConfiguration();
config.load(); config.load();
checkConfigs(); checkConfigs();
verbose = config.verboseStartup(); verbose = config.verboseStartup();
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Setup the config."); getLogger().log(Level.INFO, "Setup the config.");
} }
} }
private void restoreLevels() { private void restoreLevels() {
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
String name = player.getName(); String name = player.getName();
manager.getUserManager().addUser(new User(player.getName(), config.getLevel(name))); manager.getUserManager().addUser(new User(player.getName(), config.getLevel(name)));
if (verbose) { if (verbose) {
getLogger().log(Level.INFO, "Data for " + player.getName() + " re-applied from flatfile"); getLogger().log(Level.INFO, "Data for " + player.getName() + " re-applied from flatfile");
} }
} }
} }
public void checkConfigs() { public void checkConfigs() {
save(getResource("config.yml"), new File(getDataFolder() + "/config.yml")); save(getResource("config.yml"), new File(getDataFolder() + "/config.yml"));
save(getResource("lang.yml"), new File(getDataFolder() + "/lang.yml")); save(getResource("lang.yml"), new File(getDataFolder() + "/lang.yml"));
save(getResource("magic.yml"), new File(getDataFolder() + "/magic.yml")); save(getResource("magic.yml"), new File(getDataFolder() + "/magic.yml"));
if(!save(getResource("events.yml"), new File(getDataFolder() + "/events.yml"))) { if(!save(getResource("events.yml"), new File(getDataFolder() + "/events.yml"))) {
config.updateEvents(); config.updateEvents();
} }
} }
public static Anticheat getPlugin() { public static Anticheat getPlugin() {
return manager.getPlugin(); return manager.getPlugin();
} }
public static AnticheatManager getManager() { public static AnticheatManager getManager() {
return manager; return manager;
} }
public static boolean isUpdated() { public static boolean isUpdated() {
return !update; return !update;
} }
public static String getVersion() { public static String getVersion() {
return manager.getPlugin().getDescription().getVersion(); return manager.getPlugin().getDescription().getVersion();
} }
private void cleanup() { private void cleanup() {
eventList = null; eventList = null;
manager = null; manager = null;
config = null; config = null;
metrics = null; metrics = null;
} }
} }
@@ -20,7 +20,6 @@ package net.h31ix.anticheat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.h31ix.anticheat.util.Calibrator;
import net.h31ix.anticheat.manage.CheckType; import net.h31ix.anticheat.manage.CheckType;
import net.h31ix.anticheat.manage.User; import net.h31ix.anticheat.manage.User;
import net.h31ix.anticheat.manage.UserManager; import net.h31ix.anticheat.manage.UserManager;
@@ -29,7 +29,6 @@ import net.h31ix.anticheat.util.Distance;
import net.h31ix.anticheat.util.Language; import net.h31ix.anticheat.util.Language;
import net.h31ix.anticheat.util.Magic; import net.h31ix.anticheat.util.Magic;
import net.h31ix.anticheat.util.Utilities; import net.h31ix.anticheat.util.Utilities;
import net.h31ix.anticheat.util.yaml.CommentedConfiguration;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@@ -12,7 +12,6 @@ public class BlockHardnessTest {
for(Material m : Material.values()) { for(Material m : Material.values()) {
if(!BlockHardness.hasBlockHardness(m) && m.isBlock() && m != Material.AIR && m != Material.BEDROCK) { if(!BlockHardness.hasBlockHardness(m) && m.isBlock() && m != Material.AIR && m != Material.BEDROCK) {
Assert.fail("Material " + m + " is not accounted for by BlockHardness"); Assert.fail("Material " + m + " is not accounted for by BlockHardness");
} }
} }
} }
@@ -7,9 +7,6 @@
package net.h31ix.anticheat.update; package net.h31ix.anticheat.update;
import java.io.*; import java.io.*;
import java.lang.IllegalThreadStateException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
@@ -186,7 +186,9 @@ public enum BlockHardness {
public static BlockHardness getHardness(Material m) { public static BlockHardness getHardness(Material m) {
for (BlockHardness e : BlockHardness.values()) { for (BlockHardness e : BlockHardness.values()) {
if (e.mat.equals(m)) return e; if (e.mat.equals(m)) {
return e;
}
} }
return null; return null;
} }
@@ -23,7 +23,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.h31ix.anticheat.Anticheat; import net.h31ix.anticheat.Anticheat;
import net.h31ix.anticheat.manage.AnticheatManager; import net.h31ix.anticheat.manage.AnticheatManager;
import net.h31ix.anticheat.util.yaml.CommentedConfiguration; import net.h31ix.anticheat.util.yaml.CommentedConfiguration;
@@ -21,7 +21,6 @@ package net.h31ix.anticheat.util;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -18,11 +18,10 @@
package net.h31ix.anticheat.util; package net.h31ix.anticheat.util;
import net.h31ix.anticheat.Anticheat;
import org.bukkit.ChatColor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.h31ix.anticheat.Anticheat;
import org.bukkit.ChatColor;
public class Level { public class Level {
@@ -29,7 +29,6 @@ import org.bukkit.block.BlockFace;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.NumberConversions; import org.bukkit.util.NumberConversions;
public final class Utilities { public final class Utilities {
@@ -108,9 +107,11 @@ public final class Utilities {
public static boolean isHoveringOverWater(Location player, int blocks) { public static boolean isHoveringOverWater(Location player, int blocks) {
for (int i = player.getBlockY(); i > player.getBlockY() - blocks; i--) { for (int i = player.getBlockY(); i > player.getBlockY() - blocks; i--) {
Block newloc = (new Location(player.getWorld(), player.getBlockX(), i, player.getBlockZ())).getBlock(); Block newloc = (new Location(player.getWorld(), player.getBlockX(), i, player.getBlockZ())).getBlock();
if (newloc.getTypeId() != 0 && newloc.isLiquid()) if (newloc.getTypeId() != 0 && newloc.isLiquid()) {
return true; return true;
else if (newloc.getTypeId() != 0) return false; } else if (newloc.getTypeId() != 0) {
return false;
}
} }
return false; return false;
@@ -193,12 +194,15 @@ public final class Utilities {
result += 150; result += 150;
if (enchantlvl > 0) if (enchantlvl > 0) {
result /= enchantlvl * enchantlvl + 1L; result /= enchantlvl * enchantlvl + 1L;
}
if (result > 25000) if (result > 25000) {
result = 25000; result = 25000;
else if (result < 0) result = 0; } else if (result < 0) {
result = 0;
}
return result; return result;
} }
@@ -206,7 +210,9 @@ public final class Utilities {
public static boolean blockIsnt(Block block, Material[] materials) { public static boolean blockIsnt(Block block, Material[] materials) {
Material type = block.getType(); Material type = block.getType();
for(Material m : materials) { for(Material m : materials) {
if(m == type) return false; if(m == type) {
return false;
}
} }
return false; return false;
} }