/* * AntiCheat for Bukkit. * Copyright (C) 2012-2014 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.gravitydevelopment.anticheat.util.rule; import net.gravitydevelopment.anticheat.AntiCheat; import net.gravitydevelopment.anticheat.check.CheckType; import net.gravitydevelopment.anticheat.util.User; import net.gravitydevelopment.anticheat.util.Group; import net.gravitydevelopment.anticheat.util.Utilities; import org.bukkit.GameMode; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; import java.util.SortedMap; import java.util.TreeMap; /** * 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:
*

*

* 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.gravitydevelopment.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.gravitydevelopment.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 = 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.gravitydevelopment.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()); map.put("player_group", user.getGroup() != null ? user.getGroup().getName().toLowerCase() : "low"); map.put("player_uuid", user.getUUID().toString()); map.put("player_gamemode", user.getPlayer().getGameMode().toString()); map.put("player_world", user.getPlayer().getWorld().getName()); map.put("player_health", user.getPlayer().getHealth()); for (CheckType t : CheckType.values()) { map.put("check_" + t.name().toLowerCase(), t.getUses(user.getUUID())); } return map; } protected void setVariable(String variable, String value, User user) { if (variable.equals("player_level") && Utilities.isInt(value)) { user.setLevel(Integer.parseInt(value)); } else if (variable.equals("player_group") && Utilities.isInt(value)) { for (Group group : AntiCheat.getManager().getConfiguration().getGroups().getGroups()) { if (group.getName().equalsIgnoreCase(value)) { user.setLevel(group.getLevel()); } } } else if (variable.equals("player_gamemode")) { try { GameMode mode = GameMode.valueOf(value); user.getPlayer().setGameMode(mode); } catch (IllegalArgumentException ex) { } } else if (variable.equals("player_health") && Utilities.isDouble(value)) { user.getPlayer().setHealth(Double.parseDouble(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 + "}"; } }