File size: 23,305 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package net.minecraft.server.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.util.Collection;
import java.util.Collections;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.ComponentArgument;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.ResourceLocationArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.bossevents.CustomBossEvent;
import net.minecraft.server.bossevents.CustomBossEvents;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.BossEvent;
import net.minecraft.world.entity.player.Player;

public class BossBarCommands {
    private static final DynamicCommandExceptionType ERROR_ALREADY_EXISTS = new DynamicCommandExceptionType(
        p_308633_ -> Component.translatableEscape("commands.bossbar.create.failed", p_308633_)
    );
    private static final DynamicCommandExceptionType ERROR_DOESNT_EXIST = new DynamicCommandExceptionType(
        p_308632_ -> Component.translatableEscape("commands.bossbar.unknown", p_308632_)
    );
    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 = (p_136587_, p_136588_) -> SharedSuggestionProvider.suggestResource(
            p_136587_.getSource().getServer().getCustomBossEvents().getIds(), p_136588_
        );

    public static void register(CommandDispatcher<CommandSourceStack> p_136583_, CommandBuildContext p_332961_) {
        p_136583_.register(
            Commands.literal("bossbar")
                .requires(p_136627_ -> p_136627_.hasPermission(2))
                .then(
                    Commands.literal("add")
                        .then(
                            Commands.argument("id", ResourceLocationArgument.id())
                                .then(
                                    Commands.argument("name", ComponentArgument.textComponent(p_332961_))
                                        .executes(
                                            p_136693_ -> createBar(
                                                    p_136693_.getSource(),
                                                    ResourceLocationArgument.getId(p_136693_, "id"),
                                                    ComponentArgument.getComponent(p_136693_, "name")
                                                )
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("remove")
                        .then(
                            Commands.argument("id", ResourceLocationArgument.id())
                                .suggests(SUGGEST_BOSS_BAR)
                                .executes(p_136691_ -> removeBar(p_136691_.getSource(), getBossBar(p_136691_)))
                        )
                )
                .then(Commands.literal("list").executes(p_136689_ -> listBars(p_136689_.getSource())))
                .then(
                    Commands.literal("set")
                        .then(
                            Commands.argument("id", ResourceLocationArgument.id())
                                .suggests(SUGGEST_BOSS_BAR)
                                .then(
                                    Commands.literal("name")
                                        .then(
                                            Commands.argument("name", ComponentArgument.textComponent(p_332961_))
                                                .executes(
                                                    p_136687_ -> setName(
                                                            p_136687_.getSource(), getBossBar(p_136687_), ComponentArgument.getComponent(p_136687_, "name")
                                                        )
                                                )
                                        )
                                )
                                .then(
                                    Commands.literal("color")
                                        .then(
                                            Commands.literal("pink")
                                                .executes(p_136685_ -> setColor(p_136685_.getSource(), getBossBar(p_136685_), BossEvent.BossBarColor.PINK))
                                        )
                                        .then(
                                            Commands.literal("blue")
                                                .executes(p_136683_ -> setColor(p_136683_.getSource(), getBossBar(p_136683_), BossEvent.BossBarColor.BLUE))
                                        )
                                        .then(
                                            Commands.literal("red")
                                                .executes(p_136681_ -> setColor(p_136681_.getSource(), getBossBar(p_136681_), BossEvent.BossBarColor.RED))
                                        )
                                        .then(
                                            Commands.literal("green")
                                                .executes(p_136679_ -> setColor(p_136679_.getSource(), getBossBar(p_136679_), BossEvent.BossBarColor.GREEN))
                                        )
                                        .then(
                                            Commands.literal("yellow")
                                                .executes(p_136677_ -> setColor(p_136677_.getSource(), getBossBar(p_136677_), BossEvent.BossBarColor.YELLOW))
                                        )
                                        .then(
                                            Commands.literal("purple")
                                                .executes(p_136675_ -> setColor(p_136675_.getSource(), getBossBar(p_136675_), BossEvent.BossBarColor.PURPLE))
                                        )
                                        .then(
                                            Commands.literal("white")
                                                .executes(p_136673_ -> setColor(p_136673_.getSource(), getBossBar(p_136673_), BossEvent.BossBarColor.WHITE))
                                        )
                                )
                                .then(
                                    Commands.literal("style")
                                        .then(
                                            Commands.literal("progress")
                                                .executes(
                                                    p_136671_ -> setStyle(p_136671_.getSource(), getBossBar(p_136671_), BossEvent.BossBarOverlay.PROGRESS)
                                                )
                                        )
                                        .then(
                                            Commands.literal("notched_6")
                                                .executes(
                                                    p_136669_ -> setStyle(p_136669_.getSource(), getBossBar(p_136669_), BossEvent.BossBarOverlay.NOTCHED_6)
                                                )
                                        )
                                        .then(
                                            Commands.literal("notched_10")
                                                .executes(
                                                    p_136667_ -> setStyle(p_136667_.getSource(), getBossBar(p_136667_), BossEvent.BossBarOverlay.NOTCHED_10)
                                                )
                                        )
                                        .then(
                                            Commands.literal("notched_12")
                                                .executes(
                                                    p_136665_ -> setStyle(p_136665_.getSource(), getBossBar(p_136665_), BossEvent.BossBarOverlay.NOTCHED_12)
                                                )
                                        )
                                        .then(
                                            Commands.literal("notched_20")
                                                .executes(
                                                    p_136663_ -> setStyle(p_136663_.getSource(), getBossBar(p_136663_), BossEvent.BossBarOverlay.NOTCHED_20)
                                                )
                                        )
                                )
                                .then(
                                    Commands.literal("value")
                                        .then(
                                            Commands.argument("value", IntegerArgumentType.integer(0))
                                                .executes(
                                                    p_136661_ -> setValue(
                                                            p_136661_.getSource(), getBossBar(p_136661_), IntegerArgumentType.getInteger(p_136661_, "value")
                                                        )
                                                )
                                        )
                                )
                                .then(
                                    Commands.literal("max")
                                        .then(
                                            Commands.argument("max", IntegerArgumentType.integer(1))
                                                .executes(
                                                    p_136659_ -> setMax(
                                                            p_136659_.getSource(), getBossBar(p_136659_), IntegerArgumentType.getInteger(p_136659_, "max")
                                                        )
                                                )
                                        )
                                )
                                .then(
                                    Commands.literal("visible")
                                        .then(
                                            Commands.argument("visible", BoolArgumentType.bool())
                                                .executes(
                                                    p_136657_ -> setVisible(
                                                            p_136657_.getSource(), getBossBar(p_136657_), BoolArgumentType.getBool(p_136657_, "visible")
                                                        )
                                                )
                                        )
                                )
                                .then(
                                    Commands.literal("players")
                                        .executes(p_136655_ -> setPlayers(p_136655_.getSource(), getBossBar(p_136655_), Collections.emptyList()))
                                        .then(
                                            Commands.argument("targets", EntityArgument.players())
                                                .executes(
                                                    p_136653_ -> setPlayers(
                                                            p_136653_.getSource(), getBossBar(p_136653_), EntityArgument.getOptionalPlayers(p_136653_, "targets")
                                                        )
                                                )
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("get")
                        .then(
                            Commands.argument("id", ResourceLocationArgument.id())
                                .suggests(SUGGEST_BOSS_BAR)
                                .then(Commands.literal("value").executes(p_136648_ -> getValue(p_136648_.getSource(), getBossBar(p_136648_))))
                                .then(Commands.literal("max").executes(p_136643_ -> getMax(p_136643_.getSource(), getBossBar(p_136643_))))
                                .then(Commands.literal("visible").executes(p_136638_ -> getVisible(p_136638_.getSource(), getBossBar(p_136638_))))
                                .then(Commands.literal("players").executes(p_136625_ -> getPlayers(p_136625_.getSource(), getBossBar(p_136625_))))
                        )
                )
        );
    }

    private static int getValue(CommandSourceStack p_136596_, CustomBossEvent p_136597_) {
        p_136596_.sendSuccess(() -> Component.translatable("commands.bossbar.get.value", p_136597_.getDisplayName(), p_136597_.getValue()), true);
        return p_136597_.getValue();
    }

    private static int getMax(CommandSourceStack p_136629_, CustomBossEvent p_136630_) {
        p_136629_.sendSuccess(() -> Component.translatable("commands.bossbar.get.max", p_136630_.getDisplayName(), p_136630_.getMax()), true);
        return p_136630_.getMax();
    }

    private static int getVisible(CommandSourceStack p_136640_, CustomBossEvent p_136641_) {
        if (p_136641_.isVisible()) {
            p_136640_.sendSuccess(() -> Component.translatable("commands.bossbar.get.visible.visible", p_136641_.getDisplayName()), true);
            return 1;
        } else {
            p_136640_.sendSuccess(() -> Component.translatable("commands.bossbar.get.visible.hidden", p_136641_.getDisplayName()), true);
            return 0;
        }
    }

    private static int getPlayers(CommandSourceStack p_136645_, CustomBossEvent p_136646_) {
        if (p_136646_.getPlayers().isEmpty()) {
            p_136645_.sendSuccess(() -> Component.translatable("commands.bossbar.get.players.none", p_136646_.getDisplayName()), true);
        } else {
            p_136645_.sendSuccess(
                () -> Component.translatable(
                        "commands.bossbar.get.players.some",
                        p_136646_.getDisplayName(),
                        p_136646_.getPlayers().size(),
                        ComponentUtils.formatList(p_136646_.getPlayers(), Player::getDisplayName)
                    ),
                true
            );
        }

        return p_136646_.getPlayers().size();
    }

    private static int setVisible(CommandSourceStack p_136619_, CustomBossEvent p_136620_, boolean p_136621_) throws CommandSyntaxException {
        if (p_136620_.isVisible() == p_136621_) {
            if (p_136621_) {
                throw ERROR_ALREADY_VISIBLE.create();
            } else {
                throw ERROR_ALREADY_HIDDEN.create();
            }
        } else {
            p_136620_.setVisible(p_136621_);
            if (p_136621_) {
                p_136619_.sendSuccess(() -> Component.translatable("commands.bossbar.set.visible.success.visible", p_136620_.getDisplayName()), true);
            } else {
                p_136619_.sendSuccess(() -> Component.translatable("commands.bossbar.set.visible.success.hidden", p_136620_.getDisplayName()), true);
            }

            return 0;
        }
    }

    private static int setValue(CommandSourceStack p_136599_, CustomBossEvent p_136600_, int p_136601_) throws CommandSyntaxException {
        if (p_136600_.getValue() == p_136601_) {
            throw ERROR_NO_VALUE_CHANGE.create();
        } else {
            p_136600_.setValue(p_136601_);
            p_136599_.sendSuccess(() -> Component.translatable("commands.bossbar.set.value.success", p_136600_.getDisplayName(), p_136601_), true);
            return p_136601_;
        }
    }

    private static int setMax(CommandSourceStack p_136632_, CustomBossEvent p_136633_, int p_136634_) throws CommandSyntaxException {
        if (p_136633_.getMax() == p_136634_) {
            throw ERROR_NO_MAX_CHANGE.create();
        } else {
            p_136633_.setMax(p_136634_);
            p_136632_.sendSuccess(() -> Component.translatable("commands.bossbar.set.max.success", p_136633_.getDisplayName(), p_136634_), true);
            return p_136634_;
        }
    }

    private static int setColor(CommandSourceStack p_136603_, CustomBossEvent p_136604_, BossEvent.BossBarColor p_136605_) throws CommandSyntaxException {
        if (p_136604_.getColor().equals(p_136605_)) {
            throw ERROR_NO_COLOR_CHANGE.create();
        } else {
            p_136604_.setColor(p_136605_);
            p_136603_.sendSuccess(() -> Component.translatable("commands.bossbar.set.color.success", p_136604_.getDisplayName()), true);
            return 0;
        }
    }

    private static int setStyle(CommandSourceStack p_136607_, CustomBossEvent p_136608_, BossEvent.BossBarOverlay p_136609_) throws CommandSyntaxException {
        if (p_136608_.getOverlay().equals(p_136609_)) {
            throw ERROR_NO_STYLE_CHANGE.create();
        } else {
            p_136608_.setOverlay(p_136609_);
            p_136607_.sendSuccess(() -> Component.translatable("commands.bossbar.set.style.success", p_136608_.getDisplayName()), true);
            return 0;
        }
    }

    private static int setName(CommandSourceStack p_136615_, CustomBossEvent p_136616_, Component p_136617_) throws CommandSyntaxException {
        Component component = ComponentUtils.updateForEntity(p_136615_, p_136617_, null, 0);
        if (p_136616_.getName().equals(component)) {
            throw ERROR_NO_NAME_CHANGE.create();
        } else {
            p_136616_.setName(component);
            p_136615_.sendSuccess(() -> Component.translatable("commands.bossbar.set.name.success", p_136616_.getDisplayName()), true);
            return 0;
        }
    }

    private static int setPlayers(CommandSourceStack p_136611_, CustomBossEvent p_136612_, Collection<ServerPlayer> p_136613_) throws CommandSyntaxException {
        boolean flag = p_136612_.setPlayers(p_136613_);
        if (!flag) {
            throw ERROR_NO_PLAYER_CHANGE.create();
        } else {
            if (p_136612_.getPlayers().isEmpty()) {
                p_136611_.sendSuccess(() -> Component.translatable("commands.bossbar.set.players.success.none", p_136612_.getDisplayName()), true);
            } else {
                p_136611_.sendSuccess(
                    () -> Component.translatable(
                            "commands.bossbar.set.players.success.some",
                            p_136612_.getDisplayName(),
                            p_136613_.size(),
                            ComponentUtils.formatList(p_136613_, Player::getDisplayName)
                        ),
                    true
                );
            }

            return p_136612_.getPlayers().size();
        }
    }

    private static int listBars(CommandSourceStack p_136590_) {
        Collection<CustomBossEvent> collection = p_136590_.getServer().getCustomBossEvents().getEvents();
        if (collection.isEmpty()) {
            p_136590_.sendSuccess(() -> Component.translatable("commands.bossbar.list.bars.none"), false);
        } else {
            p_136590_.sendSuccess(
                () -> Component.translatable(
                        "commands.bossbar.list.bars.some", collection.size(), ComponentUtils.formatList(collection, CustomBossEvent::getDisplayName)
                    ),
                false
            );
        }

        return collection.size();
    }

    private static int createBar(CommandSourceStack p_136592_, ResourceLocation p_136593_, Component p_136594_) throws CommandSyntaxException {
        CustomBossEvents custombossevents = p_136592_.getServer().getCustomBossEvents();
        if (custombossevents.get(p_136593_) != null) {
            throw ERROR_ALREADY_EXISTS.create(p_136593_.toString());
        } else {
            CustomBossEvent custombossevent = custombossevents.create(p_136593_, ComponentUtils.updateForEntity(p_136592_, p_136594_, null, 0));
            p_136592_.sendSuccess(() -> Component.translatable("commands.bossbar.create.success", custombossevent.getDisplayName()), true);
            return custombossevents.getEvents().size();
        }
    }

    private static int removeBar(CommandSourceStack p_136650_, CustomBossEvent p_136651_) {
        CustomBossEvents custombossevents = p_136650_.getServer().getCustomBossEvents();
        p_136651_.removeAllPlayers();
        custombossevents.remove(p_136651_);
        p_136650_.sendSuccess(() -> Component.translatable("commands.bossbar.remove.success", p_136651_.getDisplayName()), true);
        return custombossevents.getEvents().size();
    }

    public static CustomBossEvent getBossBar(CommandContext<CommandSourceStack> p_136585_) throws CommandSyntaxException {
        ResourceLocation resourcelocation = ResourceLocationArgument.getId(p_136585_, "id");
        CustomBossEvent custombossevent = p_136585_.getSource().getServer().getCustomBossEvents().get(resourcelocation);
        if (custombossevent == null) {
            throw ERROR_DOESNT_EXIST.create(resourcelocation.toString());
        } else {
            return custombossevent;
        }
    }
}