ClientSuggestionProvider.java

net.minecraft.client.multiplayer.ClientSuggestionProvider

信息

  • 全限定名:net.minecraft.client.multiplayer.ClientSuggestionProvider
  • 类型:public class
  • 包:net.minecraft.client.multiplayer
  • 源码路径:src/main/java/net/minecraft/client/multiplayer/ClientSuggestionProvider.java
  • 起始行号:L36
  • 实现:SharedSuggestionProvider
  • 职责:

    TODO

字段/常量

  • connection

    • 类型: ClientPacketListener
    • 修饰符: private final
    • 源码定位: L37
    • 说明:

      TODO

  • minecraft

    • 类型: Minecraft
    • 修饰符: private final
    • 源码定位: L38
    • 说明:

      TODO

  • pendingSuggestionsId

    • 类型: int
    • 修饰符: private
    • 源码定位: L39
    • 说明:

      TODO

  • pendingSuggestionsFuture

    • 类型: CompletableFuture<Suggestions>
    • 修饰符: private
    • 源码定位: L40
    • 说明:

      TODO

  • customCompletionSuggestions

    • 类型: Set<String>
    • 修饰符: private final
    • 源码定位: L41
    • 说明:

      TODO

  • permissions

    • 类型: PermissionSet
    • 修饰符: private final
    • 源码定位: L42
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ClientSuggestionProvider(ClientPacketListener connection, Minecraft minecraft, PermissionSet permissions) @ L44

  • 构造器名:ClientSuggestionProvider
  • 源码定位:L44
  • 修饰符:public

参数:

  • connection: ClientPacketListener
  • minecraft: Minecraft
  • permissions: PermissionSet

说明:

TODO

方法

下面的方法块按源码顺序生成。

public Collection<String> getOnlinePlayerNames() @ L50

  • 方法名:getOnlinePlayerNames
  • 源码定位:L50
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Collection<String> getCustomTabSuggestions() @ L61

  • 方法名:getCustomTabSuggestions
  • 源码定位:L61
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Collection<String> getSelectedEntities() @ L72

  • 方法名:getSelectedEntities
  • 源码定位:L72
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Collection<String> getAllTeams() @ L79

  • 方法名:getAllTeams
  • 源码定位:L79
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Stream<Identifier> getAvailableSounds() @ L84

  • 方法名:getAvailableSounds
  • 源码定位:L84
  • 返回类型:Stream
  • 修饰符:public

参数:

说明:

TODO

public PermissionSet permissions() @ L89

  • 方法名:permissions
  • 源码定位:L89
  • 返回类型:PermissionSet
  • 修饰符:public

参数:

说明:

TODO

public CompletableFuture<Suggestions> suggestRegistryElements(ResourceKey<?extends Registry<?>> key, SharedSuggestionProvider.ElementSuggestionType elements, SuggestionsBuilder builder, CommandContext<?> context) @ L94

  • 方法名:suggestRegistryElements
  • 源码定位:L94
  • 返回类型:CompletableFuture
  • 修饰符:public

参数:

  • key: ResourceKey>
  • elements: SharedSuggestionProvider.ElementSuggestionType
  • builder: SuggestionsBuilder
  • context: CommandContext<?>

说明:

TODO

public CompletableFuture<Suggestions> customSuggestion(CommandContext<?> context) @ L104

  • 方法名:customSuggestion
  • 源码定位:L104
  • 返回类型:CompletableFuture
  • 修饰符:public

参数:

  • context: CommandContext<?>

说明:

TODO

private static String prettyPrint(double value) @ L116

  • 方法名:prettyPrint
  • 源码定位:L116
  • 返回类型:String
  • 修饰符:private static

参数:

  • value: double

说明:

TODO

private static String prettyPrint(int value) @ L120

  • 方法名:prettyPrint
  • 源码定位:L120
  • 返回类型:String
  • 修饰符:private static

参数:

  • value: int

说明:

TODO

public Collection<SharedSuggestionProvider.TextCoordinates> getRelevantCoordinates() @ L124

  • 方法名:getRelevantCoordinates
  • 源码定位:L124
  • 返回类型:Collection<SharedSuggestionProvider.TextCoordinates>
  • 修饰符:public

参数:

说明:

TODO

public Collection<SharedSuggestionProvider.TextCoordinates> getAbsoluteCoordinates() @ L137

  • 方法名:getAbsoluteCoordinates
  • 源码定位:L137
  • 返回类型:Collection<SharedSuggestionProvider.TextCoordinates>
  • 修饰符:public

参数:

说明:

TODO

public Set<ResourceKey<Level>> levels() @ L148

  • 方法名:levels
  • 源码定位:L148
  • 返回类型:Set<ResourceKey>
  • 修饰符:public

参数:

说明:

TODO

public RegistryAccess registryAccess() @ L153

  • 方法名:registryAccess
  • 源码定位:L153
  • 返回类型:RegistryAccess
  • 修饰符:public

参数:

说明:

TODO

public FeatureFlagSet enabledFeatures() @ L158

  • 方法名:enabledFeatures
  • 源码定位:L158
  • 返回类型:FeatureFlagSet
  • 修饰符:public

参数:

说明:

TODO

public void completeCustomSuggestions(int id, Suggestions result) @ L163

  • 方法名:completeCustomSuggestions
  • 源码定位:L163
  • 返回类型:void
  • 修饰符:public

参数:

  • id: int
  • result: Suggestions

说明:

TODO

public void modifyCustomCompletions(ClientboundCustomChatCompletionsPacket.Action action, List<String> entries) @ L171

  • 方法名:modifyCustomCompletions
  • 源码定位:L171
  • 返回类型:void
  • 修饰符:public

