From 2153377c0e996a271178d6b8eb644b67d8caf1e0 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Wed, 4 Jul 2018 17:10:30 -0300 Subject: [PATCH] client to server plugin message rewriting --- .../packets/InventoryPackets.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java index 7942820c4..33073a2cc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_12_2/packets/InventoryPackets.java @@ -200,6 +200,39 @@ public class InventoryPackets { } ); + // Plugin message + protocol.registerIncoming(State.PLAY, 0x09, 0x09, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); // Channel + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + String channel = wrapper.get(Type.STRING, 0); + String originalChannel = channel; + channel = getOldPluginChannelId(channel).orNull(); + if (channel == null) { + System.out.println("Plugin message cancelled " + originalChannel); // TODO remove this debug + wrapper.cancel(); + return; + } else if (channel.equals("REGISTER") || channel.equals("UNREGISTER")) { + String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0"); + List rewrittenChannels = new ArrayList<>(); + for (int i = 0; i < channels.length; i++) { + String rewritten = getOldPluginChannelId(channels[i]).orNull(); + if (rewritten != null) + rewrittenChannels.add(rewritten); + else + System.out.println("Ignoring plugin channel in REGISTER: " + channels[i]); + } + wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(rewrittenChannels).getBytes(StandardCharsets.UTF_8)); + } + wrapper.set(Type.STRING, 0, channel); + } + }); + } + }); + // Creative Inventory Action protocol.registerIncoming(State.PLAY, 0x1B, 0x22, new PacketRemapper() { @Override @@ -461,4 +494,17 @@ public class InventoryPackets { return Optional.of("minecraft:unregister"); return Optional.absent(); } + + public static Optional getOldPluginChannelId(String newId) { + switch (newId) { + case "minecraft:register": + return Optional.of("REGISTER"); + case "minecraft:unregister": + return Optional.of("UNREGISTER"); + case "minecraft:brand": + return Optional.of("MC|Brand"); + default: + return Optional.absent(); + } + } }