From a14abd6f4f1373402991b483ea6ffa7e6ee4e196 Mon Sep 17 00:00:00 2001 From: Matsv Date: Thu, 19 May 2016 12:18:04 +0200 Subject: [PATCH] Handle the new Metadata value for 16w20a --- .../us/myles/ViaVersion/api/type/Type.java | 1 + .../type/types/minecraft/OptPositionType.java | 25 ++++++++++ .../BasicHandlerConstructor.java | 1 - .../protocol1_9to1_8/metadata/NewType.java | 34 +++++++------ .../MetaListSnapshotType.java | 42 ++++++++++++++++ .../MetaSnapshotType.java | 33 ++++++++++++ .../ProtocolSnapshotTo1_9_3.java | 50 +++++++++++++++++++ 7 files changed, 171 insertions(+), 15 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/api/type/types/minecraft/OptPositionType.java create mode 100644 src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaListSnapshotType.java create mode 100644 src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaSnapshotType.java diff --git a/src/main/java/us/myles/ViaVersion/api/type/Type.java b/src/main/java/us/myles/ViaVersion/api/type/Type.java index 53cdb5dfc..7b5e01b7b 100644 --- a/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -61,6 +61,7 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type NBT = new NBTType(); public static final Type OPTIONAL_UUID = new OptUUIDType(); + public static final Type OPTIONAL_POSITION = new OptPositionType(); public static final Type ITEM = new ItemType(); public static final Type ITEM_ARRAY = new ItemArrayType(); diff --git a/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/OptPositionType.java b/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/OptPositionType.java new file mode 100644 index 000000000..c4c6d5fe8 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/OptPositionType.java @@ -0,0 +1,25 @@ +package us.myles.ViaVersion.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.api.type.Type; + +public class OptPositionType extends Type { + public OptPositionType() { + super(Position.class); + } + + @Override + public Position read(ByteBuf buffer) throws Exception { + boolean present = buffer.readBoolean(); + if (!present) return null; + return Type.POSITION.read(buffer); + } + + @Override + public void write(ByteBuf buffer, Position object) throws Exception { + buffer.writeBoolean(object != null); + if (object != null) + Type.POSITION.write(buffer, object); + } +} diff --git a/src/main/java/us/myles/ViaVersion/classgenerator/BasicHandlerConstructor.java b/src/main/java/us/myles/ViaVersion/classgenerator/BasicHandlerConstructor.java index 2dfd4d49b..d94d4feaf 100644 --- a/src/main/java/us/myles/ViaVersion/classgenerator/BasicHandlerConstructor.java +++ b/src/main/java/us/myles/ViaVersion/classgenerator/BasicHandlerConstructor.java @@ -3,7 +3,6 @@ package us.myles.ViaVersion.classgenerator; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.classgenerator.HandlerConstructor; import us.myles.ViaVersion.handlers.ViaDecodeHandler; import us.myles.ViaVersion.handlers.ViaEncodeHandler; diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/NewType.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/NewType.java index 3c8c8d874..53ae361f2 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/NewType.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/NewType.java @@ -2,25 +2,31 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata; import lombok.Getter; import lombok.RequiredArgsConstructor; +import us.myles.ViaVersion.api.type.Type; @RequiredArgsConstructor @Getter public enum NewType { - Byte(0), - VarInt(1), - Float(2), - String(3), - Chat(4), - Slot(5), - Boolean(6), - Vector3F(7), - Position(8), - OptPosition(9), - Direction(10), - OptUUID(11), - BlockID(12), - Discontinued(99); + Byte(0, Type.BYTE), + VarInt(1, Type.VAR_INT), + Float(2, Type.FLOAT), + String(3, Type.STRING), + Chat(4, Type.STRING), + Slot(5, Type.ITEM), + Boolean(6, Type.BOOLEAN), + Vector3F(7, Type.ROTATION), + Position(8, Type.POSITION), + OptPosition(9, Type.OPTIONAL_POSITION), + Direction(10, Type.VAR_INT), + OptUUID(11, Type.OPTIONAL_UUID), + BlockID(12, Type.VAR_INT), + Discontinued(99, null); private final int typeID; + private final Type type; + + public static NewType byId(int id) { + return values()[id]; + } } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaListSnapshotType.java b/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaListSnapshotType.java new file mode 100644 index 000000000..ac81ff271 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaListSnapshotType.java @@ -0,0 +1,42 @@ +package us.myles.ViaVersion.protocols.protocolsnapshotto1_9_3; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.NewType; + +import java.util.ArrayList; +import java.util.List; + +public class MetaListSnapshotType extends Type> { + public MetaListSnapshotType() { + super(List.class); + } + + @Override + public List read(ByteBuf buffer) throws Exception { + List list = new ArrayList<>(); + Metadata meta; + do { + meta = ProtocolSnapshotTo1_9_3.METADATA.read(buffer); + if (meta != null) + list.add(meta); + } while (meta != null); + + return list; + } + + @Override + public void write(ByteBuf buffer, List object) throws Exception { + for (Metadata m : object) { + if (m.getId() >= 5) + m.setId(m.getId() + 1); + if (m.getId() == 4) + ProtocolSnapshotTo1_9_3.METADATA.write(buffer, new Metadata(5, NewType.Boolean.getTypeID(), Type.BOOLEAN, false)); // No gravity metadata + ProtocolSnapshotTo1_9_3.METADATA.write(buffer, m); + } + + // Write end of list + ProtocolSnapshotTo1_9_3.METADATA.write(buffer, null); + } +} diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaSnapshotType.java b/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaSnapshotType.java new file mode 100644 index 000000000..eb833a2ec --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/MetaSnapshotType.java @@ -0,0 +1,33 @@ +package us.myles.ViaVersion.protocols.protocolsnapshotto1_9_3; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.NewType; + +public class MetaSnapshotType extends Type { + public MetaSnapshotType() { + super(Metadata.class); + } + + @Override + public Metadata read(ByteBuf buffer) throws Exception { + short index = buffer.readUnsignedByte(); + + if (index == 0xff) return null; //End of metadata + NewType type = NewType.byId(buffer.readByte()); + + return new Metadata(index, type.getTypeID(), type.getType(), type.getType().read(buffer)); + } + + @Override + public void write(ByteBuf buffer, Metadata object) throws Exception { + if (object == null) { + buffer.writeByte(255); + } else { + buffer.writeByte(object.getId()); + buffer.writeByte(object.getTypeID()); + object.getType().write(buffer, object.getValue()); + } + } +} diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/ProtocolSnapshotTo1_9_3.java b/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/ProtocolSnapshotTo1_9_3.java index 401a0acf8..d8f8755ab 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/ProtocolSnapshotTo1_9_3.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocolsnapshotto1_9_3/ProtocolSnapshotTo1_9_3.java @@ -1,12 +1,18 @@ package us.myles.ViaVersion.protocols.protocolsnapshotto1_9_3; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; +import java.util.List; + public class ProtocolSnapshotTo1_9_3 extends Protocol { + public static final Type> METADATA_LIST = new MetaListSnapshotType(); + public static final Type METADATA = new MetaSnapshotType(); + @Override protected void registerPackets() { // Named sound effect @@ -37,6 +43,50 @@ public class ProtocolSnapshotTo1_9_3 extends Protocol { } }); + // Metadata packet + registerOutgoing(State.PLAY, 0x39, 0x39, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + map(METADATA_LIST); // 1 - Metadata list + } + }); + + // Spawn Mob + registerOutgoing(State.PLAY, 0x03, 0x03, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity id + map(Type.UUID); // 1 - UUID + map(Type.UNSIGNED_BYTE); // 2 - Entity Type + map(Type.DOUBLE); // 3 - X + map(Type.DOUBLE); // 4 - Y + map(Type.DOUBLE); // 5 - Z + map(Type.BYTE); // 6 - Yaw + map(Type.BYTE); // 7 - Pitch + map(Type.BYTE); // 8 - Head Pitch + map(Type.SHORT); // 9 - Velocity X + map(Type.SHORT); // 10 - Velocity Y + map(Type.SHORT); // 11 - Velocity Z + map(METADATA_LIST); // 12 - Metadata + } + }); + + // Spawn Player + registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + map(Type.UUID); // 1 - Player UUID + map(Type.DOUBLE); // 2 - X + map(Type.DOUBLE); // 3 - Y + map(Type.DOUBLE); // 4 - Z + map(Type.BYTE); // 5 - Yaw + map(Type.BYTE); // 6 - Pitch + map(METADATA_LIST); // 7 - Metadata list + } + }); + } @Override