From 3b4067eb515f31a174c98177b42af1dd316e4391 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Wed, 28 Feb 2024 23:34:09 +0100 Subject: [PATCH] Implemented MC|TPack custom payload translation --- .../Protocol1_4_2to1_3_1_2.java | 8 ++--- .../Protocol1_6_1to1_5_2.java | 29 +++++++++++++++++++ .../Protocol1_8to1_7_6_10.java | 14 ++++----- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/Protocol1_4_2to1_3_1_2.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/Protocol1_4_2to1_3_1_2.java index 6ce9546..838e466 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/Protocol1_4_2to1_3_1_2.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/Protocol1_4_2to1_3_1_2.java @@ -271,7 +271,7 @@ public void register() { public void register() { handler(wrapper -> { final String channel = wrapper.read(Types1_6_4.STRING); // channel - int length = wrapper.read(Type.SHORT); // length + short length = wrapper.read(Type.SHORT); // length if (channel.equals("MC|TrList")) { wrapper.passthrough(Type.INT); // window Id @@ -284,12 +284,12 @@ public void register() { } wrapper.write(Type.BOOLEAN, false); // unavailable } - length = PacketUtil.calculateLength(wrapper); + length = (short) PacketUtil.calculateLength(wrapper); } wrapper.resetReader(); - wrapper.write(Type.STRING, channel); // channel - wrapper.write(Type.UNSIGNED_SHORT, length); // length + wrapper.write(Types1_6_4.STRING, channel); // channel + wrapper.write(Type.SHORT, length); // length }); } }); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java index 87c86bb..ab16364 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java @@ -29,6 +29,7 @@ import net.raphimc.vialegacy.api.protocol.StatelessProtocol; import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; import net.raphimc.vialegacy.api.splitter.PreNettySplitter; +import net.raphimc.vialegacy.api.util.PacketUtil; import net.raphimc.vialegacy.protocols.release.protocol1_6_1to1_5_2.metadata.MetadataRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_6_1to1_5_2.rewriter.ItemRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_6_1to1_5_2.rewriter.SoundRewriter; @@ -40,6 +41,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetaIndex1_8to1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; +import java.nio.charset.StandardCharsets; import java.util.List; public class Protocol1_6_1to1_5_2 extends StatelessProtocol { @@ -293,6 +295,33 @@ public void register() { }); } }); + this.registerClientbound(ClientboundPackets1_5_2.PLUGIN_MESSAGE, new PacketHandlers() { + @Override + public void register() { + handler(wrapper -> { + String channel = wrapper.read(Types1_6_4.STRING); // channel + short length = wrapper.read(Type.SHORT); // length + + if (channel.equals("MC|TPack")) { + channel = "MC|RPack"; + final String[] data = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); // data + final String url = data[0]; + final String resolution = data[1]; + if (!resolution.equals("16")) { + wrapper.cancel(); + return; + } + + wrapper.write(Type.REMAINING_BYTES, url.getBytes(StandardCharsets.UTF_8)); + length = (short) PacketUtil.calculateLength(wrapper); + } + + wrapper.resetReader(); + wrapper.write(Types1_6_4.STRING, channel); // channel + wrapper.write(Type.SHORT, length); // length + }); + } + }); this.registerServerbound(ServerboundPackets1_6_4.SERVER_PING, wrapper -> { wrapper.clearPacket(); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java index b52932d..2ef6d34 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java @@ -1120,9 +1120,7 @@ public void register() { switch (channel) { case "MC|Brand": { - final byte[] data = wrapper.read(Type.REMAINING_BYTES); - final String brand = new String(data, StandardCharsets.UTF_8); - wrapper.write(Type.STRING, brand); + wrapper.write(Type.STRING, new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8)); // brand break; } case "MC|TrList": @@ -1150,12 +1148,11 @@ public void register() { } break; case "MC|RPack": { - final byte[] data = wrapper.read(Type.REMAINING_BYTES); - final String resourcePackURL = new String(data, StandardCharsets.UTF_8); - wrapper.setPacketType(ClientboundPackets1_8.RESOURCE_PACK); + final String url = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8); // url wrapper.clearPacket(); - wrapper.write(Type.STRING, resourcePackURL); - wrapper.write(Type.STRING, "legacy"); + wrapper.setPacketType(ClientboundPackets1_8.RESOURCE_PACK); + wrapper.write(Type.STRING, url); // url + wrapper.write(Type.STRING, "legacy"); // hash break; } } @@ -1261,7 +1258,6 @@ public void register() { if (item != null && item.identifier() == ItemList1_6.writtenBook.itemID && direction == 255) { // If placed item is a book then cancel it and send a MC|BOpen to the client final PacketWrapper openBook = PacketWrapper.create(ClientboundPackets1_8.PLUGIN_MESSAGE, wrapper.user()); openBook.write(Type.STRING, "MC|BOpen"); // channel - openBook.write(Type.REMAINING_BYTES, new byte[0]); // data openBook.send(Protocol1_8to1_7_6_10.class); wrapper.cancel(); }