有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java命令不注册MinecraftForge 1.8

我试图在用户执行命令后注册一个命令,我创建了一个实现ICommand的内部类,我确信它是有效的。我还检查了我得到的MinecraftServer实例是否有效且不为空。下面是我注册命令的步骤:

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

这不起作用,当我试图执行该命令时,它表明该命令是未知的


共 (1) 个答案

  1. # 1 楼答案

    当然,你需要一个命令处理程序。这是我的一个赤裸裸的版本。 查看代码中的注释以获得有用的提示

    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());
    }