diff --git a/src/main/java/us/myles/ViaVersion2/api/metadata/Metadata.java b/src/main/java/us/myles/ViaVersion2/api/metadata/Metadata.java new file mode 100644 index 000000000..dfe962c66 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/metadata/Metadata.java @@ -0,0 +1,15 @@ +package us.myles.ViaVersion2.api.metadata; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import us.myles.ViaVersion2.api.type.Type; + +@AllArgsConstructor +@Getter +@Setter +public class Metadata { + private final int id; + private Type type; + private Object value; +} diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/Protocol1_9TO1_8.java b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/Protocol1_9TO1_8.java index 0c9885b47..75f78ed4e 100644 --- a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/Protocol1_9TO1_8.java +++ b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/Protocol1_9TO1_8.java @@ -1,11 +1,21 @@ package us.myles.ViaVersion2.api.protocol1_9to1_8; import us.myles.ViaVersion2.api.data.UserConnection; +import us.myles.ViaVersion2.api.metadata.Metadata; import us.myles.ViaVersion2.api.protocol.Protocol; import us.myles.ViaVersion2.api.protocol1_9to1_8.packets.SpawnPackets; import us.myles.ViaVersion2.api.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion2.api.protocol1_9to1_8.types.MetadataListType; +import us.myles.ViaVersion2.api.protocol1_9to1_8.types.MetadataType; +import us.myles.ViaVersion2.api.type.Type; + +import java.util.List; public class Protocol1_9TO1_8 extends Protocol { + public static Type> METADATA_LIST = new MetadataListType(); + + public static Type METADATA = new MetadataType(); + @Override public void registerPackets() { // Example PLAY_SPAWN_OBJECT(State.PLAY, Direction.OUTGOING, 0x0E, 0x00), diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/packets/SpawnPackets.java b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/packets/SpawnPackets.java index aa45d9b47..74647ad3f 100644 --- a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/packets/SpawnPackets.java +++ b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/packets/SpawnPackets.java @@ -3,12 +3,21 @@ package us.myles.ViaVersion2.api.protocol1_9to1_8.packets; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion2.api.PacketWrapper; import us.myles.ViaVersion2.api.protocol.Protocol; +import us.myles.ViaVersion2.api.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion2.api.protocol1_9to1_8.storage.EntityTracker; import us.myles.ViaVersion2.api.remapper.PacketRemapper; import us.myles.ViaVersion2.api.remapper.ValueCreator; +import us.myles.ViaVersion2.api.remapper.ValueTransformer; import us.myles.ViaVersion2.api.type.Type; public class SpawnPackets { + private static ValueTransformer toNewDouble = new ValueTransformer(Type.DOUBLE) { + @Override + public Double transform(PacketWrapper wrapper, Integer inputValue) { + return inputValue / 32D; + } + }; + public static void register(Protocol protocol) { // Spawn Object Packet protocol.registerOutgoing(State.PLAY, 0x0E, 0x00, new PacketRemapper() { @@ -26,9 +35,9 @@ public class SpawnPackets { }); map(Type.BYTE); // 2 - Type - map(Type.INT, Type.DOUBLE); // 3 - X - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 4 - Y - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 5 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - X - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 4 - Y - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 5 - Z - Needs to be divide by 32 map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 7 - Yaw @@ -63,9 +72,9 @@ public class SpawnPackets { public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID - map(Type.INT, Type.DOUBLE); // 1 - X - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 2 - Y - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 3 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 1 - X - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 2 - Y - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - Z - Needs to be divide by 32 map(Type.INT); // 4 - Data } @@ -78,9 +87,9 @@ public class SpawnPackets { map(Type.VAR_INT); // 0 - Entity ID map(Type.BYTE); // 1 - Type - map(Type.INT, Type.DOUBLE); // 2 - X - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 3 - Y - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 4 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 2 - X - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - Y - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 4 - Z - Needs to be divide by 32 } }); @@ -100,9 +109,9 @@ public class SpawnPackets { }); map(Type.UNSIGNED_BYTE); // 2 - Type - map(Type.INT, Type.DOUBLE); // 3 - X - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 4 - Y - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 5 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - X - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 4 - Y - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 5 - Z - Needs to be divide by 32 map(Type.BYTE); // 6 - Yaw map(Type.BYTE); // 7 - Pitch @@ -112,7 +121,8 @@ public class SpawnPackets { map(Type.SHORT); // 10 - Velocity Y map(Type.SHORT); // 11 - Velocity Z - // TODO Read Metadata + // TODO Rewrite Metadata + map(Protocol1_9TO1_8.METADATA_LIST); } }); @@ -145,16 +155,17 @@ public class SpawnPackets { map(Type.VAR_INT); // 0 - Entity ID map(Type.UUID); // 1 - Player UUID - map(Type.INT, Type.DOUBLE); // 2 - X - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 3 - Y - Needs to be divide by 32 - map(Type.INT, Type.DOUBLE); // 4 - Z - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 2 - X - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 3 - Y - Needs to be divide by 32 + map(Type.INT, toNewDouble); // 4 - Z - Needs to be divide by 32 map(Type.BYTE); // 5 - Yaw map(Type.BYTE); // 6 - Pitch map(Type.SHORT, Type.NOTHING); // Current Item is discontinued - // TODO Read Metadata + // TODO Rewrite Metadata + map(Protocol1_9TO1_8.METADATA_LIST); } }); } diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataListType.java b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataListType.java new file mode 100644 index 000000000..7be6e913d --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataListType.java @@ -0,0 +1,38 @@ +package us.myles.ViaVersion2.api.protocol1_9to1_8.types; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion2.api.metadata.Metadata; +import us.myles.ViaVersion2.api.protocol1_9to1_8.Protocol1_9TO1_8; +import us.myles.ViaVersion2.api.type.Type; + +import java.util.ArrayList; +import java.util.List; + +public class MetadataListType extends Type> { + public MetadataListType() { + super(List.class); + } + + @Override + public List read(ByteBuf buffer) { + List list = new ArrayList<>(); + Metadata m; + do { + m = Protocol1_9TO1_8.METADATA.read(buffer); + if (m != null) { + list.add(m); + } + } while (m != null); + + return list; + } + + @Override + public void write(ByteBuf buffer, List object) { + for(Metadata m:object){ + Protocol1_9TO1_8.METADATA.write(buffer, m); + } + // Write end of list + Protocol1_9TO1_8.METADATA.write(buffer, null); + } +} diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataType.java b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataType.java new file mode 100644 index 000000000..1330118fe --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataType.java @@ -0,0 +1,32 @@ +package us.myles.ViaVersion2.api.protocol1_9to1_8.types; + + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion2.api.metadata.Metadata; +import us.myles.ViaVersion2.api.type.Type; + +public class MetadataType extends Type { + + public MetadataType() { + super(Metadata.class); + } + + @Override + public Metadata read(ByteBuf buffer) { + byte item = buffer.readByte(); + if (item == 127) return null; // end of metadata + MetadataTypes type = MetadataTypes.byId((item & 0xE0) >> 5); + int id = item & 0x1F; + return new Metadata(id, type.getType(), type.getType().read(buffer)); + } + + @Override + public void write(ByteBuf buffer, Metadata object) { + if (object == null) { + buffer.writeByte(127); + } else { + buffer.writeByte(object.getId()); + object.getType().write(buffer, object.getValue()); + } + } +} diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataTypes.java b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataTypes.java new file mode 100644 index 000000000..efab77587 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/protocol1_9to1_8/types/MetadataTypes.java @@ -0,0 +1,26 @@ +package us.myles.ViaVersion2.api.protocol1_9to1_8.types; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import us.myles.ViaVersion2.api.type.Type; + +@RequiredArgsConstructor +@Getter +public enum MetadataTypes { + Byte(0, Type.BYTE), + Short(1, Type.SHORT), + Int(2, Type.INT), + Float(3, Type.FLOAT), + String(4, Type.STRING), + Slot(5, Type.ITEM), + Position(6, Type.VECTOR), + Rotation(7, Type.ROTATION), + NonExistent(-1, Type.NOTHING); + + private final int typeID; + private final Type type; + + public static MetadataTypes byId(int id) { + return values()[id]; + } +} diff --git a/src/main/java/us/myles/ViaVersion2/api/remapper/PacketRemapper.java b/src/main/java/us/myles/ViaVersion2/api/remapper/PacketRemapper.java index dcd89d681..deba9d240 100644 --- a/src/main/java/us/myles/ViaVersion2/api/remapper/PacketRemapper.java +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/PacketRemapper.java @@ -1,14 +1,14 @@ package us.myles.ViaVersion2.api.remapper; import us.myles.ViaVersion2.api.PacketWrapper; -import us.myles.ViaVersion2.api.util.Pair; import us.myles.ViaVersion2.api.type.Type; +import us.myles.ViaVersion2.api.util.Pair; import java.util.ArrayList; import java.util.List; public abstract class PacketRemapper { - private List> valueRemappers = new ArrayList<>(); + private List> valueRemappers = new ArrayList<>(); public PacketRemapper() { registerMap(); @@ -23,8 +23,12 @@ public abstract class PacketRemapper { map(new TypeRemapper(oldType), new TypeRemapper(newType)); } - public void map(ValueReader inputRemapper, ValueTransformer outputRemapper) { - valueRemappers.add(new Pair(inputRemapper, outputRemapper)); + public void map(Type oldType, ValueTransformer transformer) { + map(new TypeRemapper(oldType), transformer); + } + + public void map(ValueReader inputReader, ValueWriter outputWriter) { + valueRemappers.add(new Pair(inputReader, outputWriter)); } public void create(ValueCreator transformer) { @@ -35,7 +39,7 @@ public abstract class PacketRemapper { public void remap(PacketWrapper packetWrapper) { // Read all the current values - for(Pair valueRemapper : valueRemappers){ + for (Pair valueRemapper : valueRemappers) { Object object = valueRemapper.getKey().read(packetWrapper); // Convert object to write type :O!!! // TODO: Data converter lol diff --git a/src/main/java/us/myles/ViaVersion2/api/remapper/TypeRemapper.java b/src/main/java/us/myles/ViaVersion2/api/remapper/TypeRemapper.java index 7b15f2a53..148c3ea8e 100644 --- a/src/main/java/us/myles/ViaVersion2/api/remapper/TypeRemapper.java +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/TypeRemapper.java @@ -3,7 +3,7 @@ package us.myles.ViaVersion2.api.remapper; import us.myles.ViaVersion2.api.PacketWrapper; import us.myles.ViaVersion2.api.type.Type; -public class TypeRemapper implements ValueReader, ValueTransformer { +public class TypeRemapper implements ValueReader, ValueWriter { private final Type type; public TypeRemapper(Type type) { diff --git a/src/main/java/us/myles/ViaVersion2/api/remapper/ValueCreator.java b/src/main/java/us/myles/ViaVersion2/api/remapper/ValueCreator.java index cef6dae39..30d475527 100644 --- a/src/main/java/us/myles/ViaVersion2/api/remapper/ValueCreator.java +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/ValueCreator.java @@ -2,7 +2,7 @@ package us.myles.ViaVersion2.api.remapper; import us.myles.ViaVersion2.api.PacketWrapper; -public abstract class ValueCreator implements ValueTransformer{ +public abstract class ValueCreator implements ValueWriter { public abstract void write(PacketWrapper wrapper); @Override diff --git a/src/main/java/us/myles/ViaVersion2/api/remapper/ValueTransformer.java b/src/main/java/us/myles/ViaVersion2/api/remapper/ValueTransformer.java index 18331a5e5..e3df6aa20 100644 --- a/src/main/java/us/myles/ViaVersion2/api/remapper/ValueTransformer.java +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/ValueTransformer.java @@ -1,7 +1,19 @@ package us.myles.ViaVersion2.api.remapper; import us.myles.ViaVersion2.api.PacketWrapper; +import us.myles.ViaVersion2.api.type.Type; -public interface ValueTransformer { - public void write(PacketWrapper writer, T inputValue); +public abstract class ValueTransformer implements ValueWriter { + private final Type outputType; + + public ValueTransformer(Type outputType) { + this.outputType = outputType; + } + + public abstract T2 transform(PacketWrapper wrapper, T1 inputValue); + + @Override + public void write(PacketWrapper writer, T1 inputValue) { + writer.write(outputType, transform(writer, inputValue)); + } } diff --git a/src/main/java/us/myles/ViaVersion2/api/remapper/ValueWriter.java b/src/main/java/us/myles/ViaVersion2/api/remapper/ValueWriter.java new file mode 100644 index 000000000..ad2f528c1 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/ValueWriter.java @@ -0,0 +1,7 @@ +package us.myles.ViaVersion2.api.remapper; + +import us.myles.ViaVersion2.api.PacketWrapper; + +public interface ValueWriter { + public void write(PacketWrapper writer, T inputValue); +} diff --git a/src/main/java/us/myles/ViaVersion2/api/type/Type.java b/src/main/java/us/myles/ViaVersion2/api/type/Type.java index 562cd4cac..bff9345e7 100644 --- a/src/main/java/us/myles/ViaVersion2/api/type/Type.java +++ b/src/main/java/us/myles/ViaVersion2/api/type/Type.java @@ -1,9 +1,13 @@ package us.myles.ViaVersion2.api.type; -import javafx.geometry.Pos; import lombok.Getter; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; import us.myles.ViaVersion2.api.type.types.*; +import us.myles.ViaVersion2.api.type.types.minecraft.EulerAngleType; +import us.myles.ViaVersion2.api.type.types.minecraft.PositionType; +import us.myles.ViaVersion2.api.type.types.minecraft.VectorType; import us.myles.ViaVersion2.api.util.Position; import java.util.UUID; @@ -46,17 +50,21 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { /* Special Types */ public static final Type NOTHING = new VoidType(); // This is purely used for remapping. /* MC Types */ - public static final Type POSITION = new PositionType(); // This is purely used for remapping. + public static final Type POSITION = new PositionType(); + public static final Type ROTATION = new EulerAngleType(); + public static final Type VECTOR = new VectorType(); + + public static final Type ITEM = null; // TODO /* Actual Class */ - private final Class outputClass; + private final Class outputClass; private final String typeName; - public Type(Class outputClass) { + public Type(Class outputClass) { this(outputClass.getSimpleName(), outputClass); } - public Type(String typeName, Class outputClass) { + public Type(String typeName, Class outputClass) { this.outputClass = outputClass; this.typeName = typeName; } diff --git a/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/EulerAngleType.java b/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/EulerAngleType.java new file mode 100644 index 000000000..f0e44dfc4 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/EulerAngleType.java @@ -0,0 +1,28 @@ +package us.myles.ViaVersion2.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; +import us.myles.ViaVersion2.api.type.Type; + +public class EulerAngleType extends Type { + public EulerAngleType() { + super(EulerAngle.class); + } + + @Override + public EulerAngle read(ByteBuf buffer) { + float x = Type.FLOAT.read(buffer); + float y = Type.FLOAT.read(buffer); + float z = Type.FLOAT.read(buffer); + + return new EulerAngle(x, y, z); + } + + @Override + public void write(ByteBuf buffer, EulerAngle object) { + Type.FLOAT.write(buffer, (float) object.getX()); + Type.FLOAT.write(buffer, (float) object.getY()); + Type.FLOAT.write(buffer, (float) object.getZ()); + } +} \ No newline at end of file diff --git a/src/main/java/us/myles/ViaVersion2/api/type/types/PositionType.java b/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/PositionType.java similarity index 93% rename from src/main/java/us/myles/ViaVersion2/api/type/types/PositionType.java rename to src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/PositionType.java index e58c00a73..51892c8c1 100644 --- a/src/main/java/us/myles/ViaVersion2/api/type/types/PositionType.java +++ b/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/PositionType.java @@ -1,4 +1,4 @@ -package us.myles.ViaVersion2.api.type.types; +package us.myles.ViaVersion2.api.type.types.minecraft; import io.netty.buffer.ByteBuf; import us.myles.ViaVersion2.api.type.Type; diff --git a/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/VectorType.java b/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/VectorType.java new file mode 100644 index 000000000..fde8a2dbc --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/type/types/minecraft/VectorType.java @@ -0,0 +1,27 @@ +package us.myles.ViaVersion2.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import org.bukkit.util.Vector; +import us.myles.ViaVersion2.api.type.Type; + +public class VectorType extends Type { + public VectorType() { + super(Vector.class); + } + + @Override + public Vector read(ByteBuf buffer) { + int x = Type.INT.read(buffer); + int y = Type.INT.read(buffer); + int z = Type.INT.read(buffer); + + return new Vector(x, y, z); + } + + @Override + public void write(ByteBuf buffer, Vector object) { + Type.INT.write(buffer, object.getBlockX()); + Type.INT.write(buffer, object.getBlockY()); + Type.INT.write(buffer, object.getBlockZ()); + } +}