From 82bcac2029b4967127d558a30e3d71cbc473e6e5 Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 7 Aug 2012 21:14:31 +0200 Subject: [PATCH] switch to enum --- CompatNoCheatPlus/cncp_lists.txt | 6 + .../asofold/bpl/cncp/CompatNoCheatPlus.java | 23 ++-- .../asofold/bpl/cncp/hooks/AbstractHook.java | 4 +- .../src/me/asofold/bpl/cncp/hooks/Hook.java | 5 +- .../cncp/hooks/generic/HookPlayerClass.java | 3 +- .../bpl/cncp/hooks/mcmmo/HookmcMMO.java | 40 +++--- .../asofold/bpl/cncp/hooks/ncp/CheckType.java | 51 ++++++++ .../asofold/bpl/cncp/hooks/ncp/NCPHook.java | 5 +- .../bpl/cncp/hooks/ncp/NCPHookManager.java | 120 +++++------------- 9 files changed, 131 insertions(+), 126 deletions(-) create mode 100644 CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/CheckType.java diff --git a/CompatNoCheatPlus/cncp_lists.txt b/CompatNoCheatPlus/cncp_lists.txt index 9ebd336..80f0498 100644 --- a/CompatNoCheatPlus/cncp_lists.txt +++ b/CompatNoCheatPlus/cncp_lists.txt @@ -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 --------------------------- diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/CompatNoCheatPlus.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/CompatNoCheatPlus.java index d4f0806..9f7ac61 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/CompatNoCheatPlus.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/CompatNoCheatPlus.java @@ -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); } diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/AbstractHook.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/AbstractHook.java index 2635977..cfb2bf8 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/AbstractHook.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/AbstractHook.java @@ -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; } diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/Hook.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/Hook.java index 2e2608a..43d9f4c 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/Hook.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/Hook.java @@ -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. diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/generic/HookPlayerClass.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/generic/HookPlayerClass.java index 588ae1a..ba6a92f 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/generic/HookPlayerClass.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/generic/HookPlayerClass.java @@ -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; diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/mcmmo/HookmcMMO.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/mcmmo/HookmcMMO.java index 394b156..0419f87 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/mcmmo/HookmcMMO.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/mcmmo/HookmcMMO.java @@ -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 cancelChecksBlockBreak = new HashMap(); - private static final Map cancelChecksBlockDamage = new HashMap(); - private static final Map cancelChecksDamage = new HashMap(); + private static final Map cancelChecksBlockBreak = new HashMap(); + private static final Map cancelChecksBlockDamage = new HashMap(); + private static final Map cancelChecksDamage = new HashMap(); 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 cancelChecks = new HashMap(); + private final Map cancelChecks = new HashMap(); @@ -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 cancelChecks){ + private final void setPlayer(final Entity entity, Map cancelChecks){ if (entity instanceof Player){ setPlayer((Player) entity, cancelChecks); } // no projectiles etc. } - private final void setPlayer(final Player player, Map cancelChecks){ + private final void setPlayer(final Player player, Map 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()); diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/CheckType.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/CheckType.java new file mode 100644 index 0000000..10e21bb --- /dev/null +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/CheckType.java @@ -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; + } +} diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHook.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHook.java index 7f08d13..64a58ef 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHook.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHook.java @@ -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).
* 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); } diff --git a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHookManager.java b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHookManager.java index 126adfc..12edcfe 100644 --- a/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHookManager.java +++ b/CompatNoCheatPlus/src/me/asofold/bpl/cncp/hooks/ncp/NCPHookManager.java @@ -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> hooksByChecks = new HashMap>(); + private static final Map> hooksByChecks = new HashMap>(); /* ----------------------------------------------------------------- */ /* 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 hooks = hooksByChecks.get(checkId); + private static void addToMapping(CheckType checkType, NCPHook hook) { + List hooks = hooksByChecks.get(checkType); if (hooks == null){ hooks = new ArrayList(); 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 rem = new LinkedList(); - for (Integer checkId : hooksByChecks.keySet()){ + List rem = new LinkedList(); + for (CheckType checkId : hooksByChecks.keySet()){ List 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 hooks) { + private static final boolean applyHooks(final CheckType checkType, final Player player, final List 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 hooksCheck = hooksByChecks.get(checkId); + final List 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 hooksGroup= hooksByChecks.get(groupId); + if (checkType.group != null){ + final List 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 hooksAll = hooksByChecks.get(ALL); + final List 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);