switch to enum

This commit is contained in:
asofold 2012-08-07 21:14:31 +02:00
parent 447ef2f4a9
commit 82bcac2029
9 changed files with 131 additions and 126 deletions

View File

@ -18,6 +18,12 @@ STACK
? another sequence number (for standard events)
*** ADD MORE GENERIC HOOKS
!add check type and permission hooks, also for worldguard regions.
*** ADAPT TO FINAL API FOR NCP
VERSION HISTORY
---------------------------

View File

@ -12,6 +12,7 @@ import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.NewConfig;
import me.asofold.bpl.cncp.hooks.Hook;
import me.asofold.bpl.cncp.hooks.generic.HookPlayerClass;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import me.asofold.bpl.cncp.hooks.ncp.NCPHook;
import me.asofold.bpl.cncp.hooks.ncp.NCPHookManager;
import me.asofold.bpl.cncp.setttings.Settings;
@ -103,7 +104,7 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
return false;
}
if (enabled) registerListeners(hook);
Integer[] checkIds = hook.getCheckSpec();
CheckType[] checkIds = hook.getCheckTypes();
NCPHookManager.addHook(checkIds, hook); // This logs the message.
return true;
}
@ -249,18 +250,18 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
// TODO: This will be replaced by NCP invoking NCPHookManager.should... directly.
final Integer checkId;
final CheckType checkId;
// horrible :)
if (event instanceof SurvivalFlyEvent) checkId = NCPHookManager.MOVING_SURVIVALFLY;
else if (event instanceof CreativeFlyEvent) checkId = NCPHookManager.MOVING_CREATIVEFLY;
else if (event instanceof NoFallEvent) checkId = NCPHookManager.MOVING_NOFALL;
else if (event instanceof FastBreakEvent) checkId = NCPHookManager.BLOCKBREAK_FASTBREAK;
else if (event instanceof NoSwingEvent) checkId = NCPHookManager.BLOCKBREAK_NOSWING;
else if (event instanceof DirectionEvent) checkId = NCPHookManager.BLOCKBREAK_DIRECTION;
else if (event instanceof SpeedEvent) checkId = NCPHookManager.FIGHT_SPEED;
else if (event instanceof AngleEvent) checkId = NCPHookManager.FIGHT_ANGLE;
else checkId = NCPHookManager.UNKNOWN;
if (event instanceof SurvivalFlyEvent) checkId = CheckType.MOVING_SURVIVALFLY;
else if (event instanceof CreativeFlyEvent) checkId = CheckType.MOVING_CREATIVEFLY;
else if (event instanceof NoFallEvent) checkId = CheckType.MOVING_NOFALL;
else if (event instanceof FastBreakEvent) checkId = CheckType.BLOCKBREAK_FASTBREAK;
else if (event instanceof NoSwingEvent) checkId = CheckType.BLOCKBREAK_NOSWING;
else if (event instanceof DirectionEvent) checkId = CheckType.BLOCKBREAK_DIRECTION;
else if (event instanceof SpeedEvent) checkId = CheckType.FIGHT_SPEED;
else if (event instanceof AngleEvent) checkId = CheckType.FIGHT_ANGLE;
else checkId = CheckType.UNKNOWN;
if (NCPHookManager.shouldCancelVLProcessing(checkId, event.getPlayer())) event.setCancelled(true);
}

View File

