Improved velocity handling (#132), some refactoring and small changes

This commit is contained in:
Rammelkast committed 2021-10-12 12:47:53 +02:00
1 parent 9550cc1d09
commit e2d0fbda18
15 files changed
+211 -160

No files matched your search

@@ -81,6 +81,8 @@ public final class MovementManager {
public int elytraEffectTicks;
// Used by Velocity check, represents the currently expected Y motion
public double velocityExpectedMotionY;
// Used by Velocity check, represents the currently expected XZ motion
public double velocityExpectedMotionXZ;
// Amount of ticks a player is sneaking
public int sneakingTicks;
// Ticks counter after being near a liquid
@@ -243,7 +245,11 @@ public final class MovementManager {
}
this.lastUpdate = System.currentTimeMillis();
final User user = AntiCheatReloaded.getManager().getUserManager().getUser(player.getUniqueId());
// Tick velocity tracker
user.getVelocityTracker().tick();
// Update "good location"
AntiCheatReloaded.getManager().getUserManager().getUser(player.getUniqueId()).setGoodLocation(from);
user.setGoodLocation(from);
}
}
@@ -38,7 +38,7 @@ public class UpdateManager {
private boolean isAhead;
public UpdateManager() {
this.update();
update();
}
public void update() {
@@ -59,26 +59,25 @@ public class UpdateManager {
this.isAhead = splitCompare > 0;
}
private String getOnlineData(String url) {
private String getOnlineData(final String url) {
String data = null;
InputStream stream = null;
try {
stream = new URL(url).openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
StringBuilder builder = new StringBuilder();
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
final StringBuilder builder = new StringBuilder();
int readChar;
while ((readChar = reader.read()) != -1) {
builder.append((char) readChar);
}
data = builder.toString();
reader.close();
} catch (IOException exception) {}
} catch (final IOException exception) {}
finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
}
} catch (final IOException e) {}
}
}
return data;
@@ -110,19 +109,20 @@ public class UpdateManager {
version = version.substring(0, version.length() - 4);
}
String[] versionSplit = version.split("\\.");
final String[] versionSplit = version.split("\\.");
if (versionSplit.length != 3) {
// Illegal version
throw new Exception("Version " + version + " is illegal!");
}
try {
int major = Integer.parseInt(versionSplit[0]);
int minor = Integer.parseInt(versionSplit[1]);
int build = Integer.parseInt(versionSplit[2]);
final int major = Integer.parseInt(versionSplit[0]);
final int minor = Integer.parseInt(versionSplit[1]);
final int build = Integer.parseInt(versionSplit[2]);
if (major <= 0) {
throw new Exception("Illegal version!");
}
this.major = major;
this.minor = minor;
this.build = build;
@@ -32,6 +32,8 @@ import com.rammelkast.anticheatreloaded.check.CheckType;
import com.rammelkast.anticheatreloaded.config.Configuration;
import com.rammelkast.anticheatreloaded.util.rule.Rule;
import lombok.Getter;
public final class User {
private final UUID uuid;
private final String name;
@@ -52,7 +54,10 @@ public final class User {
private int ping = -1;
private int lastPing = -1;
private final MovementManager movementManager = new MovementManager();
@Getter
private final MovementManager movementManager;
@Getter
private final VelocityTracker velocityTracker;
/**
* Initialize an AntiCheat user
@@ -64,6 +69,10 @@ public final class User {
this.name = getPlayer() != null && getPlayer().isOnline() ? getPlayer().getName() : "";
this.id = getPlayer() != null && getPlayer().isOnline() ? getPlayer().getEntityId() : -1;
this.ping = getPlayer() != null && getPlayer().isOnline() ? VersionUtil.getPlayerPing(getPlayer()) : -1;
this.movementManager = new MovementManager();
this.velocityTracker = new VelocityTracker(this.config.getMagic().VELOCITY_TIME());
setIsWaitingOnLevelSync(true);
config.getLevels().loadLevelToUser(this);
}
@@ -412,7 +421,7 @@ public final class User {
}
public int getLastPing() {
int lastPing = this.lastPing;
final int lastPing = this.lastPing;
if (lastPing < 0) {
return this.getPing();
}
@@ -424,10 +433,6 @@ public final class User {
&& this.ping > config.getMagic().LAG_DETERMINATION();
}
public MovementManager getMovementManager() {
return this.movementManager;
}
@Override
public String toString() {
return "User {name = " + getName() + ", level = " + level + "}";
@@ -0,0 +1,49 @@
package com.rammelkast.anticheatreloaded.util;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.util.Vector;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public final class VelocityTracker {
private final long velocitizedTime;
private final List<VelocityWrapper> velocities = new ArrayList<>();
public void registerVelocity(final Vector vector) {
this.velocities.add(new VelocityWrapper(vector.getX(), vector.getY(), vector.getZ(),
Math.hypot(vector.getX(), vector.getZ()), Math.abs(vector.getY()), System.currentTimeMillis()));
}
public void tick() {
this.velocities
.removeIf(velocity -> (velocity.getTimestamp() + this.velocitizedTime < System.currentTimeMillis()));
}
public double getHorizontal() {
return Math
.sqrt(this.velocities.parallelStream().mapToDouble(VelocityWrapper::getHorizontal).max().orElse(0.0D));
}
public double getVertical() {
return Math
.sqrt(this.velocities.parallelStream().mapToDouble(VelocityWrapper::getVertical).max().orElse(0.0D));
}
public boolean isVelocitized() {
return this.velocities.size() != 0;
}
@RequiredArgsConstructor
@Getter
private class VelocityWrapper {
private final double motionX, motionY, motionZ;
private final double horizontal, vertical;
private final long timestamp;
}
}