2015-11-06 67 views
1

我想在用户执行命令后注册一个命令,我创建了一个实现ICommand的内部类,我相信它是有效的。我也检查过,以确保我得到的MinecraftServer实例有效且不为空。这里是我做的注册命令:命令不注册MinecraftForge 1.8

CommandHandler commandHandler = (CommandHandler) minecraftServer.getCommandManager(); 
commandHandler.registerCommand(new Command()); 

这不起作用,当我尝试执行该命令时,它显示命令未知。

回答

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

​​
+0

我这样做是为了注册一个命令,它仍然没有工作。 – CatSources

+0

你可以用你的命令定义和注册来更新你的问题吗? – Tschallacka