@ -1,5 +1,7 @@
package me.asofold.bpl.cncp.hooks;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
@ -13,7 +15,7 @@ import org.bukkit.event.Listener;
public abstract class AbstractHook implements Hook{
@Override
public Integer[] getCheckSpec() {
public CheckType[] getCheckTypes() {
// Handle all CheckEvents (improbable).
return null;
}

View File

@ -1,5 +1,6 @@
package me.asofold.bpl.cncp.hooks;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import me.asofold.bpl.cncp.hooks.ncp.NCPHook;
import org.bukkit.event.Listener;
@ -16,9 +17,9 @@ import org.bukkit.event.Listener;
public interface Hook extends NCPHook{
/**
* The check ids to register for, see NCPHookManager for reference, you can use ALL,
* The check types to register for, see NCPHookManager for reference, you can use ALL,
*/
public Integer[] getCheckSpec();
public CheckType[] getCheckTypes();
/**
* Get listener instances to be registered with cncp.

View File

@ -5,6 +5,7 @@ import java.util.HashSet;
import java.util.Set;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import org.bukkit.entity.Player;
@ -54,7 +55,7 @@ public final class HookPlayerClass extends AbstractHook {
@Override
public final boolean onCheckFailure(final Integer groupId, final Integer checkId, final Player player) {
public final boolean onCheckFailure(CheckType checkType, final Player player) {
if (exemptAll && !player.getClass().getSimpleName().equals(playerClassName)) return true;
else {
if (classNames.isEmpty()) return false;

View File

@ -4,7 +4,7 @@ import java.util.HashMap;
import java.util.Map;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.ncp.NCPHookManager;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import me.asofold.bpl.cncp.utils.PluginGetter;
import org.bukkit.entity.Entity;
@ -20,16 +20,16 @@ import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
public final class HookmcMMO extends AbstractHook implements Listener {
private static final Map<Integer, Integer> cancelChecksBlockBreak = new HashMap<Integer, Integer>();
private static final Map<Integer, Integer> cancelChecksBlockDamage = new HashMap<Integer, Integer>();
private static final Map<Integer, Integer> cancelChecksDamage = new HashMap<Integer, Integer>();
private static final Map<CheckType, Integer> cancelChecksBlockBreak = new HashMap<CheckType, Integer>();
private static final Map<CheckType, Integer> cancelChecksBlockDamage = new HashMap<CheckType, Integer>();
private static final Map<CheckType, Integer> cancelChecksDamage = new HashMap<CheckType, Integer>();
static{
cancelChecksBlockBreak.put(NCPHookManager.BLOCKBREAK_NOSWING, 1);
cancelChecksBlockBreak.put(NCPHookManager.BLOCKBREAK_FASTBREAK, 2);
cancelChecksBlockDamage.put(NCPHookManager.BLOCKBREAK_FASTBREAK, 1);
cancelChecksBlockBreak.put(CheckType.BLOCKBREAK_NOSWING, 1);
cancelChecksBlockBreak.put(CheckType.BLOCKBREAK_FASTBREAK, 2);
cancelChecksBlockDamage.put(CheckType.BLOCKBREAK_FASTBREAK, 1);
cancelChecksDamage.put(NCPHookManager.FIGHT_ANGLE, 1);
cancelChecksDamage.put(NCPHookManager.FIGHT_SPEED, 1);
cancelChecksDamage.put(CheckType.FIGHT_ANGLE, 1);
cancelChecksDamage.put(CheckType.FIGHT_SPEED, 1);
}
public HookmcMMO(){
@ -42,7 +42,7 @@ public final class HookmcMMO extends AbstractHook implements Listener {
private String cancel = null;
private long cancelTicks = 0;
private final Map<Integer, Integer> cancelChecks = new HashMap<Integer, Integer>();
private final Map<CheckType, Integer> cancelChecks = new HashMap<CheckType, Integer>();
@ -57,10 +57,10 @@ public final class HookmcMMO extends AbstractHook implements Listener {
}
@Override
public Integer[] getCheckSpec() {
return new Integer[]{
NCPHookManager.BLOCKBREAK_FASTBREAK, NCPHookManager.BLOCKBREAK_NOSWING,
NCPHookManager.FIGHT_ANGLE, NCPHookManager.FIGHT_SPEED,
public CheckType[] getCheckTypes() {
return new CheckType[]{
CheckType.BLOCKBREAK_FASTBREAK, CheckType.BLOCKBREAK_NOSWING,
CheckType.FIGHT_ANGLE, CheckType.FIGHT_SPEED,
};
}
@ -70,14 +70,14 @@ public final class HookmcMMO extends AbstractHook implements Listener {
return new Listener[]{this, fetch};
}
private final void setPlayer(final Entity entity, Map<Integer, Integer> cancelChecks){
private final void setPlayer(final Entity entity, Map<CheckType, Integer> cancelChecks){
if (entity instanceof Player){
setPlayer((Player) entity, cancelChecks);
}
// no projectiles etc.
}
private final void setPlayer(final Player player, Map<Integer, Integer> cancelChecks){
private final void setPlayer(final Player player, Map<CheckType, Integer> cancelChecks){
cancel = player.getName();
cancelTicks = player.getTicksLived();
this.cancelChecks.clear();
@ -120,7 +120,7 @@ public final class HookmcMMO extends AbstractHook implements Listener {
@Override
public final boolean onCheckFailure(final Integer groupId, final Integer checkId, final Player player) {
public final boolean onCheckFailure(CheckType checkType, final Player player) {
// System.out.println("[cncp] Handle event: " + event.getEventName());
if (cancel == null){
// System.out.println("[cncp] Return on cancel == null: "+event.getPlayer().getName());
@ -135,15 +135,15 @@ public final class HookmcMMO extends AbstractHook implements Listener {
cancel = null;
}
else{
final Integer n = cancelChecks.get(checkId);
final Integer n = cancelChecks.get(checkType);
if (n == null){
// System.out.println("[cncp] Expired("+check+"): "+event.getPlayer().getName());
return false;
}
else if (n > 0){
// System.out.println("Check with n = "+n);
if (n == 1) cancelChecks.remove(checkId);
else cancelChecks.put(checkId, n - 1);
if (n == 1) cancelChecks.remove(checkType);
else cancelChecks.put(checkType, n - 1);
}
// else: allow arbitrary numbers
// System.out.println("[cncp] Cancel: "+event.getPlayer().getName());

View File

@ -0,0 +1,51 @@
package me.asofold.bpl.cncp.hooks.ncp;
public enum CheckType {
// GENERAL
/**
* Use to register for all checks.
*/
ALL,
/**
* Do not use to register, only for internals / compatibility issues,
* it might be passed to NCPHook.onCheckFailure.
*/
UNKNOWN,
// MOVING
MOVING,
MOVING_NOFALL(MOVING),
MOVING_SURVIVALFLY(MOVING),
MOVING_CREATIVEFLY(MOVING),
// FIGHT
FIGHT,
FIGHT_SPEED(FIGHT),
FIGHT_ANGLE(FIGHT),
// BLOCKBREAK
BLOCKBREAK,
BLOCKBREAK_FASTBREAK(BLOCKBREAK),
BLOCKBREAK_NOSWING(BLOCKBREAK),
BLOCKBREAK_DIRECTION(BLOCKBREAK),
// ....
; // end of members.
/**
* The check group, null if it is a group itself.
*/
public final CheckType group;
private CheckType(){
group = null;
}
private CheckType(CheckType group){
this.group = group;
}
}

View File

@ -28,12 +28,11 @@ public interface NCPHook{
* This is the minimal interface, one should probably add specific information
* like (target) locations and VL, but with this a lot is possible already (cncp).<br>
* See AbstractNCPHook for future questions.
* @param groupId Check group id.
* @param checkId Check id for the specific check (for instance: NCPHookManager.MOVING_NOFALL).
* @param checkType the check that failed.
* @param player The player that failed the check.
* @return If to cancel the check failure processing.
*/
public boolean onCheckFailure(Integer groupId, Integer checkId, Player player);
public boolean onCheckFailure(CheckType checkType, Player player);
}

View File

@ -26,51 +26,6 @@ import org.bukkit.entity.Player;
public final class NCPHookManager {
/* ----------------------------------------------------------------------------*
/*
* Ids for groups and checks:
*
* TODO: could change to Enums with int values.
*
* A check group starts at thousands and covers all
* ids till next thousand - 1, for instance 1000 to 1999.
*
* This is important for also being able to check the check group for the check.
*/
// GENERAL
/**
* Use to register for all checkfailures.
*/
public static final Integer ALL = 0;
/**
* Do not use to register, only for internals / compatibility issues,
* it might be passed to NCPHook.onCheckFailure.
*/
public static final Integer UNKNOWN = -1;
// MOVING
public static final Integer MOVING = 1000;
public static final Integer MOVING_NOFALL = 1001;
public static final Integer MOVING_SURVIVALFLY = 1002;
public static final Integer MOVING_CREATIVEFLY = 1003;
// FIGHT
public static final Integer FIGHT = 2000;
public static final Integer FIGHT_SPEED = 2001;
public static final Integer FIGHT_ANGLE = 2002;
// BLOCKBREAK
public static final Integer BLOCKBREAK = 3000;
public static final Integer BLOCKBREAK_FASTBREAK = 3001;
public static final Integer BLOCKBREAK_NOSWING = 3002;
public static final Integer BLOCKBREAK_DIRECTION = 3003;
// BLOCKPLACE
// ...
/* ----------------------------------------------------------------------- */
/* Internal data: */
@ -87,7 +42,7 @@ public final class NCPHookManager {
/**
* Mapping the check ids to the hooks.
*/
private static final Map<Integer, List<NCPHook>> hooksByChecks = new HashMap<Integer, List<NCPHook>>();
private static final Map<CheckType, List<NCPHook>> hooksByChecks = new HashMap<CheckType, List<NCPHook>>();
/* ----------------------------------------------------------------- */
/* Internals: manage hooks internally */
@ -118,38 +73,28 @@ public final class NCPHookManager {
return id;
}
/**
* Get the Id of the group for a given check id. (If the given id is a group id it will return the same value but a different object).
* @param checkId
* @return
*/
private final static Integer getGroupId(final Integer checkId){
return (checkId.intValue() / 1000) * 1000;
}
/**
* Add hook to the hooksByChecks mappings, for the check id and if different group id.
* assumes that the hook already has been registered in the allHooks map.
* @param checkId
* @param checkType
* @param hook
*/
private static void addToMappings(Integer checkId, NCPHook hook) {
Integer groupId = getGroupId(checkId);
addToMapping(checkId, hook);
if (checkId.intValue() != groupId.intValue()) addToMapping(groupId, hook);
private static void addToMappings(CheckType checkType, NCPHook hook) {
addToMapping(checkType, hook);
if (checkType.group != null) addToMapping(checkType.group, hook);
}
/**
*
* Add to the mapping for given check type (only).
* @param checkId
* @param hook
*/
private static void addToMapping(Integer checkId, NCPHook hook) {
List<NCPHook> hooks = hooksByChecks.get(checkId);
private static void addToMapping(CheckType checkType, NCPHook hook) {
List<NCPHook> hooks = hooksByChecks.get(checkType);
if (hooks == null){
hooks = new ArrayList<NCPHook>();
hooks.add(hook);
hooksByChecks.put(checkId, hooks);
hooksByChecks.put(checkType, hooks);
}
else if (!hooks.contains(hook)) hooks.add(hook);
}
@ -161,14 +106,14 @@ public final class NCPHookManager {
*/
private static void removeFromMappings(NCPHook hook, Integer hookId) {
allHooks.remove(hookId);
List<Integer> rem = new LinkedList<Integer>();
for (Integer checkId : hooksByChecks.keySet()){
List<CheckType> rem = new LinkedList<CheckType>();
for (CheckType checkId : hooksByChecks.keySet()){
List<NCPHook> hooks = hooksByChecks.get(checkId);
if (hooks.remove(hook)){
if (hooks.isEmpty()) rem.add(checkId);
}
}
for (Integer checkId : rem){
for (CheckType checkId : rem){
hooksByChecks.remove(checkId);
}
}
@ -185,13 +130,13 @@ public final class NCPHookManager {
Bukkit.getLogger().info("[NoCheatPlus/Compat] Removed hook: " + getHookDescription(hook));
}
private static final void logHookFailure(final Integer groupId, final Integer checkId, final Player player, final NCPHook hook, final Throwable t){
private static final void logHookFailure(final CheckType checkType, final Player player, final NCPHook hook, final Throwable t){
// TODO: might accumulate failure rate and only log every so and so seconds or disable hook if spamming (leads to ncp spam though)?
final StringBuilder builder = new StringBuilder(1024);
builder.append("[NoCheatPlus/Compat] Hook " + getHookDescription(hook) + " encountered an unexpected exception:\n");
builder.append("Processing: ");
if (checkId.intValue() != groupId.intValue()) builder.append("Group " + groupId + " ");
builder.append("Check " + checkId);
if (checkType.group != null) builder.append("Group " + checkType.group + " ");
builder.append("Check " + checkType);
builder.append(" Player " + player.getName());
builder.append("\n");
builder.append("Exception (" + t.getClass().getSimpleName() + "): " + t.getMessage() + "\n");
@ -202,15 +147,15 @@ public final class NCPHookManager {
Bukkit.getLogger().severe(builder.toString());
}
private static final boolean applyHooks(final Integer groupId, final Integer checkId, final Player player, final List<NCPHook> hooks) {
private static final boolean applyHooks(final CheckType checkType, final Player player, final List<NCPHook> hooks) {
for (int i = 0; i < hooks.size(); i ++){
final NCPHook hook = hooks.get(i);
try{
if (hook.onCheckFailure(groupId, checkId, player)) return true;
if (hook.onCheckFailure(checkType, player)) return true;
}
catch (Throwable t){
// TODO: maybe distinguish some exceptions here (Interrupted ?).
logHookFailure(groupId, checkId, player, hook, t);
logHookFailure(checkType, player, hook, t);
}
}
return false;
@ -228,30 +173,29 @@ public final class NCPHookManager {
* @param player The player that fails the check.
* @return if to cancel the VL processing.
*/
public static final boolean shouldCancelVLProcessing(final Integer checkId, final Player player){
public static final boolean shouldCancelVLProcessing(final CheckType checkType, final Player player){
// checks for hooks registered for all events, only for the group and specifically for the check.
// A Paradigm could be to return true as soon as one hook has returned true.
final Integer groupId = getGroupId(checkId);
// Most specific:
final List<NCPHook> hooksCheck = hooksByChecks.get(checkId);
final List<NCPHook> hooksCheck = hooksByChecks.get(checkType);
if (hooksCheck != null){
if (applyHooks(groupId, checkId, player, hooksCheck)) return true;
if (applyHooks(checkType, player, hooksCheck)) return true;
}
// Group:
if (checkId.intValue() != groupId.intValue()){
final List<NCPHook> hooksGroup= hooksByChecks.get(groupId);
if (checkType.group != null){
final List<NCPHook> hooksGroup= hooksByChecks.get(checkType);
if (hooksCheck != null){
if (applyHooks(groupId, checkId, player, hooksGroup)) return true;
if (applyHooks(checkType, player, hooksGroup)) return true;
}
}
// general (all):
final List<NCPHook> hooksAll = hooksByChecks.get(ALL);
final List<NCPHook> hooksAll = hooksByChecks.get(CheckType.ALL);
if (hooksAll != null){
if (applyHooks(groupId, checkId, player, hooksAll)) return true;
if (applyHooks(checkType, player, hooksAll)) return true;
}
return false;
@ -263,13 +207,13 @@ public final class NCPHookManager {
/**
* Register a hook for a specific check id (all, group, or an individual check).
* @param checkId
* @param checkType
* @param hook
* @return An id to identify the hook, will return the existing id if the hook was already present somewhere.
*/
public static Integer addHook(Integer checkId, NCPHook hook){
public static Integer addHook(CheckType checkType, NCPHook hook){
Integer hookId = getId(hook);
addToMappings(checkId, hook);
addToMappings(checkType, hook);
logHookAdded(hook);
return hookId;
}
@ -280,10 +224,10 @@ public final class NCPHookManager {
* @param hook
* @return
*/
public static Integer addHook(Integer[] checkIds, NCPHook hook){
if (checkIds == null) checkIds = new Integer[]{NCPHookManager.ALL};
public static Integer addHook(CheckType[] checkIds, NCPHook hook){
if (checkIds == null) checkIds = new CheckType[]{CheckType.ALL};
Integer hookId = getId(hook);
for (Integer checkId : checkIds){
for (CheckType checkId : checkIds){
addToMappings(checkId, hook);
}
logHookAdded(hook);