| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.ParseResults;
|
| 5 | import com.mojang.brigadier.StringReader;
|
| 6 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import java.util.concurrent.CompletableFuture;
|
| 9 | import java.util.concurrent.atomic.AtomicReference;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.server.MinecraftServer;
|
| 12 | import net.minecraft.server.level.ServerPlayer;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public class ServerCommandSuggestionsProvider {
|
| 17 | private static final int MAX_COMMAND_SUGGESTIONS = 1000;
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private static final int MIN_INTERVAL_TICKS = 1;
|
| 20 | private final ServerPlayer player;
|
| 21 | private final AtomicReference<ServerCommandSuggestionsProvider.@Nullable Request> lastRequest = new AtomicReference<>();
|
| 22 | private volatile boolean hasBudget;
|
| 23 |
|
| 24 | public ServerCommandSuggestionsProvider(final ServerPlayer player) {
|
| 25 | this.player = player;
|
| 26 | }
|
| 27 |
|
| 28 | public void tick() {
|
| 29 | if (this.player.tickCount % 1 == 0) {
|
| 30 | this.hasBudget = true;
|
| 31 | this.tryProcessRequest();
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | private void tryProcessRequest() {
|
| 36 | if (this.hasBudget) {
|
| 37 | ServerCommandSuggestionsProvider.Request request = this.lastRequest.getAndSet(null);
|
| 38 | if (request != null) {
|
| 39 | this.hasBudget = false;
|
| 40 |
|
| 41 | try {
|
| 42 | StringReader reader = new StringReader(request.command);
|
| 43 | if (reader.canRead() && reader.peek() == '/') {
|
| 44 | reader.skip();
|
| 45 | }
|
| 46 |
|
| 47 | MinecraftServer server = this.player.level().getServer();
|
| 48 | CommandDispatcher<CommandSourceStack> dispatcher = server.getCommands().getDispatcher();
|
| 49 | ParseResults<CommandSourceStack> parse = dispatcher.parse(reader, this.player.createCommandSourceStack());
|
| 50 | if (this.lastRequest.get() != null) {
|
| 51 | return;
|
| 52 | }
|
| 53 |
|
| 54 | dispatcher.getCompletionSuggestions(parse).thenAccept(suggestions -> request.future.complete(limitSuggestionCount(suggestions)));
|
| 55 | } catch (Exception e) {
|
| 56 | LOGGER.error("Failed to resolve command suggestions for {}", this.player.getGameProfile().name(), e);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private static Suggestions limitSuggestionCount(final Suggestions suggestions) {
|
| 63 | return suggestions.getList().size() <= 1000 ? suggestions : new Suggestions(suggestions.getRange(), suggestions.getList().subList(0, 1000));
|
| 64 | }
|
| 65 |
|
| 66 | private void trySchedule() {
|
| 67 | if (this.hasBudget) {
|
| 68 | this.player.level().getServer().execute(this::tryProcessRequest);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | public CompletableFuture<Suggestions> request(final String command) {
|
| 73 | ServerCommandSuggestionsProvider.Request request = new ServerCommandSuggestionsProvider.Request(command);
|
| 74 | ServerCommandSuggestionsProvider.Request oldRequest = this.lastRequest.getAndSet(request);
|
| 75 | if (oldRequest == null) {
|
| 76 | this.trySchedule();
|
| 77 | }
|
| 78 |
|
| 79 | return request.future;
|
| 80 | }
|
| 81 |
|
| 82 | private static class Request {
|
| 83 | private final String command;
|
| 84 | private final CompletableFuture<Suggestions> future = new CompletableFuture<>();
|
| 85 |
|
| 86 | private Request(final String command) {
|
| 87 | this.command = command;
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|