我尝试在用户执行命令后注册命令,我创建了一个实现ICommand的内部类,我确信它是有效的。我还检查过以确保我获得的MinecraftServer实例是有效的而不是null。这是我注册命令所做的:
CommandHandler commandHandler = (CommandHandler) minecraftServer.getCommandManager();
commandHandler.registerCommand(new Command());
这不起作用,当我尝试执行命令时,它显示命令未知。
答案 0 :(得分:3)
嗯,当然你需要一个命令处理程序。这是我的准系统版本。 检查代码以获取有用的提示。
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import akka.actor.FSM.Event;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.event.ClickEvent;
import net.minecraft.event.HoverEvent;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
public class InspectCommand implements ICommand
{
private final List aliases;
public InspectCommand()
{
aliases = new ArrayList();
// These are the commands that can be used to invoke your command
aliases.add("whotookmycookies");
aliases.add("wtmc");
aliases.add("co");
}
@Override
public int compareTo(Object o)
{
return 0;
}
@Override
public String getName()
{
// The big name of your command
return "whotookmycookies";
}
@Override
public String getCommandUsage(ICommandSender var1)
{
// Help file summary
return "whotookmycookies <text>";
}
@Override
public boolean isUsernameIndex(String[] var1, int var2)
{
// Can the user use this command I think
return true;
}
@Override
public List getAliases()
{
// Return all the aliases
return this.aliases;
}
@Override
public void execute(ICommandSender sender, String[] args) throws CommandException
{
World world = sender.getEntityWorld();
// Wanna execute on the player side? Do stuff here.
if (world.isRemote)
{
}
// Execute serverside
else
{
if (args.length == 0)
{
// Send a message to the command giving entity. Remember, this can also be a command block and at this point you haven't done a type check yet.
sender.addChatMessage(new ChatComponentText("WHO TOOK MY COOKIES! Seriously though. type an argument. type /wtmc help to see available commands"));
return;
}
if (sender instanceof EntityPlayer)
{
}
}
}
@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
{
// Look at native Minecraft commands like /tp to see how to return this if you wish to add tab auto completion.
return null;
}
}
然后你需要在FMLServerStartingEvent
@EventHandler
public void start(FMLServerStartingEvent event)
{
event.registerServerCommand(new InspectCommand());
}