Skip to content

Commit

Permalink
Translate carpet tick rate packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Mar 23, 2024
1 parent 576644c commit 2ad9b89
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<ClientboundPacket1_20_2, ClientboundPacket1_20_3, ServerboundPacket1_20_2, ServerboundPacket1_20_3> {
@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());
}
}
1 change: 1 addition & 0 deletions src/main/resources/viavanillaplus.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 2ad9b89

Please sign in to comment.