我正在为1.8.9开发一些Minecraft mod。
我正在尝试创建的命令只是将消息发送给发件人。

这是命令类和主类的代码

命令类:

package happyandjust.happymod.commands;

import java.util.HashMap;
import java.util.List;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;

public class Command extends CommandBase {

    private HashMap<String, String> collection = new HashMap<String, String>();

    @Override
    public String getCommandName() {
        return "collection";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "collection <enchant name>";
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) throws CommandException {

        collection.put("harvesting", "Wheat Collection Level 2");
        collection.put("cubism", "Pumpkin Collection Level 5");

        if (args.length < 1) {
            sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Usage: /collection [Enchant Name]"));
            return;
        }

        if (args.length == 1) {
            String enchant_name = args[0].toLowerCase();
            String collec = collection.get(enchant_name);
            if (collec == null) {
                sender.addChatMessage(new ChatComponentText(
                        EnumChatFormatting.RED + enchant_name.toUpperCase() + " is not valid Enchant Name"));
                return;
            }
            sender.addChatMessage(new ChatComponentText(
                    EnumChatFormatting.GREEN + enchant_name.toUpperCase() + " is at " + collection.get(enchant_name)));
        }

    }

    @Override
    public boolean canCommandSenderUseCommand(ICommandSender sender) {
        return true;

    }

}


主类:

package happyandjust.happymod.main;

import happyandjust.happymod.commands.Command;
import happyandjust.happymod.proxy.CommonProxy;
import happyandjust.happymod.util.Reference;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;

@Mod(modid = Reference.MOD_ID, name = Reference.NAME, version = Reference.VERSION)

public class HappyMod {

    @Instance
    public static HappyMod instance;

    @SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.COMMON_PROXY_CLASS)
    public static CommonProxy proxy;

    @EventHandler
    public static void preInit(FMLPostInitializationEvent e) {
    }

    @EventHandler
    public static void init(FMLInitializationEvent e) {
        ClientCommandHandler.instance.registerCommand(new Command());
    }

    @EventHandler
    public static void postInit(FMLPostInitializationEvent e) {

    }

}


它在单人播放器中工作正常,但如果我像hypixel一样转到多人服务器。
它说“未知命令”

我不知道要这样做

谁能帮助我在多人服务器中使用此命令?

最佳答案

您需要从getRequiredPermissionLevel()覆盖CommandBase方法,该方法才能在多人游戏中使用。

@Override
public int getRequiredPermissionLevel() {
    return 0;
}

08-05 07:34