BossBarCommands.java

net.minecraft.server.commands.BossBarCommands

信息

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

    TODO

字段/常量

  • ERROR_ALREADY_EXISTS

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

      TODO

  • ERROR_DOESNT_EXIST

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

      TODO

  • ERROR_NO_PLAYER_CHANGE

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

      TODO

  • ERROR_NO_NAME_CHANGE

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

      TODO

  • ERROR_NO_COLOR_CHANGE

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

      TODO

  • ERROR_NO_STYLE_CHANGE

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

      TODO

  • ERROR_NO_VALUE_CHANGE

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

      TODO

  • ERROR_NO_MAX_CHANGE

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

      TODO

  • ERROR_ALREADY_HIDDEN

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

      TODO

  • ERROR_ALREADY_VISIBLE

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

      TODO

  • SUGGEST_BOSS_BAR

    • 类型: SuggestionProvider<CommandSourceStack>
    • 修饰符: public static final
    • 源码定位: L61
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

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

public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L65

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int getValue(CommandSourceStack source, CustomBossEvent bossBar) @ L178

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent

说明:

TODO

private static int getMax(CommandSourceStack source, CustomBossEvent bossBar) @ L183

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent

说明:

TODO

private static int getVisible(CommandSourceStack source, CustomBossEvent bossBar) @ L188

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent

说明:

TODO

private static int getPlayers(CommandSourceStack source, CustomBossEvent bossBar) @ L198

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent

说明:

TODO

private static int setVisible(CommandSourceStack source, CustomBossEvent bossBar, boolean visible) @ L216

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • visible: boolean

说明:

TODO

private static int setValue(CommandSourceStack source, CustomBossEvent bossBar, int value) @ L235

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • value: int

说明:

TODO

private static int setMax(CommandSourceStack source, CustomBossEvent bossBar, int value) @ L245

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • value: int

说明:

TODO

private static int setColor(CommandSourceStack source, CustomBossEvent bossBar, BossEvent.BossBarColor color) @ L255

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • color: BossEvent.BossBarColor

说明:

TODO

private static int setStyle(CommandSourceStack source, CustomBossEvent bossBar, BossEvent.BossBarOverlay style) @ L265

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • style: BossEvent.BossBarOverlay

说明:

TODO

private static int setName(CommandSourceStack source, CustomBossEvent bossBar, Component name) @ L275

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • name: Component

说明:

TODO

private static int setPlayers(CommandSourceStack source, CustomBossEvent bossBar, Collection<ServerPlayer> targets) @ L286

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent
  • targets: Collection

说明:

TODO

private static int listBars(CommandSourceStack source) @ L309

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int createBar(CommandSourceStack source, Identifier id, Component name) @ L325

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

参数:

  • source: CommandSourceStack
  • id: Identifier
  • name: Component

说明:

TODO

private static int removeBar(CommandSourceStack source, CustomBossEvent bossBar) @ L340

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

参数:

  • source: CommandSourceStack
  • bossBar: CustomBossEvent

说明:

TODO

public static CustomBossEvent getBossBar(CommandContext<CommandSourceStack> context) @ L348

  • 方法名:getBossBar
  • 源码定位:L348
  • 返回类型:CustomBossEvent
  • 修饰符:public static

参数:

  • context: CommandContext

说明:

TODO

代码

