0

So I'm working on a plugin loader for a game and what it does is load plugins (class objects) from a .jar inside a folder, then add them to an ArrayList (so users can make their own mods). But my java experience isn't the best :/ So far it gets the classes in the jar but I need to check what methods are in the class (such as the constructor for labeling). Or if someone knows a better way to do this. Any help would be appreciated.

Plugin.java:

package me.rigamortis.faurax.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;

public class Plugin {

    public Minecraft mc = Minecraft.getMinecraft();
    final File folder = new File(mc.mcDataDir + File.separator + "Faurax/Plugins");
    public String jar;
    public static String className;
    private String name;
    private String desc;
    private int color;
    private int key;

    public Plugin() {
        if(!folder.exists())
            folder.mkdirs();

        loadPlugins(folder);

        if(folder != null)
            getClassNames(folder);
    }

    public void setName(String newName) {
        name = newName;
    }

    public void setDesc(String newDesc) {
        desc = newDesc;
    }

    public void setColor(int newColor) {
        color = newColor;
    }

    public void setKey(int newKey) {
        key = newKey;
    }

    public String getName() {
        return this.name;
    }

    public String getDesc() {
        return this.desc;
    }

    public int getKey() {
        return this.key;
    }

    public int getColor() {
        return this.color;
    }

    /**
     * A hook for plugins
     */
    public void onTick() {}

    public void loadPlugins(final File folder) {
        try{
            for (final File fileEntry : folder.listFiles()) {
                if ( !fileEntry.isDirectory()) {
                    if(fileEntry.getName().endsWith(".jar")){
                        jar = fileEntry.getName();
                        Core.logNormal("Loading " + fileEntry.getName());
                    }
                }
            }
        }
        catch(Exception e) {
            Core.logError("Error? jar isin't a plugin.");
            e.printStackTrace();
        }
    }

    public void getClassNames(final File dir) {
        try{
            ZipInputStream zip = new ZipInputStream(new FileInputStream(dir + File.seperator + jar));
            for(ZipEntry entry = zip.getNextEntry(); entry != null; entry =     zip.getNextEntry())
                if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
                    Core.logNormal("Found "+entry);
                    className = entry.toString();
                }
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}

PluginLoader.java:

package me.rigamortis.faurax.plugin;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;

public class PluginLoader {

    public static List<Plugin> plugins = new ArrayList<Plugin>();

    public PluginLoader() {
        loadPlugins();
    }

    public void loadPlugins() {}

}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
420Blaze
  • 11
  • 2
  • 3
    Use __reflection__!!!~ Or you can instruct the program to remember the methods anyway, since they're not going to be changed. By the way, why are you trying to build a Minecraft plugin without using Bukkit? – Unihedron Jul 23 '14 at 13:22
  • Making a modded griefing client :P Ill check out reflection though. Quite new to java .-. – 420Blaze Jul 23 '14 at 13:24
  • check http://stackoverflow.com/questions/22200676/recursively-list-all-properties-exposed-by-a-class/22201894#22201894 , upvote if it is useful – robermann Jul 23 '14 at 13:24
  • 1
    have you thought about using JarClassLoader? – Oliver Watkins Jul 23 '14 at 13:25
  • I have heard about it but, I dont know much. Ill take a look though. – 420Blaze Jul 23 '14 at 13:29

1 Answers1

0

I answered a similar question to this not too long ago. Here's the code to get all methods from a given class, I think it's fairly self-explanatory:

void foo(Object o)
{
    Class<?> c = o.getClass();
    for (Method m : c.getDeclaredMethods())
    {
        //do whatever
    }
}

This takes advantage of the Java's Reflection API. You can read more about it here.

Azar
  • 1,086
  • 12
  • 27