diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index 35c68c4..2a50056 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -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 diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index 333bec6..671eb46 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -36,34 +36,22 @@ public abstract class PacketHandler { private final Map, 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 packetClass = (Class) method.getParameterTypes()[0]; - HANDLER_MAP.put(packetClass, method); + if (method.isAnnotationPresent(Handler.class)) { + Class packetClass = (Class) 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)