Comments + formatting + spaces.

This commit is contained in:
asofold 2014-11-05 22:57:33 +01:00
parent 1f2f18a748
commit 9de6061a9e
3 changed files with 124 additions and 113 deletions

View File

@ -45,36 +45,36 @@ public abstract class Action <D extends ActionData, L extends AbstractActionList
*
* @param violationData
* the violation data
* @return true, if successful
* @return true, if to cancel players actions, false otherwise. [SUBJECT TO REMOVAL]
*/
public abstract boolean execute(final D violationData);
/**
* Check if parameters are needed at all for faster processing.
* @return
*/
public boolean needsParameters(){
return false;
public boolean needsParameters() {
return false;
}
/**
* Indicates that the action will be executed in any case (delay, repeat).
* executed at all.
*
* @return
*/
public boolean executesAlways() {
return delay == 0 && repeat == 0;
}
/**
* Indicates that the action will be executed in any case (delay, repeat).
* executed at all.
*
* @return
*/
public boolean executesAlways() {
return delay == 0 && repeat == 0;
}
/**
* Get an optimized copy, given the config in use. The default implementation returns this instance.<br>
* TODO: "Copy" does not match this.
* @param config
* @param threshold
* @return Can return this (unchanged), null (not to be executed ever) or a new instance (changed, optimized).
*/
public Action<D, L> getOptimizedCopy(final ConfigFileWithActions<D, L> config, final Integer threshold) {
return this;
}
/**
* Get an optimized copy, given the config in use. The default implementation returns this instance.<br>
* TODO: "Copy" does not match this.
* @param config
* @param threshold
* @return Can return this (unchanged), null (not to be executed ever) or a new instance (changed, optimized).
*/
public Action<D, L> getOptimizedCopy(final ConfigFileWithActions<D, L> config, final Integer threshold) {
return this;
}
}

View File

