Ensure the parameters at the point of time of the

violation are used (refactor).
This commit is contained in:
asofold 2012-09-15 16:25:14 +02:00
parent 52fc47bd46
commit dcf7bacfa5
15 changed files with 165 additions and 161 deletions

View File

@ -57,4 +57,12 @@ public abstract class Action {
* @return true, if successful
*/
public abstract boolean execute(final ViolationData violationData);
/**
* Check if parameters are needed at all for faster processing.
* @return
*/
public boolean needsParameters(){
return false;
}
}

View File

@ -65,7 +65,7 @@ public abstract class ActionWithParameters extends Action {
if (part instanceof String)
log.append((String) part);
else
log.append(violationData.check.getParameter((ParameterName) part, violationData));
log.append(violationData.getParameter((ParameterName) part));
return log.toString();
}
@ -103,4 +103,9 @@ public abstract class ActionWithParameters extends Action {
}
}
}
@Override
public boolean needsParameters() {
return true;
}
}

View File

@ -135,31 +135,18 @@ public abstract class Check {
// (Design change: Permission checks are moved to cached permissions, lazily updated.)
return hasCancel;
}
/**
* Replace a parameter for commands or log actions with an actual value. Individual checks should override this to
* get their own parameters handled too.
*
* @param wildcard
* the wildcard
* @param violationData
* the violation data
* @return the parameter
* Fill in parameters for creating violation data.
* Individual checks should override this to fill in check specific parameters,
* which then are fetched by the violation data instance.
* @param player
* @return
*/
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.CHECK)
return getClass().getSimpleName();
else if (wildcard == ParameterName.PLAYER)
return violationData.player.getName();
else if (wildcard == ParameterName.VIOLATIONS) {
try {
return "" + Math.round(violationData.vL);
} catch (final Exception e) {
e.printStackTrace();
}
return "";
} else
return "The author was lazy and forgot to define " + wildcard + ".";
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

@ -1,9 +1,12 @@
package fr.neatmonster.nocheatplus.checks;
import java.util.Map;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.actions.Action;
import fr.neatmonster.nocheatplus.actions.ActionList;
import fr.neatmonster.nocheatplus.actions.ParameterName;
import fr.neatmonster.nocheatplus.actions.types.CancelAction;
/*
@ -40,6 +43,9 @@ public class ViolationData {
/** The violation level. */
public final double vL;
/** Filled in parameters. */
private final Map<ParameterName, String> parameters;
/**
* Instantiates a new violation data.
@ -63,6 +69,14 @@ public class ViolationData {
this.addedVL = addedVL;
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;
}
}
parameters = needsParameters ? check.getParameterMap(this) : null;
}
/**
@ -110,5 +124,26 @@ public class ViolationData {
}
return false;
}
/**
* Get the parameters value for this violation.
* @param parameterName
* @return Will always return some string, if not set: "<?PARAMETERNAME>".
*/
public String getParameter(final ParameterName parameterName){
switch (parameterName) {
case CHECK:
return check.getClass().getSimpleName();
case PLAYER:
return player.getName();
case VIOLATIONS:
return String.valueOf(Math.round(vL));
default:
break;
}
if (parameters == null) return "<?" + parameterName + ">";
final String value = parameters.get(parameterName);
return(value == null) ? ("<?" + parameterName + ">") : value;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.blockbreak;
import java.util.Map;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -80,15 +82,11 @@ public class Reach extends Check {
return cancel;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName, org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.REACH_DISTANCE)
return String.valueOf(Math.round(BlockBreakData.getData(violationData.player).reachDistance));
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.REACH_DISTANCE, String.valueOf(Math.round(BlockBreakData.getData(violationData.player).reachDistance)));
return parameters;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.blockinteract;
import java.util.Map;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -74,15 +76,12 @@ public class Reach extends Check {
return cancel;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName, org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.REACH_DISTANCE)
return String.valueOf(Math.round(BlockInteractData.getData(violationData.player).reachDistance));
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.REACH_DISTANCE, String.valueOf(Math.round(BlockInteractData.getData(violationData.player).reachDistance)));
return parameters;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.blockplace;
import java.util.Map;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -83,15 +85,12 @@ public class Reach extends Check {
return cancel;
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.REACH_DISTANCE, "" +Math.round(BlockPlaceData.getData(violationData.player).reachDistance));
return parameters;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName, org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.REACH_DISTANCE)
return String.valueOf(Math.round(BlockPlaceData.getData(violationData.player).reachDistance));
else
return super.getParameter(wildcard, violationData);
}
}

View File

