Javadoc cleanup, event implementation

This commit is contained in:
h31ix committed 2013-06-01 15:17:31 -04:00
1 parent 78b84531bc
commit 6dd5b20629
6 files changed
+319 -44

No files matched your search

@@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}
@@ -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.");
}
@@ -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();
}
}
@@ -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<String, Integer> level = new HashMap<String, Integer>();
/**
* 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]);
@@ -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<ItemStack>();
for(int i=0;i<is.length;i++) {
if(is[i] != null) {
@@ -142,11 +208,18 @@ public class User {
}
}
public void removeSnapshot() {
/**
* Remove the current inventory snapshot
*/
public void removeInventorySnapshot() {
inventorySnapshot = null;
}
public void restore(Inventory inventory) {
/**
* Restore the player's inventory with the current inventory snapshot
* @param inventory Player's inventory
*/
public void restoreInventory(Inventory inventory) {
if(inventorySnapshot != null) {
inventory.clear();
for(ItemStack is : inventorySnapshot) {
@@ -157,16 +230,34 @@ public class User {
}
}
/**
* Set the player's to location
* @param x X value
* @param y Y value
* @param z Z value
*/
public void setTo(double x, double y, double z) {
toX = (int)x;
toY = (int)y;
toZ = (int)z;
}
/**
* Check if a given to value is the same as the stored value
* @param x X value
* @param y Y value
* @param z Z value
* @return true if coordinates are all equal
*/
public boolean checkTo(double x, double y, double z) {
return (int)x == toX && (int)y == toY && (int)z == toZ;
}
/**
* Log a player's chat message
*
* @param message Message to log
*/
public void addMessage(String message) {
messages[1] = messages[0];
messages[0] = message;
@@ -175,19 +266,39 @@ public class User {
messageTimes[0] = System.currentTimeMillis();
}
/**
* Get a stored chat message
*
* @param index Index of the chat in storage
* @return the message
*/
public String getMessage(int index) {
return messages[index];
}
/**
* Get the time a message in storage was sent
*
* @param index Index of the chat in storage
* @return time the message was sent
*/
public Long getMessageTime(int index) {
return messageTimes[index];
}
/**
* Clear all stored messages
*/
public void clearMessages() {
messages = new String[2];
messageTimes = new Long[2];
}
/**
* Get the time the very last stored message was sent
*
* @return the last message's time
*/
public Long getLastMessageTime() {
return getMessageTime(0) == null ? -1 : getMessageTime(0);
}
@@ -36,12 +36,22 @@ public class UserManager {
private static final ChatColor YELLOW = ChatColor.YELLOW;
private static final ChatColor RED = ChatColor.RED;
private static List<String> 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<User> 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<String> messageArray = new ArrayList<String>();
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<String> 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<String> actions, final CheckType type, final String kickReason, final List<String> warning, final String banReason) {
// Execute synchronously for thread safety when called from AsyncPlayerChatEvent
Bukkit.getScheduler().scheduleSyncDelayedTask(Anticheat.getPlugin(), new Runnable() {