diff --git a/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java b/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java index ab91ad7ad..edd526b09 100644 --- a/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java +++ b/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java @@ -32,12 +32,14 @@ public class PacketWrapper { } public T read(Type type) { + System.out.println("Reading: " + type.getTypeName()); // We could in the future log input read values, but honestly for things like bulk maps, mem waste D: return type.read(inputBuffer); } public void write(Type type, T value) { + System.out.println("Writing " + type.getTypeName() + " - " + value); packetValues.add(new Pair(type, value)); } diff --git a/src/main/java/us/myles/ViaVersion2/api/data/UserConnection.java b/src/main/java/us/myles/ViaVersion2/api/data/UserConnection.java index 9ea0fd148..a1cd97968 100644 --- a/src/main/java/us/myles/ViaVersion2/api/data/UserConnection.java +++ b/src/main/java/us/myles/ViaVersion2/api/data/UserConnection.java @@ -6,7 +6,7 @@ import java.util.List; public class UserConnection { List storedObjects = new ArrayList<>(); - public T object(Class objectClass) { + public T get(Class objectClass) { for (StoredObject o : storedObjects) { if (o.getClass().equals(objectClass)) return (T) o; @@ -22,7 +22,7 @@ public class UserConnection { return false; } - public void add(StoredObject object) { + public void put(StoredObject object) { storedObjects.add(object); } } diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol/Protocol.java b/src/main/java/us/myles/ViaVersion2/api/protocol/Protocol.java index b9b9e6c09..dfb36caa1 100644 --- a/src/main/java/us/myles/ViaVersion2/api/protocol/Protocol.java +++ b/src/main/java/us/myles/ViaVersion2/api/protocol/Protocol.java @@ -6,22 +6,26 @@ import lombok.Getter; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion2.api.PacketWrapper; -import us.myles.ViaVersion2.api.util.Pair; import us.myles.ViaVersion2.api.data.UserConnection; import us.myles.ViaVersion2.api.remapper.PacketRemapper; import us.myles.ViaVersion2.api.type.Type; +import us.myles.ViaVersion2.api.util.Pair; import java.util.HashMap; import java.util.Map; public abstract class Protocol { - public abstract void registerPackets(); + private Map, ProtocolPacket> incoming = new HashMap<>(); + private Map, ProtocolPacket> outgoing = new HashMap<>(); + + public Protocol() { + registerPackets(); + } + + protected abstract void registerPackets(); public abstract void init(UserConnection userConnection); - public Map, ProtocolPacket> incoming = new HashMap<>(); - public Map, ProtocolPacket> outgoing = new HashMap<>(); - public void registerIncoming(State state, int oldPacketID, int newPacketID) { registerIncoming(state, oldPacketID, newPacketID, null); } 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 75f78ed4e..4c180c313 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 @@ -17,7 +17,7 @@ public class Protocol1_9TO1_8 extends Protocol { public static Type METADATA = new MetadataType(); @Override - public void registerPackets() { + protected void registerPackets() { // Example PLAY_SPAWN_OBJECT(State.PLAY, Direction.OUTGOING, 0x0E, 0x00), SpawnPackets.register(this); } @@ -25,6 +25,6 @@ public class Protocol1_9TO1_8 extends Protocol { @Override public void init(UserConnection userConnection) { // Entity tracker - userConnection.add(new EntityTracker()); + userConnection.put(new EntityTracker()); } } 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 74647ad3f..6216c6436 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 @@ -1,10 +1,13 @@ package us.myles.ViaVersion2.api.protocol1_9to1_8.packets; +import org.bukkit.entity.EntityType; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.util.EntityUtil; 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.PacketHandler; import us.myles.ViaVersion2.api.remapper.PacketRemapper; import us.myles.ViaVersion2.api.remapper.ValueCreator; import us.myles.ViaVersion2.api.remapper.ValueTransformer; @@ -24,17 +27,28 @@ public class SpawnPackets { @Override public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID - // 1 - UUID + create(new ValueCreator() { @Override public void write(PacketWrapper wrapper) { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().object(EntityTracker.class); - wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); map(Type.BYTE); // 2 - Type + // Parse this info + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) { + int entityID = wrapper.get(Type.VAR_INT, 0); + int typeID = wrapper.get(Type.BYTE, 0); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.getClientEntityTypes().put(entityID, EntityUtil.getTypeFromID(typeID, true)); + } + }); + 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 @@ -62,7 +76,6 @@ public class SpawnPackets { wrapper.write(Type.SHORT, vZ); } }); - } }); @@ -72,6 +85,16 @@ public class SpawnPackets { public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID + // Parse this info + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) { + int entityID = wrapper.get(Type.VAR_INT, 0); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.getClientEntityTypes().put(entityID, EntityType.EXPERIENCE_ORB); + } + }); + 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 @@ -86,6 +109,16 @@ public class SpawnPackets { public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID map(Type.BYTE); // 1 - Type + // Parse this info + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) { + // Currently only lightning uses this + int entityID = wrapper.get(Type.VAR_INT, 0); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.getClientEntityTypes().put(entityID, EntityType.LIGHTNING); + } + }); map(Type.INT, toNewDouble); // 2 - X - Needs to be divide by 32 map(Type.INT, toNewDouble); // 3 - Y - Needs to be divide by 32 @@ -98,17 +131,28 @@ public class SpawnPackets { @Override public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID - // 1 - UUID + create(new ValueCreator() { @Override public void write(PacketWrapper wrapper) { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().object(EntityTracker.class); - wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); map(Type.UNSIGNED_BYTE); // 2 - Type + // Parse this info + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) { + int entityID = wrapper.get(Type.VAR_INT, 0); + int typeID = wrapper.get(Type.UNSIGNED_BYTE, 0); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.getClientEntityTypes().put(entityID, EntityUtil.getTypeFromID(typeID, false)); + } + }); + 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 @@ -131,15 +175,27 @@ public class SpawnPackets { @Override public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID - // 1 - UUID + + // Parse this info + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) { + int entityID = wrapper.get(Type.VAR_INT, 0); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.getClientEntityTypes().put(entityID, EntityType.PAINTING); + } + }); + + create(new ValueCreator() { @Override public void write(PacketWrapper wrapper) { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().object(EntityTracker.class); - wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); + map(Type.STRING); // 2 - Title map(Type.POSITION); // 3 - Position diff --git a/src/main/java/us/myles/ViaVersion2/api/remapper/PacketHandler.java b/src/main/java/us/myles/ViaVersion2/api/remapper/PacketHandler.java new file mode 100644 index 000000000..1e486e7b5 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/PacketHandler.java @@ -0,0 +1,12 @@ +package us.myles.ViaVersion2.api.remapper; + +import us.myles.ViaVersion2.api.PacketWrapper; + +public abstract class PacketHandler implements ValueWriter { + public abstract void handle(PacketWrapper wrapper); + + @Override + public void write(PacketWrapper writer, Object inputValue) { + handle(writer); + } +} 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 deba9d240..5005e40a1 100644 --- a/src/main/java/us/myles/ViaVersion2/api/remapper/PacketRemapper.java +++ b/src/main/java/us/myles/ViaVersion2/api/remapper/PacketRemapper.java @@ -31,8 +31,12 @@ public abstract class PacketRemapper { valueRemappers.add(new Pair(inputReader, outputWriter)); } - public void create(ValueCreator transformer) { - map(new TypeRemapper(Type.NOTHING), transformer); + public void create(ValueCreator creator) { + map(new TypeRemapper(Type.NOTHING), creator); + } + + public void handler(PacketHandler handler) { + map(new TypeRemapper(Type.NOTHING), handler); } public abstract void registerMap();