| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.commands.CommandSourceStack;
|
| 7 | import net.minecraft.commands.Commands;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.ComponentUtils;
|
| 10 | import net.minecraft.server.level.ServerPlayer;
|
| 11 | import net.minecraft.server.players.PlayerList;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 |
|
| 14 | public class ListPlayersCommand {
|
| 15 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 16 | dispatcher.register(
|
| 17 | Commands.literal("list").executes(c -> listPlayers(c.getSource())).then(Commands.literal("uuids").executes(c -> listPlayersWithUuids(c.getSource())))
|
| 18 | );
|
| 19 | }
|
| 20 |
|
| 21 | private static int listPlayers(final CommandSourceStack source) {
|
| 22 | return format(source, Player::getDisplayName);
|
| 23 | }
|
| 24 |
|
| 25 | private static int listPlayersWithUuids(final CommandSourceStack source) {
|
| 26 | return format(source, player -> Component.translatable("commands.list.nameAndId", player.getName(), Component.translationArg(player.getGameProfile().id())));
|
| 27 | }
|
| 28 |
|
| 29 | private static int format(final CommandSourceStack source, final Function<ServerPlayer, Component> formatter) {
|
| 30 | PlayerList playerList = source.getServer().getPlayerList();
|
| 31 | List<ServerPlayer> players = playerList.getPlayers();
|
| 32 | Component listComponent = ComponentUtils.formatList(players, formatter);
|
| 33 | source.sendSuccess(() -> Component.translatable("commands.list.players", players.size(), playerList.getMaxPlayers(), listComponent), false);
|
| 34 | return players.size();
|
| 35 | }
|
| 36 | }
|
| 37 |
|