ClientSuggestionProvider .java

net.minecraft.client.multiplayer.ClientSuggestionProvider (class, 196 lines): members: customCompletionSuggestions, completeCustomSuggestions, getAllTeams.

net.minecraft.client.multiplayerclass196 linesclient6.4 KB

Implements

API Surface (16)

Collection<String> getOnlinePlayerNames()
Collection<String> getCustomTabSuggestions()
Collection<String> getSelectedEntities()
Collection<String> getAllTeams()
Stream<Identifier> getAvailableSounds()
Stream<Identifier> getAvailablePostEffects()
PermissionSet permissions()
<E> CompletableFuture<Suggestions> suggestRegistryElements(final ResourceKey<? extends Registry<E>> key, final ElementSuggestionType elements, final SuggestionsBuilder builder, final CommandContext<?> context, final Predicate<E> filter)
CompletableFuture<Suggestions> customSuggestion(final CommandContext<?> context)
Collection<TextCoordinates> getRelevantCoordinates()
Collection<TextCoordinates> getAbsoluteCoordinates()
Set<ResourceKey<Level>> levels()
RegistryAccess registryAccess()
FeatureFlagSet enabledFeatures()
void completeCustomSuggestions(final int id, final Suggestions result)
void modifyCustomCompletions(final Action action, final List<String> entries)

Fields & Constants (10)

connection
minecraft
pendingSuggestionsId
pendingSuggestionsFuture
customCompletionSuggestions
permissions
key
elements
builder
context

Full Source

