From 67dc219fac080fda3ad4152cc1628d60afa9a096 Mon Sep 17 00:00:00 2001 From: Nos <NosBit@users.noreply.github.com> Date: Mon, 21 Jul 2014 19:45:50 +0000 Subject: [PATCH] BETA: Reworked `MasterHandler` registering of handlers to allow a handler to implement more than one interface of handlers. --- .../data/scripts/handlers/MasterHandler.java | 98 +++++++++---------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/L2J_DataPack_BETA/dist/game/data/scripts/handlers/MasterHandler.java b/L2J_DataPack_BETA/dist/game/data/scripts/handlers/MasterHandler.java index d02f21f47b..cca33f49f1 100644 --- a/L2J_DataPack_BETA/dist/game/data/scripts/handlers/MasterHandler.java +++ b/L2J_DataPack_BETA/dist/game/data/scripts/handlers/MasterHandler.java @@ -19,6 +19,9 @@ package handlers; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; @@ -28,6 +31,7 @@ import com.l2jserver.gameserver.handler.ActionShiftHandler; import com.l2jserver.gameserver.handler.AdminCommandHandler; import com.l2jserver.gameserver.handler.BypassHandler; import com.l2jserver.gameserver.handler.ChatHandler; +import com.l2jserver.gameserver.handler.IHandler; import com.l2jserver.gameserver.handler.ItemHandler; import com.l2jserver.gameserver.handler.PunishmentHandler; import com.l2jserver.gameserver.handler.TargetHandler; @@ -274,19 +278,19 @@ public class MasterHandler { private static final Logger _log = Logger.getLogger(MasterHandler.class.getName()); - private static final Class<?>[] _loadInstances = + private static final IHandler<?, ?>[] _loadInstances = { - ActionHandler.class, - ActionShiftHandler.class, - AdminCommandHandler.class, - BypassHandler.class, - ChatHandler.class, - ItemHandler.class, - PunishmentHandler.class, - UserCommandHandler.class, - VoicedCommandHandler.class, - TargetHandler.class, - TelnetHandler.class, + ActionHandler.getInstance(), + ActionShiftHandler.getInstance(), + AdminCommandHandler.getInstance(), + BypassHandler.getInstance(), + ChatHandler.getInstance(), + ItemHandler.getInstance(), + PunishmentHandler.getInstance(), + UserCommandHandler.getInstance(), + VoicedCommandHandler.getInstance(), + TargetHandler.getInstance(), + TelnetHandler.getInstance(), }; private static final Class<?>[][] _handlers = @@ -566,27 +570,27 @@ public class MasterHandler { _log.log(Level.INFO, "Loading Handlers..."); - Object loadInstance = null; - Method method = null; - Class<?>[] interfaces = null; - Object handler = null; - - for (int i = 0; i < _loadInstances.length; i++) + Map<IHandler<?, ?>, Method> registerHandlerMethods = new HashMap<>(); + for (IHandler<?, ?> loadInstance : _loadInstances) { - try - { - method = _loadInstances[i].getMethod("getInstance"); - loadInstance = method.invoke(_loadInstances[i]); - } - catch (Exception e) + registerHandlerMethods.put(loadInstance, null); + for (Method method : loadInstance.getClass().getMethods()) { - _log.log(Level.WARNING, "Failed invoking getInstance method for handler: " + _loadInstances[i].getSimpleName(), e); - continue; + if (method.getName().equals("registerHandler") && !method.isBridge()) + { + registerHandlerMethods.put(loadInstance, method); + } } - - method = null; - - for (Class<?> c : _handlers[i]) + } + + registerHandlerMethods.entrySet().stream().filter(e -> e.getValue() == null).forEach(e -> + { + _log.log(Level.WARNING, "Failed loading handlers of: " + e.getKey().getClass().getSimpleName() + " seems registerHandler function does not exist."); + }); + + for (Class<?> classes[] : _handlers) + { + for (Class<?> c : classes) { if (c == null) { @@ -595,18 +599,13 @@ public class MasterHandler try { - // Don't wtf some classes extending another like ItemHandler, Elixir, etc.. and we need to find where the hell is interface xD - interfaces = c.getInterfaces().length > 0 ? // Standardly handler has implementation - c.getInterfaces() : c.getSuperclass().getInterfaces().length > 0 ? // No? then it extends another handler like (ItemSkills->ItemSkillsTemplate) - c.getSuperclass().getInterfaces() : c.getSuperclass().getSuperclass().getInterfaces(); // O noh that's Elixir->ItemSkills->ItemSkillsTemplate - if (method == null) + Object handler = c.newInstance(); + for (Entry<IHandler<?, ?>, Method> entry : registerHandlerMethods.entrySet()) { - method = loadInstance.getClass().getMethod("registerHandler", interfaces); - } - handler = c.newInstance(); - if (method.getParameterTypes()[0].isInstance(handler)) - { - method.invoke(loadInstance, handler); + if ((entry.getValue() != null) && entry.getValue().getParameterTypes()[0].isInstance(handler)) + { + entry.getValue().invoke(entry.getKey(), handler); + } } } catch (Exception e) @@ -615,18 +614,11 @@ public class MasterHandler continue; } } - // And lets try get size - try - { - method = loadInstance.getClass().getMethod("size"); - Object returnVal = method.invoke(loadInstance); - _log.log(Level.INFO, loadInstance.getClass().getSimpleName() + ": Loaded " + returnVal + " Handlers"); - } - catch (Exception e) - { - _log.log(Level.WARNING, "Failed invoking size method for handler: " + loadInstance.getClass().getSimpleName(), e); - continue; - } + } + + for (IHandler<?, ?> loadInstance : _loadInstances) + { + _log.log(Level.INFO, loadInstance.getClass().getSimpleName() + ": Loaded " + loadInstance.size() + " Handlers"); } _log.log(Level.INFO, "Handlers Loaded..."); -- GitLab