Implement support for ActionPenalties in ViolationData.

This commit is contained in:
asofold 2018-04-17 13:49:22 +02:00
parent fd08d1be9b
commit e20fe539ef
3 changed files with 58 additions and 5 deletions

View File

@ -16,7 +16,7 @@ package fr.neatmonster.nocheatplus.actions;
/**
* This is data relevant for an action.
* @author mc_dev
* @author asofold
*
*/
public interface ActionData {

View File

@ -133,6 +133,7 @@ public class ViolationData implements IViolationInfo, ActionData {
needsParameters = true;
}
if (action instanceof PenaltyAction) {
// Note that penalty:action:... or action:penalty:... should be forbidden.
((PenaltyAction<ViolationData, ActionList>) action).evaluate(this.penaltyList);
}
}
@ -148,6 +149,13 @@ public class ViolationData implements IViolationInfo, ActionData {
return willCancel;
}
/**
* For penalty evaluation.
*/
public void forceCancel() {
willCancel = true;
}
/**
* Override willCancel.
*/
@ -193,10 +201,10 @@ public class ViolationData implements IViolationInfo, ActionData {
if (!penaltyList.isEmpty()) {
penaltyList.applyAllApplicablePenalties(player, true);
}
/*
* TODO: Concept for penalties running actions. E.g.
* penaltyList.applyAllApplicablePenalties(ViolationData, true);
*/
// PenaltyAction instances.
if (!penaltyList.isEmpty()) {
penaltyList.applyPenaltiesPrecisely(ViolationData.class, this, true);
}
} catch (final Exception e) {
StaticLog.logSevere(e);
// On exceptions cancel events.
@ -204,6 +212,29 @@ public class ViolationData implements IViolationInfo, ActionData {
}
}
/**
* Execute a single action with try-catch and cancel-on-exception. Intended
* for interoperability with penalties.
*
* @param action
*/
public void executeAction(final Action<ViolationData, ActionList> action) {
try {
// Statistics.
ViolationHistory.getHistory(player).log(check.getClass().getName(), addedVL);
final long time = System.currentTimeMillis() / 1000L;
// Execute actions, if history wants it. TODO: Consider storing applicableActions only if history wants it.
if (Check.getHistory(player).executeAction(this, action, time)) {
action.execute(this);
}
}
catch (final Exception e) {
StaticLog.logSevere(e);
// On exceptions cancel events.
willCancel = true;
}
}
@Override
@Deprecated
public boolean hasCancel() {

View File

@ -0,0 +1,22 @@
package fr.neatmonster.nocheatplus.penalties;
import fr.neatmonster.nocheatplus.actions.Action;
import fr.neatmonster.nocheatplus.actions.ActionList;
import fr.neatmonster.nocheatplus.checks.ViolationData;
public class ActionPenalty extends AbstractPenalty<ViolationData> {
private final Action<ViolationData, ActionList> action;
public ActionPenalty(Action<ViolationData, ActionList> action) {
super(ViolationData.class);
this.action = action;
}
@Override
public boolean apply(ViolationData input) {
input.executeAction(action);
return true;
}
}