fix: hopefully fix core using old position after receiving teleport packet from server

This commit is contained in:
Chayapak Supasakul 2025-04-19 10:02:48 +07:00
parent ea16395c15
commit 0cc74be280
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
4 changed files with 57 additions and 12 deletions

View file

@ -2,6 +2,7 @@ package land.chipmunk.chipmunkmod.listeners;
import net.minecraft.network.packet.Packet;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec3d;
public interface Listener {
default void packetReceived (final Packet<?> packet) { }
@ -15,4 +16,6 @@ public interface Listener {
default void coreReady () { }
default void coreMoved () { }
default void positionChanged (final Vec3d newPosition) { }
}

View file

@ -9,18 +9,22 @@ import land.chipmunk.chipmunkmod.listeners.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import land.chipmunk.chipmunkmod.modules.*;
import land.chipmunk.chipmunkmod.modules.custom_chat.CustomChat;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.network.packet.s2c.play.CommandTreeS2CPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.*;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.resource.featuretoggle.FeatureSet;
import net.minecraft.text.PlainTextContent;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@ -29,7 +33,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(value = ClientPlayNetworkHandler.class, priority = 1001)
public class ClientPlayNetworkHandlerMixin {
public abstract class ClientPlayNetworkHandlerMixin {
@Final
@Shadow
private FeatureSet enabledFeatures;
@ -39,6 +43,8 @@ public class ClientPlayNetworkHandlerMixin {
@Shadow
private CommandDispatcher<CommandSource> commandDispatcher;
@Shadow public abstract ClientWorld getWorld ();
@Inject(method = "onGameJoin", at = @At("TAIL"))
private void onGameJoin (final GameJoinS2CPacket packet, final CallbackInfo ci) {
final CommandRegistryAccess commandRegistryAccess = CommandRegistryAccess.of(this.combinedDynamicRegistries, this.enabledFeatures);
@ -136,4 +142,28 @@ public class ClientPlayNetworkHandlerMixin {
CustomChat.INSTANCE.chat(content);
ci.cancel();
}
@Inject(method = "onPlayerPositionLook", at = @At("TAIL"))
private void setPosition (final PlayerPositionLookS2CPacket packet, final CallbackInfo ci) {
final Vec3d position = packet.change().position();
final BlockPos origin = CommandCore.INSTANCE.origin;
if (origin == null) {
CommandCore.INSTANCE.move(position);
return;
}
final MinecraftClient client = MinecraftClient.getInstance();
final ClientPlayerEntity player = client.player;
if (player == null) return;
final int distanceSquared = player.getChunkPos().getSquaredDistance(new ChunkPos(origin));
final int distance = (int) Math.sqrt(distanceSquared);
if (distance > getWorld().getSimulationDistance()) {
CommandCore.INSTANCE.alreadyFilled = true;
CommandCore.INSTANCE.move(position);
}
}
}

View file

@ -37,7 +37,7 @@ public abstract class ClientPlayerEntityMixin extends Entity {
final int distance = (int) Math.sqrt(distanceSquared);
if (distance > networkHandler.getWorld().getSimulationDistance()) {
CommandCore.INSTANCE.clientPlayerEntityFilled = true;
CommandCore.INSTANCE.alreadyFilled = true;
CommandCore.INSTANCE.move(this.getPos());
}
}

View file

@ -32,7 +32,7 @@ public class CommandCore {
public BlockPos block;
public BlockBox withPos;
public boolean runFillCommand = true;
public boolean clientPlayerEntityFilled = false;
public boolean alreadyFilled = false;
private Timer timer;
private boolean shouldRefill = false;
private DimensionType oldDimension;
@ -54,8 +54,8 @@ public class CommandCore {
final TimerTask refillTask = new TimerTask() {
@Override
public void run () {
if (clientPlayerEntityFilled) {
clientPlayerEntityFilled = false;
if (alreadyFilled) {
alreadyFilled = false;
return;
}
@ -75,6 +75,8 @@ public class CommandCore {
timer.schedule(refillTask, 50, 1000);
if (client.player == null) return;
move(client.player.getPos());
}
@ -163,7 +165,9 @@ public class CommandCore {
}
public void refill () {
if (!runFillCommand || client.world == null || withPos == null) return;
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (!runFillCommand || client.world == null || networkHandler == null || withPos == null) return;
final Chunk chunk = client.world.getChunk(withPos.getCenter());
@ -209,7 +213,11 @@ public class CommandCore {
public void run (final String command) {
if (command.length() > 32767) return;
final ClientConnection connection = client.getNetworkHandler().getConnection();
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) return;
final ClientConnection connection = networkHandler.getConnection();
if (block == null) return;
@ -254,7 +262,11 @@ public class CommandCore {
}
public CompletableFuture<NbtCompound> runTracked (final String command) {
final ClientConnection connection = client.getNetworkHandler().getConnection();
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler == null) return new CompletableFuture<>();
final ClientConnection connection = networkHandler.getConnection();
if (block == null) return new CompletableFuture<>();
@ -303,7 +315,7 @@ public class CommandCore {
public void run () {
client.getNetworkHandler().getDataQueryHandler().queryBlockNbt(block, future::complete);
timer.cancel(); // ? Is this necesary?
timer.cancel(); // ? Is this necessary?
timer.purge();
}
};