@ -22,6 +22,7 @@ import fr.neatmonster.nocheatplus.utilities.TickTask;
*/
public abstract class Check implements MCAccessHolder{
// TODO: Do these get cleaned up ?
/** The execution histories of each check. */
protected static Map<String, ExecutionHistory> histories = new HashMap<String, ExecutionHistory>();
@ -33,8 +34,9 @@ public abstract class Check implements MCAccessHolder{
* @return the history
*/
protected static ExecutionHistory getHistory(final Player player) {
if (!histories.containsKey(player.getName()))
if (!histories.containsKey(player.getName())) {
histories.put(player.getName(), new ExecutionHistory());
}
return histories.get(player.getName());
}
@ -101,7 +103,7 @@ public abstract class Check implements MCAccessHolder{
* the violation data
* @return true, if the event should be cancelled
*/
protected boolean executeActions(final ViolationData violationData){
protected boolean executeActions(final ViolationData violationData) {
return executeActions(violationData, true);
}
@ -116,17 +118,20 @@ public abstract class Check implements MCAccessHolder{
protected boolean executeActions(final ViolationData violationData, final boolean isMainThread) {
// Dispatch the VL processing to the hook manager (now thread safe).
if (NCPHookManager.shouldCancelVLProcessing(violationData))
if (NCPHookManager.shouldCancelVLProcessing(violationData)) {
// One of the hooks has decided to cancel the VL processing, return false.
return false;
}
final boolean hasCancel = violationData.hasCancel();
if (isMainThread)
if (isMainThread) {
return violationData.executeActions();
else
}
else {
// Always schedule to add to ViolationHistory.
TickTask.requestActionsExecution(violationData);
}
// (Design change: Permission checks are moved to cached permissions, lazily updated.)
return hasCancel;
@ -139,7 +144,7 @@ public abstract class Check implements MCAccessHolder{
* @param player
* @return
*/
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData){
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> params = new HashMap<ParameterName, String>();
// (Standard parameters like player, vl, check name are filled in in ViolationData.getParameter!)
return params;

View File

@ -24,7 +24,7 @@ public class ViolationData implements IViolationInfo, ActionData{
/** The actions to be executed. */
public final ActionList actions;
/** The actions applicable for the violation level. */
public final Action<ViolationData, ActionList>[] applicableActions;
@ -39,10 +39,10 @@ public class ViolationData implements IViolationInfo, ActionData{
/** The violation level. */
public final double vL;
/** Filled in parameters. */
private Map<ParameterName, String> parameters;
private boolean needsParameters = false;
/**
@ -68,11 +68,11 @@ public class ViolationData implements IViolationInfo, ActionData{
this.actions = actions;
this.applicableActions = actions.getActions(vL);
boolean needsParameters = false;
for (int i = 0; i < applicableActions.length; i++){
if (applicableActions[i].needsParameters()){
needsParameters = true;
break;
}
for (int i = 0; i < applicableActions.length; i++) {
if (applicableActions[i].needsParameters()) {
needsParameters = true;
break;
}
}
parameters = needsParameters ? check.getParameterMap(this) : null;
this.needsParameters = needsParameters;
@ -86,84 +86,90 @@ public class ViolationData implements IViolationInfo, ActionData{
public Action<ViolationData, ActionList> [] getActions() {
return applicableActions;
}
/**
* Execute actions and return if cancel. Does add it to history.
* @return
*/
public boolean executeActions(){
try {
ViolationHistory.getHistory(player).log(check.getClass().getName(), addedVL);
/**
* Execute actions and return if cancel. Does add it to history.
* @return
*/
public boolean executeActions() {
try {
// Statistics.
ViolationHistory.getHistory(player).log(check.getClass().getName(), addedVL);
// TODO: the time is taken here, which makes sense for delay, but otherwise ?
final long time = System.currentTimeMillis() / 1000L;
boolean cancel = false;
for (final Action<ViolationData, ActionList> action : getActions()) {
if (Check.getHistory(player).executeAction(this, action, time)) {
// The execution history said it really is time to execute the action, find out what it is and do
// what is needed.
if (action.execute(this)) {
cancel = true;
}
}
}
return cancel;
} catch (final Exception e) {
LogUtil.logSevere(e);
// On exceptions cancel events.
return true;
}
}
// TODO: the time is taken here, which makes sense for delay, but otherwise ?
final long time = System.currentTimeMillis() / 1000L;
boolean cancel = false;
for (final Action<ViolationData, ActionList> action : getActions())
if (Check.getHistory(player).executeAction(this, action, time))
// The execution history said it really is time to execute the action, find out what it is and do
// what is needed.
if (action.execute(this)) cancel = true;
/**
* Check if the actions contain a cancel.
* @return
*/
@Override
public boolean hasCancel() {
for (final Action<ViolationData, ActionList> action : applicableActions) {
if (action instanceof CancelAction) return true;
}
return false;
}
return cancel;
} catch (final Exception e) {
LogUtil.logSevere(e);
// On exceptions cancel events.
return true;
}
}
/**
* Check if the actions contain a cancel.
* @return
*/
@Override
public boolean hasCancel(){
for (final Action<ViolationData, ActionList> action : applicableActions){
if (action instanceof CancelAction) return true;
}
return false;
}
@Override
public String getParameter(final ParameterName parameterName){
if (parameterName == null) return "<???>";
switch (parameterName) {
case CHECK:
return check.getClass().getSimpleName();
case PLAYER:
return player.getName();
case VIOLATIONS:
return String.valueOf((long) Math.round(vL));
case UUID:
return player.getUniqueId().toString();
default:
break;
}
if (parameters == null) return "<?" + parameterName + ">";
final String value = parameters.get(parameterName);
return(value == null) ? ("<?" + parameterName + ">") : value;
}
@Override
public void setParameter(final ParameterName parameterName, String value){
if (parameters == null) {
parameters = new HashMap<ParameterName, String>();
}
parameters.put(parameterName, value);
}
@Override
public String getParameter(final ParameterName parameterName) {
if (parameterName == null) {
return "<???>";
}
switch (parameterName) {
case CHECK:
return check.getClass().getSimpleName();
case PLAYER:
return player.getName();
case VIOLATIONS:
return String.valueOf((long) Math.round(vL));
case UUID:
return player.getUniqueId().toString();
default:
break;
}
if (parameters == null) {
return "<?" + parameterName + ">";
}
final String value = parameters.get(parameterName);
return(value == null) ? ("<?" + parameterName + ">") : value;
}
@Override
@Override
public void setParameter(final ParameterName parameterName, String value) {
if (parameters == null) {
parameters = new HashMap<ParameterName, String>();
}
parameters.put(parameterName, value);
}
@Override
public boolean needsParameters() {
return needsParameters;
}
@Override
public boolean hasParameters() {
return parameters != null && !parameters.isEmpty();
}
@Override
public boolean hasParameters() {
return parameters != null && !parameters.isEmpty();
}
@Override
@Override
public double getAddedVl() {
return addedVL;
}
@ -173,11 +179,11 @@ public class ViolationData implements IViolationInfo, ActionData{
return vL;
}
public String getPermissionSilent() {
return actions.permissionSilent;
}
public ActionList getActionList(){
return actions;
}
public String getPermissionSilent() {
return actions.permissionSilent;
}
public ActionList getActionList() {
return actions;
}
}