参数:

  • action: ClientboundCustomChatCompletionsPacket.Action
  • entries: List

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class ClientSuggestionProvider implements SharedSuggestionProvider {
    private final ClientPacketListener connection;
    private final Minecraft minecraft;
    private int pendingSuggestionsId = -1;
    private @Nullable CompletableFuture<Suggestions> pendingSuggestionsFuture;
    private final Set<String> customCompletionSuggestions = new HashSet<>();
    private final PermissionSet permissions;
 
    public ClientSuggestionProvider(ClientPacketListener connection, Minecraft minecraft, PermissionSet permissions) {
        this.connection = connection;
        this.minecraft = minecraft;
        this.permissions = permissions;
    }
 
    @Override
    public Collection<String> getOnlinePlayerNames() {
        List<String> result = Lists.newArrayList();
 
        for (PlayerInfo info : this.connection.getOnlinePlayers()) {
            result.add(info.getProfile().name());
        }
 
        return result;
    }
 
    @Override
    public Collection<String> getCustomTabSuggestions() {
        if (this.customCompletionSuggestions.isEmpty()) {
            return this.getOnlinePlayerNames();
        } else {
            Set<String> result = new HashSet<>(this.getOnlinePlayerNames());
            result.addAll(this.customCompletionSuggestions);
            return result;
        }
    }
 
    @Override
    public Collection<String> getSelectedEntities() {
        return (Collection<String>)(this.minecraft.hitResult != null && this.minecraft.hitResult.getType() == HitResult.Type.ENTITY
            ? Collections.singleton(((EntityHitResult)this.minecraft.hitResult).getEntity().getStringUUID())
            : Collections.emptyList());
    }
 
    @Override
    public Collection<String> getAllTeams() {
        return this.connection.scoreboard().getTeamNames();
    }
 
    @Override
    public Stream<Identifier> getAvailableSounds() {
        return this.minecraft.getSoundManager().getAvailableSounds().stream();
    }
 
    @Override
    public PermissionSet permissions() {
        return this.permissions;
    }
 
    @Override
    public CompletableFuture<Suggestions> suggestRegistryElements(
        ResourceKey<? extends Registry<?>> key, SharedSuggestionProvider.ElementSuggestionType elements, SuggestionsBuilder builder, CommandContext<?> context
    ) {
        return this.registryAccess().lookup(key).map(registry -> {
            this.suggestRegistryElements(registry, elements, builder);
            return builder.buildFuture();
        }).orElseGet(() -> this.customSuggestion(context));
    }
 
    @Override
    public CompletableFuture<Suggestions> customSuggestion(CommandContext<?> context) {
        if (this.pendingSuggestionsFuture != null) {
            this.pendingSuggestionsFuture.cancel(false);
        }
 
        this.pendingSuggestionsFuture = new CompletableFuture<>();
        int id = ++this.pendingSuggestionsId;
        this.connection.send(new ServerboundCommandSuggestionPacket(id, context.getInput()));
        return this.pendingSuggestionsFuture;
    }
 
    private static String prettyPrint(double value) {
        return String.format(Locale.ROOT, "%.2f", value);
    }
 
    private static String prettyPrint(int value) {
        return Integer.toString(value);
    }
 
    @Override
    public Collection<SharedSuggestionProvider.TextCoordinates> getRelevantCoordinates() {
        HitResult hitResult = this.minecraft.hitResult;
        if (hitResult != null && hitResult.getType() == HitResult.Type.BLOCK) {
            BlockPos pos = ((BlockHitResult)hitResult).getBlockPos();
            return Collections.singleton(
                new SharedSuggestionProvider.TextCoordinates(prettyPrint(pos.getX()), prettyPrint(pos.getY()), prettyPrint(pos.getZ()))
            );
        } else {
            return SharedSuggestionProvider.super.getRelevantCoordinates();
        }
    }
 
    @Override
    public Collection<SharedSuggestionProvider.TextCoordinates> getAbsoluteCoordinates() {
        HitResult hitResult = this.minecraft.hitResult;
        if (hitResult != null && hitResult.getType() == HitResult.Type.BLOCK) {
            Vec3 pos = hitResult.getLocation();
            return Collections.singleton(new SharedSuggestionProvider.TextCoordinates(prettyPrint(pos.x), prettyPrint(pos.y), prettyPrint(pos.z)));
        } else {
            return SharedSuggestionProvider.super.getAbsoluteCoordinates();
        }
    }
 
    @Override
    public Set<ResourceKey<Level>> levels() {
        return this.connection.levels();
    }
 
    @Override
    public RegistryAccess registryAccess() {
        return this.connection.registryAccess();
    }
 
    @Override
    public FeatureFlagSet enabledFeatures() {
        return this.connection.enabledFeatures();
    }
 
    public void completeCustomSuggestions(int id, Suggestions result) {
        if (id == this.pendingSuggestionsId) {
            this.pendingSuggestionsFuture.complete(result);
            this.pendingSuggestionsFuture = null;
            this.pendingSuggestionsId = -1;
        }
    }
 
    public void modifyCustomCompletions(ClientboundCustomChatCompletionsPacket.Action action, List<String> entries) {
        switch (action) {
            case ADD:
                this.customCompletionSuggestions.addAll(entries);
                break;
            case REMOVE:
                entries.forEach(this.customCompletionSuggestions::remove);
                break;
            case SET:
                this.customCompletionSuggestions.clear();
                this.customCompletionSuggestions.addAll(entries);
        }
    }
}

引用的其他类