From e20fe539ef9bfe5dbb217c5dffad1e06c512c3d9 Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 17 Apr 2018 13:49:22 +0200 Subject: [PATCH] Implement support for ActionPenalties in ViolationData. --- .../nocheatplus/actions/ActionData.java | 2 +- .../nocheatplus/checks/ViolationData.java | 39 +++++++++++++++++-- .../nocheatplus/penalties/ActionPenalty.java | 22 +++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 NCPCore/src/main/java/fr/neatmonster/nocheatplus/penalties/ActionPenalty.java diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/actions/ActionData.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/actions/ActionData.java index f5a713c8..e2ac4888 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/actions/ActionData.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/actions/ActionData.java @@ -16,7 +16,7 @@ package fr.neatmonster.nocheatplus.actions; /** * This is data relevant for an action. - * @author mc_dev + * @author asofold * */ public interface ActionData { diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/ViolationData.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/ViolationData.java index 42e00cf4..aa623898 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/ViolationData.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/ViolationData.java @@ -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) 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 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() { diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/penalties/ActionPenalty.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/penalties/ActionPenalty.java new file mode 100644 index 00000000..0638e1ec --- /dev/null +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/penalties/ActionPenalty.java @@ -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 { + + private final Action action; + + public ActionPenalty(Action action) { + super(ViolationData.class); + this.action = action; + } + + @Override + public boolean apply(ViolationData input) { + input.executeAction(action); + return true; + } + +}