diff --git a/src/main/java/net/h31ix/anticheat/api/CheckFailEvent.java b/src/main/java/net/h31ix/anticheat/api/CheckFailEvent.java new file mode 100644 index 0000000..f12d5ba --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/api/CheckFailEvent.java @@ -0,0 +1,50 @@ +/* + * AntiCheat for Bukkit. + * Copyright (C) 2012-2013 AntiCheat Team | http://gravitydevelopment.net + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.h31ix.anticheat.api; + +import net.h31ix.anticheat.manage.CheckType; +import net.h31ix.anticheat.manage.User; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class CheckFailEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + private final User user; + private final CheckType type; + + public CheckFailEvent(User user, CheckType type) { + this.user = user; + this.type = type; + } + + public User getUser() { + return user; + } + + public CheckType getCheck() { + return type; + } + + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/h31ix/anticheat/event/EventListener.java b/src/main/java/net/h31ix/anticheat/event/EventListener.java index 0565968..83988a7 100644 --- a/src/main/java/net/h31ix/anticheat/event/EventListener.java +++ b/src/main/java/net/h31ix/anticheat/event/EventListener.java @@ -49,7 +49,7 @@ public class EventListener implements Listener { USAGE_LIST.put(type, getCheats(type) + 1); // Ignore plugins that are creating NPCs with no names (why the hell) if (user != null && user.getName() != null) { - type.logUse(user.getName()); + type.logUse(user); if (Anticheat.getManager().getConfiguration().getFileLogLevel() == 2 && type.getUses(user.getName()) % 10 == 0) { Anticheat.getManager().fileLog(user.getName() + " has triggered multiple " + type + " checks."); } diff --git a/src/main/java/net/h31ix/anticheat/event/InventoryListener.java b/src/main/java/net/h31ix/anticheat/event/InventoryListener.java index b81d644..4bd76cd 100644 --- a/src/main/java/net/h31ix/anticheat/event/InventoryListener.java +++ b/src/main/java/net/h31ix/anticheat/event/InventoryListener.java @@ -38,7 +38,7 @@ public class InventoryListener extends EventListener { CheckResult result = getBackend().checkInventoryClicks(player); if (result.failed()) { if (!silentMode()) { - getUserManager().getUser(player.getName()).restore(event.getInventory()); + getUserManager().getUser(player.getName()).restoreInventory(event.getInventory()); player.getInventory().clear(); } log(result.getMessage(), player, CheckType.FAST_INVENTORY); @@ -54,12 +54,12 @@ public class InventoryListener extends EventListener { @EventHandler public void onInventoryOpen(InventoryOpenEvent event) { if(event.getInventory().getType() != InventoryType.BEACON) { - getUserManager().getUser(event.getPlayer().getName()).setSnapshot(event.getInventory().getContents()); + getUserManager().getUser(event.getPlayer().getName()).setInventorySnapshot(event.getInventory().getContents()); } } @EventHandler public void onInventoryClose(InventoryCloseEvent event) { - getUserManager().getUser(event.getPlayer().getName()).removeSnapshot(); + getUserManager().getUser(event.getPlayer().getName()).removeInventorySnapshot(); } } diff --git a/src/main/java/net/h31ix/anticheat/manage/CheckType.java b/src/main/java/net/h31ix/anticheat/manage/CheckType.java index 2c1224f..2524c68 100644 --- a/src/main/java/net/h31ix/anticheat/manage/CheckType.java +++ b/src/main/java/net/h31ix/anticheat/manage/CheckType.java @@ -20,7 +20,10 @@ package net.h31ix.anticheat.manage; import java.util.HashMap; import java.util.Map; + +import net.h31ix.anticheat.api.CheckFailEvent; import net.h31ix.anticheat.util.Permission; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; /** @@ -57,28 +60,62 @@ public enum CheckType { private final Permission permission; private final Map level = new HashMap(); - + + /** + * Initialize a CheckType + * + * @param perm Permission that applies to this check + */ private CheckType(Permission perm) { this.permission = perm; } - + + /** + * Determine whether a player has permission to bypass this check + * + * @param player Player to check + * @return true if the player can bypass + */ public boolean checkPermission(Player player) { return permission.get(player); } - - public void logUse(String name) { - int amount = level.get(name) == null ? 1 : level.get(name) + 1; - level.put(name, amount); + + /** + * Log the failure of this check + * + * @param user User who failed the check + */ + public void logUse(User user) { + int amount = level.get(user.getName()) == null ? 1 : level.get(user.getName()) + 1; + level.put(user.getName(), amount); + Bukkit.getServer().getPluginManager().callEvent(new CheckFailEvent(user, this)); } - + + /** + * Clear failure history of this check for a user + * + * @param name User's name to clear + */ public void clearUse(String name) { level.put(name, 0); } - + + /** + * Get how many times a user has failed this check + * + * @param name User's name + * @return number of times failed + */ public int getUses(String name) { return level.get(name) != null ? level.get(name) : 0; } - + + /** + * Get the reference name of a check + * + * @param type Type of check + * @return reference name + */ public static String getName(CheckType type) { char[] chars = type.toString().replaceAll("_", " ").toLowerCase().toCharArray(); chars[0] = Character.toUpperCase(chars[0]); diff --git a/src/main/java/net/h31ix/anticheat/manage/User.java b/src/main/java/net/h31ix/anticheat/manage/User.java index 8847a35..97c69ec 100644 --- a/src/main/java/net/h31ix/anticheat/manage/User.java +++ b/src/main/java/net/h31ix/anticheat/manage/User.java @@ -44,35 +44,67 @@ public class User { private String [] messages = new String[2]; private Long [] messageTimes = new Long[2]; + /** + * Initialize an AntiCheat user + * + * @param name Player's name + */ public User(String name) { this.name = name; getConfigInfo(); } - + + /** + * Initialize an AntiCheat user with a predefined level + * + * @param name Player's name + * @param level Player's level + */ public User(String name, int level) { this.name = name; this.level = level; getConfigInfo(); } - + private void getConfigInfo() { config = Anticheat.getManager().getConfiguration(); silentMode = config.silentMode(); levels = config.getLevels(); } - + + /** + * Get the player's name + * + * @return Player name + */ public String getName() { return name; } - + + /** + * Get the Bukkit player + * + * @return Player + */ public Player getPlayer() { return Bukkit.getPlayer(name); } - + + /** + * Get the user's level value + * + * @return level + */ public int getLevel() { return level; } - + + /** + * Increase the player's level (from failing a check) + * + * @param type The check failed + * @return true if the level was increased + */ public boolean increaseLevel(CheckType type) { if (getPlayer() != null && getPlayer().isOnline()) { if (silentMode && type.getUses(name) % 4 != 0) { @@ -101,17 +133,31 @@ public class User { return false; } } - + + /** + * Decrease the player's level by one value + */ public void decreaseLevel() { level = level != 0 ? level - 1 : 0; } - - public void setLevel(int level) { + + /** + * Set the users level to a specific value. Must be 0 < level < config-defined highest level + * @param level level to set + * @return true if the level was valid and set properly + */ + public boolean setLevel(int level) { if (level > 0 && level <= config.getHighestLevel()) { this.level = level; + return true; + } else { + return false; } } - + + /** + * Reset the user's level to zero and clear all check failures + */ public void resetLevel() { level = 0; for (CheckType type : CheckType.values()) { @@ -119,21 +165,41 @@ public class User { } } - public Location getGoodLocation(Location e) { - if (goodLocation == null) { return e; } + /** + * Get the last known valid location of the player for use in resetting + * + * @param location default value to be returned if no known good location exists + * @return last known valid location + */ + public Location getGoodLocation(Location location) { + if (goodLocation == null) { + return location; + } return goodLocation; } - - public void setGoodLocation(Location e) { - if (Utilities.cantStandAtExp(e) || (e.getBlock().isLiquid() && !Utilities.isFullyInWater(e))) { - return; + + /** + * Set the last known valid location of the player + * + * @param location location to be set + * @return true if the location was valid and set properly + */ + public boolean setGoodLocation(Location location) { + if (Utilities.cantStandAtExp(location) || (location.getBlock().isLiquid() && !Utilities.isFullyInWater(location))) { + return false; } - goodLocation = e; + goodLocation = location; + return true; } - public void setSnapshot(ItemStack[] is) { + /** + * Store a copy of the player's inventory, for use in resetting + * + * @param is ItemStack list to store + */ + public void setInventorySnapshot(ItemStack[] is) { inventorySnapshot = new ArrayList(); for(int i=0;i alert; - + + /** + * Initialize the user manager with a given configuration + * + * @param conf Configuration to use + */ public UserManager(Configuration conf) { config = conf; alert = config.getLang().getAlert(); } - + + /** + * Get a user with the given name + * @param name Name + * @return User with name + */ public User getUser(String name) { for (User user : users) { if (user.getName().equalsIgnoreCase(name)) { return user; } @@ -49,20 +59,41 @@ public class UserManager { // Otherwise try to load them return loadUserFromFile(name); } - + + /** + * Get all users + * + * @return List of users + */ public List getUsers() { return users; } - + + /** + * Add a user to the list + * + * @param user User to add + */ public void addUser(User user) { users.add(user); } - + + /** + * Remove a user from the list + * + * @param user User to remove + */ public void remove(User user) { config.saveLevel(user.getName(), user.getLevel()); users.remove(user); } - + + /** + * Get a user's level, or 0 if the player isn't found + * + * @param name Name of the player + * @return player level + */ public int safeGetLevel(String name) { User user = getUser(name); if (user == null) { @@ -71,21 +102,37 @@ public class UserManager { return user.getLevel(); } } - + + /** + * Set a user's level + * + * @param name Name of the player + * @param level Level to set + */ public void safeSetLevel(String name, int level) { User user = getUser(name); if (user != null) { user.setLevel(level); } } - + + /** + * Reset a user + * @param name Name of the user + */ public void safeReset(String name) { User user = getUser(name); if (user != null) { user.resetLevel(); } } - + + /** + * Load a user into the list from flatfile + * + * @param name Name of the user + * @return true if found in the file + */ public boolean addUserFromFile(String name) { User user = loadUserFromFile(name); if (user != null) { @@ -95,7 +142,13 @@ public class UserManager { return false; } } - + + /** + * Get a user from flatfile + * + * @param name Name of the user + * @return the user if found, null otherwise + */ public User loadUserFromFile(String name) { int level = config.getLevel(name); if (level == -1) { @@ -104,7 +157,14 @@ public class UserManager { return new User(name, level); } } - + + /** + * Fire an alert + * + * @param user The user to alert + * @param level The user's level + * @param type The CheckType that triggered the alert + */ public void alert(User user, Level level, CheckType type) { ArrayList messageArray = new ArrayList(); for (int i = 0; i < alert.size(); i++) { @@ -117,10 +177,27 @@ public class UserManager { Utilities.alert(messageArray); execute(user, level.getActions(), type); } + + /** + * Execute configuration actions for an alert + * @param user The user + * @param actions The list of actions to execute + * @param type The CheckType that triggered the alert + */ public void execute(User user, List actions, CheckType type) { execute(user, actions, type, config.getLang().getKickReason(), config.getLang().getWarning(), config.getLang().getBanReason()); } + /** + * Execute configuration actions for an alert + * + * @param user The user + * @param actions The list of actions to execute + * @param type The CheckType that triggered the alert + * @param kickReason The config's kick reason + * @param warning The config's warning format + * @param banReason The config's ban reason + */ public void execute(final User user, final List actions, final CheckType type, final String kickReason, final List warning, final String banReason) { // Execute synchronously for thread safety when called from AsyncPlayerChatEvent Bukkit.getScheduler().scheduleSyncDelayedTask(Anticheat.getPlugin(), new Runnable() {