| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import java.util.Collection;
|
| 6 | import net.minecraft.commands.CommandBuildContext;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.EntityArgument;
|
| 10 | import net.minecraft.commands.arguments.ResourceOrIdArgument;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.protocol.common.ClientboundClearDialogPacket;
|
| 14 | import net.minecraft.server.dialog.Dialog;
|
| 15 | import net.minecraft.server.level.ServerPlayer;
|
| 16 |
|
| 17 | public class DialogCommand {
|
| 18 | private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_SHOW = CommandResponseTracker.messages(
|
| 19 | (player, var1) -> Component.translatable("commands.dialog.show.single", player.getDisplayName()),
|
| 20 | (playerCount, var1) -> Component.translatable("commands.dialog.show.multiple", playerCount)
|
| 21 | );
|
| 22 | private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_CLEAR = CommandResponseTracker.messages(
|
| 23 | (player, var1) -> Component.translatable("commands.dialog.clear.single", player.getDisplayName()),
|
| 24 | (playerCount, var1) -> Component.translatable("commands.dialog.clear.multiple", playerCount)
|
| 25 | );
|
| 26 |
|
| 27 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 28 | dispatcher.register(
|
| 29 | Commands.literal("dialog")
|
| 30 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 31 | .then(
|
| 32 | Commands.literal("show")
|
| 33 | .then(
|
| 34 | Commands.argument("targets", EntityArgument.players())
|
| 35 | .then(
|
| 36 | Commands.argument("dialog", ResourceOrIdArgument.dialog(context))
|
| 37 | .executes(c -> showDialog((CommandSourceStack)c.getSource(), EntityArgument.getPlayers(c, "targets"), ResourceOrIdArgument.getDialog(c, "dialog")))
|
| 38 | )
|
| 39 | )
|
| 40 | )
|
| 41 | .then(
|
| 42 | Commands.literal("clear")
|
| 43 | .then(Commands.argument("targets", EntityArgument.players()).executes(c -> clearDialog(c.getSource(), EntityArgument.getPlayers(c, "targets"))))
|
| 44 | )
|
| 45 | );
|
| 46 | }
|
| 47 |
|
| 48 | private static int showDialog(final CommandSourceStack sender, final Collection<ServerPlayer> targets, final Holder<Dialog> dialog) throws CommandSyntaxException {
|
| 49 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 50 |
|
| 51 | for (ServerPlayer target : targets) {
|
| 52 | target.openDialog(dialog);
|
| 53 | tracker.track(target);
|
| 54 | }
|
| 55 |
|
| 56 | return tracker.sendFeedback(sender, true, RESPONSE_SHOW);
|
| 57 | }
|
| 58 |
|
| 59 | private static int clearDialog(final CommandSourceStack sender, final Collection<ServerPlayer> targets) throws CommandSyntaxException {
|
| 60 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 61 |
|
| 62 | for (ServerPlayer target : targets) {
|
| 63 | target.connection.send(ClientboundClearDialogPacket.INSTANCE);
|
| 64 | tracker.track(target);
|
| 65 | }
|
| 66 |
|
| 67 | return tracker.sendFeedback(sender, true, RESPONSE_CLEAR);
|
| 68 | }
|
| 69 | }
|
| 70 |
|