[Bleeding] Replace LagMeasureTask by TickTask.

Contains some fixes. Removes the debugging message for lag. Might
in some places use wild guesses for lag estimation
(TickTask.getLag(1000) < 1.5).
This commit is contained in:
asofold 2013-01-17 01:47:07 +01:00
parent 48705669dd
commit fd2469490a
10 changed files with 58 additions and 154 deletions

View File

@ -55,15 +55,14 @@ import fr.neatmonster.nocheatplus.event.IHaveMethodOrder;
import fr.neatmonster.nocheatplus.event.ListenerManager;
import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager;
import fr.neatmonster.nocheatplus.metrics.Metrics;
import fr.neatmonster.nocheatplus.metrics.MetricsData;
import fr.neatmonster.nocheatplus.metrics.Metrics.Graph;
import fr.neatmonster.nocheatplus.metrics.Metrics.Plotter;
import fr.neatmonster.nocheatplus.metrics.MetricsData;
import fr.neatmonster.nocheatplus.permissions.PermissionUtil;
import fr.neatmonster.nocheatplus.permissions.PermissionUtil.CommandProtectionEntry;
import fr.neatmonster.nocheatplus.permissions.Permissions;
import fr.neatmonster.nocheatplus.players.DataManager;
import fr.neatmonster.nocheatplus.utilities.BlockProperties;
import fr.neatmonster.nocheatplus.utilities.LagMeasureTask;
import fr.neatmonster.nocheatplus.utilities.LogUtil;
import fr.neatmonster.nocheatplus.utilities.TickTask;
import fr.neatmonster.nocheatplus.utilities.Updates;
@ -357,10 +356,6 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
metrics = null;
}
// Stop the lag measuring task.
if (verbose) LogUtil.logInfo("[NoCheatPlus] Stop LagMeasureTask...");
LagMeasureTask.cancel();
// Just to be sure nothing gets left out.
if (verbose) LogUtil.logInfo("[NoCheatPlus] Stop all remaining tasks...");
getServer().getScheduler().cancelTasks(this);
@ -499,9 +494,6 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
command.setExecutor(commandHandler);
// (CommandHandler is TabExecutor.)
// Set up a task to monitor server lag.
LagMeasureTask.start(this);
// Set up the tick task.
TickTask.start(this);

View File

@ -178,6 +178,7 @@ public abstract class Check {
if (!type.isEnabled(player) || player.hasPermission(type.getPermission()))
return false;
} catch (final Exception e) {
// TODO: this should be mostly obsolete.
LogUtil.logSevere(e);
}
return !NCPExemptionManager.isExempted(player, type);

View File

