| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.authlib.GameProfile;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import java.util.HashSet;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.UUID;
|
| 8 | import net.minecraft.commands.CommandBuildContext;
|
| 9 | import net.minecraft.commands.CommandSourceStack;
|
| 10 | import net.minecraft.commands.Commands;
|
| 11 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 12 | import net.minecraft.commands.arguments.EntityArgument;
|
| 13 | import net.minecraft.commands.arguments.ResourceOrIdArgument;
|
| 14 | import net.minecraft.commands.arguments.UuidArgument;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.network.Connection;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.network.protocol.common.ClientboundShowDialogPacket;
|
| 19 | import net.minecraft.server.MinecraftServer;
|
| 20 | import net.minecraft.server.dialog.Dialog;
|
| 21 | import net.minecraft.server.level.ServerPlayer;
|
| 22 | import net.minecraft.server.network.ServerConfigurationPacketListenerImpl;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public class DebugConfigCommand {
|
| 26 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 27 | dispatcher.register(
|
| 28 | Commands.literal("debugconfig")
|
| 29 | .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
|
| 30 | .then(
|
| 31 | Commands.literal("config")
|
| 32 | .then(Commands.argument("target", EntityArgument.player()).executes(c -> config(c.getSource(), EntityArgument.getPlayer(c, "target"))))
|
| 33 | )
|
| 34 | .then(
|
| 35 | Commands.literal("unconfig")
|
| 36 | .then(
|
| 37 | Commands.argument("target", UuidArgument.uuid())
|
| 38 | .suggests((c, p) -> SharedSuggestionProvider.suggest(getUuidsInConfig(c.getSource().getServer()), p))
|
| 39 | .executes(c -> unconfig(c.getSource(), UuidArgument.getUuid(c, "target")))
|
| 40 | )
|
| 41 | )
|
| 42 | .then(
|
| 43 | Commands.literal("dialog")
|
| 44 | .then(
|
| 45 | Commands.argument("target", UuidArgument.uuid())
|
| 46 | .suggests((c, p) -> SharedSuggestionProvider.suggest(getUuidsInConfig(c.getSource().getServer()), p))
|
| 47 | .then(
|
| 48 | Commands.argument("dialog", ResourceOrIdArgument.dialog(context))
|
| 49 | .executes(c -> showDialog((CommandSourceStack)c.getSource(), UuidArgument.getUuid(c, "target"), ResourceOrIdArgument.getDialog(c, "dialog")))
|
| 50 | )
|
| 51 | )
|
| 52 | )
|
| 53 | );
|
| 54 | }
|
| 55 |
|
| 56 | private static Iterable<String> getUuidsInConfig(final MinecraftServer server) {
|
| 57 | Set<String> result = new HashSet<>();
|
| 58 |
|
| 59 | for (Connection connection : server.getConnection().getConnections()) {
|
| 60 | if (connection.getPacketListener() instanceof ServerConfigurationPacketListenerImpl configListener) {
|
| 61 | result.add(configListener.getOwner().id().toString());
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return result;
|
| 66 | }
|
| 67 |
|
| 68 | private static int config(final CommandSourceStack source, final ServerPlayer target) {
|
| 69 | GameProfile gameProfile = target.getGameProfile();
|
| 70 | target.connection.switchToConfig();
|
| 71 | source.sendSuccess(() -> Component.literal("Switched player " + gameProfile.name() + "(" + gameProfile.id() + ") to config mode"), false);
|
| 72 | return 1;
|
| 73 | }
|
| 74 |
|
| 75 | private static @Nullable ServerConfigurationPacketListenerImpl findConfigPlayer(final MinecraftServer server, final UUID target) {
|
| 76 | for (Connection connection : server.getConnection().getConnections()) {
|
| 77 | if (connection.getPacketListener() instanceof ServerConfigurationPacketListenerImpl configListener && configListener.getOwner().id().equals(target)) {
|
| 78 | return configListener;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | return null;
|
| 83 | }
|
| 84 |
|
| 85 | private static int unconfig(final CommandSourceStack source, final UUID target) {
|
| 86 | ServerConfigurationPacketListenerImpl listener = findConfigPlayer(source.getServer(), target);
|
| 87 | if (listener != null) {
|
| 88 | listener.returnToWorld();
|
| 89 | return 1;
|
| 90 | } else {
|
| 91 | source.sendFailure(Component.literal("Can't find player to unconfig"));
|
| 92 | return 0;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | private static int showDialog(final CommandSourceStack source, final UUID target, final Holder<Dialog> dialog) {
|
| 97 | ServerConfigurationPacketListenerImpl listener = findConfigPlayer(source.getServer(), target);
|
| 98 | if (listener != null) {
|
| 99 | listener.send(new ClientboundShowDialogPacket(dialog));
|
| 100 | return 1;
|
| 101 | } else {
|
| 102 | source.sendFailure(Component.literal("Can't find player to talk to"));
|
| 103 | return 0;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|