From f679eb5d16dfaa28e7ffc4afbd69a60f954b0611 Mon Sep 17 00:00:00 2001 From: asofold Date: Thu, 9 Aug 2012 19:05:52 +0200 Subject: [PATCH] Fix for asynchronous permission / enabled checks (got lost somewhere). --- .../neatmonster/nocheatplus/checks/Check.java | 39 +++++++++---------- .../nocheatplus/checks/ViolationData.java | 14 +++++++ .../nocheatplus/checks/chat/ChatListener.java | 6 +-- .../nocheatplus/checks/chat/Color.java | 15 ++++++- .../nocheatplus/checks/chat/NoPwnage.java | 9 ++++- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/checks/Check.java b/src/fr/neatmonster/nocheatplus/checks/Check.java index 27d1f56d..175c8711 100644 --- a/src/fr/neatmonster/nocheatplus/checks/Check.java +++ b/src/fr/neatmonster/nocheatplus/checks/Check.java @@ -90,7 +90,7 @@ public abstract class Check { } /** The type. */ - private final CheckType type; + protected final CheckType type; /** * Instantiates a new check. @@ -102,24 +102,6 @@ public abstract class Check { this.type = type; } - /** - * Execute actions in a thread safe manner if isMainThread is set to false.
- * @param player - * @param VL - * @param actions - * @param isMainThread - * @return - */ - public boolean executeActionsThreadSafe(final Player player, final double VL, final ActionList actions, final boolean isMainThread){ - if (isMainThread){ - // Just execute. - return executeActions(player, VL, actions); - } - else { - return executeActionsThreadSafe(player, VL, actions); - } - } - /** * Execute actions in a thread safe manner.
* @param player @@ -127,12 +109,22 @@ public abstract class Check { * @param actions * @return */ - public boolean executeActionsThreadSafe(final Player player, final double VL, final ActionList actions){ + public boolean executeActionsThreadSafe(final Player player, final double VL, final ActionList actions, final String bypassPermission){ // Sync it into the main thread by using an event. - final ExecuteActionsEvent event = new ExecuteActionsEvent(new ViolationData(this, player, VL, actions)); + return executeActionsThreadSafe(new ViolationData(this, player, VL, actions, bypassPermission)); + } + + /** + * Execute actions in a thread safe manner.
+ * @param violationData + * @return + */ + public boolean executeActionsThreadSafe(final ViolationData violationData){ + final ExecuteActionsEvent event = new ExecuteActionsEvent(violationData); Bukkit.getPluginManager().callEvent(event); return event.getCancel(); } + /** * Convenience method. @@ -157,6 +149,11 @@ public abstract class Check { boolean special = false; final Player player = violationData.player; + // Check a bypass permission: + if (violationData.bypassPermission != null){ + if (player.hasPermission(violationData.bypassPermission)) return false; + } + // final Object configFactory = type.getConfig().getDeclaredMethod("getConfig", Player.class).invoke(null, player); // final Object dataFactory = type.getData().getDeclaredMethod("getData", Player.class).invoke(null, player); // final ActionList actionList = (ActionList) type.getConfig().getDeclaredField(type.getName() + "Actions") diff --git a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java index d3db95ba..80756239 100644 --- a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java +++ b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java @@ -16,12 +16,26 @@ public class ViolationData { public final Player player; public final double VL; public final ActionList actions; + public final String bypassPermission; public ViolationData(final Check check, final Player player, final double VL, final ActionList actions){ + this(check, player, VL, actions, null); + } + + /** + * + * @param check + * @param player + * @param VL + * @param actions + * @param bypassPermission Permission to bypass the execution, if not null. + */ + public ViolationData(final Check check, final Player player, final double VL, final ActionList actions, final String bypassPermission){ this.check = check; this.player = player; this.VL = VL; this.actions = actions; + this.bypassPermission = bypassPermission; } } diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java b/src/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java index 188f8d06..461f038e 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/ChatListener.java @@ -52,8 +52,7 @@ public class ChatListener implements Listener { final Player player = event.getPlayer(); // First the color check. - if (color.isEnabled(player)) - event.setMessage(color.check(player, event.getMessage(), false)); + event.setMessage(color.check(player, event.getMessage(), false)); // Then the no pwnage check. if (noPwnage.check(player, event, false)) @@ -106,8 +105,7 @@ public class ChatListener implements Listener { } // First the color check. - if (color.isEnabled(player)) - event.setMessage(color.check(player, event.getMessage(), true)); + event.setMessage(color.check(player, event.getMessage(), true)); // Then the no pwnage check. if (noPwnage.check(player, event, true)) diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/Color.java b/src/fr/neatmonster/nocheatplus/checks/chat/Color.java index ed373ef9..4b82db12 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/Color.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/Color.java @@ -2,6 +2,7 @@ package fr.neatmonster.nocheatplus.checks.chat; import org.bukkit.entity.Player; +import fr.neatmonster.nocheatplus.actions.types.ActionList; import fr.neatmonster.nocheatplus.checks.Check; import fr.neatmonster.nocheatplus.checks.CheckType; @@ -36,6 +37,13 @@ public class Color extends Check { * @return the string */ public String check(final Player player, final String message, final boolean isMainThread) { + + if (isMainThread && !isEnabled(player)) return message; + + final ChatConfig cc = ChatConfig.getConfig(player); + + if (!isMainThread && !cc.isEnabled(this.type)) return message; // Leave out the permission check. + final ChatData data = ChatData.getData(player); synchronized(data){ // [keep related to ChatData/NoPwnage/Color used lock.] // If the message contains colors... @@ -44,7 +52,7 @@ public class Color extends Check { data.colorVL++; // Find out if we need to remove the colors or not. - if (executeActionsThreadSafe(player, data.colorVL, ChatConfig.getConfig(player).colorActions, isMainThread)) + if (executeActionsThreadSafe(player, data.colorVL, cc.colorActions, isMainThread)) // Remove color codes. message.replaceAll("\302\247.", "").replaceAll("\247.", ""); } @@ -52,4 +60,9 @@ public class Color extends Check { return message; } + + private boolean executeActionsThreadSafe(Player player, double VL, ActionList actions, boolean isMainThread) { + if (isMainThread) return super.executeActions(player, VL, actions); + else return super.executeActionsThreadSafe(player, VL, actions, type.getPermission()); + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java b/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java index a046053f..8f23b438 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java @@ -131,6 +131,8 @@ public class NoPwnage extends Check { final ChatConfig cc = ChatConfig.getConfig(player); final ChatData data = ChatData.getData(player); + if (!isMainThread && !cc.isEnabled(this.type)) return false; + synchronized(data){ // [keep related to ChatData/NoPwnage/Color used lock.] return unsafeCheck(player, event, isMainThread, cc, data); } @@ -290,7 +292,12 @@ public class NoPwnage extends Check { * @return */ public final boolean executeActionsThreadSafe(final Player player, double VL, ActionList actions, boolean isMainThread){ - final boolean cancel = super.executeActionsThreadSafe(player, VL, actions, isMainThread); + final boolean cancel; + if (isMainThread) cancel = super.executeActions(player, VL, actions); + else{ + // Permission check is done in the main thread (!). + cancel = super.executeActionsThreadSafe(player, VL, actions, type.getPermission()); + } if (cancel) ChatData.getData(player).clearNoPwnageData(); return cancel; }