@ -8,7 +8,7 @@ import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.Check;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.utilities.CheckUtils;
import fr.neatmonster.nocheatplus.utilities.LagMeasureTask;
import fr.neatmonster.nocheatplus.utilities.TickTask;
/*
* MMP"""""""MM dP
@ -122,9 +122,11 @@ public class Angle extends Check {
// Is the violation is superior to the threshold defined in the configuration?
if (violation > cc.angleThreshold) {
// Has the server lagged?
if (!LagMeasureTask.skipCheck())
if (TickTask.getLag(1000) < 1.5f){
// TODO: 1.5 is a fantasy value.
// If it hasn't, increment the violation level.
data.angleVL += violation;
}
// Execute whatever actions are associated with this check and the violation level and find out if we should
// cancel the event.

View File

@ -6,9 +6,12 @@ import org.bukkit.potion.PotionEffectType;
import fr.neatmonster.nocheatplus.checks.Check;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.moving.MovingConfig;
import fr.neatmonster.nocheatplus.checks.moving.MovingData;
import fr.neatmonster.nocheatplus.checks.moving.MovingListener;
import fr.neatmonster.nocheatplus.utilities.CheckUtils;
import fr.neatmonster.nocheatplus.utilities.LagMeasureTask;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
import fr.neatmonster.nocheatplus.utilities.TickTask;
/*
* MM'""""'YMM oo dP oo dP
@ -49,6 +52,7 @@ public class Critical extends Check {
final PlayerLocation location = new PlayerLocation(mcAccess, mcAccess.getBlockCache(loc.getWorld()));
location.set(loc, player);
if (location.isIllegal()) {
// TODO: This should be impossible !
location.cleanup();
CheckUtils.onIllegalMove(player);
return true;
@ -59,8 +63,10 @@ public class Critical extends Check {
if (player.getFallDistance() > 0f && !location.isOnGround() && !location.isOnClimbable() && !location.isInLiquid()
&& !player.hasPotionEffect(PotionEffectType.BLINDNESS)){
// It was a critical hit, now check if the player has jumped or has sent a packet to mislead the server.
if (player.getFallDistance() < cc.criticalFallDistance
|| Math.abs(player.getVelocity().getY()) < cc.criticalVelocity) {
if (player.getFallDistance() < cc.criticalFallDistance || Math.abs(player.getVelocity().getY()) < cc.criticalVelocity) {
final MovingConfig ccM = MovingConfig.getConfig(player);
final MovingData dataM = MovingData.getData(player);
if (MovingListener.shouldCheckSurvivalFly(player, dataM, ccM)){
final double deltaFallDistance = (cc.criticalFallDistance - player.getFallDistance())
/ cc.criticalFallDistance;
final double deltaVelocity = (cc.criticalVelocity - Math.abs(player.getVelocity().getY()))
@ -69,14 +75,19 @@ public class Critical extends Check {
: 0D + deltaVelocity > 0D ? deltaVelocity : 0D;
// Player failed the check, but this is influenced by lag so don't do it if there was lag.
if (!LagMeasureTask.skipCheck())
if (TickTask.getLag(1000) < 1.5){
// TODO: 1.5 is a fantasy value.
// Increment the violation level.
data.criticalVL += delta;
}
// Execute whatever actions are associated with this check and the violation level and find out if we
// should cancel the event.
cancel = executeActions(player, data.criticalVL, delta, cc.criticalActions);
}
}
}
location.cleanup(); // Slightly better for gc.

View File

@ -5,7 +5,7 @@ import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.Check;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.utilities.LagMeasureTask;
import fr.neatmonster.nocheatplus.utilities.TickTask;
/*
* M""MMMMM""M dP dP dP
@ -41,18 +41,21 @@ public class Knockback extends Check {
boolean cancel = false;
final long time = System.currentTimeMillis();
// If the item has the knockback enchantment, do not check.
if (player.getItemInHand().containsEnchantment(Enchantment.KNOCKBACK)
|| player.getItemInHand().containsEnchantment(Enchantment.ARROW_KNOCKBACK))
return false;
// How long ago has the player started sprinting?
if (data.knockbackSprintTime > 0L
&& System.currentTimeMillis() - data.knockbackSprintTime < cc.knockbackInterval) {
final double difference = cc.knockbackInterval - System.currentTimeMillis() + data.knockbackSprintTime;
final long usedTime = (time - data.knockbackSprintTime);
final long effectiveTime = (long) ((float) usedTime * (cc.lag ? TickTask.getLag(usedTime): 1f));
// Pretty rough: Completely skip on lag.
if (data.knockbackSprintTime > 0L && effectiveTime < cc.knockbackInterval) {
final double difference = cc.knockbackInterval - time + data.knockbackSprintTime;
// Player failed the check, but this is influenced by lag, so don't do it if there was lag.
if (!LagMeasureTask.skipCheck())
// Increment the violation level
data.knockbackVL += difference;

View File

@ -14,7 +14,7 @@ import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.combined.Improbable;
import fr.neatmonster.nocheatplus.permissions.Permissions;
import fr.neatmonster.nocheatplus.utilities.CheckUtils;
import fr.neatmonster.nocheatplus.utilities.LagMeasureTask;
import fr.neatmonster.nocheatplus.utilities.TickTask;
/*
* MM"""""""`MM dP
@ -103,8 +103,10 @@ public class Reach extends Check {
if (violation > 0) {
// He failed, increment violation level. This is influenced by lag, so don't do it if there was lag.
if (!LagMeasureTask.skipCheck())
if (TickTask.getLag(1000) < 1.5f){
// TODO: 1.5 is a fantasy value.
data.reachVL += violation;
}
// Execute whatever actions are associated with this check and the violation level and find out if we should
// cancel the event.

View File

@ -8,7 +8,6 @@ import fr.neatmonster.nocheatplus.actions.ParameterName;
import fr.neatmonster.nocheatplus.checks.Check;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.ViolationData;
import fr.neatmonster.nocheatplus.utilities.LagMeasureTask;
import fr.neatmonster.nocheatplus.utilities.TickTask;
/*
@ -81,7 +80,6 @@ public class Speed extends Check {
// Too many attacks?
if (max > cc.speedLimit) {
// If there was lag, don't count it towards violation level.
if (!LagMeasureTask.skipCheck())
data.speedVL += max - cc.speedLimit;
// Execute whatever actions are associated with this check and the violation level and find out if we should

View File

@ -997,9 +997,9 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
* @param cc
* @return
*/
public final boolean shouldCheckSurvivalFly(final Player player, final MovingData data, final MovingConfig cc){
public static final boolean shouldCheckSurvivalFly(final Player player, final MovingData data, final MovingConfig cc){
if (player.hasPermission(Permissions.MOVING_CREATIVEFLY)) return false;
else if (!survivalFly.isEnabled(player)) return false;
else if (!cc.survivalFlyCheck || NCPExemptionManager.isExempted(player, CheckType.MOVING_SURVIVALFLY) || player.hasPermission(Permissions.MOVING_SURVIVALFLY)) return false;
else if ((cc.ignoreCreative || player.getGameMode() != GameMode.CREATIVE) && (cc.ignoreAllowFlight || !player.getAllowFlight())){
return true;
}

View File

@ -1,112 +0,0 @@
package fr.neatmonster.nocheatplus.utilities;
import org.bukkit.Bukkit;
import fr.neatmonster.nocheatplus.NoCheatPlus;
import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigManager;
import fr.neatmonster.nocheatplus.metrics.MetricsData;
/*
* M""MMMMMMMM M"""""`'"""`YM
* M MMMMMMMM M mm. mm. M
* M MMMMMMMM .d8888b. .d8888b. M MMM MMM M .d8888b. .d8888b. .d8888b. dP dP 88d888b. .d8888b.
* M MMMMMMMM 88' `88 88' `88 M MMM MMM M 88ooood8 88' `88 Y8ooooo. 88 88 88' `88 88ooood8
* M MMMMMMMM 88. .88 88. .88 M MMM MMM M 88. ... 88. .88 88 88. .88 88 88. ...
* M M `88888P8 `8888P88 M MMM MMM M `88888P' `88888P8 `88888P' `88888P' dP `88888P'
* MMMMMMMMMMM .88 MMMMMMMMMMMMMM
* d8888P
*
* M""""""""M dP
* Mmmm mmmM 88
* MMMM MMMM .d8888b. .d8888b. 88 .dP
* MMMM MMMM 88' `88 Y8ooooo. 88888"
* MMMM MMMM 88. .88 88 88 `8b.
* MMMM MMMM `88888P8 `88888P' dP `YP
* MMMMMMMMMM
*/
/**
* A task running in the background that measures tick time vs. real time.
*/
public class LagMeasureTask implements Runnable {
/** The instance of the class for a static access. */
private static LagMeasureTask instance = new LagMeasureTask();
/**
* Cancel the task.
*/
public static void cancel() {
if (instance.lagMeasureTaskId != -1) {
try {
Bukkit.getServer().getScheduler().cancelTask(instance.lagMeasureTaskId);
} catch (final Exception e) {
LogUtil.logWarning("[NoCheatPlus] Couldn't cancel LagMeasureTask: " + e.getMessage() + ".");
}
instance.lagMeasureTaskId = -1;
}
}
/**
* Returns if checking must be skipped (lag).
*
* @return true, if successful
*/
public static boolean skipCheck() {
return instance.skipCheck;
}
/**
* Start the task.
*
* @param plugin
* the instance of NoCheatPlus
*/
public static void start(final NoCheatPlus plugin) {
instance.lagMeasureTaskId = Bukkit.getServer().getScheduler()
.scheduleSyncRepeatingTask(plugin, instance, 20L, 20L);
}
/** The last in game second time. */
private long lastInGameSecondTime = System.currentTimeMillis();
/** The last in game second duration. */
private long lastInGameSecondDuration = 2000L;
/** The lag measure task id. */
private int lagMeasureTaskId = -1;
/** The skip check. */
private boolean skipCheck = false;
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
final boolean oldStatus = skipCheck;
// If the previous second took to long, skip checks during this second.
skipCheck = lastInGameSecondDuration > 2000;
// Add the number of ticks the last second have contained to the Metrics data.
int ticks = (int) Math.round(20000D / lastInGameSecondDuration);
if (ticks > 20)
ticks = 20;
MetricsData.addTicks(ticks);
// Show the debug messages.
if (ConfigManager.getConfigFile().getBoolean(ConfPaths.LOGGING_DEBUG))
if (oldStatus != skipCheck && skipCheck)
LogUtil.logInfo("[NoCheatPlus] Detected server lag, some checks will not work.");
else if (oldStatus != skipCheck && !skipCheck)
LogUtil.logInfo("[NoCheatPlus] Server lag seems to have stopped, reenabling checks.");
final long time = System.currentTimeMillis();
lastInGameSecondDuration = time - lastInGameSecondTime;
lastInGameSecondTime = time;
} catch (final Exception e) {
// Just prevent this thread from dying for whatever reason.
}
}
}

View File

@ -15,6 +15,7 @@ import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.ViolationData;
import fr.neatmonster.nocheatplus.checks.access.ICheckData;
import fr.neatmonster.nocheatplus.components.TickListener;
import fr.neatmonster.nocheatplus.metrics.MetricsData;
import fr.neatmonster.nocheatplus.players.DataManager;
/**
@ -409,6 +410,12 @@ public class TickTask implements Runnable {
updatePermissions();
// Listeners.
notifyListeners();
// Metrics
if (tick > 0 && (tick % 20) == 0){
// Count every second.
final int ticks = Math.min(20, (int) (1000f / (50f * getLag(1000, true))));
MetricsData.addTicks(ticks);
}
// Measure time after heavy stuff.
final long time = System.currentTimeMillis();