[BLEEDING] Switch moving.morepackets to use Actionfrequency.

This commit is contained in:
asofold 2014-07-18 00:21:19 +02:00
parent 7cc936878e
commit 85a104ed75
2 changed files with 50 additions and 60 deletions

View File

@ -19,12 +19,11 @@ import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
*/ */
public class MorePackets extends Check { public class MorePackets extends Check {
/** /** The maximum number of packets per second that we accept. */
* The usual number of packets per timeframe. private final static int maxPackets = 22;
*
* 20 would be for perfect internet connections, 22 is good enough. /** Assumed number of packets per second under ideal conditions. */
*/ private final static int idealPackets = 20;
private final static int packetsPerTimeframe = 22;
/** /**
* Instantiates a new more packets check. * Instantiates a new more packets check.
@ -52,9 +51,6 @@ public class MorePackets extends Check {
public Location check(final Player player, final PlayerLocation from, final PlayerLocation to, final MovingData data, final MovingConfig cc) { public Location check(final Player player, final PlayerLocation from, final PlayerLocation to, final MovingData data, final MovingConfig cc) {
// Take time once, first: // Take time once, first:
final long time = System.currentTimeMillis(); final long time = System.currentTimeMillis();
Location newTo = null;
if (!data.hasMorePacketsSetBack()){ if (!data.hasMorePacketsSetBack()){
// TODO: Check if other set-back is appropriate or if to set on other events. // TODO: Check if other set-back is appropriate or if to set on other events.
@ -65,63 +61,57 @@ public class MorePackets extends Check {
data.setMorePacketsSetBack(from); data.setMorePacketsSetBack(from);
} }
} }
// Take a packet from the buffer. // Add packet to frequency count.
data.morePacketsBuffer--; data.morePacketsFreq.add(time, 1f);
// Fill up all "used" time windows (minimum we can do without other events.
boolean used = false;
final float burnScore = (float) idealPackets * (float) data.morePacketsFreq.bucketDuration() / 1000f;
for (int i = 1; i < data.morePacketsFreq.numberOfBuckets(); i++) {
final float score = data.morePacketsFreq.bucketScore(i);
if (score > 0f) {
if (used) {
// Burn this one.
data.morePacketsFreq.setBucket(i, Math.max(score, burnScore));
}
else {
// Burn all after this.
used = true;
}
}
}
// TODO: Burn time windows based on other activity counting [e.g. same resolution ActinFrequency with keep-alive].
// Compare score to maximum allowed.
final float fullCount = data.morePacketsFreq.score(1f);
final double violation = (double) fullCount - (double) (data.morePacketsFreq.bucketDuration() * data.morePacketsFreq.numberOfBuckets() * maxPackets / 1000);
// Player used up buffer, they fail the check. // Player used up buffer, they fail the check.
if (data.morePacketsBuffer < 0) { if (violation > 0.0) {
// Increment violation level. // Increment violation level.
data.morePacketsVL = -data.morePacketsBuffer; data.morePacketsVL = violation; // TODO: Accumulate somehow [e.g. always += 1, decrease with continuous moving without violation]?
// Execute whatever actions are associated with this check and the violation level and find out if we should // Violation handling.
// cancel the event. final ViolationData vd = new ViolationData(this, player, data.morePacketsVL, violation, cc.morePacketsActions);
final ViolationData vd = new ViolationData(this, player, data.morePacketsVL, -data.morePacketsBuffer, cc.morePacketsActions);
if (cc.debug || vd.needsParameters()) { if (cc.debug || vd.needsParameters()) {
vd.setParameter(ParameterName.PACKETS, Integer.toString(-data.morePacketsBuffer)); vd.setParameter(ParameterName.PACKETS, Integer.toString(new Double(violation).intValue()));
} }
if (executeActions(vd)){ if (executeActions(vd)){
newTo = data.getMorePacketsSetBack(); return data.getMorePacketsSetBack();
} }
}
else {
// Set the new "setback" location. (CHANGED to only update, if not a violation.)
// (Might update whenever newTo == null)
data.setMorePacketsSetBack(from);
} }
if (data.morePacketsLastTime + 1000 < time) { // No set-back.
// More than 1 second elapsed, but how many? return null;
final double seconds = (time - data.morePacketsLastTime) / 1000D;
// For each second, fill the buffer.
data.morePacketsBuffer += packetsPerTimeframe * seconds;
// If there was a long pause (maybe server lag?), allow buffer to grow up to 100.
if (seconds > 2) {
if (data.morePacketsBuffer > 100) {
data.morePacketsBuffer = 100;
}
} else if (data.morePacketsBuffer > 50) {
// Only allow growth up to 50.
data.morePacketsBuffer = 50;
}
// Set the new "last" time.
data.morePacketsLastTime = time;
// Set the new "setback" location.
if (newTo == null) {
data.setMorePacketsSetBack(from);
}
} else if (data.morePacketsLastTime > time) {
// Security check, maybe system time changed.
data.morePacketsLastTime = time;
}
if (newTo == null) {
return null;
}
// Compose a new location based on coordinates of "newTo" and viewing direction of "event.getTo()" to allow the
// 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());
} }
} }

View File

@ -14,6 +14,7 @@ import fr.neatmonster.nocheatplus.checks.access.ACheckData;
import fr.neatmonster.nocheatplus.checks.access.CheckDataFactory; import fr.neatmonster.nocheatplus.checks.access.CheckDataFactory;
import fr.neatmonster.nocheatplus.checks.access.ICheckData; import fr.neatmonster.nocheatplus.checks.access.ICheckData;
import fr.neatmonster.nocheatplus.utilities.ActionAccumulator; import fr.neatmonster.nocheatplus.utilities.ActionAccumulator;
import fr.neatmonster.nocheatplus.utilities.ActionFrequency;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation; import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
/** /**
@ -147,8 +148,7 @@ public class MovingData extends ACheckData {
public boolean creativeFlyPreviousRefused; public boolean creativeFlyPreviousRefused;
// Data of the more packets check. // Data of the more packets check.
public int morePacketsBuffer = 50; public final ActionFrequency morePacketsFreq = new ActionFrequency(10, 500);
public long morePacketsLastTime;
private Location morePacketsSetback = null; private Location morePacketsSetback = null;
// Data of the more packets vehicle check. // Data of the more packets vehicle check.