Skip to content
Snippets Groups Projects
Commit 67dc219f authored by Nos's avatar Nos
Browse files

BETA: Reworked `MasterHandler` registering of handlers to allow a handler to...

BETA: Reworked `MasterHandler` registering of handlers to allow a handler to implement more than one interface of handlers.
parent 1a7b79b8
No related branches found
No related tags found
No related merge requests found
......@@ -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...");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment