diff --git a/src/main/java/net/h31ix/anticheat/Anticheat.java b/src/main/java/net/h31ix/anticheat/Anticheat.java index a9fb963..03faeb3 100644 --- a/src/main/java/net/h31ix/anticheat/Anticheat.java +++ b/src/main/java/net/h31ix/anticheat/Anticheat.java @@ -110,7 +110,7 @@ public class Anticheat extends JavaPlugin { out.close(); in.close(); } catch (Exception e) { - getLogger().severe("Unable to save files. Is the directory writeable?"); + getLogger().severe("Unable to save files. Is the directory writable?"); } if (verbose) { getLogger().log(Level.INFO, file.getName() + " created."); @@ -213,8 +213,8 @@ public class Anticheat extends JavaPlugin { private void setupConfig() { config = manager.getConfiguration(); - config.load(); checkConfigs(); + config.load(); verbose = config.verboseStartup(); if (verbose) { getLogger().log(Level.INFO, "Setup the config."); @@ -235,8 +235,8 @@ public class Anticheat extends JavaPlugin { save(getResource("config.yml"), new File(getDataFolder() + "/config.yml")); save(getResource("lang.yml"), new File(getDataFolder() + "/lang.yml")); save(getResource("magic.yml"), new File(getDataFolder() + "/magic.yml")); - if(!save(getResource("events.yml"), new File(getDataFolder() + "/events.yml"))) { - config.updateEvents(); + if(save(getResource("events.yml"), new File(getDataFolder() + "/events.yml"))) { + config.setUpdateEvents(); } } diff --git a/src/main/java/net/h31ix/anticheat/event/EventListener.java b/src/main/java/net/h31ix/anticheat/event/EventListener.java index d9e0d9d..6c49e88 100644 --- a/src/main/java/net/h31ix/anticheat/event/EventListener.java +++ b/src/main/java/net/h31ix/anticheat/event/EventListener.java @@ -27,94 +27,97 @@ import org.bukkit.entity.Player; import org.bukkit.event.Listener; public class EventListener implements Listener { - private static final Map USAGE_LIST = new EnumMap(CheckType.class); - private static final Map DECREASE_LIST = new HashMap(); - private static final CheckManager CHECK_MANAGER = Anticheat.getManager().getCheckManager(); - private static final Backend BACKEND = Anticheat.getManager().getBackend(); - private static final Anticheat PLUGIN = Anticheat.getManager().getPlugin(); - private static final UserManager USER_MANAGER = Anticheat.getManager().getUserManager(); - - public static void log(String message, Player player, CheckType type) { - User user = getUserManager().getUser(player.getName()); - if (user.increaseLevel(type)) { - Anticheat.getManager().log(message); - } - removeDecrease(user); - logCheat(type, user); - } - - private static void logCheat(CheckType type, User user) { - USAGE_LIST.put(type, getCheats(type) + 1); - type.logUse(user.getName()); - // Ignore plugins that are creating NPCs with no names (why the hell) - if (user.getName() != null) { - if (Anticheat.getManager().getConfiguration().getFileLogLevel() == 2 && type.getUses(user.getName()) % 10 == 0) { - Anticheat.getManager().fileLog(user.getName() + " has triggered multiple " + type + " checks."); - } - } - } - - public void resetCheck(CheckType type) { - USAGE_LIST.put(type, 0); - } - - public static int getCheats(CheckType type) { - int x = 0; - if (USAGE_LIST.get(type) != null) { - x = USAGE_LIST.get(type); - } - return x; - } - - private static void removeDecrease(User user) { - int x = 0; - // Ignore plugins that are creating NPCs with no names - if (user.getName() != null) { - if (DECREASE_LIST.get(user.getName()) != null) { - x = DECREASE_LIST.get(user.getName()); - x -= 2; - if (x < 0) { - x = 0; - } - } - DECREASE_LIST.put(user.getName(), x); - } - } - - public static void decrease(Player player) { - User user = getUserManager().getUser(player.getName()); - // Ignore plugins that are creating NPCs with no names - if (user.getName() != null) { - int x = 0; - if (DECREASE_LIST.get(user.getName()) != null) { - x = DECREASE_LIST.get(user.getName()); - } - x += 1; - DECREASE_LIST.put(user.getName(), x); - if (x >= 10) { - user.decreaseLevel(); - DECREASE_LIST.put(user.getName(), 0); - } - } - } - - public static CheckManager getCheckManager() { - return CHECK_MANAGER; - } - - public static AnticheatManager getManager() { - return Anticheat.getManager(); - } - - public static Backend getBackend() { - return BACKEND; - } - - public static UserManager getUserManager() { - return USER_MANAGER; - } - - public static Anticheat getPlugin() { - return PLUGIN; - } + private static final Map USAGE_LIST = new EnumMap(CheckType.class); + private static final Map DECREASE_LIST = new HashMap(); + private static final CheckManager CHECK_MANAGER = Anticheat.getManager().getCheckManager(); + private static final Backend BACKEND = Anticheat.getManager().getBackend(); + private static final Anticheat PLUGIN = Anticheat.getManager().getPlugin(); + private static final UserManager USER_MANAGER = Anticheat.getManager().getUserManager(); + + public static void log(String message, Player player, CheckType type) { + User user = getUserManager().getUser(player.getName()); + logCheat(type, user); + if (user.increaseLevel(type)) { + Anticheat.getManager().log(message); + } + removeDecrease(user); + } + + private static void logCheat(CheckType type, User user) { + USAGE_LIST.put(type, getCheats(type) + 1); + type.logUse(user.getName()); + // Ignore plugins that are creating NPCs with no names (why the hell) + if (user.getName() != null) { + if (Anticheat.getManager().getConfiguration().getFileLogLevel() == 2 && type.getUses(user.getName()) % 10 == 0) { + Anticheat.getManager().fileLog(user.getName() + " has triggered multiple " + type + " checks."); + } + } + } + + public void resetCheck(CheckType type) { + USAGE_LIST.put(type, 0); + } + + public static int getCheats(CheckType type) { + int x = 0; + if (USAGE_LIST.get(type) != null) { + x = USAGE_LIST.get(type); + } + return x; + } + + private static void removeDecrease(User user) { + int x = 0; + // Ignore plugins that are creating NPCs with no names + if (user.getName() != null) { + if (DECREASE_LIST.get(user.getName()) != null) { + x = DECREASE_LIST.get(user.getName()); + x -= 2; + if (x < 0) { + x = 0; + } + } + DECREASE_LIST.put(user.getName(), x); + } + } + + public static void decrease(Player player) { + User user = getUserManager().getUser(player.getName()); + // Ignore plugins that are creating NPCs with no names + if (user.getName() != null) { + int x = 0; + + if (DECREASE_LIST.get(user.getName()) != null) { + x = DECREASE_LIST.get(user.getName()); + } + + x += 1; + DECREASE_LIST.put(user.getName(), x); + + if (x >= 10) { + user.decreaseLevel(); + DECREASE_LIST.put(user.getName(), 0); + } + } + } + + public static CheckManager getCheckManager() { + return CHECK_MANAGER; + } + + public static AnticheatManager getManager() { + return Anticheat.getManager(); + } + + public static Backend getBackend() { + return BACKEND; + } + + public static UserManager getUserManager() { + return USER_MANAGER; + } + + public static Anticheat getPlugin() { + return PLUGIN; + } } diff --git a/src/main/java/net/h31ix/anticheat/manage/User.java b/src/main/java/net/h31ix/anticheat/manage/User.java index ff37510..4ab4486 100644 --- a/src/main/java/net/h31ix/anticheat/manage/User.java +++ b/src/main/java/net/h31ix/anticheat/manage/User.java @@ -22,6 +22,7 @@ import net.h31ix.anticheat.Anticheat; import net.h31ix.anticheat.util.Configuration; import net.h31ix.anticheat.util.Level; import net.h31ix.anticheat.util.Utilities; +import net.h31ix.anticheat.util.rule.Rule; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -77,6 +78,8 @@ public class User { return false; } else { level++; + + // Check levels for(Level l : levels) { if(l.getValue() == level) { Anticheat.getManager().getUserManager().alert(this, l, type); @@ -85,6 +88,11 @@ public class User { } } } + + // Execute rules + for(Rule rule : config.getRules()) { + rule.check(this, type); + } return true; } } else { diff --git a/src/main/java/net/h31ix/anticheat/manage/UserManager.java b/src/main/java/net/h31ix/anticheat/manage/UserManager.java index 577aeed..285cb41 100644 --- a/src/main/java/net/h31ix/anticheat/manage/UserManager.java +++ b/src/main/java/net/h31ix/anticheat/manage/UserManager.java @@ -142,6 +142,8 @@ public class UserManager { if (!msg.equals("")) { Bukkit.broadcastMessage(msg); } + } else if (event.equalsIgnoreCase("RESET")) { + user.resetLevel(); } } } diff --git a/src/main/java/net/h31ix/anticheat/util/Configuration.java b/src/main/java/net/h31ix/anticheat/util/Configuration.java index 8e176e3..4753fb3 100644 --- a/src/main/java/net/h31ix/anticheat/util/Configuration.java +++ b/src/main/java/net/h31ix/anticheat/util/Configuration.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.logging.Logger; import net.h31ix.anticheat.Anticheat; import net.h31ix.anticheat.manage.AnticheatManager; +import net.h31ix.anticheat.util.rule.Rule; import net.h31ix.anticheat.util.yaml.CommentedConfiguration; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -61,8 +62,10 @@ public class Configuration { private static Language language; private List exemptWorlds = new ArrayList(); private List levels = new ArrayList(); + private List rules = new ArrayList(); private Magic magicInstance; private int highestLevel; + private boolean updateEvents = false; public Configuration(AnticheatManager instance) { micromanage = instance; @@ -153,9 +156,18 @@ public class Configuration { } public int getHighestLevel() { + // TODO: setup return highestLevel = -99; } + public List getRules() { + return rules; + } + + public void setUpdateEvents() { + updateEvents = true; + } + public void updateEvents() { // Update the old event system to events.yml CommentedConfiguration config = CommentedConfiguration.loadConfig(configFile); @@ -178,6 +190,7 @@ public class Configuration { levels.add(customLevel); writeEvents(); + updateEvents = false; } private void writeEvents() { @@ -198,6 +211,10 @@ public class Configuration { levels = new ArrayList(); for(String string : events.getStringList("levels")) { Level level = Level.load(string); + if(level == null) { + continue; + } + levels.add(level); highestLevel = level.getValue() > highestLevel ? level.getValue() : highestLevel; } @@ -217,10 +234,31 @@ public class Configuration { } } } + + rules = new ArrayList(); + List tempRules = events.getStringList("rules"); + for(int i=0;i 0 ? Level=20 : none")) { + tempRules.set(i, "Check_SPIDER < 0 ? Player.KICK : null"); + events.set("rules", tempRules); + continue; + } else if (string.equals("Check_SPIDER < 0 ? Player.KICK : null")) { + // New default rule, won't ever run so we shouldn't load it; only used as example + continue; + } + Rule rule = Rule.load(string); + if(rule != null) { + rules.add(rule); + } else { + Anticheat.getPlugin().getLogger().warning("Couldn't load rule '"+string+"' from config. Improper formatting used."); + } + } } public final void load() { - boolean secondLoad = config != null; config = CommentedConfiguration.loadConfig(configFile); level = YamlConfiguration.loadConfiguration(levelFile); magic = YamlConfiguration.loadConfiguration(magicFile); @@ -275,10 +313,13 @@ public class Configuration { exemptWorlds = config.getStringList("Disable in"); - if(secondLoad) { - readEvents(); + if(updateEvents) { + updateEvents(); } + + readEvents(); // End pulling values from config + save(); magicInstance = new Magic(getMagic(), this, CommentedConfiguration.loadConfiguration(micromanage.getPlugin().getResource("magic.yml"))); if(micromanage.getBackend() != null) { // If this is first run, backend may not be setup yet diff --git a/src/main/java/net/h31ix/anticheat/util/Level.java b/src/main/java/net/h31ix/anticheat/util/Level.java index e48d75a..c5d68b3 100644 --- a/src/main/java/net/h31ix/anticheat/util/Level.java +++ b/src/main/java/net/h31ix/anticheat/util/Level.java @@ -80,7 +80,7 @@ public class Level { public static Level load(String string) { try { - string = string.trim(); + string = Utilities.removeWhitespace(string); String name = string.split(":")[0]; int level = Integer.parseInt(string.split(":")[1]); if(string.split(":").length == 3) { @@ -90,8 +90,8 @@ public class Level { return new Level(name, level); } } catch (Exception ex) { - Anticheat.getPlugin().getLogger().warning("An event was intialized with an invalid string: '"+string+"'"); - Anticheat.getPlugin().getLogger().warning("The proper format is: 'name: threshold, color' such as 'high: 50, RED'"); + Anticheat.getPlugin().getLogger().warning("An event was initialized with an invalid string: '"+string+"'"); + Anticheat.getPlugin().getLogger().warning("The proper format is: 'name: threshold : color' such as 'High : 50 : RED'"); Anticheat.getPlugin().getLogger().warning("This event will NOT run. ("+ex.getMessage()+")"); return null; } diff --git a/src/main/java/net/h31ix/anticheat/util/Utilities.java b/src/main/java/net/h31ix/anticheat/util/Utilities.java index 2ed7523..804d844 100644 --- a/src/main/java/net/h31ix/anticheat/util/Utilities.java +++ b/src/main/java/net/h31ix/anticheat/util/Utilities.java @@ -405,6 +405,16 @@ public final class Utilities { return command.replaceAll("COMMAND\\[", "").replaceAll("]", "").split(";"); } + /** + * Remove all whitespace from the given string to ready it for parsing + * + * @param string the string to parse + * @return string with whitespace removed + */ + public static String removeWhitespace(String string) { + return string.replaceAll(" ", ""); + } + /** * Determine if a player has the given enchantment on their armor * diff --git a/src/main/java/net/h31ix/anticheat/util/rule/ConditionalRule.java b/src/main/java/net/h31ix/anticheat/util/rule/ConditionalRule.java new file mode 100644 index 0000000..47173e7 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/util/rule/ConditionalRule.java @@ -0,0 +1,94 @@ +/* + * 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.util.rule; + +import net.h31ix.anticheat.manage.CheckType; +import net.h31ix.anticheat.manage.User; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import java.util.SortedMap; + +/** + * The conditional rule follows the same syntax as the traditional CS conditional operator.
+ * (CONDITION) ? (TRUE RESULT) : (FALSE RESULT)
+ * Where CONDITION is a statement that will be evaluated as either TRUE or FALSE + * and the results are actions to be taken for either outcome. + *

+ * + * Should you need additional actions, the system will recursively parse the following values and handle appropriately. + *
+ * For instance, (CONDITION) ? (TRUE RESULT) : (CONDITION) ? (TRUE RESULT) : (FALSE RESULT) is a valid rule + *

+ * + * An example of a valid Conditional Rule:
+ * Check_SPIDER > 0 ? Player.KICK : null
+ * The above statement would read 'If the spider check has been failed over zero times, kick the player. Otherwise, do nothing.' + * + * To see syntax for variables and functions that you may use, see {@link net.h31ix.anticheat.util.rule.Rule} + */ +public class ConditionalRule extends Rule { + + private static ScriptEngineManager factory = new ScriptEngineManager(); + private static ScriptEngine engine = factory.getEngineByName("js"); + + private static final String TRUE_DELIMITER = "\\?"; + private static final String FALSE_DELIMITER = ":"; + private static final Type TYPE = Type.CONDITIONAL; + + public ConditionalRule(String string) { + super(string, TYPE); + } + + @Override + public boolean check(User user, CheckType type) { + try { + // Load all variables + SortedMap map = getVariables(user, type); + for(String key : map.keySet()) { + engine.put(key, map.get(key)); + } + + boolean value = (Boolean)engine.eval(getString().split(TRUE_DELIMITER)[0]); + // Yo dawg I heard you like conditionals... + String next = value ? getString().split(TRUE_DELIMITER)[1].split(FALSE_DELIMITER)[0] : getString().split(TRUE_DELIMITER)[1].split(FALSE_DELIMITER)[1]; + + execute(next, user, type); + return value; + } catch (ScriptException e) { + e.printStackTrace(); + } + return true; + } + + private void execute(String string, User user, CheckType type) { + // If we're told to do nothing + if(string.equalsIgnoreCase("null") || string.equalsIgnoreCase("none")) return; + + // If this string is a new conditional statement + if(TYPE.matches(string)) { + new ConditionalRule(string).check(user, type); + } else if(isVariableSet(string)) { + setVariable(string.split("=")[0], string.split("=")[1], user); + } else if(isFunction(string)) { + doFunction(string, type, user); + } + } +} diff --git a/src/main/java/net/h31ix/anticheat/util/rule/Rule.java b/src/main/java/net/h31ix/anticheat/util/rule/Rule.java new file mode 100644 index 0000000..4a24e3a --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/util/rule/Rule.java @@ -0,0 +1,175 @@ +/* + * 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.util.rule; + +import net.h31ix.anticheat.Anticheat; +import net.h31ix.anticheat.manage.CheckType; +import net.h31ix.anticheat.manage.User; +import net.h31ix.anticheat.util.Utilities; + +import java.lang.reflect.Constructor; +import java.util.*; + +/** + * A rule is a scriptable function used to customize the functions of AntiCheat
+ * Rules are executed whenever a user's level rises.

+ * + * There are various variables you can use to interact with AntiCheat through your rules
+ * Variables are denoted with the type followed by a _ followed by the variable name.
+ * For instance, a variable coming from a user who has used FLY 5 times and is checked by a rule containing 'Check_FLY' will produce a value of 5. + *

+ * + * Types of variables:
+ * - Check: Contains all valid checks as listed in {@link net.h31ix.anticheat.manage.CheckType}, and will return the number of times this user has failed the given check
+ * - Player: Contains LEVEL, the player's current level and CHECK, the check that was just failed + *

+ * + * There are also functions you can use to execute an action within AntiCheat
+ * Functions are denoted with the type followed by a period followed by the function name.
+ * For instance, a rule containing Player.KICK will result in the user being kicked.
+ *

+ * + * Types of functions:
+ * - Player: RESET, KICK, BAN, COMMAND[command] OR COMMAND[command1;command2] + * - when using commands &player will be replaced by the player name, &world will be replaced by the player's world, + * and &check will be replaced by the check that caused this rule to be run
+ *

+ * + * The Rule class itself is not an functional rule setup, + * it is inherited and made functional by different implementations of the rule parser.
+ * The only current Rule implementation is the {@link net.h31ix.anticheat.util.rule.ConditionalRule} + * + */ +public class Rule { + + private static String string; + private static final String VARIABLE_SET_REGEX = ".*(_).*=.*"; + private static final String FUNCTION_REGEX = ".*\\..*"; + private Type type; + + public enum Type { + CONDITIONAL(".*[?]*.*:.*", "net.h31ix.anticheat.util.rule.ConditionalRule"); + + private String regex; + private String c; + + private Type(String regex, String c) { + this.regex = regex; + this.c = c; + } + + public boolean matches(String s) { + return s.matches(regex); + } + + public String getInstance() { + return c; + } + } + + public Rule(String string, Type type) { + this.string = string = Utilities.removeWhitespace(string).toLowerCase(); + this.type = type; + } + + /** + * Check if the rule passed or failed + * + * @param user the user to check + * @return true if the rule has passed, false if failed + */ + public boolean check(User user, CheckType type) { + // Default value + return true; + } + + /** + * Load a rule by its string value + * + * @param string the string value to load + * @return an instance of Rule if an implementation exists to handle this rule, null if none are found + */ + public static Rule load(String string) { + for(Type type : Type.values()) { + if(type.matches(string)) { + try { + Class c = Class.forName(type.getInstance()); + Constructor con = c.getConstructor(String.class); + return (Rule)con.newInstance(string); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + return null; + } + + /** + * Get the type of rule this is + * + * @return a {@link net.h31ix.anticheat.util.rule.Rule.Type} + */ + public Type getType() { + return type; + } + + protected String getString() { + return string; + } + + protected SortedMap getVariables(User user, CheckType type) { + SortedMap map = new TreeMap(); + map.put("player_check", type.name().toLowerCase()); + map.put("player_level", user.getLevel()); + for(CheckType t : CheckType.values()) { + map.put("check_"+t.name().toLowerCase(), t.getUses(user.getName())); + } + return map; + } + + protected void setVariable(String variable, String value, User user) { + // Only variable that can be set for now, may change in the future + // others wouldn't make any sense to set + if(variable.equals("player_level") && Utilities.isInt(value)) { + user.setLevel(Integer.parseInt(value)); + } + } + + protected void doFunction(String text, CheckType type, User user) { + if(text.toLowerCase().startsWith("player")) { + text = text.split("\\.")[1]; + List action = new ArrayList(); + action.add(text); + Anticheat.getManager().getUserManager().execute(user, action, type); + } + } + + protected boolean isFunction(String string) { + return string.matches(FUNCTION_REGEX); + } + + protected boolean isVariableSet(String string) { + return string.matches(VARIABLE_SET_REGEX); + } + + @Override + public String toString() { + return type+"{"+string+"}"; + } +} diff --git a/src/main/resources/events.yml b/src/main/resources/events.yml index 620e3b4..44d9cc0 100644 --- a/src/main/resources/events.yml +++ b/src/main/resources/events.yml @@ -9,5 +9,5 @@ actions: - 'High: KICK' - 'Custom: COMMAND[ban &player;say hello world]' rules: - # if spider check is over 0, set level to 20. otherwise, do nothing - - 'Check:SPIDER > 0 ? Level=20 : none' \ No newline at end of file + # if spider check is less than 0, set level to 20. otherwise, do nothing + - 'Check_SPIDER < 0 ? Player.KICK : null' \ No newline at end of file