ClientSuggestionProvider.java196 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.multiplayer;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.brigadier.context.CommandContext;
5
import com.mojang.brigadier.suggestion.Suggestions;
6
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
7
import java.util.Collection;
8
import java.util.Collections;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Locale;
12
import java.util.Set;
13
import java.util.concurrent.CompletableFuture;
14
import java.util.function.Predicate;
15
import java.util.stream.Stream;
16
import net.fabricmc.api.EnvType;
17
import net.fabricmc.api.Environment;
18
import net.minecraft.client.Minecraft;
19
import net.minecraft.commands.SharedSuggestionProvider;
20
import net.minecraft.commands.SharedSuggestionProvider.ElementSuggestionType;
21
import net.minecraft.commands.SharedSuggestionProvider.TextCoordinates;
22
import net.minecraft.core.BlockPos;
23
import net.minecraft.core.Registry;
24
import net.minecraft.core.RegistryAccess;
25
import net.minecraft.network.protocol.game.ServerboundCommandSuggestionPacket;
26
import net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket.Action;
27
import net.minecraft.resources.Identifier;
28
import net.minecraft.resources.ResourceKey;
29
import net.minecraft.server.permissions.PermissionSet;
30
import net.minecraft.world.flag.FeatureFlagSet;
31
import net.minecraft.world.level.Level;
32
import net.minecraft.world.phys.BlockHitResult;
33
import net.minecraft.world.phys.EntityHitResult;
34
import net.minecraft.world.phys.HitResult;
35
import net.minecraft.world.phys.Vec3;
36
import net.minecraft.world.phys.HitResult.Type;
37
import org.jspecify.annotations.Nullable;
38
39
@Environment(EnvType.CLIENT)
40
public class ClientSuggestionProvider implements SharedSuggestionProvider {
41
	private final ClientPacketListener connection;
42
	private final Minecraft minecraft;
43
	private int pendingSuggestionsId = -1;
44
	private @Nullable CompletableFuture<Suggestions> pendingSuggestionsFuture;
45
	private final Set<String> customCompletionSuggestions = new HashSet<>();
46
	private final PermissionSet permissions;
47
48
	public ClientSuggestionProvider(final ClientPacketListener connection, final Minecraft minecraft, final PermissionSet permissions) {
49
		this.connection = connection;
50
		this.minecraft = minecraft;
51
		this.permissions = permissions;
52
	}
53
54
	@Override
55
	public Collection<String> getOnlinePlayerNames() {
56
		List<String> result = Lists.newArrayList();
57
58
		for (PlayerInfo info : this.connection.getOnlinePlayers()) {
59
			result.add(info.getProfile().name());
60
		}
61
62
		return result;
63
	}
64
65
	@Override
66
	public Collection<String> getCustomTabSuggestions() {
67
		if (this.customCompletionSuggestions.isEmpty()) {
68
			return this.getOnlinePlayerNames();
69
		}
70
71
		Set<String> result = new HashSet<>(this.getOnlinePlayerNames());
72
		result.addAll(this.customCompletionSuggestions);
73
		return result;
74
	}
75
76
	@Override
77
	public Collection<String> getSelectedEntities() {
78
		return this.minecraft.hitResult != null && this.minecraft.hitResult.getType() == Type.ENTITY
79
			? Collections.singleton(((EntityHitResult)this.minecraft.hitResult).getEntity().getStringUUID())
80
			: Collections.emptyList();
81
	}
82
83
	@Override
84
	public Collection<String> getAllTeams() {
85
		return this.connection.scoreboard().getTeamNames();
86
	}
87
88
	@Override
89
	public Stream<Identifier> getAvailableSounds() {
90
		return this.minecraft.getSoundManager().getAvailableSounds().stream();
91
	}
92
93
	@Override
94
	public Stream<Identifier> getAvailablePostEffects() {
95
		return this.minecraft.getShaderManager().getAvailablePostEffects();
96
	}
97
98
	@Override
99
	public PermissionSet permissions() {
100
		return this.permissions;
101
	}
102
103
	@Override
104
	public <E> CompletableFuture<Suggestions> suggestRegistryElements(
105
		final ResourceKey<? extends Registry<E>> key,
106
		final ElementSuggestionType elements,
107
		final SuggestionsBuilder builder,
108
		final CommandContext<?> context,
109
		final Predicate<E> filter
110
	) {
111
		return this.registryAccess().lookup(key).map(registry -> {
112
			this.suggestRegistryElements(registry, elements, builder, filter);
113
			return builder.buildFuture();
114
		}).orElseGet(() -> this.customSuggestion(context));
115
	}
116
117
	@Override
118
	public CompletableFuture<Suggestions> customSuggestion(final CommandContext<?> context) {
119
		if (this.pendingSuggestionsFuture != null) {
120
			this.pendingSuggestionsFuture.cancel(false);
121
		}
122
123
		this.pendingSuggestionsFuture = new CompletableFuture<>();
124
		int id = ++this.pendingSuggestionsId;
125
		this.connection.send(new ServerboundCommandSuggestionPacket(id, context.getInput()));
126
		return this.pendingSuggestionsFuture;
127
	}
128
129
	private static String prettyPrint(final double value) {
130
		return String.format(Locale.ROOT, "%.2f", value);
131
	}
132
133
	private static String prettyPrint(final int value) {
134
		return Integer.toString(value);
135
	}
136
137
	@Override
138
	public Collection<TextCoordinates> getRelevantCoordinates() {
139
		HitResult hitResult = this.minecraft.hitResult;
140
		if (hitResult != null && hitResult.getType() == Type.BLOCK) {
141
			BlockPos pos = ((BlockHitResult)hitResult).getBlockPos();
142
			return Collections.singleton(new TextCoordinates(prettyPrint(pos.getX()), prettyPrint(pos.getY()), prettyPrint(pos.getZ())));
143
		} else {
144
			return SharedSuggestionProvider.super.getRelevantCoordinates();
145
		}
146
	}
147
148
	@Override
149
	public Collection<TextCoordinates> getAbsoluteCoordinates() {
150
		HitResult hitResult = this.minecraft.hitResult;
151
		if (hitResult != null && hitResult.getType() == Type.BLOCK) {
152
			Vec3 pos = hitResult.getLocation();
153
			return Collections.singleton(new TextCoordinates(prettyPrint(pos.x), prettyPrint(pos.y), prettyPrint(pos.z)));
154
		} else {
155
			return SharedSuggestionProvider.super.getAbsoluteCoordinates();
156
		}
157
	}
158
159
	@Override
160
	public Set<ResourceKey<Level>> levels() {
161
		return this.connection.levels();
162
	}
163
164
	@Override
165
	public RegistryAccess registryAccess() {
166
		return this.connection.registryAccess();
167
	}
168
169
	@Override
170
	public FeatureFlagSet enabledFeatures() {
171
		return this.connection.enabledFeatures();
172
	}
173
174
	public void completeCustomSuggestions(final int id, final Suggestions result) {
175
		if (id == this.pendingSuggestionsId) {
176
			this.pendingSuggestionsFuture.complete(result);
177
			this.pendingSuggestionsFuture = null;
178
			this.pendingSuggestionsId = -1;
179
		}
180
	}
181
182
	public void modifyCustomCompletions(final Action action, final List<String> entries) {
183
		switch (action) {
184
			case ADD:
185
				this.customCompletionSuggestions.addAll(entries);
186
				break;
187
			case REMOVE:
188
				entries.forEach(this.customCompletionSuggestions::remove);
189
				break;
190
			case SET:
191
				this.customCompletionSuggestions.clear();
192
				this.customCompletionSuggestions.addAll(entries);
193
		}
194
	}
195
}
196

Decompiled Minecraft 26.3 source of ClientSuggestionProvider (net/minecraft/client/multiplayer/ClientSuggestionProvider.java) with Mojang official mappings, stored verbatim on the site.