Skip to content

Commit

Permalink
Fix to support new ViaVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Dec 18, 2023
1 parent a9d67c8 commit 97f4450
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.parallel=true
# check these on https://fabricmc.net/develop
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.4
loader_version=0.14.24
loader_version=0.15.2

# Mod Properties
mod_version=1.1.0
Expand All @@ -16,5 +16,5 @@ archives_base_name=viavanillaplus
# Dependencies
carpet_version=1.4.121
fabric_version=0.90.4+1.20.2
viafabricplus_version=2.9.7-SNAPSHOT
viaversion_version=4.9.0-23w42a-SNAPSHOT
viafabricplus_version=2.10.1
viaversion_version=4.9.3-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.earthcomputer.viavanillaplus.mixin.carpet;

import com.llamalad7.mixinextras.sugar.Local;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
Expand All @@ -15,48 +16,50 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(value = Protocol1_20_2To1_20.class, remap = false)
public class Protocol1_20_2To1_20Mixin extends AbstractProtocol<ClientboundPackets1_19_4, ClientboundPackets1_20_2, ServerboundPackets1_19_4, ServerboundPackets1_20_2> {
@SuppressWarnings("deprecation")
public Protocol1_20_2To1_20Mixin() {
}

@Inject(method = "lambda$registerPackets$0", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD)
private static void onClientboundPluginMessage(PacketWrapper wrapper, CallbackInfo ci, String channel) throws Exception {
@Inject(method = "sanitizeCustomPayload", at = @At("RETURN"))
private void onSanitizeCustomPayload(PacketWrapper wrapper, CallbackInfo ci, @Local(name = "channel") String channel) throws Exception {
if (channel.equals("carpet:hello")) {
int command = wrapper.read(Type.VAR_INT);
if (command == 69) { // hi
String version = wrapper.read(Type.STRING);
CompoundTag dataTag = new CompoundTag();
dataTag.put("69", new StringTag(version));
wrapper.write(Type.COMPOUND_TAG, dataTag);
} else if (command == 1) {
wrapper.write(Type.COMPOUND_TAG, wrapper.read(Type.NAMED_COMPOUND_TAG));
} else {
wrapper.cancel();
if (wrapper.getPacketType() == null) {
return;
}
}
}

@Inject(method = "lambda$registerPackets$1", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD)
private static void onServerboundPluginMessage(PacketWrapper wrapper, CallbackInfo ci, String channel) throws Exception {
if (channel.equals("carpet:hello")) {
CompoundTag dataTag = wrapper.read(Type.COMPOUND_TAG);
Tag hello = dataTag.remove("420");
if (hello instanceof StringTag versionTag) {
PacketWrapper newPacket = PacketWrapper.create(ServerboundPackets1_19_4.PLUGIN_MESSAGE, wrapper.user());
newPacket.write(Type.STRING, "carpet:hello");
newPacket.write(Type.VAR_INT, 420); // hello
newPacket.write(Type.STRING, versionTag.getValue());
newPacket.sendToServer(Protocol1_20_2To1_20.class);
}
if (dataTag.isEmpty()) {
wrapper.cancel();
} else {
wrapper.write(Type.VAR_INT, 1); // data
wrapper.write(Type.NAMED_COMPOUND_TAG, dataTag);
switch (wrapper.getPacketType().direction()) {
case CLIENTBOUND -> {
int command = wrapper.read(Type.VAR_INT);
if (command == 69) { // hi
String version = wrapper.read(Type.STRING);
CompoundTag dataTag = new CompoundTag();
dataTag.put("69", new StringTag(version));
wrapper.write(Type.COMPOUND_TAG, dataTag);
} else if (command == 1) {
wrapper.write(Type.COMPOUND_TAG, wrapper.read(Type.NAMED_COMPOUND_TAG));
} else {
wrapper.cancel();
}
}
case SERVERBOUND -> {
CompoundTag dataTag = wrapper.read(Type.COMPOUND_TAG);
Tag hello = dataTag.remove("420");
if (hello instanceof StringTag versionTag) {
PacketWrapper newPacket = PacketWrapper.create(ServerboundPackets1_19_4.PLUGIN_MESSAGE, wrapper.user());
newPacket.write(Type.STRING, "carpet:hello");
newPacket.write(Type.VAR_INT, 420); // hello
newPacket.write(Type.STRING, versionTag.getValue());
newPacket.sendToServer(Protocol1_20_2To1_20.class);
}
if (dataTag.isEmpty()) {
wrapper.cancel();
} else {
wrapper.write(Type.VAR_INT, 1); // data
wrapper.write(Type.NAMED_COMPOUND_TAG, dataTag);
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
],
"depends": {
"fabricloader": ">=0.15.0",
"minecraft": "~1.20.2",
"java": ">=17"
}
Expand Down

0 comments on commit 97f4450

Please sign in to comment.