Use a provided interface to fill in input-specific penalties.

This seems to be more appropriate, for the case of multiple checks
triggering, but also for better performance, in case the check doesn't
even support input-specific penalties.
This commit is contained in:
asofold 2016-02-12 00:51:18 +01:00
parent 6d20b92ab6
commit 0915acdee6
2 changed files with 44 additions and 22 deletions

View File

@ -0,0 +1,15 @@
package fr.neatmonster.nocheatplus.actions.types.penalty;
/**
* Contain applicable penalty types that need to be handled outside of ViolationData.executeActions, for access by ViolationData.
*
* @author asofold
*
*/
public interface IPenaltyList {
/**
* Add an input-specific penalty.
* @param penalty
*/
public void addInputSpecificPenalty(InputSpecificPenalty penalty);
}

View File

@ -13,6 +13,7 @@ import fr.neatmonster.nocheatplus.actions.ParameterName;
import fr.neatmonster.nocheatplus.actions.types.CancelAction; import fr.neatmonster.nocheatplus.actions.types.CancelAction;
import fr.neatmonster.nocheatplus.actions.types.GenericLogAction; import fr.neatmonster.nocheatplus.actions.types.GenericLogAction;
import fr.neatmonster.nocheatplus.actions.types.penalty.CancelPenalty; import fr.neatmonster.nocheatplus.actions.types.penalty.CancelPenalty;
import fr.neatmonster.nocheatplus.actions.types.penalty.IPenaltyList;
import fr.neatmonster.nocheatplus.actions.types.penalty.InputSpecificPenalty; import fr.neatmonster.nocheatplus.actions.types.penalty.InputSpecificPenalty;
import fr.neatmonster.nocheatplus.actions.types.penalty.Penalty; import fr.neatmonster.nocheatplus.actions.types.penalty.Penalty;
import fr.neatmonster.nocheatplus.actions.types.penalty.PenaltyAction; import fr.neatmonster.nocheatplus.actions.types.penalty.PenaltyAction;
@ -60,7 +61,28 @@ public class ViolationData implements IViolationInfo, ActionData {
/** hasPlayerEffects returned true. */ /** hasPlayerEffects returned true. */
private ArrayList<Penalty> playerPenalties = null; private ArrayList<Penalty> playerPenalties = null;
/** hasInputSpecificEffects returned true. */ /** hasInputSpecificEffects returned true. */
private ArrayList<InputSpecificPenalty> inputSpecificPenalties = null; private final IPenaltyList penaltyList;
/**
* Instantiates a new violation data without syupport for input-specific penalties..
* <hr>
* This constructor must be thread-safe for checks that might be executed
* outside of the primary thread.
*
* @param check
* the check
* @param player
* the player
* @param vL
* the violation level
* @param addedVL
* the violation level added
* @param actions
* the actions
*/
public ViolationData(final Check check, final Player player, final double vL, final double addedVL, final ActionList actions) {
this(check, player, vL, addedVL, actions, null);
}
/** /**
* Instantiates a new violation data. * Instantiates a new violation data.
@ -78,14 +100,17 @@ public class ViolationData implements IViolationInfo, ActionData {
* the violation level added * the violation level added
* @param actions * @param actions
* the actions * the actions
* @param penaltyList
* IPenaltyList instances for filling in, or null to skip.
*/ */
public ViolationData(final Check check, final Player player, final double vL, final double addedVL, final ActionList actions) { public ViolationData(final Check check, final Player player, final double vL, final double addedVL, final ActionList actions, final IPenaltyList penaltyList) {
this.check = check; this.check = check;
this.player = player; this.player = player;
this.vL = vL; this.vL = vL;
this.addedVL = addedVL; this.addedVL = addedVL;
this.actions = actions; this.actions = actions;
this.applicableActions = actions.getActions(vL); // TODO: Consider storing applicableActions only if history wants it. this.applicableActions = actions.getActions(vL); // TODO: Consider storing applicableActions only if history wants it.
this.penaltyList = penaltyList;
boolean needsParameters = false; boolean needsParameters = false;
final ArrayList<Penalty> applicablePenalties = new ArrayList<Penalty>(); final ArrayList<Penalty> applicablePenalties = new ArrayList<Penalty>();
@ -133,31 +158,13 @@ public class ViolationData implements IViolationInfo, ActionData {
} }
playerPenalties.add(penalty); playerPenalties.add(penalty);
} }
if (penalty.hasInputSpecificEffects()) { if (penaltyList != null && penalty.hasInputSpecificEffects()) {
if (inputSpecificPenalties == null) { penaltyList.addInputSpecificPenalty((InputSpecificPenalty) penalty);
inputSpecificPenalties = new ArrayList<InputSpecificPenalty>();
}
inputSpecificPenalties.add((InputSpecificPenalty) penalty);
} }
} }
} }
} }
/**
* Call with a specific input (obviously not meant for vast number of
* penalties and/or a a vast number of calls with differing input types).
*
* @param input
*/
public void applyInputSpecificPenalties(final Object input) {
if (inputSpecificPenalties == null) {
return;
}
for (int i = 0; i < inputSpecificPenalties.size(); i++) {
inputSpecificPenalties.get(i).apply(input);
}
}
@Override @Override
public boolean willCancel() { public boolean willCancel() {
return willCancel; return willCancel;