javamethodspluginsminecraft

Is there a better way than a bunch of if statements to call my methods?


I am currently writing a plugin for SpigotMC/Minecraft. It is written in Java.

In my code, found on my Github I am attempting to call a different method depending on what the input of the player was.

My current way of doing this is grabbing the command they executed, then just using and if else if statement which looks something like this:

if (args[0].equalsIgnoreCase("create"))
        new SubCommandLootCrateCreate(plugin, sender, args).runSubCommand(false);

    else if (args[0].equalsIgnoreCase("key"))
        new SubCommandLootCrateKey(plugin, sender, args).runSubCommand(true);

    else if (args[0].equalsIgnoreCase("add"))
        new SubCommandLootCrateAdd(plugin, sender, args).runSubCommand(true);

    else if (args[0].equalsIgnoreCase("remove"))
        new SubCommandLootCrateRemove(plugin, sender, args).runSubCommand(false);

Theres about 10 more lines like this, and it just seems very inefficient. Feel free to explore my project more get a better idea of what I mean if you are confused.

Here is an example of the #runSubCommand(boolean)

public class SubCommandLootCrateCreate extends SubCommand
{
    private String[] args;
    private CommandSender sender;
    private LootCrate plugin;

    public SubCommandLootCrateCreate(LootCrate plugin, CommandSender sender, String[] args)
    {
    super(plugin, sender, args, Permission.COMMAND_LOOTCRATE_CREATE, Permission.COMMAND_LOOTCRATE_ADMIN);
    this.plugin = plugin;
    this.sender = sender;
    this.args = args;
    }

    @Override
    public void runSubCommand(boolean playerRequired)
    {
        //code
    }
}

Would anyone have a better solution to this kind of problem? I would like to see my code become more efficient. TIA!

TLDR; Need more efficient way than if else to call custom methods from custom objects

If I am missing anything or any information you need, please let me know and I will provide it.


Solution

  • A cleaner approach will be to use switch-case as shown below:

    switch(args[0].toUpperCase()) {
        case "CREATE":
            new SubCommandLootCrateCreate(plugin, sender, args).runSubCommand(false);
            break;
    
        case "KEY":
            new SubCommandLootCrateKey(plugin, sender, args).runSubCommand(true);
            break;
    
        case "ADD":
            new SubCommandLootCrateAdd(plugin, sender, args).runSubCommand(true);
            break;
    
        case "REMOVE":
            new SubCommandLootCrateRemove(plugin, sender, args).runSubCommand(false);
            break;
            
        //....
    
        default:
            //...
    }
    

    Java-14 onwards:

    You can use the switch statement as shown above or use the switch expression as shown below:

    switch(args[0].toUpperCase()) {
        case "CREATE" -> new SubCommandLootCrateCreate(plugin, sender, args).runSubCommand(false);
    
        case "KEY" -> new SubCommandLootCrateKey(plugin, sender, args).runSubCommand(true);
    
        case "ADD" -> new SubCommandLootCrateAdd(plugin, sender, args).runSubCommand(true);
    
        case "REMOVE" -> new SubCommandLootCrateRemove(plugin, sender, args).runSubCommand(false);
    
        //....
    
        default ->
            //...
    };