0

name.class ファイルのフォルダーをスキャンし、それらをプログラムの arraylist にロードするにはどうすればよいでしょうか。

何をする必要があるかについての一般的な考えはありますが、それをコードに実装するために何を使用する必要があるのか​​ わかりません。

フォルダをスキャンします 見つかった .class ファイルを読み込みます array.add( new class(params)); を使用してクラスを arraylist に追加します 結局のところ、クラスにあるメソッドを実行しています。

これは、モジュールをクライアントにロードするシステムを実行する現在の方法です(モジュールが呼び出された場合)。

 package pro.skid.Gabooltheking.Module;

import java.util.ArrayList;

public class ModuleLoader {

    public static ArrayList<Module> module = new ArrayList<Module>();
    public final ArrayList<Module> getModule(){ return module; }

    public static void startModule(){
        module.clear();
    }

        public final Module getModuleByName(String moduleName){  
            for( Module module : getModule()){
                if(module.getName().equalsIgnoreCase(moduleName)){ return module; }
            }
            return null;
        }

        public static void keyBind(int key){ 
            for(Module m : module){
                if(m.getKey() == key){
                    m.toggle();
                }
            }
        }

        public static void runModuleTick(){
            for(Module m : module){
                if(m.getState()){
                    m.onTick();
                }
            }
        }

}

これは、抽象 Module クラスがどのように見えるかです

package pro.skid.Gabooltheking.Module;

public abstract class Module {

    int key,color;
    String name;
    boolean state;

    /**
     * Set's the following variables
     * @param name- name of mod
     * @param key- keybind for mod
     * @param color- color in gui
     */
    public Module(String name, int key, int color){
        this.name = name;
        this.key = key;
        this.color = color;
    }
    /**
     * Set's the state of the mod to on or off.
     */
    public void toggle() 
    { state = !state; 
        if(this.getState())
            { 
             this.onToggle();
            }else{ 
             this.onDisable();
        } 
    }
    /**
     * Does something when mod is first toggled.
     * Does it only once.
     */
    public abstract void onToggle();

    /**
     * Does something when mod is disabled.
     * Does it only once.
     */
    public abstract void onDisable();

    /**
     * Does something when mod is toggled.
     * Loops untill hack is disabled.
     */
    public abstract void onTick();


    public String getName(){return this.name; }
    public int getKey(){ return this.key; }
    public int getColor(){ return this.color; }
    public boolean getState(){ return this.state; }
}

私の意見では、すべてのヘルプは良いヘルプです。また、各メソッドが何をするかを覚えておくためのくだらないコメントは無視してください。

4

1 に答える 1