3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-25 15:50:10 +01:00

API: Work towards rewriting metadata, (This is nowhere near finished and I need to work on Item and NBT), Also change the names of a few classes to be more appropriate and implement transformation of the doubles.

(I probably messed up somewhere, nothing is tested as this is essentially API design)
Dieser Commit ist enthalten in:
Myles 2016-03-12 22:33:43 +00:00
Ursprung 7cff20f218
Commit e19a2920b9
15 geänderte Dateien mit 250 neuen und 32 gelöschten Zeilen

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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<List<Metadata>> METADATA_LIST = new MetadataListType();
public static Type<Metadata> METADATA = new MetadataType();
@Override
public void registerPackets() {
// Example PLAY_SPAWN_OBJECT(State.PLAY, Direction.OUTGOING, 0x0E, 0x00),

Datei anzeigen

@ -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<Integer, Double> toNewDouble = new ValueTransformer<Integer, Double>(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);
}
});
}

Datei anzeigen

@ -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<List<Metadata>> {
public MetadataListType() {
super(List.class);
}
@Override
public List<Metadata> read(ByteBuf buffer) {
List<Metadata> 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<Metadata> object) {
for(Metadata m:object){
Protocol1_9TO1_8.METADATA.write(buffer, m);
}
// Write end of list
Protocol1_9TO1_8.METADATA.write(buffer, null);
}
}

Datei anzeigen

@ -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<Metadata> {
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());
}
}
}

Datei anzeigen

@ -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];
}
}

Datei anzeigen

@ -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<Pair<ValueReader, ValueTransformer>> valueRemappers = new ArrayList<>();
private List<Pair<ValueReader, ValueWriter>> valueRemappers = new ArrayList<>();
public PacketRemapper() {
registerMap();
@ -23,8 +23,12 @@ public abstract class PacketRemapper {
map(new TypeRemapper(oldType), new TypeRemapper(newType));
}
public <T> void map(ValueReader<T> inputRemapper, ValueTransformer<T> outputRemapper) {
valueRemappers.add(new Pair<ValueReader, ValueTransformer>(inputRemapper, outputRemapper));
public <T1, T2> void map(Type<T1> oldType, ValueTransformer<T1, T2> transformer) {
map(new TypeRemapper(oldType), transformer);
}
public <T> void map(ValueReader<T> inputReader, ValueWriter<T> outputWriter) {
valueRemappers.add(new Pair<ValueReader, ValueWriter>(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<ValueReader, ValueTransformer> valueRemapper : valueRemappers){
for (Pair<ValueReader, ValueWriter> valueRemapper : valueRemappers) {
Object object = valueRemapper.getKey().read(packetWrapper);
// Convert object to write type :O!!!
// TODO: Data converter lol

Datei anzeigen

@ -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<T> implements ValueReader<T>, ValueTransformer<T> {
public class TypeRemapper<T> implements ValueReader<T>, ValueWriter<T> {
private final Type<T> type;
public TypeRemapper(Type<T> type) {

Datei anzeigen

@ -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

Datei anzeigen

@ -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<T> {
public void write(PacketWrapper writer, T inputValue);
public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> {
private final Type<T2> outputType;
public ValueTransformer(Type<T2> 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));
}
}

Datei anzeigen

@ -0,0 +1,7 @@
package us.myles.ViaVersion2.api.remapper;
import us.myles.ViaVersion2.api.PacketWrapper;
public interface ValueWriter<T> {
public void write(PacketWrapper writer, T inputValue);
}

Datei anzeigen

@ -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<T> implements ByteBufReader<T>, ByteBufWriter<T> {
/* Special Types */
public static final Type<Void> NOTHING = new VoidType(); // This is purely used for remapping.
/* MC Types */
public static final Type<Position> POSITION = new PositionType(); // This is purely used for remapping.
public static final Type<Position> POSITION = new PositionType();
public static final Type<EulerAngle> ROTATION = new EulerAngleType();
public static final Type<Vector> VECTOR = new VectorType();
public static final Type<Object> ITEM = null; // TODO
/* Actual Class */
private final Class<T> outputClass;
private final Class<? super T> outputClass;
private final String typeName;
public Type(Class<T> outputClass) {
public Type(Class<? super T> outputClass) {
this(outputClass.getSimpleName(), outputClass);
}
public Type(String typeName, Class<T> outputClass) {
public Type(String typeName, Class<? super T> outputClass) {
this.outputClass = outputClass;
this.typeName = typeName;
}

Datei anzeigen

@ -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<EulerAngle> {
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());
}
}

Datei anzeigen

@ -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;

Datei anzeigen

@ -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<Vector> {
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());
}
}