| 1 | package com.jellypudding.offlineclient.mixin;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.OfflineClient;
|
| 4 | import com.jellypudding.offlineclient.event.events.EntityAddedEvent;
|
| 5 | import com.jellypudding.offlineclient.modules.player.NoRotate;
|
| 6 | import com.jellypudding.offlineclient.modules.render.NewChunks;
|
| 7 | import com.jellypudding.offlineclient.modules.render.NoRender;
|
| 8 | import com.jellypudding.offlineclient.util.Modules;
|
| 9 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 10 | import net.minecraft.client.player.LocalPlayer;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
|
| 14 | import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket;
|
| 15 | import net.minecraft.network.protocol.game.ClientboundPlayerRotationPacket;
|
| 16 | import org.spongepowered.asm.mixin.Mixin;
|
| 17 | import org.spongepowered.asm.mixin.Unique;
|
| 18 | import org.spongepowered.asm.mixin.injection.At;
|
| 19 | import org.spongepowered.asm.mixin.injection.Inject;
|
| 20 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
| 21 |
|
| 22 | @Mixin(ClientPacketListener.class)
|
| 23 | public abstract class ClientPacketListenerMixin {
|
| 24 |
|
| 25 | // Dropped on the network thread before the packet is handed to the main thread.
|
| 26 | @Inject(method = "handleAddEntity(Lnet/minecraft/network/protocol/game/ClientboundAddEntityPacket;)V",
|
| 27 | at = @At("HEAD"), cancellable = true)
|
| 28 | private void onDropAddEntity(ClientboundAddEntityPacket packet, CallbackInfo ci) {
|
| 29 | NoRender noRender = Modules.get(NoRender.class);
|
| 30 | if (noRender != null && noRender.dropsSpawnPacket(packet.getType())) {
|
| 31 | ci.cancel();
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | // Runs once on the network thread to bounce to the main thread. Only the main run has the entity.
|
| 36 | @Inject(method = "handleAddEntity(Lnet/minecraft/network/protocol/game/ClientboundAddEntityPacket;)V",
|
| 37 | at = @At("TAIL"))
|
| 38 | private void onAddEntity(ClientboundAddEntityPacket packet, CallbackInfo ci) {
|
| 39 | if (!OfflineClient.MC.isSameThread() || OfflineClient.MC.level == null) {
|
| 40 | return;
|
| 41 | }
|
| 42 | Entity entity = OfflineClient.MC.level.getEntity(packet.getId());
|
| 43 | if (entity != null) {
|
| 44 | OfflineClient.INSTANCE.getEventBus().post(new EntityAddedEvent(entity));
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Unique
|
| 49 | private float offlineclient$savedYaw;
|
| 50 | @Unique
|
| 51 | private float offlineclient$savedPitch;
|
| 52 |
|
| 53 | // Packet handlers run once on the network thread to bounce onto the main thread.
|
| 54 | // Only the main thread run touches the player.
|
| 55 | @Unique
|
| 56 | private boolean offlineclient$noRotateActive() {
|
| 57 | if (!OfflineClient.MC.isSameThread() || OfflineClient.MC.player == null) {
|
| 58 | return false;
|
| 59 | }
|
| 60 | return Modules.enabled(NoRotate.class);
|
| 61 | }
|
| 62 |
|
| 63 | @Unique
|
| 64 | private void offlineclient$saveRotation() {
|
| 65 | LocalPlayer player = OfflineClient.MC.player;
|
| 66 | offlineclient$savedYaw = player.getYRot();
|
| 67 | offlineclient$savedPitch = player.getXRot();
|
| 68 | }
|
| 69 |
|
| 70 | // The tiny offset makes the next position update carry a rotation.
|
| 71 | @Unique
|
| 72 | private void offlineclient$restoreRotation() {
|
| 73 | LocalPlayer player = OfflineClient.MC.player;
|
| 74 | player.setYRot(offlineclient$savedYaw + 0.000001f);
|
| 75 | player.setXRot(offlineclient$savedPitch + 0.000001f);
|
| 76 | player.yHeadRot = offlineclient$savedYaw;
|
| 77 | player.yBodyRot = offlineclient$savedYaw;
|
| 78 | }
|
| 79 |
|
| 80 | // The chunk is in the world and every later packet is still queued behind this one.
|
| 81 | // NewChunks reads the liquid here whilst the data is untouched.
|
| 82 | @Inject(
|
| 83 | method = "handleLevelChunkWithLight"
|
| 84 | + "(Lnet/minecraft/network/protocol/game/ClientboundLevelChunkWithLightPacket;)V",
|
| 85 | at = @At("TAIL"))
|
| 86 | private void onChunkLoaded(ClientboundLevelChunkWithLightPacket packet, CallbackInfo ci) {
|
| 87 | NewChunks newChunks = Modules.get(NewChunks.class);
|
| 88 | if (newChunks != null) {
|
| 89 | newChunks.onChunkLoaded(packet.getX(), packet.getZ());
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Inject(
|
| 94 | method = "handleMovePlayer(Lnet/minecraft/network/protocol/game/ClientboundPlayerPositionPacket;)V",
|
| 95 | at = @At("HEAD"))
|
| 96 | private void onMovePlayerHead(ClientboundPlayerPositionPacket packet, CallbackInfo ci) {
|
| 97 | if (offlineclient$noRotateActive()) {
|
| 98 | offlineclient$saveRotation();
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | @Inject(
|
| 103 | method = "handleMovePlayer(Lnet/minecraft/network/protocol/game/ClientboundPlayerPositionPacket;)V",
|
| 104 | at = @At("RETURN"))
|
| 105 | private void onMovePlayerReturn(ClientboundPlayerPositionPacket packet, CallbackInfo ci) {
|
| 106 | if (offlineclient$noRotateActive()) {
|
| 107 | offlineclient$restoreRotation();
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Inject(
|
| 112 | method = "handleRotatePlayer(Lnet/minecraft/network/protocol/game/ClientboundPlayerRotationPacket;)V",
|
| 113 | at = @At("HEAD"))
|
| 114 | private void onRotatePlayerHead(ClientboundPlayerRotationPacket packet, CallbackInfo ci) {
|
| 115 | if (offlineclient$noRotateActive()) {
|
| 116 | offlineclient$saveRotation();
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @Inject(
|
| 121 | method = "handleRotatePlayer(Lnet/minecraft/network/protocol/game/ClientboundPlayerRotationPacket;)V",
|
| 122 | at = @At("RETURN"))
|
| 123 | private void onRotatePlayerReturn(ClientboundPlayerRotationPacket packet, CallbackInfo ci) {
|
| 124 | if (offlineclient$noRotateActive()) {
|
| 125 | offlineclient$restoreRotation();
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|