From 1e166960d2cfdf6f94a64b5b4ae14626ddd99c15 Mon Sep 17 00:00:00 2001 From: coooookies <1164557342@qq.com> Date: Sun, 1 May 2022 20:47:54 +0800 Subject: [PATCH] Improve registration methods. --- .../server/game/GameServerPacketHandler.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java b/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java index dbead5240..35303b639 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java +++ b/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java @@ -23,8 +23,20 @@ public class GameServerPacketHandler { this.registerHandlers(handlerClass); } - public void registerPacketHandler(int opcode, PacketHandler handler) { - this.handlers.put(opcode, handler); + public void registerPacketHandler(Class handlerClass) { + try { + Opcodes opcode = handlerClass.getAnnotation(Opcodes.class); + + if (opcode == null || opcode.disabled() || opcode.value() <= 0) { + return; + } + + PacketHandler packetHandler = (PacketHandler) handlerClass.newInstance(); + + this.handlers.put(opcode.value(), packetHandler); + } catch (Exception e) { + e.printStackTrace(); + } } public void registerHandlers(Class handlerClass) { @@ -32,21 +44,7 @@ public class GameServerPacketHandler { Set handlerClasses = reflections.getSubTypesOf(handlerClass); for (Object obj : handlerClasses) { - Class c = (Class) obj; - - try { - Opcodes opcode = c.getAnnotation(Opcodes.class); - - if (opcode == null || opcode.disabled() || opcode.value() <= 0) { - continue; - } - - PacketHandler packetHandler = (PacketHandler) c.newInstance(); - - this.handlers.put(opcode.value(), packetHandler); - } catch (Exception e) { - e.printStackTrace(); - } + this.registerPacketHandler((Class) obj); } // Debug