From edaffad0458326a224bcd13ae0bcbb0b1e716515 Mon Sep 17 00:00:00 2001 From: Mats Date: Wed, 4 May 2016 17:32:38 +0200 Subject: [PATCH] 1.9.3 support --- .../api/protocol/ProtocolPipeline.java | 3 +- .../api/protocol/ProtocolRegistry.java | 2 + .../api/protocol/ProtocolVersion.java | 2 + .../Protocol1_9_1TO1_9.java | 2 +- .../Protocol1_9_3TO1_9_1_2.java | 81 +++++++++++++++++++ .../listeners/ArmorListener.java | 9 ++- .../packets/WorldPackets.java | 6 +- .../protocol1_9to1_8/types/ChunkType.java | 5 -- 8 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java index f749ec68e..dd749e645 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java @@ -7,7 +7,6 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.base.BaseProtocol; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.ArrayList; @@ -83,7 +82,7 @@ public class ProtocolPipeline extends Protocol { // For 1.8/1.9 server version, eventually we'll probably get an API for this... if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_8.getId() && - ProtocolRegistry.SERVER_PROTOCOL <= ProtocolVersion.v1_9_2.getId()) { + ProtocolRegistry.SERVER_PROTOCOL <= ProtocolVersion.v1_9_3.getId()) { PacketType type; if (ProtocolRegistry.SERVER_PROTOCOL == ProtocolVersion.v1_8.getId()) { diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index 8c2f40c34..25d3b65d3 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -4,6 +4,7 @@ import org.bukkit.Bukkit; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.protocols.base.BaseProtocol; import us.myles.ViaVersion.protocols.protocol1_9_1to1_9.Protocol1_9_1TO1_9; +import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.Protocol1_9_3TO1_9_1_2; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import java.util.*; @@ -22,6 +23,7 @@ public class ProtocolRegistry { // Register built in protocols registerProtocol(new Protocol1_9TO1_8(), Collections.singletonList(ProtocolVersion.v1_9.getId()), ProtocolVersion.v1_8.getId()); registerProtocol(new Protocol1_9_1TO1_9(), Arrays.asList(ProtocolVersion.v1_9_1.getId(), ProtocolVersion.v1_9_2.getId()), ProtocolVersion.v1_9.getId()); + registerProtocol(new Protocol1_9_3TO1_9_1_2(), Arrays.asList(ProtocolVersion.v1_9_3.getId()), ProtocolVersion.v1_9_2.getId()); } /** diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java index 7a366be27..04e08aec3 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java @@ -15,6 +15,7 @@ public class ProtocolVersion { public static final ProtocolVersion v1_9; public static final ProtocolVersion v1_9_1; public static final ProtocolVersion v1_9_2; + public static final ProtocolVersion v1_9_3; private final int id; private final String name; @@ -26,6 +27,7 @@ public class ProtocolVersion { register(v1_9 = new ProtocolVersion(107, "1.9")); register(v1_9_1 = new ProtocolVersion(108, "1.9.1")); register(v1_9_2 = new ProtocolVersion(109, "1.9.2")); + register(v1_9_3 = new ProtocolVersion(110, "1.9.3")); } public static void register(@NonNull ProtocolVersion protocol) { diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1TO1_9.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1TO1_9.java index da366152c..609c35ea1 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1TO1_9.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1TO1_9.java @@ -9,7 +9,7 @@ import us.myles.ViaVersion.packets.State; public class Protocol1_9_1TO1_9 extends Protocol { @Override protected void registerPackets() { - // Currently supports 1.9.1 PRE 2 + // Currently supports 1.9.1 and 1.9.2 // Join Game Packet registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() { @Override diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java new file mode 100644 index 000000000..96801a622 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java @@ -0,0 +1,81 @@ +package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2; + +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import org.spacehq.opennbt.tag.builtin.IntTag; +import org.spacehq.opennbt.tag.builtin.StringTag; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; + +public class Protocol1_9_3TO1_9_1_2 extends Protocol { + @Override + protected void registerPackets() { + + //Unchanged packet structure + registerOutgoing(State.PLAY, 0x47, 0x46); //Sound effect + registerOutgoing(State.PLAY, 0x48, 0x47); //Player list header and footer + registerOutgoing(State.PLAY, 0x49, 0x48); //Collect item + registerOutgoing(State.PLAY, 0x4A, 0x49); //Entity teleport + registerOutgoing(State.PLAY, 0x4B, 0x4A); //Entity properties + registerOutgoing(State.PLAY, 0x4C, 0x4B); //Entity effect + + //Sign update + registerOutgoing(State.PLAY, 0x46, -1, new PacketRemapper() { + @Override + public void registerMap() { + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + //read data + Position position = wrapper.read(Type.POSITION); + String[] lines = new String[4]; + for (int i = 0; i < 4; i++) + lines[i] = wrapper.read(Type.STRING); + + wrapper.clearInputBuffer(); + + //write data + wrapper.setId(0x09); //Update block entity + wrapper.write(Type.POSITION, position); //Block location + wrapper.write(Type.UNSIGNED_BYTE, (short) 9); //Action type (9 update sign) + + //Create nbt + CompoundTag tag = new CompoundTag(""); + tag.put(new StringTag("id", "Sign")); + tag.put(new IntTag("x", position.getX().intValue())); + tag.put(new IntTag("y", position.getY().intValue())); + tag.put(new IntTag("z", position.getZ().intValue())); + for (int i = 0; i < lines.length; i++) + tag.put(new StringTag("Text" + (i + 1), lines[i])); + + wrapper.write(Type.NBT, tag); + } + }); + } + }); + + registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() { + @Override + public void registerMap() { + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + wrapper.passthroughAll(); + wrapper.write(Type.VAR_INT, 0); + } + }); + } + }); + } + + @Override + public void init(UserConnection userConnection) { + + } +} diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/ArmorListener.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/ArmorListener.java index 762bf61f5..8c15e3fa0 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/ArmorListener.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/ArmorListener.java @@ -19,6 +19,7 @@ import org.bukkit.inventory.CraftingInventory; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ArmorType; @@ -39,9 +40,12 @@ public class ArmorListener implements Listener { if (!userConnection.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9TO1_8.class)) return; int armor = ArmorType.calculateArmorPoints(player.getInventory().getArmorContents()); + int protocol = userConnection.get(ProtocolInfo.class).getProtocolVersion(); + ByteBuf buf = Unpooled.buffer(); try { - ByteBuf buf = Unpooled.buffer(); - Type.VAR_INT.write(buf, 0x4B); // Entity Properties + + //TODO possibility to send packets by Protocol version, to let the transformer do the work + Type.VAR_INT.write(buf, (protocol >= ProtocolVersion.v1_9_3.getId()) ? 0x4A : 0x4B); // Entity Properties Type.VAR_INT.write(buf, player.getEntityId()); buf.writeInt(1); // only 1 property Type.STRING.write(buf, "generic.armor"); @@ -53,6 +57,7 @@ public class ArmorListener implements Listener { ViaVersion.getInstance().sendRawPacket(player, buf); } catch (Exception ignored) { + buf.release(); } } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 8e8c8e27c..9fa71b7a7 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -6,6 +6,7 @@ import org.spacehq.opennbt.tag.builtin.StringTag; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -114,7 +115,10 @@ public class WorldPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { ClientChunks clientChunks = wrapper.user().get(ClientChunks.class); - wrapper.passthrough(new ChunkType(clientChunks)); + Chunk chunk = wrapper.passthrough(new ChunkType(clientChunks)); + if (chunk.isUnloadPacket()) + wrapper.setId(0x1D); + // eat any other data (Usually happens with unload packets) wrapper.read(Type.REMAINING_BYTES); } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java index 230ad6f8d..871ffd663 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java @@ -128,11 +128,6 @@ public class ChunkType extends PartialType { @Override public void write(ByteBuf output, ClientChunks param, Chunk chunk) throws Exception { - if (chunk.isUnloadPacket()) { - output.clear(); - Type.VAR_INT.write(output, 0x1D); // Unload packet ID - } - // Write primary info output.writeInt(chunk.getX()); output.writeInt(chunk.getZ());