Welcome on MasterOf13FPS! MasterOf13FPS

Register today or sign up if you are already a member and never miss any cool content again :)

Reflection ModuleManager

MasterOf13FPS

Administrator
Staff member
Administrator
MasterOf13FPS
Joined
Jul 11, 2020
Messages
0
Reaction score
25
Points
0
Hier ein Modulemanager, bei dem man die Modules nicht mehr eintragen muss(Hab den ein bisschen auf die Standart-Basen angepasst):
[Bei "net.finx.module.impl" müsst ihr das Package einfügen, in dem eure Modules sind!]
[Die Imports müsst ihr selbst machen!]
[Hide]
ModuleManager
Code:
public class ModuleManager{
   private ArrayList<Module> list = new ArrayList<Module>();
   public ModuleManager() {
     try {
       for (Class<?> clazz : ClassUtil.getClasses("net.finx.module.impl")) {
         this.getList().add((Module) clazz.newInstance());
       }
     } catch (Throwable t) {
       t.printStackTrace();
     }

     Collections.sort(getList(), new Comparator<Module>() {

       @Override
       public int compare(Module o1, Module o2) {
         return Minecraft.getMinecraft().fontRendererObj.getStringWidth(o2.getName())
             - Minecraft.getMinecraft().fontRendererObj.getStringWidth(o1.getName());
       }
     });
   }
   public ArrayList<Module> getList() {
     return list;
   }
}

ClassUtil
Code:
public class ClassUtil {
   public static Class[] getClasses(String packageName)
    throws ClassNotFoundException, IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
    classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
   }
 
   private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
    List<Class> classes = new ArrayList<Class>();
    if (!directory.exists()) {
    return classes;
    }
    File[] files = directory.listFiles();
    for (File file : files) {
    if (file.isDirectory()) {
    assert !file.getName().contains(".");
    classes.addAll(findClasses(file, packageName + "." + file.getName()));
    } else if (file.getName().endsWith(".class")) {
    classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
    }
    }
    return classes;
   }
}
[/Hide]
 
Obfuscation macht das aber so weit ich weiß useless.
 
Btw @Finx, es ist soweit ich weiß nicht nötig Throwable zu catchen, Exception / die spezifischen Exceptions reichen.
ja, aber ich catche immer Throwables, bin es so gewohnt und finde es praktischer, da es dann bei jeder Exeption ist. ich könnte zwar uahc einfach eine Exeption catchen, aber wie gesagt, bins so gewohnt.
 
Hier ein Modulemanager, bei dem man die Modules nicht mehr eintragen muss(Hab den ein bisschen auf die Standart-Basen angepasst):
[Bei "net.finx.module.impl" müsst ihr das Package einfügen, in dem eure Modules sind!]
[Die Imports müsst ihr selbst machen!]
***Hidden content cannot be quoted.***
ty
 
shape1
shape2
shape3
shape4
shape5
shape6
Back
Top