public class BossBarCommands {
    private static final DynamicCommandExceptionType ERROR_ALREADY_EXISTS = new DynamicCommandExceptionType(
        id -> Component.translatableEscape("commands.bossbar.create.failed", id)
    );
    private static final DynamicCommandExceptionType ERROR_DOESNT_EXIST = new DynamicCommandExceptionType(
        id -> Component.translatableEscape("commands.bossbar.unknown", id)
    );
    private static final SimpleCommandExceptionType ERROR_NO_PLAYER_CHANGE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.players.unchanged")
    );
    private static final SimpleCommandExceptionType ERROR_NO_NAME_CHANGE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.name.unchanged")
    );
    private static final SimpleCommandExceptionType ERROR_NO_COLOR_CHANGE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.color.unchanged")
    );
    private static final SimpleCommandExceptionType ERROR_NO_STYLE_CHANGE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.style.unchanged")
    );
    private static final SimpleCommandExceptionType ERROR_NO_VALUE_CHANGE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.value.unchanged")
    );
    private static final SimpleCommandExceptionType ERROR_NO_MAX_CHANGE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.max.unchanged")
    );
    private static final SimpleCommandExceptionType ERROR_ALREADY_HIDDEN = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.visibility.unchanged.hidden")
    );
    private static final SimpleCommandExceptionType ERROR_ALREADY_VISIBLE = new SimpleCommandExceptionType(
        Component.translatable("commands.bossbar.set.visibility.unchanged.visible")
    );
    public static final SuggestionProvider<CommandSourceStack> SUGGEST_BOSS_BAR = (c, b) -> SharedSuggestionProvider.suggestResource(
        c.getSource().getServer().getCustomBossEvents().getIds(), b
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("bossbar")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("add")
                        .then(
                            Commands.argument("id", IdentifierArgument.id())
                                .then(
                                    Commands.argument("name", ComponentArgument.textComponent(context))
                                        .executes(
                                            c -> createBar(c.getSource(), IdentifierArgument.getId(c, "id"), ComponentArgument.getResolvedComponent(c, "name"))
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("remove")
                        .then(
                            Commands.argument("id", IdentifierArgument.id()).suggests(SUGGEST_BOSS_BAR).executes(c -> removeBar(c.getSource(), getBossBar(c)))
                        )
                )
                .then(Commands.literal("list").executes(c -> listBars(c.getSource())))
                .then(
                    Commands.literal("set")
                        .then(
                            Commands.argument("id", IdentifierArgument.id())
                                .suggests(SUGGEST_BOSS_BAR)
                                .then(
                                    Commands.literal("name")
                                        .then(
                                            Commands.argument("name", ComponentArgument.textComponent(context))
                                                .executes(c -> setName(c.getSource(), getBossBar(c), ComponentArgument.getResolvedComponent(c, "name")))
                                        )
                                )
                                .then(
                                    Commands.literal("color")
                                        .then(Commands.literal("pink").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.PINK)))
                                        .then(Commands.literal("blue").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.BLUE)))
                                        .then(Commands.literal("red").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.RED)))
                                        .then(Commands.literal("green").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.GREEN)))
                                        .then(Commands.literal("yellow").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.YELLOW)))
                                        .then(Commands.literal("purple").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.PURPLE)))
                                        .then(Commands.literal("white").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.WHITE)))
                                )
                                .then(
                                    Commands.literal("style")
                                        .then(
                                            Commands.literal("progress")
                                                .executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.PROGRESS))
                                        )
                                        .then(
                                            Commands.literal("notched_6")
                                                .executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_6))
                                        )
                                        .then(
                                            Commands.literal("notched_10")
                                                .executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_10))
                                        )
                                        .then(
                                            Commands.literal("notched_12")
                                                .executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_12))
                                        )
                                        .then(
                                            Commands.literal("notched_20")
                                                .executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_20))
                                        )
                                )
                                .then(
                                    Commands.literal("value")
                                        .then(
                                            Commands.argument("value", IntegerArgumentType.integer(0))
                                                .executes(c -> setValue(c.getSource(), getBossBar(c), IntegerArgumentType.getInteger(c, "value")))
                                        )
                                )
                                .then(
                                    Commands.literal("max")
                                        .then(
                                            Commands.argument("max", IntegerArgumentType.integer(1))
                                                .executes(c -> setMax(c.getSource(), getBossBar(c), IntegerArgumentType.getInteger(c, "max")))
                                        )
                                )
                                .then(
                                    Commands.literal("visible")
                                        .then(
                                            Commands.argument("visible", BoolArgumentType.bool())
                                                .executes(c -> setVisible(c.getSource(), getBossBar(c), BoolArgumentType.getBool(c, "visible")))
                                        )
                                )
                                .then(
                                    Commands.literal("players")
                                        .executes(c -> setPlayers(c.getSource(), getBossBar(c), Collections.emptyList()))
                                        .then(
                                            Commands.argument("targets", EntityArgument.players())
                                                .executes(c -> setPlayers(c.getSource(), getBossBar(c), EntityArgument.getOptionalPlayers(c, "targets")))
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("get")
                        .then(
                            Commands.argument("id", IdentifierArgument.id())
                                .suggests(SUGGEST_BOSS_BAR)
                                .then(Commands.literal("value").executes(c -> getValue(c.getSource(), getBossBar(c))))
                                .then(Commands.literal("max").executes(c -> getMax(c.getSource(), getBossBar(c))))
                                .then(Commands.literal("visible").executes(c -> getVisible(c.getSource(), getBossBar(c))))
                                .then(Commands.literal("players").executes(c -> getPlayers(c.getSource(), getBossBar(c))))
                        )
                )
        );
    }
 
    private static int getValue(CommandSourceStack source, CustomBossEvent bossBar) {
        source.sendSuccess(() -> Component.translatable("commands.bossbar.get.value", bossBar.getDisplayName(), bossBar.value()), true);
        return bossBar.value();
    }
 
    private static int getMax(CommandSourceStack source, CustomBossEvent bossBar) {
        source.sendSuccess(() -> Component.translatable("commands.bossbar.get.max", bossBar.getDisplayName(), bossBar.max()), true);
        return bossBar.max();
    }
 
    private static int getVisible(CommandSourceStack source, CustomBossEvent bossBar) {
        if (bossBar.isVisible()) {
            source.sendSuccess(() -> Component.translatable("commands.bossbar.get.visible.visible", bossBar.getDisplayName()), true);
            return 1;
        } else {
            source.sendSuccess(() -> Component.translatable("commands.bossbar.get.visible.hidden", bossBar.getDisplayName()), true);
            return 0;
        }
    }
 
    private static int getPlayers(CommandSourceStack source, CustomBossEvent bossBar) {
        if (bossBar.getPlayers().isEmpty()) {
            source.sendSuccess(() -> Component.translatable("commands.bossbar.get.players.none", bossBar.getDisplayName()), true);
        } else {
            source.sendSuccess(
                () -> Component.translatable(
                    "commands.bossbar.get.players.some",
                    bossBar.getDisplayName(),
                    bossBar.getPlayers().size(),
                    ComponentUtils.formatList(bossBar.getPlayers(), Player::getDisplayName)
                ),
                true
            );
        }
 
        return bossBar.getPlayers().size();
    }
 
    private static int setVisible(CommandSourceStack source, CustomBossEvent bossBar, boolean visible) throws CommandSyntaxException {
        if (bossBar.isVisible() == visible) {
            if (visible) {
                throw ERROR_ALREADY_VISIBLE.create();
            } else {
                throw ERROR_ALREADY_HIDDEN.create();
            }
        } else {
            bossBar.setVisible(visible);
            if (visible) {
                source.sendSuccess(() -> Component.translatable("commands.bossbar.set.visible.success.visible", bossBar.getDisplayName()), true);
            } else {
                source.sendSuccess(() -> Component.translatable("commands.bossbar.set.visible.success.hidden", bossBar.getDisplayName()), true);
            }
 
            return 0;
        }
    }
 
    private static int setValue(CommandSourceStack source, CustomBossEvent bossBar, int value) throws CommandSyntaxException {
        if (bossBar.value() == value) {
            throw ERROR_NO_VALUE_CHANGE.create();
        } else {
            bossBar.setValue(value);
            source.sendSuccess(() -> Component.translatable("commands.bossbar.set.value.success", bossBar.getDisplayName(), value), true);
            return value;
        }
    }
 
    private static int setMax(CommandSourceStack source, CustomBossEvent bossBar, int value) throws CommandSyntaxException {
        if (bossBar.max() == value) {
            throw ERROR_NO_MAX_CHANGE.create();
        } else {
            bossBar.setMax(value);
            source.sendSuccess(() -> Component.translatable("commands.bossbar.set.max.success", bossBar.getDisplayName(), value), true);
            return value;
        }
    }
 
    private static int setColor(CommandSourceStack source, CustomBossEvent bossBar, BossEvent.BossBarColor color) throws CommandSyntaxException {
        if (bossBar.getColor().equals(color)) {
            throw ERROR_NO_COLOR_CHANGE.create();
        } else {
            bossBar.setColor(color);
            source.sendSuccess(() -> Component.translatable("commands.bossbar.set.color.success", bossBar.getDisplayName()), true);
            return 0;
        }
    }
 
    private static int setStyle(CommandSourceStack source, CustomBossEvent bossBar, BossEvent.BossBarOverlay style) throws CommandSyntaxException {
        if (bossBar.getOverlay().equals(style)) {
            throw ERROR_NO_STYLE_CHANGE.create();
        } else {
            bossBar.setOverlay(style);
            source.sendSuccess(() -> Component.translatable("commands.bossbar.set.style.success", bossBar.getDisplayName()), true);
            return 0;
        }
    }
 
    private static int setName(CommandSourceStack source, CustomBossEvent bossBar, Component name) throws CommandSyntaxException {
        Component replaced = ComponentUtils.resolve(ResolutionContext.builder().withSource(source).withEntityOverride(null).build(), name);
        if (bossBar.getName().equals(replaced)) {
            throw ERROR_NO_NAME_CHANGE.create();
        } else {
            bossBar.setName(replaced);
            source.sendSuccess(() -> Component.translatable("commands.bossbar.set.name.success", bossBar.getDisplayName()), true);
            return 0;
        }
    }
 
    private static int setPlayers(CommandSourceStack source, CustomBossEvent bossBar, Collection<ServerPlayer> targets) throws CommandSyntaxException {
        boolean changed = bossBar.setPlayers(targets);
        if (!changed) {
            throw ERROR_NO_PLAYER_CHANGE.create();
        } else {
            if (bossBar.getPlayers().isEmpty()) {
                source.sendSuccess(() -> Component.translatable("commands.bossbar.set.players.success.none", bossBar.getDisplayName()), true);
            } else {
                source.sendSuccess(
                    () -> Component.translatable(
                        "commands.bossbar.set.players.success.some",
                        bossBar.getDisplayName(),
                        targets.size(),
                        ComponentUtils.formatList(targets, Player::getDisplayName)
                    ),
                    true
                );
            }
 
            return bossBar.getPlayers().size();
        }
    }
 
    private static int listBars(CommandSourceStack source) {
        Collection<CustomBossEvent> events = source.getServer().getCustomBossEvents().getEvents();
        if (events.isEmpty()) {
            source.sendSuccess(() -> Component.translatable("commands.bossbar.list.bars.none"), false);
        } else {
            source.sendSuccess(
                () -> Component.translatable(
                    "commands.bossbar.list.bars.some", events.size(), ComponentUtils.formatList(events, CustomBossEvent::getDisplayName)
                ),
                false
            );
        }
 
        return events.size();
    }
 
    private static int createBar(CommandSourceStack source, Identifier id, Component name) throws CommandSyntaxException {
        CustomBossEvents events = source.getServer().getCustomBossEvents();
        if (events.get(id) != null) {
            throw ERROR_ALREADY_EXISTS.create(id.toString());
        } else {
            CustomBossEvent event = events.create(
                source.getLevel().getRandom(),
                id,
                ComponentUtils.resolve(ResolutionContext.builder().withSource(source).withEntityOverride(null).build(), name)
            );
            source.sendSuccess(() -> Component.translatable("commands.bossbar.create.success", event.getDisplayName()), true);
            return events.getEvents().size();
        }
    }
 
    private static int removeBar(CommandSourceStack source, CustomBossEvent bossBar) {
        CustomBossEvents events = source.getServer().getCustomBossEvents();
        bossBar.removeAllPlayers();
        events.remove(bossBar);
        source.sendSuccess(() -> Component.translatable("commands.bossbar.remove.success", bossBar.getDisplayName()), true);
        return events.getEvents().size();
    }
 
    public static CustomBossEvent getBossBar(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
        Identifier id = IdentifierArgument.getId(context, "id");
        CustomBossEvent event = context.getSource().getServer().getCustomBossEvents().get(id);
        if (event == null) {
            throw ERROR_DOESNT_EXIST.create(id.toString());
        } else {
            return event;
        }
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数/字段
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: SharedSuggestionProvider.suggestResource()
  • ComponentArgument

    • 引用位置: 方法调用
    • 关联成员: ComponentArgument.getResolvedComponent(), ComponentArgument.textComponent()
  • EntityArgument

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.getOptionalPlayers(), EntityArgument.players()
  • IdentifierArgument

    • 引用位置: 方法调用
    • 关联成员: IdentifierArgument.getId(), IdentifierArgument.id()
  • Component

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

    • 引用位置: 方法调用
    • 关联成员: ComponentUtils.formatList(), ComponentUtils.resolve()
  • ResolutionContext

    • 引用位置: 方法调用
    • 关联成员: ResolutionContext.builder()
  • Identifier

    • 引用位置: 参数
  • CustomBossEvent

    • 引用位置: 参数/返回值
  • ServerPlayer

    • 引用位置: 参数
  • BossEvent

    • 引用位置: 参数