Skip to content

Commit

Permalink
Fixed client freezing when joining a server and the world has particl…
Browse files Browse the repository at this point in the history
…es. (CCBlueX#1614)
  • Loading branch information
mems01 authored Dec 3, 2023
1 parent cfd1d33 commit 8ab96ab
Showing 1 changed file with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.DownloadingTerrainScreen;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;
Expand All @@ -53,30 +54,30 @@ public class MixinClientPlayNetworkHandler {

@Shadow
@Final
private ClientConnection connection;
private MinecraftClient client;

@Shadow
@Final
private MinecraftClient client;
private ClientConnection connection;

@Inject(method = "onChunkData", at = @At("RETURN"))
private void injectChunkLoadEvent(final ChunkDataS2CPacket packet, final CallbackInfo ci) {
private void injectChunkLoadEvent(ChunkDataS2CPacket packet, CallbackInfo ci) {
EventManager.INSTANCE.callEvent(new ChunkLoadEvent(packet.getX(), packet.getZ()));
}

@Inject(method = "onUnloadChunk", at = @At("RETURN"))
private void injectUnloadEvent(final UnloadChunkS2CPacket packet, final CallbackInfo ci) {
private void injectUnloadEvent(UnloadChunkS2CPacket packet, CallbackInfo ci) {
EventManager.INSTANCE.callEvent(new ChunkUnloadEvent(packet.getX(), packet.getZ()));
}

@Redirect(method = "onExplosion", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/Vec3d;add(DDD)Lnet/minecraft/util/math/Vec3d;"))
private Vec3d onExplosionVelocity(final Vec3d instance, final double x, final double y, final double z) {
final Vec3d originalVector = new Vec3d(x, y, z);
private Vec3d onExplosionVelocity(Vec3d instance, double x, double y, double z) {
Vec3d originalVector = new Vec3d(x, y, z);
if (ModuleAntiExploit.INSTANCE.getEnabled() && ModuleAntiExploit.INSTANCE.getLimitExplosionStrength()) {
final double fixedX = MathHelper.clamp(x, -1000.0, 1000.0);
final double fixedY = MathHelper.clamp(y, -1000.0, 1000.0);
final double fixedZ = MathHelper.clamp(z, -1000.0, 1000.0);
final Vec3d newVector = new Vec3d(fixedX, fixedY, fixedZ);
double fixedX = MathHelper.clamp(x, -1000.0, 1000.0);
double fixedY = MathHelper.clamp(y, -1000.0, 1000.0);
double fixedZ = MathHelper.clamp(z, -1000.0, 1000.0);
Vec3d newVector = new Vec3d(fixedX, fixedY, fixedZ);
if (!originalVector.equals(newVector)) {
ModuleAntiExploit.INSTANCE.notifyAboutExploit("Limited too strong explosion", true);
return instance.add(newVector);
Expand All @@ -85,8 +86,8 @@ private Vec3d onExplosionVelocity(final Vec3d instance, final double x, final do
return instance.add(x, y, z);
}

@Redirect(method = "onParticle", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/ParticleS2CPacket;getCount()I"))
private int onParticleAmount(final ParticleS2CPacket instance) {
@Redirect(method = "onParticle", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/ParticleS2CPacket;getCount()I", ordinal = 1))
private int onParticleAmount(ParticleS2CPacket instance) {
if (ModuleAntiExploit.INSTANCE.getEnabled() && ModuleAntiExploit.INSTANCE.getLimitParticlesAmount() && 500 <= instance.getCount()) {
ModuleAntiExploit.INSTANCE.notifyAboutExploit("Limited too many particles", true);
return 100;
Expand All @@ -95,18 +96,18 @@ private int onParticleAmount(final ParticleS2CPacket instance) {
}

@Redirect(method = "onParticle", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/ParticleS2CPacket;getSpeed()F"))
private float onParticleSpeed(final ParticleS2CPacket instance) {
private float onParticleSpeed(ParticleS2CPacket instance) {
if (ModuleAntiExploit.INSTANCE.getEnabled() && ModuleAntiExploit.INSTANCE.getLimitParticlesSpeed() && 10.0f <= instance.getSpeed()) {
ModuleAntiExploit.INSTANCE.notifyAboutExploit("Limited too fast particles speed", true);
return 10.0f;
}
return instance.getCount();
return instance.getSpeed();
}

@Redirect(method = "onExplosion", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/ExplosionS2CPacket;getRadius()F"))
private float onExplosionWorld(final ExplosionS2CPacket instance) {
if (ModuleAntiExploit.INSTANCE.getLimitExplosionRange()) {
final float radius = MathHelper.clamp(instance.getRadius(), -1000.0f, 1000.0f);
private float onExplosionWorld(ExplosionS2CPacket instance) {
if (ModuleAntiExploit.INSTANCE.getEnabled() && ModuleAntiExploit.INSTANCE.getLimitExplosionRange()) {
float radius = MathHelper.clamp(instance.getRadius(), -1000.0f, 1000.0f);
if (radius != instance.getRadius()) {
ModuleAntiExploit.INSTANCE.notifyAboutExploit("Limited too big TNT explosion radius", true);
return radius;
Expand All @@ -116,27 +117,33 @@ private float onExplosionWorld(final ExplosionS2CPacket instance) {
}

@Redirect(method = "onGameStateChange", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/GameStateChangeS2CPacket;getReason()Lnet/minecraft/network/packet/s2c/play/GameStateChangeS2CPacket$Reason;"))
private GameStateChangeS2CPacket.Reason onGameStateChange(final GameStateChangeS2CPacket instance) {
private GameStateChangeS2CPacket.Reason onGameStateChange(GameStateChangeS2CPacket instance) {
if (ModuleAntiExploit.INSTANCE.getEnabled() && instance.getReason() == GameStateChangeS2CPacket.DEMO_MESSAGE_SHOWN && ModuleAntiExploit.INSTANCE.getCancelDemo()) {
ModuleAntiExploit.INSTANCE.notifyAboutExploit("Cancelled demo GUI (just annoying thing)", false);
return null;
} else {
return instance.getReason();
}
return instance.getReason();
}

@Inject(method = "onHealthUpdate", at = @At("RETURN"))
private void injectHealthUpdate(HealthUpdateS2CPacket packet, CallbackInfo ci) {
EventManager.INSTANCE.callEvent(new HealthUpdateEvent(packet.getHealth(), packet.getFood(), packet.getSaturation(), client.player.getHealth()));
ClientPlayerEntity player = this.client.player;

if (player == null) {
return;
}

EventManager.INSTANCE.callEvent(new HealthUpdateEvent(packet.getHealth(), packet.getFood(), packet.getSaturation(), player.getHealth()));

if (packet.getHealth() == 0) {
EventManager.INSTANCE.callEvent(new DeathEvent());
}
}

@Inject(method = "onPlayerPositionLook", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;setVelocity(DDD)V", shift = At.Shift.AFTER), locals = LocalCapture.CAPTURE_FAILHARD)
private void injectNoRotateSet(final PlayerPositionLookS2CPacket packet, final CallbackInfo ci, final PlayerEntity playerEntity, final Vec3d vec3d, final boolean bl, final boolean bl2, final boolean bl3, final double d, final double e, final double f, final double g, final double h, final double i) {
final float j = packet.getYaw();
final float k = packet.getPitch();
private void injectNoRotateSet(PlayerPositionLookS2CPacket packet, CallbackInfo ci, PlayerEntity playerEntity, Vec3d vec3d, boolean bl, boolean bl2, boolean bl3, double d, double e, double f, double g, double h, double i) {
float j = packet.getYaw();
float k = packet.getPitch();

if (!ModuleNoRotateSet.INSTANCE.getEnabled() || MinecraftClient.getInstance().currentScreen instanceof DownloadingTerrainScreen) {
return;
Expand All @@ -146,11 +153,10 @@ private void injectNoRotateSet(final PlayerPositionLookS2CPacket packet, final C
this.connection.send(new TeleportConfirmC2SPacket(packet.getTeleportId()));
// Silently accept yaw and pitch values requested by the server.
this.connection.send(new PlayerMoveC2SPacket.Full(playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), j, k, false));
final Choice activeChoice = ModuleNoRotateSet.INSTANCE.getMode().getActiveChoice();
Choice activeChoice = ModuleNoRotateSet.INSTANCE.getMode().getActiveChoice();
if (activeChoice.equals(ModuleNoRotateSet.ResetRotation.INSTANCE)) {
// Changes your server side rotation and then resets it with provided settings
var aimPlan = ModuleNoRotateSet.ResetRotation.INSTANCE.getRotationsConfigurable().toAimPlan(new Rotation(j, k),
true);
var aimPlan = ModuleNoRotateSet.ResetRotation.INSTANCE.getRotationsConfigurable().toAimPlan(new Rotation(j, k), true);
RotationManager.INSTANCE.aimAt(aimPlan);
} else {
// Increase yaw and pitch by a value so small that the difference cannot be seen, just to update the rotation server-side.
Expand Down

0 comments on commit 8ab96ab

Please sign in to comment.