| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.BoolArgumentType;
|
| 5 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 9 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 10 | import com.mojang.brigadier.suggestion.SuggestionProvider;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.Collections;
|
| 13 | import net.minecraft.commands.CommandBuildContext;
|
| 14 | import net.minecraft.commands.CommandSourceStack;
|
| 15 | import net.minecraft.commands.Commands;
|
| 16 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 17 | import net.minecraft.commands.arguments.ComponentArgument;
|
| 18 | import net.minecraft.commands.arguments.EntityArgument;
|
| 19 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.network.chat.ComponentUtils;
|
| 22 | import net.minecraft.network.chat.ResolutionContext;
|
| 23 | import net.minecraft.resources.Identifier;
|
| 24 | import net.minecraft.server.bossevents.CustomBossEvent;
|
| 25 | import net.minecraft.server.bossevents.CustomBossEvents;
|
| 26 | import net.minecraft.server.level.ServerPlayer;
|
| 27 | import net.minecraft.world.BossEvent;
|
| 28 | import net.minecraft.world.entity.player.Player;
|
| 29 |
|
| 30 | public class BossBarCommands {
|
| 31 | private static final DynamicCommandExceptionType ERROR_ALREADY_EXISTS = new DynamicCommandExceptionType(
|
| 32 | id -> Component.translatableEscape("commands.bossbar.create.failed", id)
|
| 33 | );
|
| 34 | private static final DynamicCommandExceptionType ERROR_DOESNT_EXIST = new DynamicCommandExceptionType(
|
| 35 | id -> Component.translatableEscape("commands.bossbar.unknown", id)
|
| 36 | );
|
| 37 | private static final SimpleCommandExceptionType ERROR_NO_PLAYER_CHANGE = new SimpleCommandExceptionType(
|
| 38 | Component.translatable("commands.bossbar.set.players.unchanged")
|
| 39 | );
|
| 40 | private static final SimpleCommandExceptionType ERROR_NO_NAME_CHANGE = new SimpleCommandExceptionType(
|
| 41 | Component.translatable("commands.bossbar.set.name.unchanged")
|
| 42 | );
|
| 43 | private static final SimpleCommandExceptionType ERROR_NO_COLOR_CHANGE = new SimpleCommandExceptionType(
|
| 44 | Component.translatable("commands.bossbar.set.color.unchanged")
|
| 45 | );
|
| 46 | private static final SimpleCommandExceptionType ERROR_NO_STYLE_CHANGE = new SimpleCommandExceptionType(
|
| 47 | Component.translatable("commands.bossbar.set.style.unchanged")
|
| 48 | );
|
| 49 | private static final SimpleCommandExceptionType ERROR_NO_VALUE_CHANGE = new SimpleCommandExceptionType(
|
| 50 | Component.translatable("commands.bossbar.set.value.unchanged")
|
| 51 | );
|
| 52 | private static final SimpleCommandExceptionType ERROR_NO_MAX_CHANGE = new SimpleCommandExceptionType(
|
| 53 | Component.translatable("commands.bossbar.set.max.unchanged")
|
| 54 | );
|
| 55 | private static final SimpleCommandExceptionType ERROR_ALREADY_HIDDEN = new SimpleCommandExceptionType(
|
| 56 | Component.translatable("commands.bossbar.set.visibility.unchanged.hidden")
|
| 57 | );
|
| 58 | private static final SimpleCommandExceptionType ERROR_ALREADY_VISIBLE = new SimpleCommandExceptionType(
|
| 59 | Component.translatable("commands.bossbar.set.visibility.unchanged.visible")
|
| 60 | );
|
| 61 | public static final SuggestionProvider<CommandSourceStack> SUGGEST_BOSS_BAR = (c, b) -> SharedSuggestionProvider.suggestResource(
|
| 62 | c.getSource().getServer().getCustomBossEvents().getIds(), b
|
| 63 | );
|
| 64 |
|
| 65 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 66 | dispatcher.register(
|
| 67 | Commands.literal("bossbar")
|
| 68 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 69 | .then(
|
| 70 | Commands.literal("add")
|
| 71 | .then(
|
| 72 | Commands.argument("id", IdentifierArgument.id())
|
| 73 | .then(
|
| 74 | Commands.argument("name", ComponentArgument.textComponent(context))
|
| 75 | .executes(c -> createBar(c.getSource(), IdentifierArgument.getId(c, "id"), ComponentArgument.getResolvedComponent(c, "name")))
|
| 76 | )
|
| 77 | )
|
| 78 | )
|
| 79 | .then(
|
| 80 | Commands.literal("remove")
|
| 81 | .then(Commands.argument("id", IdentifierArgument.id()).suggests(SUGGEST_BOSS_BAR).executes(c -> removeBar(c.getSource(), getBossBar(c))))
|
| 82 | )
|
| 83 | .then(Commands.literal("list").executes(c -> listBars(c.getSource())))
|
| 84 | .then(
|
| 85 | Commands.literal("set")
|
| 86 | .then(
|
| 87 | Commands.argument("id", IdentifierArgument.id())
|
| 88 | .suggests(SUGGEST_BOSS_BAR)
|
| 89 | .then(
|
| 90 | Commands.literal("name")
|
| 91 | .then(
|
| 92 | Commands.argument("name", ComponentArgument.textComponent(context))
|
| 93 | .executes(c -> setName(c.getSource(), getBossBar(c), ComponentArgument.getResolvedComponent(c, "name")))
|
| 94 | )
|
| 95 | )
|
| 96 | .then(
|
| 97 | Commands.literal("color")
|
| 98 | .then(Commands.literal("pink").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.PINK)))
|
| 99 | .then(Commands.literal("blue").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.BLUE)))
|
| 100 | .then(Commands.literal("red").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.RED)))
|
| 101 | .then(Commands.literal("green").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.GREEN)))
|
| 102 | .then(Commands.literal("yellow").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.YELLOW)))
|
| 103 | .then(Commands.literal("purple").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.PURPLE)))
|
| 104 | .then(Commands.literal("white").executes(c -> setColor(c.getSource(), getBossBar(c), BossEvent.BossBarColor.WHITE)))
|
| 105 | )
|
| 106 | .then(
|
| 107 | Commands.literal("style")
|
| 108 | .then(Commands.literal("progress").executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.PROGRESS)))
|
| 109 | .then(Commands.literal("notched_6").executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_6)))
|
| 110 | .then(Commands.literal("notched_10").executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_10)))
|
| 111 | .then(Commands.literal("notched_12").executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_12)))
|
| 112 | .then(Commands.literal("notched_20").executes(c -> setStyle(c.getSource(), getBossBar(c), BossEvent.BossBarOverlay.NOTCHED_20)))
|
| 113 | )
|
| 114 | .then(
|
| 115 | Commands.literal("value")
|
| 116 | .then(
|
| 117 | Commands.argument("value", IntegerArgumentType.integer(0))
|
| 118 | .executes(c -> setValue(c.getSource(), getBossBar(c), IntegerArgumentType.getInteger(c, "value")))
|
| 119 | )
|
| 120 | )
|
| 121 | .then(
|
| 122 | Commands.literal("max")
|
| 123 | .then(
|
| 124 | Commands.argument("max", IntegerArgumentType.integer(1))
|
| 125 | .executes(c -> setMax(c.getSource(), getBossBar(c), IntegerArgumentType.getInteger(c, "max")))
|
| 126 | )
|
| 127 | )
|
| 128 | .then(
|
| 129 | Commands.literal("visible")
|
| 130 | .then(
|
| 131 | Commands.argument("visible", BoolArgumentType.bool())
|
| 132 | .executes(c -> setVisible(c.getSource(), getBossBar(c), BoolArgumentType.getBool(c, "visible")))
|
| 133 | )
|
| 134 | )
|
| 135 | .then(
|
| 136 | Commands.literal("players")
|
| 137 | .executes(c -> setPlayers(c.getSource(), getBossBar(c), Collections.emptyList()))
|
| 138 | .then(
|
| 139 | Commands.argument("targets", EntityArgument.players())
|
| 140 | .executes(c -> setPlayers(c.getSource(), getBossBar(c), EntityArgument.getOptionalPlayers(c, "targets")))
|
| 141 | )
|
| 142 | )
|
| 143 | )
|
| 144 | )
|
| 145 | .then(
|
| 146 | Commands.literal("get")
|
| 147 | .then(
|
| 148 | Commands.argument("id", IdentifierArgument.id())
|
| 149 | .suggests(SUGGEST_BOSS_BAR)
|
| 150 | .then(Commands.literal("value").executes(c -> getValue(c.getSource(), getBossBar(c))))
|
| 151 | .then(Commands.literal("max").executes(c -> getMax(c.getSource(), getBossBar(c))))
|
| 152 | .then(Commands.literal("visible").executes(c -> getVisible(c.getSource(), getBossBar(c))))
|
| 153 | .then(Commands.literal("players").executes(c -> getPlayers(c.getSource(), getBossBar(c))))
|
| 154 | )
|
| 155 | )
|
| 156 | );
|
| 157 | }
|
| 158 |
|
| 159 | private static int getValue(final CommandSourceStack source, final CustomBossEvent bossBar) {
|
| 160 | source.sendSuccess(() -> Component.translatable("commands.bossbar.get.value", bossBar.getDisplayName(), bossBar.value()), true);
|
| 161 | return bossBar.value();
|
| 162 | }
|
| 163 |
|
| 164 | private static int getMax(final CommandSourceStack source, final CustomBossEvent bossBar) {
|
| 165 | source.sendSuccess(() -> Component.translatable("commands.bossbar.get.max", bossBar.getDisplayName(), bossBar.max()), true);
|
| 166 | return bossBar.max();
|
| 167 | }
|
| 168 |
|
| 169 | private static int getVisible(final CommandSourceStack source, final CustomBossEvent bossBar) {
|
| 170 | if (bossBar.isVisible()) {
|
| 171 | source.sendSuccess(() -> Component.translatable("commands.bossbar.get.visible.visible", bossBar.getDisplayName()), true);
|
| 172 | return 1;
|
| 173 | } else {
|
| 174 | source.sendSuccess(() -> Component.translatable("commands.bossbar.get.visible.hidden", bossBar.getDisplayName()), true);
|
| 175 | return 0;
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | private static int getPlayers(final CommandSourceStack source, final CustomBossEvent bossBar) {
|
| 180 | if (bossBar.getPlayers().isEmpty()) {
|
| 181 | source.sendSuccess(() -> Component.translatable("commands.bossbar.get.players.none", bossBar.getDisplayName()), true);
|
| 182 | } else {
|
| 183 | source.sendSuccess(
|
| 184 | () -> Component.translatable(
|
| 185 | "commands.bossbar.get.players.some",
|
| 186 | bossBar.getDisplayName(),
|
| 187 | bossBar.getPlayers().size(),
|
| 188 | ComponentUtils.formatList(bossBar.getPlayers(), Player::getDisplayName)
|
| 189 | ),
|
| 190 | true
|
| 191 | );
|
| 192 | }
|
| 193 |
|
| 194 | return bossBar.getPlayers().size();
|
| 195 | }
|
| 196 |
|
| 197 | private static int setVisible(final CommandSourceStack source, final CustomBossEvent bossBar, final boolean visible) throws CommandSyntaxException {
|
| 198 | if (bossBar.isVisible() == visible) {
|
| 199 | if (visible) {
|
| 200 | throw ERROR_ALREADY_VISIBLE.create();
|
| 201 | } else {
|
| 202 | throw ERROR_ALREADY_HIDDEN.create();
|
| 203 | }
|
| 204 | } else {
|
| 205 | bossBar.setVisible(visible);
|
| 206 | if (visible) {
|
| 207 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.visible.success.visible", bossBar.getDisplayName()), true);
|
| 208 | } else {
|
| 209 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.visible.success.hidden", bossBar.getDisplayName()), true);
|
| 210 | }
|
| 211 |
|
| 212 | return 0;
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | private static int setValue(final CommandSourceStack source, final CustomBossEvent bossBar, final int value) throws CommandSyntaxException {
|
| 217 | if (bossBar.value() == value) {
|
| 218 | throw ERROR_NO_VALUE_CHANGE.create();
|
| 219 | }
|
| 220 |
|
| 221 | bossBar.setValue(value);
|
| 222 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.value.success", bossBar.getDisplayName(), value), true);
|
| 223 | return value;
|
| 224 | }
|
| 225 |
|
| 226 | private static int setMax(final CommandSourceStack source, final CustomBossEvent bossBar, final int value) throws CommandSyntaxException {
|
| 227 | if (bossBar.max() == value) {
|
| 228 | throw ERROR_NO_MAX_CHANGE.create();
|
| 229 | }
|
| 230 |
|
| 231 | bossBar.setMax(value);
|
| 232 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.max.success", bossBar.getDisplayName(), value), true);
|
| 233 | return value;
|
| 234 | }
|
| 235 |
|
| 236 | private static int setColor(final CommandSourceStack source, final CustomBossEvent bossBar, final BossEvent.BossBarColor color) throws CommandSyntaxException {
|
| 237 | if (bossBar.getColor().equals(color)) {
|
| 238 | throw ERROR_NO_COLOR_CHANGE.create();
|
| 239 | }
|
| 240 |
|
| 241 | bossBar.setColor(color);
|
| 242 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.color.success", bossBar.getDisplayName()), true);
|
| 243 | return 0;
|
| 244 | }
|
| 245 |
|
| 246 | private static int setStyle(final CommandSourceStack source, final CustomBossEvent bossBar, final BossEvent.BossBarOverlay style) throws CommandSyntaxException {
|
| 247 | if (bossBar.getOverlay().equals(style)) {
|
| 248 | throw ERROR_NO_STYLE_CHANGE.create();
|
| 249 | }
|
| 250 |
|
| 251 | bossBar.setOverlay(style);
|
| 252 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.style.success", bossBar.getDisplayName()), true);
|
| 253 | return 0;
|
| 254 | }
|
| 255 |
|
| 256 | private static int setName(final CommandSourceStack source, final CustomBossEvent bossBar, final Component name) throws CommandSyntaxException {
|
| 257 | Component replaced = ComponentUtils.resolve(ResolutionContext.builder().withSource(source).withEntityOverride(null).build(), name);
|
| 258 | if (bossBar.getName().equals(replaced)) {
|
| 259 | throw ERROR_NO_NAME_CHANGE.create();
|
| 260 | }
|
| 261 |
|
| 262 | bossBar.setName(replaced);
|
| 263 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.name.success", bossBar.getDisplayName()), true);
|
| 264 | return 0;
|
| 265 | }
|
| 266 |
|
| 267 | private static int setPlayers(final CommandSourceStack source, final CustomBossEvent bossBar, final Collection<ServerPlayer> targets) throws CommandSyntaxException {
|
| 268 | boolean changed = bossBar.setPlayers(targets);
|
| 269 | if (!changed) {
|
| 270 | throw ERROR_NO_PLAYER_CHANGE.create();
|
| 271 | }
|
| 272 |
|
| 273 | if (bossBar.getPlayers().isEmpty()) {
|
| 274 | source.sendSuccess(() -> Component.translatable("commands.bossbar.set.players.success.none", bossBar.getDisplayName()), true);
|
| 275 | } else {
|
| 276 | source.sendSuccess(
|
| 277 | () -> Component.translatable(
|
| 278 | "commands.bossbar.set.players.success.some", bossBar.getDisplayName(), targets.size(), ComponentUtils.formatList(targets, Player::getDisplayName)
|
| 279 | ),
|
| 280 | true
|
| 281 | );
|
| 282 | }
|
| 283 |
|
| 284 | return bossBar.getPlayers().size();
|
| 285 | }
|
| 286 |
|
| 287 | private static int listBars(final CommandSourceStack source) {
|
| 288 | Collection<CustomBossEvent> events = source.getServer().getCustomBossEvents().getEvents();
|
| 289 | if (events.isEmpty()) {
|
| 290 | source.sendSuccess(() -> Component.translatable("commands.bossbar.list.bars.none"), false);
|
| 291 | } else {
|
| 292 | source.sendSuccess(
|
| 293 | () -> Component.translatable("commands.bossbar.list.bars.some", events.size(), ComponentUtils.formatList(events, CustomBossEvent::getDisplayName)), false
|
| 294 | );
|
| 295 | }
|
| 296 |
|
| 297 | return events.size();
|
| 298 | }
|
| 299 |
|
| 300 | private static int createBar(final CommandSourceStack source, final Identifier id, final Component name) throws CommandSyntaxException {
|
| 301 | CustomBossEvents events = source.getServer().getCustomBossEvents();
|
| 302 | if (events.get(id) != null) {
|
| 303 | throw ERROR_ALREADY_EXISTS.create(id.toString());
|
| 304 | }
|
| 305 |
|
| 306 | CustomBossEvent event = events.create(
|
| 307 | source.getLevel().getRandom(), id, ComponentUtils.resolve(ResolutionContext.builder().withSource(source).withEntityOverride(null).build(), name)
|
| 308 | );
|
| 309 | source.sendSuccess(() -> Component.translatable("commands.bossbar.create.success", event.getDisplayName()), true);
|
| 310 | return events.getEvents().size();
|
| 311 | }
|
| 312 |
|
| 313 | private static int removeBar(final CommandSourceStack source, final CustomBossEvent bossBar) {
|
| 314 | CustomBossEvents events = source.getServer().getCustomBossEvents();
|
| 315 | bossBar.removeAllPlayers();
|
| 316 | events.remove(bossBar);
|
| 317 | source.sendSuccess(() -> Component.translatable("commands.bossbar.remove.success", bossBar.getDisplayName()), true);
|
| 318 | return events.getEvents().size();
|
| 319 | }
|
| 320 |
|
| 321 | public static CustomBossEvent getBossBar(final CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
| 322 | Identifier id = IdentifierArgument.getId(context, "id");
|
| 323 | CustomBossEvent event = context.getSource().getServer().getCustomBossEvents().get(id);
|
| 324 | if (event == null) {
|
| 325 | throw ERROR_DOESNT_EXIST.create(id.toString());
|
| 326 | } else {
|
| 327 | return event;
|
| 328 | }
|
| 329 | }
|
| 330 | }
|
| 331 |
|