Dieser Commit ist enthalten in:
Ursprung
2ddfa9e80f
Commit
ea9a4c2a81
@ -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
|
||||
|
@ -36,36 +36,24 @@ 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;
|
||||
}
|
||||
|
||||
if (method.isAnnotationPresent(Handler.class)) {
|
||||
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
|
||||
HANDLER_MAP.put(packetClass, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void register() {
|
||||
PACKET_HANDLERS.add(this);
|
||||
@ -76,21 +64,13 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
protected @interface Handler {}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren