From dcf7bacfa569339095f414ff3679ba2faa376d33 Mon Sep 17 00:00:00 2001 From: asofold Date: Sat, 15 Sep 2012 16:25:14 +0200 Subject: [PATCH] Ensure the parameters at the point of time of the violation are used (refactor). --- .../nocheatplus/actions/Action.java | 8 +++++ .../actions/types/ActionWithParameters.java | 7 +++- .../neatmonster/nocheatplus/checks/Check.java | 33 ++++++----------- .../nocheatplus/checks/ViolationData.java | 35 +++++++++++++++++++ .../nocheatplus/checks/blockbreak/Reach.java | 20 +++++------ .../checks/blockinteract/Reach.java | 21 ++++++----- .../nocheatplus/checks/blockplace/Reach.java | 19 +++++----- .../nocheatplus/checks/chat/NoPwnage.java | 22 ++++++------ .../nocheatplus/checks/fight/Speed.java | 20 +++++------ .../checks/inventory/InstantEat.java | 18 +++++----- .../checks/moving/CreativeFly.java | 30 +++++++--------- .../checks/moving/MorePackets.java | 21 +++++------ .../checks/moving/MorePacketsVehicle.java | 21 +++++------ .../nocheatplus/checks/moving/NoFall.java | 21 +++++------ .../checks/moving/SurvivalFly.java | 30 +++++++--------- 15 files changed, 165 insertions(+), 161 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/actions/Action.java b/src/fr/neatmonster/nocheatplus/actions/Action.java index 43e7a9ad..cdb11b75 100644 --- a/src/fr/neatmonster/nocheatplus/actions/Action.java +++ b/src/fr/neatmonster/nocheatplus/actions/Action.java @@ -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; + } } diff --git a/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java b/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java index 8497b74d..1916011a 100644 --- a/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java +++ b/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java @@ -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; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/Check.java b/src/fr/neatmonster/nocheatplus/checks/Check.java index 32df9b2d..68c016ed 100644 --- a/src/fr/neatmonster/nocheatplus/checks/Check.java +++ b/src/fr/neatmonster/nocheatplus/checks/Check.java @@ -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 getParameterMap(final ViolationData violationData){ + final Map params = new HashMap(); + // (Standard parameters like player, vl, check name are filled in in ViolationData.getParameter!) + return params; } /** diff --git a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java index 1cf80a24..2e05b51c 100644 --- a/src/fr/neatmonster/nocheatplus/checks/ViolationData.java +++ b/src/fr/neatmonster/nocheatplus/checks/ViolationData.java @@ -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 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: "". + */ + 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 ""; + final String value = parameters.get(parameterName); + return(value == null) ? ("") : value; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/blockbreak/Reach.java b/src/fr/neatmonster/nocheatplus/checks/blockbreak/Reach.java index 15d87f15..79e70afa 100644 --- a/src/fr/neatmonster/nocheatplus/checks/blockbreak/Reach.java +++ b/src/fr/neatmonster/nocheatplus/checks/blockbreak/Reach.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.REACH_DISTANCE, String.valueOf(Math.round(BlockBreakData.getData(violationData.player).reachDistance))); + return parameters; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/blockinteract/Reach.java b/src/fr/neatmonster/nocheatplus/checks/blockinteract/Reach.java index c9fd73c3..025c5b2f 100644 --- a/src/fr/neatmonster/nocheatplus/checks/blockinteract/Reach.java +++ b/src/fr/neatmonster/nocheatplus/checks/blockinteract/Reach.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.REACH_DISTANCE, String.valueOf(Math.round(BlockInteractData.getData(violationData.player).reachDistance))); + return parameters; + } + } diff --git a/src/fr/neatmonster/nocheatplus/checks/blockplace/Reach.java b/src/fr/neatmonster/nocheatplus/checks/blockplace/Reach.java index 29407fe6..cf20a8d7 100644 --- a/src/fr/neatmonster/nocheatplus/checks/blockplace/Reach.java +++ b/src/fr/neatmonster/nocheatplus/checks/blockplace/Reach.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map 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); - } } diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java b/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java index 6305b5f7..1f7dd944 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/NoPwnage.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.IP, violationData.player.getAddress().toString().substring(1).split(":")[0]); + return parameters; + } + } diff --git a/src/fr/neatmonster/nocheatplus/checks/fight/Speed.java b/src/fr/neatmonster/nocheatplus/checks/fight/Speed.java index 4e615099..01cea2c1 100644 --- a/src/fr/neatmonster/nocheatplus/checks/fight/Speed.java +++ b/src/fr/neatmonster/nocheatplus/checks/fight/Speed.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.LIMIT, String.valueOf(Math.round(FightConfig.getConfig(violationData.player).speedLimit))); + return parameters; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/inventory/InstantEat.java b/src/fr/neatmonster/nocheatplus/checks/inventory/InstantEat.java index f57aca43..58d8b835 100644 --- a/src/fr/neatmonster/nocheatplus/checks/inventory/InstantEat.java +++ b/src/fr/neatmonster/nocheatplus/checks/inventory/InstantEat.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.FOOD, InventoryData.getData(violationData.player).instantEatFood.toString()); + return parameters; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/moving/CreativeFly.java b/src/fr/neatmonster/nocheatplus/checks/moving/CreativeFly.java index b0763ba0..6cccb420 100644 --- a/src/fr/neatmonster/nocheatplus/checks/moving/CreativeFly.java +++ b/src/fr/neatmonster/nocheatplus/checks/moving/CreativeFly.java @@ -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 getParameterMap(final ViolationData violationData) { + final MovingData data = MovingData.getData(violationData.player); + final Map 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; + } + } diff --git a/src/fr/neatmonster/nocheatplus/checks/moving/MorePackets.java b/src/fr/neatmonster/nocheatplus/checks/moving/MorePackets.java index 9fe37df0..d53294a4 100644 --- a/src/fr/neatmonster/nocheatplus/checks/moving/MorePackets.java +++ b/src/fr/neatmonster/nocheatplus/checks/moving/MorePackets.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.PACKETS, String.valueOf(MovingData.getData(violationData.player).morePacketsPackets)); + return parameters; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/moving/MorePacketsVehicle.java b/src/fr/neatmonster/nocheatplus/checks/moving/MorePacketsVehicle.java index cab00e18..426706cc 100644 --- a/src/fr/neatmonster/nocheatplus/checks/moving/MorePacketsVehicle.java +++ b/src/fr/neatmonster/nocheatplus/checks/moving/MorePacketsVehicle.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.PACKETS, String.valueOf(MovingData.getData(violationData.player).morePacketsVehiclePackets)); + return parameters; + } } diff --git a/src/fr/neatmonster/nocheatplus/checks/moving/NoFall.java b/src/fr/neatmonster/nocheatplus/checks/moving/NoFall.java index 539d758f..68f4814f 100644 --- a/src/fr/neatmonster/nocheatplus/checks/moving/NoFall.java +++ b/src/fr/neatmonster/nocheatplus/checks/moving/NoFall.java @@ -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 getParameterMap(final ViolationData violationData) { + final Map parameters = super.getParameterMap(violationData); + parameters.put(ParameterName.FALL_DISTANCE, String.format(Locale.US, "%.2f", MovingData.getData(violationData.player).noFallFallDistance)); + return parameters; + } + } diff --git a/src/fr/neatmonster/nocheatplus/checks/moving/SurvivalFly.java b/src/fr/neatmonster/nocheatplus/checks/moving/SurvivalFly.java index 68b03068..d53ae038 100644 --- a/src/fr/neatmonster/nocheatplus/checks/moving/SurvivalFly.java +++ b/src/fr/neatmonster/nocheatplus/checks/moving/SurvivalFly.java @@ -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 getParameterMap(final ViolationData violationData) { + final MovingData data = MovingData.getData(violationData.player); + final Map 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; + } + }