Minecraft Forge Custom Player Command Issues

I am trying to make a new command for the first time and was following this tutorial which is slightly old but I believe will still work. After finishing it I tried running my mod and everything ran fine but my command did not exist. Here is my code:

public class MainRegistry {
    @EventHandler
    public void serverStart(FMLServerStartingEvent event) {
        MinecraftServer server = MinecraftServer.getServer();
        ICommandManager command = server.getCommandManager();
        ServerCommandManager manager = (ServerCommandManager) command;
        manager.registerCommand(new FireBall5());
    }

}

And my actual CommandBase class:

public class FireBall5 extends CommandBase {
    @Override
    public String getCommandName() {
        return "fireball 5";
    }

    @Override
    public String getCommandUsage(ICommandSender var1) {
        return "Shoots fireball with explosive power 5";
    }

    @Override
    public void processCommand(ICommandSender icommandsender, String[] var2) {
        if (icommandsender instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) icommandsender;
            World par2World = player.worldObj;
            if (!par2World.isRemote)
                par2World.spawnEntityInWorld(new PlayerFireBall(par2World, 5.0f));
        }

    }

}

It is calling an entity PlayerFireBall which I created myself and is simply a fireball with increased explosion power.

Answer

Commands cannot contain whitespaces. To implement your command, please follow the following:

@Override
public String getCommandName() {
    return "fireball"; // Remove the argument - Leave the command only.
}

The argument has to be read like this instead:

{
    if (sender instanceof EntityPlayer) {
        final EntityPlayer player = (EntityPlayer) sender;
        final World par2World = player.worldObj;

        final float power;

    // The "default" method:
        // power = 5; // Write the default value here!
        if (var2.length > 0) try {
            power = Float.parseFloat(var2[0]); // Parse the first argument.
        } catch(NumberFormatException ex) {}

    // The "validation" method:
        if (var2.length == 0) {
            sender.sendMessage("You forgot to specify the fireball power.");
            return;
        }
        if ( !var2[0].matches("\d{2}")) { // Asserts this argument is two digits
            sender.sendMessage("Incorrect.");
            return;
        }
        power = Float.parseFloat(var2[0]);

        if ( !par2World.isRemote)
            par2World.spawnEntityInWorld(new PlayerFireBall(par2World, power));
    }
}

Read more:

Leave a Reply

Your email address will not be published. Required fields are marked *