#pick class to create object of given string

36 messages · Page 1 of 1 (latest)

marsh ember
#

I have this code (in a minecraft mod I'm creating):

SubCommand command = null;
            
            switch (argsRaw[0]) {
                //help menu
                case "help": {
                    command = new help();
                    break;
                
                //basic calculations
                } case "airtime": case "duration": {
                    command = new airtime();
                    break;
                    
                } case "distance": {
                    command = new distance();
                    break;
                    
                } case "height": {
                    command = new height();
                    break;
                    
                //utility
                } case "face": case "rotate": {
                    command = new face();
                    break;

                } case "optimizesensitivity": case "optimizesens": case "optimisesensitivity": case "optimisesens": {
                    command = new optimizesensitivity();
                    break;
                    
                } case "setsensitivity": case "setsens": {
                    command = new setsensitivity();
                    break;
                
                //advanced calculations
                } case "taps": case "tapsetup": case "bruteforcetaps": {
                    command = new taps();
                    break;
                    
                //command doesn't exist
                } default: {
                    Main.sendChatMessage("\"" + argsRaw[0] + "\" is not a valid command. Use /mm help for a list of commands.");
                    return;
                }
            }
            //execute
            command.run(args);

Is there any way i can make it so its similar to this:

String commandName = argsRaw[0];
command = new "commandName"();
celest lanceBOT
#

Hey, @marsh ember!
Please remember to /close this post once your question has been answered!

marsh ember
#

what the commands look like

#

also, not only just finding which class to create an object of by its getCommandName() but could I also use the aliases

dull rapids
dull rapids
marsh ember
#

i have to create it

#

like

#

if argsRaw[0] is "airtime"

#

i create a new instance of the airtime class

#

etc

void gull
#

I think it will be wise to use Factory pattern. You can create CommandFactory with method getInstance(String commandName) which will go through all your SubCommand implementations and find one with getCommandName() value equals to commandName argument.

marsh ember
#

how does CommandFactory work?

void gull
#

Inside CommandFactory you can get all SubCommand implementations by using Reflection mechanism. After that by calling getCommandName method (which can be static) you can find needed implementation and create an instance.

dull rapids
#
Class<?> clazz = Class.forName("name");
var object = clazz.getDeclaredConstructor().newInstance();

you can use either this or factory pattern.

marsh ember
#

alr

#

my brains currently overloaded with something else ill look at this later

marsh ember
dull rapids
#

then use Factory pattern

marsh ember
#

what is that

#

srry is there a documentation

#

or tutorial

void gull
#

There are a lot of tutorials in google. Just write "factory pattern java explained" or something else. I found that one https://www.geeksforgeeks.org/factory-method-design-pattern-in-java/. Looks like very similar to your case.

marsh ember
dull rapids
#

there is no any better way to do this

#

or you want to use enum, but factory pattern would be the best

marsh ember
#

is there no way to just do like

#

look thru package morphmod.commands for matching alias or name

#

rather than just a massive chain which will get very annoying to work with as i add more commands

#

or is there a method to like

#

get an object array of all classes in the morphmod.commands package

#

and i can just put a for loop and check if one of the objects in the array's getCommandName or getCommandAliases match

marsh ember
#

and it works now