Skip to content

Commit

Permalink
Implemented MC|TPack custom payload translation
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Feb 28, 2024
1 parent 9bc0af5 commit 3b4067e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ClientboundPackets1_5_2, ClientboundPackets1_6_1, ServerboundPackets1_5_2, ServerboundPackets1_6_4> {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 3b4067e

Please sign in to comment.