Add Extended information about violations to hooks.

This commit is contained in:
asofold 2012-10-17 18:31:44 +02:00
parent 3286bdd0dd
commit 77216f0799
6 changed files with 90 additions and 10 deletions

View File

@ -0,0 +1,29 @@
package fr.neatmonster.nocheatplus.checks;
/**
* Access interface for extended information about violations.
* @author mc_dev
*
*/
public interface IViolationInfo {
/**
* Get the violation level just added by this violation.
* @return
*/
public double getAddedVl();
/**
* Get the total violation level the player has right now. This is not the value shown with "/ncp info <player>", but the value used for actions.
* @return
*/
public double getTotalVl();
/**
* Check if the actions contain a cancel action.
* @return
*/
boolean hasCancel();
/**
* Check if any of the actions needs parameters.
* @return If true, actions are likely to contian command or logging actions.
*/
boolean needsParameters();
}

View File

@ -24,7 +24,7 @@ import fr.neatmonster.nocheatplus.actions.types.CancelAction;
*
* @author asofold
*/
public class ViolationData {
public class ViolationData implements IViolationInfo{
/** The actions to be executed. */
public final ActionList actions;
@ -118,6 +118,7 @@ public class ViolationData {
* Check if the actions contain a cancel.
* @return
*/
@Override
public boolean hasCancel(){
for (final Action action : applicableActions){
if (action instanceof CancelAction) return true;
@ -151,8 +152,18 @@ public class ViolationData {
if (parameters != null) parameters.put(parameterName, value);
}
@Override
public boolean needsParameters() {
return parameters != null;
}
@Override
public double getAddedVl() {
return addedVL;
}
@Override
public double getTotalVl() {
return vL;
}
}

View File

@ -1,5 +1,10 @@
package fr.neatmonster.nocheatplus.hooks;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.IViolationInfo;
/*
* MMP"""""""MM dP dP dP
* M' .mmmm MM 88 88 88
@ -24,4 +29,23 @@ package fr.neatmonster.nocheatplus.hooks;
*
* @author asofold
*/
public abstract class AbstractNCPHook implements NCPHook {}
public abstract class AbstractNCPHook implements NCPHook {
/**
*
* @deprecated See new signature in NCPHook.
* @param checkType
* @param player
* @return
*/
public boolean onCheckFailure(CheckType checkType, Player player){
// Implemented because of API change.
return false;
}
@Override
public boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) {
// Kept for compatibility reasons.
return onCheckFailure(checkType, player);
}
}

View File

@ -0,0 +1,10 @@
package fr.neatmonster.nocheatplus.hooks;
/**
* Interface to indicate an object is just used to collect stats,
* it will get the violations first and can not cancel vl-processing.
* @author mc_dev
*
*/
public class IStats {
}

View File

@ -3,6 +3,7 @@ package fr.neatmonster.nocheatplus.hooks;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.IViolationInfo;
/*
* M"""""""`YM MM'""""'YMM MM"""""""`YM M""MMMMM""MM dP
@ -47,7 +48,9 @@ public interface NCPHook {
* the check that failed
* @param player
* the player that failed the check
* @param info
* Extended information about the violations.
* @return if we need to cancel the check failure processing
*/
public boolean onCheckFailure(CheckType checkType, Player player);
public boolean onCheckFailure(CheckType checkType, Player player, IViolationInfo info);
}

View File

@ -14,6 +14,7 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.IViolationInfo;
import fr.neatmonster.nocheatplus.checks.ViolationData;
/*
@ -103,8 +104,10 @@ public final class NCPHookManager {
*/
private static void addToMapping(final CheckType checkType, final NCPHook hook) {
final List<NCPHook> hooks = hooksByChecks.get(checkType);
if (!hooks.contains(hook))
hooks.add(hook);
if (!hooks.contains(hook)){
if (hook instanceof IStats) hooks.add(0, hook);
else hooks.add(hook);
}
}
/**
@ -160,11 +163,11 @@ public final class NCPHookManager {
* the hooks
* @return true, if a hook as decided to cancel the VL processing
*/
private static final boolean applyHooks(final CheckType checkType, final Player player, final List<NCPHook> hooks) {
private static final boolean applyHooks(final CheckType checkType, final Player player, final IViolationInfo info, final List<NCPHook> hooks) {
for (int i = 0; i < hooks.size(); i++) {
final NCPHook hook = hooks.get(i);
try {
if (hook.onCheckFailure(checkType, player))
if (hook.onCheckFailure(checkType, player, info))
return true;
} catch (final Throwable t) {
// TODO: maybe distinguish some exceptions here (interrupted ?).
@ -411,11 +414,11 @@ public final class NCPHookManager {
if (!hooksCheck.isEmpty()){
if (APIUtils.needsSynchronization(type)){
synchronized (hooksCheck) {
return applyHooks(type, violationData.player, hooksCheck);
return applyHooks(type, violationData.player, violationData, hooksCheck);
}
}
else{
return applyHooks(type, violationData.player, hooksCheck);
return applyHooks(type, violationData.player, violationData, hooksCheck);
}
}
return false;