Commit a8b808ba authored by coooookies's avatar coooookies Committed by Melledy
Browse files

Improve registration methods.

parent dde5a609
...@@ -23,25 +23,15 @@ public class GameServerPacketHandler { ...@@ -23,25 +23,15 @@ public class GameServerPacketHandler {
this.registerHandlers(handlerClass); this.registerHandlers(handlerClass);
} }
public void registerPacketHandler(int opcode, PacketHandler handler) { public void registerPacketHandler(Class<? extends PacketHandler> handlerClass) {
this.handlers.put(opcode, handler);
}
public void registerHandlers(Class<? extends PacketHandler> handlerClass) {
Reflections reflections = new Reflections("emu.grasscutter.server.packet");
Set<?> handlerClasses = reflections.getSubTypesOf(handlerClass);
for (Object obj : handlerClasses) {
Class<?> c = (Class<?>) obj;
try { try {
Opcodes opcode = c.getAnnotation(Opcodes.class); Opcodes opcode = handlerClass.getAnnotation(Opcodes.class);
if (opcode == null || opcode.disabled() || opcode.value() <= 0) { if (opcode == null || opcode.disabled() || opcode.value() <= 0) {
continue; return;
} }
PacketHandler packetHandler = (PacketHandler) c.newInstance(); PacketHandler packetHandler = (PacketHandler) handlerClass.newInstance();
this.handlers.put(opcode.value(), packetHandler); this.handlers.put(opcode.value(), packetHandler);
} catch (Exception e) { } catch (Exception e) {
...@@ -49,6 +39,14 @@ public class GameServerPacketHandler { ...@@ -49,6 +39,14 @@ public class GameServerPacketHandler {
} }
} }
public void registerHandlers(Class<? extends PacketHandler> handlerClass) {
Reflections reflections = new Reflections("emu.grasscutter.server.packet");
Set<?> handlerClasses = reflections.getSubTypesOf(handlerClass);
for (Object obj : handlerClasses) {
this.registerPacketHandler((Class<? extends PacketHandler>) obj);
}
// Debug // Debug
Grasscutter.getLogger().info("Registered " + this.handlers.size() + " " + handlerClass.getSimpleName() + "s"); Grasscutter.getLogger().info("Registered " + this.handlers.size() + " " + handlerClass.getSimpleName() + "s");
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment