Minecraft Forge自定义播放器命令问题 [英] Minecraft Forge Custom Player Command Issues
本文介绍了Minecraft Forge自定义播放器命令问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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());
}
}
还有我实际的CommandBase
类:
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));
}
}
}
它是我创建的实体PlayerFireBall
,它只是爆炸力增强的火球.
It is calling an entity PlayerFireBall
which I created myself and is simply a fireball with increased explosion power.
推荐答案
命令不能包含空格.要执行您的命令,请按照以下步骤操作:
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.
}
必须像这样读取参数:
{
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));
}
}
了解更多信息
这篇关于Minecraft Forge自定义播放器命令问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文