From 2ad9b890d44afa1d69cf362f38e0275663049486 Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 23 Mar 2024 15:32:01 +0000 Subject: [PATCH] Translate carpet tick rate packets --- .../viavanillaplus/carpet/TickRateState.java | 8 ++ .../carpet/Protocol1_20_3To1_20_2Mixin.java | 88 +++++++++++++++++++ src/main/resources/viavanillaplus.mixins.json | 1 + 3 files changed, 97 insertions(+) create mode 100644 src/main/java/net/earthcomputer/viavanillaplus/carpet/TickRateState.java create mode 100644 src/main/java/net/earthcomputer/viavanillaplus/mixin/carpet/Protocol1_20_3To1_20_2Mixin.java diff --git a/src/main/java/net/earthcomputer/viavanillaplus/carpet/TickRateState.java b/src/main/java/net/earthcomputer/viavanillaplus/carpet/TickRateState.java new file mode 100644 index 0000000..bf4a98d --- /dev/null +++ b/src/main/java/net/earthcomputer/viavanillaplus/carpet/TickRateState.java @@ -0,0 +1,8 @@ +package net.earthcomputer.viavanillaplus.carpet; + +import com.viaversion.viaversion.api.connection.StorableObject; + +public final class TickRateState implements StorableObject { + public float tickRate = 20; + public boolean isFrozen = false; +} diff --git a/src/main/java/net/earthcomputer/viavanillaplus/mixin/carpet/Protocol1_20_3To1_20_2Mixin.java b/src/main/java/net/earthcomputer/viavanillaplus/mixin/carpet/Protocol1_20_3To1_20_2Mixin.java new file mode 100644 index 0000000..909521f --- /dev/null +++ b/src/main/java/net/earthcomputer/viavanillaplus/mixin/carpet/Protocol1_20_3To1_20_2Mixin.java @@ -0,0 +1,88 @@ +package net.earthcomputer.viavanillaplus.mixin.carpet; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.protocol.AbstractProtocol; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPacket1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPacket1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.Protocol1_20_3To1_20_2; +import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPacket1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPacket1_20_3; +import com.viaversion.viaversion.util.Key; +import net.earthcomputer.viavanillaplus.carpet.TickRateState; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(value = Protocol1_20_3To1_20_2.class, remap = false) +public class Protocol1_20_3To1_20_2Mixin extends AbstractProtocol { + @SuppressWarnings("deprecation") + public Protocol1_20_3To1_20_2Mixin() { + } + + @Inject(method = "registerPackets", at = @At("RETURN")) + private void registerExtraPackets(CallbackInfo ci) { + appendClientbound(ClientboundPackets1_20_2.PLUGIN_MESSAGE, wrapper -> { + wrapper.resetReader(); + String channel = Key.namespaced(wrapper.passthrough(Type.STRING)); + if ("carpet:hello".equals(channel)) { + CompoundTag data = wrapper.read(Type.COMPOUND_TAG); + + Float tickRate = null; + Tag tickRateTag = data.remove("TickRate"); + if (tickRateTag instanceof NumberTag tickRateNumberTag) { + tickRate = tickRateNumberTag.asFloat(); + } + + Boolean isTickFrozen = null; + Tag tickingStateTag = data.remove("TickingState"); + if (tickingStateTag instanceof CompoundTag tickingStateCompound) { + NumberTag isTickFrozenNumber = tickingStateCompound.getNumberTag("is_frozen"); + if (isTickFrozenNumber != null) { + isTickFrozen = isTickFrozenNumber.asBoolean(); + } + } + + if (tickRate != null || isTickFrozen != null) { + TickRateState tickRateState = wrapper.user().get(TickRateState.class); + assert tickRateState != null; + if (tickRate == null) { + tickRate = tickRateState.tickRate; + } else { + tickRateState.tickRate = tickRate; + } + if (isTickFrozen == null) { + isTickFrozen = tickRateState.isFrozen; + } else { + tickRateState.isFrozen = isTickFrozen; + } + System.out.println("Sending " + tickRate + ", " + isTickFrozen); + PacketWrapper tickStatePacket = wrapper.create(ClientboundPackets1_20_3.TICKING_STATE); + tickStatePacket.write(Type.FLOAT, tickRate); + tickStatePacket.write(Type.BOOLEAN, isTickFrozen); + tickStatePacket.send(Protocol1_20_3To1_20_2.class); + } + + data.remove("SuperHotState"); + data.remove("TickPlayerActiveTimeout"); + if (data.isEmpty()) { + wrapper.cancel(); + } else { + wrapper.write(Type.COMPOUND_TAG, data); + } + } + }); + } + + @Inject(method = "init", at = @At("RETURN")) + private void onInit(UserConnection connection, CallbackInfo ci) { + connection.put(new TickRateState()); + } +} diff --git a/src/main/resources/viavanillaplus.mixins.json b/src/main/resources/viavanillaplus.mixins.json index 33c4f9c..3f6b8a6 100644 --- a/src/main/resources/viavanillaplus.mixins.json +++ b/src/main/resources/viavanillaplus.mixins.json @@ -4,6 +4,7 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "carpet.Protocol1_20_2To1_20Mixin", + "carpet.Protocol1_20_3To1_20_2Mixin", "syncmatica.Protocol1_20_2To1_20Mixin" ], "injectors": {