@ -1,5 +1,6 @@
package fr.neatmonster.nocheatplus.checks.chat;
import java.util.Map;
import java.util.Random;
import org.bukkit.Bukkit;
@ -93,18 +94,7 @@ public class NoPwnage extends AsyncCheck implements ICaptcha{
return unsafeLoginCheck(player, cc, data);
}
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName, org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.IP)
return violationData.player.getAddress().toString().substring(1).split(":")[0];
else
return super.getParameter(wildcard, violationData);
}
/**
* Only to be called form synchronized code.
*
@ -343,4 +333,12 @@ public class NoPwnage extends AsyncCheck implements ICaptcha{
return cancel;
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.IP, violationData.player.getAddress().toString().substring(1).split(":")[0]);
return parameters;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.fight;
import java.util.Map;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.actions.ParameterName;
@ -66,15 +68,11 @@ public class Speed extends Check {
return cancel;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName, org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.LIMIT)
return String.valueOf(Math.round(FightConfig.getConfig(violationData.player).speedLimit));
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.LIMIT, String.valueOf(Math.round(FightConfig.getConfig(violationData.player).speedLimit)));
return parameters;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.inventory;
import java.util.Map;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.actions.ParameterName;
@ -73,14 +75,10 @@ public class InstantEat extends Check {
return cancel;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName, org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.FOOD)
return InventoryData.getData(violationData.player).instantEatFood.toString();
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.FOOD, InventoryData.getData(violationData.player).instantEatFood.toString());
return parameters;
}
}

View File

@ -1,6 +1,7 @@
package fr.neatmonster.nocheatplus.checks.moving;
import java.util.Locale;
import java.util.Map;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.MobEffectList;
@ -150,22 +151,15 @@ public class CreativeFly extends Check {
data.setBack = to.getLocation();
return null;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName,
* org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
final MovingData data = MovingData.getData(violationData.player);
if (wildcard == ParameterName.LOCATION_FROM)
return String.format(Locale.US, "%.2f, %.2f, %.2f", data.from.getX(), data.from.getY(), data.from.getZ());
else if (wildcard == ParameterName.LOCATION_TO)
return String.format(Locale.US, "%.2f, %.2f, %.2f", data.to.getX(), data.to.getY(), data.to.getZ());
else if (wildcard == ParameterName.DISTANCE)
return String.format(Locale.US, "%.2f", data.to.getLocation().subtract(data.from.getLocation())
.lengthSquared());
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final MovingData data = MovingData.getData(violationData.player);
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.LOCATION_FROM, String.format(Locale.US, "%.2f, %.2f, %.2f", data.from.getX(), data.from.getY(), data.from.getZ()));
parameters.put(ParameterName.LOCATION_TO, String.format(Locale.US, "%.2f, %.2f, %.2f", data.to.getX(), data.to.getY(), data.to.getZ()));
parameters.put(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", data.to.getLocation().subtract(data.from.getLocation()).lengthSquared()));
return parameters;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.moving;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -118,16 +120,11 @@ public class MorePackets extends Check {
// player to look somewhere else despite getting pulled back by NoCheatPlus.
return new Location(player.getWorld(), newTo.getX(), newTo.getY(), newTo.getZ(), to.getYaw(), to.getPitch());
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName,
* org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.PACKETS)
return String.valueOf(MovingData.getData(violationData.player).morePacketsPackets);
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.PACKETS, String.valueOf(MovingData.getData(violationData.player).morePacketsPackets));
return parameters;
}
}

View File

@ -1,5 +1,7 @@
package fr.neatmonster.nocheatplus.checks.moving;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -117,16 +119,11 @@ public class MorePacketsVehicle extends Check {
// player to look somewhere else despite getting pulled back by NoCheatPlus.
return new Location(player.getWorld(), newTo.getX(), newTo.getY(), newTo.getZ(), to.getYaw(), to.getPitch());
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName,
* org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.PACKETS)
return String.valueOf(MovingData.getData(violationData.player).morePacketsVehiclePackets);
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.PACKETS, String.valueOf(MovingData.getData(violationData.player).morePacketsVehiclePackets));
return parameters;
}
}

View File

@ -1,6 +1,7 @@
package fr.neatmonster.nocheatplus.checks.moving;
import java.util.Locale;
import java.util.Map;
import net.minecraft.server.EntityPlayer;
@ -92,16 +93,12 @@ public class NoFall extends Check {
if (to.getY() > 0 && entityPlayer.locY > to.getY())
data.noFallFallDistance += entityPlayer.locY - to.getY();
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName,
* org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
if (wildcard == ParameterName.FALL_DISTANCE)
return String.format(Locale.US, "%.2f", MovingData.getData(violationData.player).noFallFallDistance);
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.FALL_DISTANCE, String.format(Locale.US, "%.2f", MovingData.getData(violationData.player).noFallFallDistance));
return parameters;
}
}

View File

@ -1,6 +1,7 @@
package fr.neatmonster.nocheatplus.checks.moving;
import java.util.Locale;
import java.util.Map;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.MobEffectList;
@ -259,22 +260,15 @@ public class SurvivalFly extends Check {
return null;
}
/* (non-Javadoc)
* @see fr.neatmonster.nocheatplus.checks.Check#getParameter(fr.neatmonster.nocheatplus.actions.ParameterName,
* org.bukkit.entity.Player)
*/
@Override
public String getParameter(final ParameterName wildcard, final ViolationData violationData) {
final MovingData data = MovingData.getData(violationData.player);
if (wildcard == ParameterName.LOCATION_FROM)
return String.format(Locale.US, "%.2f, %.2f, %.2f", data.from.getX(), data.from.getY(), data.from.getZ());
else if (wildcard == ParameterName.LOCATION_TO)
return String.format(Locale.US, "%.2f, %.2f, %.2f", data.to.getX(), data.to.getY(), data.to.getZ());
else if (wildcard == ParameterName.DISTANCE)
return String.format(Locale.US, "%.2f", data.to.getLocation().subtract(data.from.getLocation())
.lengthSquared());
else
return super.getParameter(wildcard, violationData);
}
@Override
protected Map<ParameterName, String> getParameterMap(final ViolationData violationData) {
final MovingData data = MovingData.getData(violationData.player);
final Map<ParameterName, String> parameters = super.getParameterMap(violationData);
parameters.put(ParameterName.LOCATION_FROM, String.format(Locale.US, "%.2f, %.2f, %.2f", data.from.getX(), data.from.getY(), data.from.getZ()));
parameters.put(ParameterName.LOCATION_TO, String.format(Locale.US, "%.2f, %.2f, %.2f", data.to.getX(), data.to.getY(), data.to.getZ()));
parameters.put(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", data.to.getLocation().subtract(data.from.getLocation()).lengthSquared()));
return parameters;
}
}