PublishCommand.java

net.minecraft.server.commands.PublishCommand

信息

  • 全限定名:net.minecraft.server.commands.PublishCommand
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/PublishCommand.java
  • 起始行号:L19
  • 职责:

    TODO

字段/常量

  • ERROR_FAILED

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L20
    • 说明:

      TODO

  • ERROR_ALREADY_PUBLISHED

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L21
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L25

  • 方法名:register
  • 源码定位:L25
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int publish(CommandSourceStack source, int port, boolean allowCommands, GameType type) @ L59

  • 方法名:publish
  • 源码定位:L59
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • port: int
  • allowCommands: boolean
  • type: GameType

说明:

TODO

public static MutableComponent getSuccessMessage(int port) @ L70

  • 方法名:getSuccessMessage
  • 源码定位:L70
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • port: int

说明:

TODO

代码

public class PublishCommand {
    private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.publish.failed"));
    private static final DynamicCommandExceptionType ERROR_ALREADY_PUBLISHED = new DynamicCommandExceptionType(
        port -> Component.translatableEscape("commands.publish.alreadyPublished", port)
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("publish")
                .requires(Commands.hasPermission(Commands.LEVEL_OWNERS))
                .executes(c -> publish(c.getSource(), HttpUtil.getAvailablePort(), false, null))
                .then(
                    Commands.argument("allowCommands", BoolArgumentType.bool())
                        .executes(c -> publish(c.getSource(), HttpUtil.getAvailablePort(), BoolArgumentType.getBool(c, "allowCommands"), null))
                        .then(
                            Commands.argument("gamemode", GameModeArgument.gameMode())
                                .executes(
                                    c -> publish(
                                        c.getSource(),
                                        HttpUtil.getAvailablePort(),
                                        BoolArgumentType.getBool(c, "allowCommands"),
                                        GameModeArgument.getGameMode(c, "gamemode")
                                    )
                                )
                                .then(
                                    Commands.argument("port", IntegerArgumentType.integer(0, 65535))
                                        .executes(
                                            c -> publish(
                                                c.getSource(),
                                                IntegerArgumentType.getInteger(c, "port"),
                                                BoolArgumentType.getBool(c, "allowCommands"),
                                                GameModeArgument.getGameMode(c, "gamemode")
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int publish(CommandSourceStack source, int port, boolean allowCommands, @Nullable GameType type) throws CommandSyntaxException {
        if (source.getServer().isPublished()) {
            throw ERROR_ALREADY_PUBLISHED.create(source.getServer().getPort());
        } else if (!source.getServer().publishServer(type, allowCommands, port)) {
            throw ERROR_FAILED.create();
        } else {
            source.sendSuccess(() -> getSuccessMessage(port), true);
            return port;
        }
    }
 
    public static MutableComponent getSuccessMessage(int port) {
        Component portText = ComponentUtils.copyOnClickText(String.valueOf(port));
        return Component.translatable("commands.publish.started", portText);
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • GameModeArgument

    • 引用位置: 方法调用
    • 关联成员: GameModeArgument.gameMode(), GameModeArgument.getGameMode()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable(), Component.translatableEscape()
  • ComponentUtils

    • 引用位置: 方法调用
    • 关联成员: ComponentUtils.copyOnClickText()
  • MutableComponent

    • 引用位置: 返回值
  • HttpUtil

    • 引用位置: 方法调用
    • 关联成员: HttpUtil.getAvailablePort()
  • GameType

    • 引用位置: 参数