Skip to content

Commit

Permalink
Java 17 update
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Sep 23, 2024
1 parent da1468e commit 827e764
Show file tree
Hide file tree
Showing 46 changed files with 396 additions and 702 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ plugins {
group = "ac.grim.grimac"
version = "2.3.68"
description = "Libre simulation anticheat designed for 1.21 with 1.8-1.21 support, powered by PacketEvents 2.0."
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

// Set to false for debug builds
// You cannot live reload classes if the jar relocates dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public void process(final RotationUpdate rotationUpdate) {

if (this.xRotMode.size() > SIGNIFICANT_SAMPLES_THRESHOLD) {
Pair<Double, Integer> modeX = this.xRotMode.getMode();
if (modeX.getSecond() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeX = modeX.getFirst();
if (modeX.second() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeX = modeX.first();
this.sensitivityX = convertToSensitivity(this.modeX);
}
}
if (this.yRotMode.size() > SIGNIFICANT_SAMPLES_THRESHOLD) {
Pair<Double, Integer> modeY = this.yRotMode.getMode();
if (modeY.getSecond() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeY = modeY.getFirst();
if (modeY.second() > SIGNIFICANT_SAMPLES_THRESHOLD) {
this.modeY = modeY.first();
this.sensitivityY = convertToSensitivity(this.modeY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
boolean hasID = false;

for (Pair<Long, Long> iterator : keepaliveMap) {
if (iterator.getFirst() == id) {
if (iterator.first() == id) {
hasID = true;
break;
}
Expand All @@ -55,7 +55,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
do {
data = keepaliveMap.poll();
if (data == null) break;
} while (data.getFirst() != id);
} while (data.first() != id);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ac/grim/grimac/checks/impl/combat/Reach.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private String checkReach(PacketEntity reachEntity, Vector3d from, boolean isPre
Vector eyePos = new Vector(from.getX(), from.getY() + eye, from.getZ());
Vector endReachPos = eyePos.clone().add(new Vector(lookVec.getX() * distance, lookVec.getY() * distance, lookVec.getZ() * distance));

Vector intercept = ReachUtils.calculateIntercept(targetBox, eyePos, endReachPos).getFirst();
Vector intercept = ReachUtils.calculateIntercept(targetBox, eyePos, endReachPos).first();

if (ReachUtils.isVecInside(targetBox, eyePos)) {
minDistance = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,21 @@ public void onBlockPlace(final BlockPlace place) {

// So now we have the player's possible eye positions
// So then look at the face that the player has clicked
boolean flag = false;
switch (place.getDirection()) {
case NORTH: // Z- face
flag = eyePositions.minZ > combined.minZ;
break;
case SOUTH: // Z+ face
flag = eyePositions.maxZ < combined.maxZ;
break;
case EAST: // X+ face
flag = eyePositions.maxX < combined.maxX;
break;
case WEST: // X- face
flag = eyePositions.minX > combined.minX;
break;
case UP: // Y+ face
flag = eyePositions.maxY < combined.maxY;
break;
case DOWN: // Y- face
flag = eyePositions.minY > combined.minY;
break;
}
boolean flag = switch (place.getDirection()) {
case NORTH -> // Z- face
eyePositions.minZ > combined.minZ;
case SOUTH -> // Z+ face
eyePositions.maxZ < combined.maxZ;
case EAST -> // X+ face
eyePositions.maxX < combined.maxX;
case WEST -> // X- face
eyePositions.minX > combined.minX;
case UP -> // Y+ face
eyePositions.maxY < combined.maxY;
case DOWN -> // Y- face
eyePositions.minY > combined.minY;
default -> false;
};

if (flag) {
if (flagAndAlert() && shouldModifyPackets() && shouldCancel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private boolean didRayTraceHit(BlockPlace place) {
Ray trace = new Ray(player, starting.getX(), starting.getY(), starting.getZ(), lookDir.getX(), lookDir.getY());
Pair<Vector, BlockFace> intercept = ReachUtils.calculateIntercept(box, trace.getOrigin(), trace.getPointAtDistance(distance));

if (intercept.getFirst() != null) return true;
if (intercept.first() != null) return true;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ac/grim/grimac/commands/GrimSpectate.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class GrimSpectate extends BaseCommand {
@CommandPermission("grim.spectate")
@CommandCompletion("@players")
public void onSpectate(CommandSender sender, @Optional OnlinePlayer target) {
if (!(sender instanceof Player)) return;
Player player = (Player) sender;
if (!(sender instanceof Player player)) return;

if (target != null && target.getPlayer().getUniqueId().equals(player.getUniqueId())) {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("cannot-run-on-self", "%prefix% &cYou cannot use this command on yourself!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class GrimStopSpectating extends BaseCommand {
@CommandCompletion("here")
public void onStopSpectate(CommandSender sender, String[] args) {
String string = args.length > 0 ? args[0] : null;
if (!(sender instanceof Player)) return;
Player player = (Player) sender;
if (!(sender instanceof Player player)) return;
if (GrimAPI.INSTANCE.getSpectateManager().isSpectating(player.getUniqueId())) {
boolean teleportBack = string == null || !string.equalsIgnoreCase("here");
GrimAPI.INSTANCE.getSpectateManager().disable(player, teleportBack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,10 @@ private static void handleUseItem(GrimPlayer player, ItemStack placedWith, Inter
}
}

private static void handleBlockPlaceOrUseItem(PacketWrapper packet, GrimPlayer player) {
private static void handleBlockPlaceOrUseItem(PacketWrapper<?> packet, GrimPlayer player) {
// Legacy "use item" packet
if (packet instanceof WrapperPlayClientPlayerBlockPlacement &&
if (packet instanceof WrapperPlayClientPlayerBlockPlacement place &&
PacketEvents.getAPI().getServerManager().getVersion().isOlderThan(ServerVersion.V_1_9)) {
WrapperPlayClientPlayerBlockPlacement place = (WrapperPlayClientPlayerBlockPlacement) packet;

if (player.gamemode == GameMode.SPECTATOR || player.gamemode == GameMode.ADVENTURE) return;

Expand All @@ -243,9 +242,7 @@ private static void handleBlockPlaceOrUseItem(PacketWrapper packet, GrimPlayer p
}
}

if (packet instanceof WrapperPlayClientUseItem) {
WrapperPlayClientUseItem place = (WrapperPlayClientUseItem) packet;

if (packet instanceof WrapperPlayClientUseItem place) {
if (player.gamemode == GameMode.SPECTATOR || player.gamemode == GameMode.ADVENTURE) return;

ItemStack placedWith = player.getInventory().getHeldItem();
Expand All @@ -257,9 +254,7 @@ private static void handleBlockPlaceOrUseItem(PacketWrapper packet, GrimPlayer p
}

// Check for interactable first (door, etc)
if (packet instanceof WrapperPlayClientPlayerBlockPlacement) {
WrapperPlayClientPlayerBlockPlacement place = (WrapperPlayClientPlayerBlockPlacement) packet;

if (packet instanceof WrapperPlayClientPlayerBlockPlacement place) {
ItemStack placedWith = player.getInventory().getHeldItem();
ItemStack offhand = player.getInventory().getOffHand();

Expand Down Expand Up @@ -295,8 +290,7 @@ private static void handleBlockPlaceOrUseItem(PacketWrapper packet, GrimPlayer p
}
}

if (packet instanceof WrapperPlayClientPlayerBlockPlacement) {
WrapperPlayClientPlayerBlockPlacement place = (WrapperPlayClientPlayerBlockPlacement) packet;
if (packet instanceof WrapperPlayClientPlayerBlockPlacement place) {
Vector3i blockPosition = place.getBlockPosition();
BlockFace face = place.getFace();

Expand Down Expand Up @@ -797,14 +791,14 @@ private static HitData getNearestHitResult(GrimPlayer player, StateType heldItem

for (SimpleCollisionBox box : boxes) {
Pair<Vector, BlockFace> intercept = ReachUtils.calculateIntercept(box, trace.getOrigin(), trace.getPointAtDistance(distance));
if (intercept.getFirst() == null) continue; // No intercept
if (intercept.first() == null) continue; // No intercept

Vector hitLoc = intercept.getFirst();
Vector hitLoc = intercept.first();

if (hitLoc.distanceSquared(startingVec) < bestHitResult) {
bestHitResult = hitLoc.distanceSquared(startingVec);
bestHitLoc = hitLoc;
bestFace = intercept.getSecond();
bestFace = intercept.second();
}
}
if (bestHitLoc != null) {
Expand All @@ -819,8 +813,8 @@ private static HitData getNearestHitResult(GrimPlayer player, StateType heldItem

Pair<Vector, BlockFace> intercept = ReachUtils.calculateIntercept(box, trace.getOrigin(), trace.getPointAtDistance(distance));

if (intercept.getFirst() != null) {
return new HitData(vector3i, intercept.getFirst(), intercept.getSecond(), block);
if (intercept.first() != null) {
return new HitData(vector3i, intercept.first(), intercept.second(), block);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ public void onPacketSend(PacketSendEvent event) {

if (status.getStatus() == 31) {
PacketEntity hook = player.compensatedEntities.getEntity(status.getEntityId());
if (!(hook instanceof PacketEntityHook)) return;
if (!(hook instanceof PacketEntityHook hookEntity)) return;

PacketEntityHook hookEntity = (PacketEntityHook) hook;
if (hookEntity.attached == player.entityID) {
player.sendTransaction();
// We don't transaction sandwich this, it's too rare to be a real problem.
Expand Down Expand Up @@ -455,8 +454,7 @@ private void handleMoveEntity(PacketSendEvent event, int entityId, double deltaX
player.latencyUtils.addRealTimeTask(lastTrans, () -> {
PacketEntity entity = player.compensatedEntities.getEntity(entityId);
if (entity == null) return;
if (entity instanceof PacketEntityTrackXRot && yaw != null) {
PacketEntityTrackXRot xRotEntity = (PacketEntityTrackXRot) entity;
if (entity instanceof PacketEntityTrackXRot xRotEntity && yaw != null) {
xRotEntity.packetYaw = yaw;
xRotEntity.steps = entity.isBoat() ? 10 : 3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class PacketWorldReaderEight extends BasePacketWorldReader {
@Override
public void handleMapChunkBulk(final GrimPlayer player, final PacketSendEvent event) {
PacketWrapper wrapper = new PacketWrapper(event);
PacketWrapper<?> wrapper = new PacketWrapper<>(event);
ByteBuf buffer = (ByteBuf) wrapper.getBuffer();

boolean skylight = wrapper.readBoolean();
Expand Down Expand Up @@ -47,7 +47,7 @@ public void handleMapChunkBulk(final GrimPlayer player, final PacketSendEvent ev

@Override
public void handleMapChunk(final GrimPlayer player, final PacketSendEvent event) {
PacketWrapper wrapper = new PacketWrapper(event);
PacketWrapper<?> wrapper = new PacketWrapper<>(event);

final int chunkX = wrapper.readInt();
final int chunkZ = wrapper.readInt();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/ac/grim/grimac/manager/SetbackTeleportUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ private void blockMovementsUntilResync(boolean simulateNextTickPosition, boolean
VelocityData futureExplosion = player.checkManager.getExplosionHandler().getFutureExplosion();

// Velocity sets
if (futureKb.getFirst() != null) {
clientVel = futureKb.getSecond();
if (futureKb.first() != null) {
clientVel = futureKb.second();
}

// Explosion adds
if (futureExplosion != null && (futureKb.getFirst() == null || futureKb.getFirst().transaction < futureExplosion.transaction)) {
if (futureExplosion != null && (futureKb.first() == null || futureKb.first().transaction < futureExplosion.transaction)) {
clientVel.add(futureExplosion.vector);
}

Expand Down Expand Up @@ -328,16 +328,16 @@ public boolean checkVehicleTeleportQueue(double x, double y, double z) {
while (true) {
Pair<Integer, Vector3d> teleportPos = player.vehicleData.vehicleTeleports.peek();
if (teleportPos == null) break;
if (lastTransaction < teleportPos.getFirst()) {
if (lastTransaction < teleportPos.first()) {
break;
}

Vector3d position = teleportPos.getSecond();
Vector3d position = teleportPos.second();
if (position.getX() == x && position.getY() == y && position.getZ() == z) {
player.vehicleData.vehicleTeleports.poll();

return true;
} else if (lastTransaction > teleportPos.getFirst() + 1) {
} else if (lastTransaction > teleportPos.first() + 1) {
player.vehicleData.vehicleTeleports.poll();

// Vehicles have terrible netcode so just ignore it if the teleport wasn't from us setting the player back
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/ac/grim/grimac/manager/SpectateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ public void handlePlayerStopSpectating(UUID uuid) {
spectatingPlayers.remove(uuid);
}

private static class PreviousState {
public PreviousState(org.bukkit.GameMode gameMode, Location location) {
this.gameMode = gameMode;
this.location = location;
}
private record PreviousState(org.bukkit.GameMode gameMode, Location location) {

private final org.bukkit.GameMode gameMode;
private final Location location;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void start() {
// but whatever. At least plugins can't break it, I guess.
//
// Pledge injects into another list, so we should be safe injecting into this one
List<?> wrapper = Collections.synchronizedList(new HookedListWrapper<Object>(endOfTickObject) {
List<?> wrapper = Collections.synchronizedList(new HookedListWrapper<>(endOfTickObject) {
@Override
public void onIterator() {
hasTicked = true;
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/ac/grim/grimac/player/GrimPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.github.retrooper.packetevents.protocol.player.GameMode;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.protocol.world.BlockFace;
import com.github.retrooper.packetevents.protocol.world.Dimension;
import com.github.retrooper.packetevents.protocol.world.dimension.DimensionType;
import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
Expand Down Expand Up @@ -305,7 +304,7 @@ public boolean addTransactionResponse(short id) {
boolean hasID = false;
int skipped = 0;
for (Pair<Short, Long> iterator : transactionsSent) {
if (iterator.getFirst() == id) {
if (iterator.first() == id) {
hasID = true;
break;
}
Expand All @@ -325,17 +324,17 @@ public boolean addTransactionResponse(short id) {

lastTransactionReceived.incrementAndGet();
lastTransReceived = System.currentTimeMillis();
transactionPing = (System.nanoTime() - data.getSecond());
playerClockAtLeast = data.getSecond();
} while (data.getFirst() != id);
transactionPing = (System.nanoTime() - data.second());
playerClockAtLeast = data.second();
} while (data.first() != id);

// A transaction means a new tick, so apply any block places
CheckManagerListener.handleQueuedPlaces(this, false, 0, 0, System.currentTimeMillis());
latencyUtils.handleNettySyncTransaction(lastTransactionReceived.get());
}

// Were we the ones who sent the packet?
return data != null && data.getFirst() == id;
return data != null && data.first() == id;
}

public void baseTickAddWaterPushing(Vector vector) {
Expand Down Expand Up @@ -421,8 +420,7 @@ public void timedOut() {

public void disconnect(Component reason) {
String textReason;
if (reason instanceof TranslatableComponent) {
TranslatableComponent translatableComponent = (TranslatableComponent) reason;
if (reason instanceof TranslatableComponent translatableComponent) {
textReason = translatableComponent.key();
} else {
textReason = LegacyComponentSerializer.legacySection().serialize(reason);
Expand Down
Loading

0 comments on commit 827e764

Please sign in to comment.