Fix for asynchronous permission / enabled checks (got lost somewhere).

This commit is contained in:
asofold 2012-08-09 19:05:52 +02:00
parent ade3a9276d
commit f679eb5d16
5 changed files with 56 additions and 27 deletions

View File

@ -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.<br>
* @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.<br>
* @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.<br>
* @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")

View File

@ -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;
}
}

View File

@ -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))

View File

@ -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());
}
}

View File

@ -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;
}