Minecraft Forge Custom Player Command Issues

2019-08-04 12:10发布

问题:

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.

回答1:

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:

  • Reading arguments as Integer for a Bounty in a Bukkit plugin


回答2:

See @Unihedron answer for the fix for the actual problem with this code. This answer simply cleans up his code even more.

CommandBase from which you inherit actually has several static methods that make parsing numbers and such from arguments much safer.

The ones you might want to use are:

  • CommandBase.parseDouble(ICommandSender, String) - Parses the given string and returns a double safely
  • CommandBase.parseDoubleWithMin(ICommandSender, String, int min) - Same as above, but with a required minimum value
  • CommandBase.parseDoubleBounded(ICommandSender, String, int min, int max) - Same as above, but with an upper limit as well

All these have an integer counterpart as well. Also, not useful for your context, but maybe for future use is this:

  • CommandBase.parseBoolean(ICommandSender, String) - Parses the given string and returns a boolean safely

Look through the CommandBase class for many more useful static methods.

So for example, rather than this:

if (var2.length > 0) try {
    power = Float.parseFloat(var2[0]); // Parse the first argument.
} catch(NumberFormatException ex) {}

Try this:

 if(var2.length > 0){
    //bounded because you don't want a size less than 0, could really be anything
    power = CommandBase.parseDoubleWithMin(sender, var2[0], 0);
}

Minecraft will automatically tell the player if there is something wrong with there input and safely return the parsed value to you.

Good luck with your mod and have fun!