Add MetaInfos #5

Zusammengeführt
Lixfel hat 4 Commits von MetaInfos nach master 2022-06-14 15:21:22 +02:00 zusammengeführt
2 geänderte Dateien mit 13 neuen und 29 gelöschten Zeilen
Nur Änderungen aus Commit ea9a4c2a81 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -27,6 +27,8 @@ import java.io.*;
@EqualsAndHashCode
public abstract class NetworkPacket implements Serializable {
public transient MetaInfos metaInfos;
@SneakyThrows
public byte[] serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -41,7 +43,9 @@ public abstract class NetworkPacket implements Serializable {
}
public static void handle(MetaInfos metaInfos, byte[] data) {
PacketHandler.handlePacket(metaInfos, deserialize(data));
NetworkPacket networkPacket = deserialize(data);
networkPacket.metaInfos = metaInfos;
PacketHandler.handlePacket(networkPacket);
}
@SneakyThrows

Datei anzeigen

@ -36,34 +36,22 @@ public abstract class PacketHandler {
private final Map<Class<? extends NetworkPacket>, Method> HANDLER_MAP = new HashMap<>();
public static void handlePacket(NetworkPacket packet) {
handlePacket(null, packet);
}
public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) {
for (PacketHandler handler : PACKET_HANDLERS) {
handler.handle(metaInfos, packet);
handler.handle(packet);
}
}
protected PacketHandler() {
Method[] methods = getClass().getMethods();
for (Method method : methods) {
Handler handler = method.getAnnotation(Handler.class);
if (handler == null) {
continue;
}
if (!(method.getParameterCount() > 0 && method.getParameterCount() < 3)) {
continue;
}
if (!NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
continue;
}
if (method.getParameterCount() == 2 && !MetaInfos.class.isAssignableFrom(method.getParameterTypes()[1])) {
if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
continue;
}
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
HANDLER_MAP.put(packetClass, method);
if (method.isAnnotationPresent(Handler.class)) {
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
HANDLER_MAP.put(packetClass, method);
}
}
}
@ -76,20 +64,12 @@ public abstract class PacketHandler {
}
@SneakyThrows
public void handle(MetaInfos metaInfos, NetworkPacket packet) {
public void handle(NetworkPacket packet) {
Method method = HANDLER_MAP.get(packet.getClass());
if (method == null) {
return;
}
if (method.getParameterCount() == 2) {
if (metaInfos == null || method.getParameterTypes()[1].isAssignableFrom(metaInfos.getClass())) {
method.invoke(this, packet, metaInfos);
} else {
throw new IllegalArgumentException("MetaInfos is not assignable to " + method.getParameterTypes()[1]);
}
} else {
method.invoke(this, packet);
}
method.invoke(this, packet);
}
@Retention(RetentionPolicy.RUNTIME)