Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-08 17:20:24 +01:00
Rename methods in UserConnection, add new PacketHandler.
Change the protection of some methods to be appropriate. Actually register packets Save entity types
Dieser Commit ist enthalten in:
Ursprung
e19a2920b9
Commit
81d89d3839
@ -32,12 +32,14 @@ public class PacketWrapper {
|
||||
}
|
||||
|
||||
public <T> T read(Type<T> 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 <T> void write(Type<T> type, T value) {
|
||||
System.out.println("Writing " + type.getTypeName() + " - " + value);
|
||||
packetValues.add(new Pair<Type, Object>(type, value));
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||
public class UserConnection {
|
||||
List<StoredObject> storedObjects = new ArrayList<>();
|
||||
|
||||
public <T extends StoredObject> T object(Class<T> objectClass) {
|
||||
public <T extends StoredObject> T get(Class<T> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Pair<State, Integer>, ProtocolPacket> incoming = new HashMap<>();
|
||||
private Map<Pair<State, Integer>, ProtocolPacket> outgoing = new HashMap<>();
|
||||
|
||||
public Protocol() {
|
||||
registerPackets();
|
||||
}
|
||||
|
||||
protected abstract void registerPackets();
|
||||
|
||||
public abstract void init(UserConnection userConnection);
|
||||
|
||||
public Map<Pair<State, Integer>, ProtocolPacket> incoming = new HashMap<>();
|
||||
public Map<Pair<State, Integer>, ProtocolPacket> outgoing = new HashMap<>();
|
||||
|
||||
public void registerIncoming(State state, int oldPacketID, int newPacketID) {
|
||||
registerIncoming(state, oldPacketID, newPacketID, null);
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class Protocol1_9TO1_8 extends Protocol {
|
||||
public static Type<Metadata> 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());
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
12
src/main/java/us/myles/ViaVersion2/api/remapper/PacketHandler.java
Normale Datei
12
src/main/java/us/myles/ViaVersion2/api/remapper/PacketHandler.java
Normale Datei
@ -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);
|
||||
}
|
||||
}
|
@ -31,8 +31,12 @@ public abstract class PacketRemapper {
|
||||
valueRemappers.add(new Pair<ValueReader, ValueWriter>(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();
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren