dimanche 1 décembre 2019

Plugins for my Minecraft mod can't use any classes from the Minecraft forge source

This is the code that successfully loads plugins (I know this because I used a logger in a plugin and I saw the message in the log file). The only problem is, if an event handling method in the plugin contains something from the Minecraft forge source, it won't invoke the method.

public void loadPlugin(File jar)
{
    try
    {
        ClassLoader jarClassLoader=URLClassLoader.newInstance(new URL[]{jar.toURI().toURL()},getClass().getClassLoader());
        Enumeration<JarEntry> jarEntries=new JarFile(jar).entries();
        while(jarEntries.hasMoreElements())
        {
            JarEntry jarEntry=jarEntries.nextElement();
            if(jarEntry.isDirectory()|!jarEntry.getName().endsWith(".class"))
                continue;
            Class aClass=jarClassLoader.loadClass(jarEntry.getName().substring(0,jarEntry.getName().length()-6).replace('/','.'));
            if(aClass.isAnnotationPresent(PatchPlugin.class))
            {
                plugins.add(aClass);
                EventHandler.passEvent(aClass,new LoadEvent());
            }
        }
    }
    catch(IOException|ClassNotFoundException ignored){}
}

And this code passes events to a plugin

public static boolean passEvent(Class plugin,Event event)
{
    for(Method method:plugin.getDeclaredMethods())
    {
        if(method.isAnnotationPresent(EventTarget.class))
        {
            try
            {
                Class[] parTypes=method.getParameterTypes();
                if(parTypes.length==1&parTypes[0].isInstance(event))
                {
                    method.invoke(plugin.newInstance(),event);
                    return event.isCancelled();
                }
            }
            catch(IllegalAccessException|InvocationTargetException|InstantiationException ignored){}
        }
    }
    return false;
}




Aucun commentaire:

Enregistrer un commentaire