| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import com.mojang.brigadier.context.StringRange;
|
| 4 | import com.mojang.brigadier.suggestion.Suggestion;
|
| 5 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.chat.ComponentSerialization;
|
| 11 | import net.minecraft.network.chat.ComponentUtils;
|
| 12 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.network.protocol.Packet;
|
| 15 | import net.minecraft.network.protocol.PacketType;
|
| 16 |
|
| 17 | public record ClientboundCommandSuggestionsPacket(int id, int start, int length, List<ClientboundCommandSuggestionsPacket.Entry> suggestions)
|
| 18 | implements Packet<ClientGamePacketListener> {
|
| 19 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundCommandSuggestionsPacket> STREAM_CODEC = StreamCodec.composite(
|
| 20 | ByteBufCodecs.VAR_INT,
|
| 21 | ClientboundCommandSuggestionsPacket::id,
|
| 22 | ByteBufCodecs.VAR_INT,
|
| 23 | ClientboundCommandSuggestionsPacket::start,
|
| 24 | ByteBufCodecs.VAR_INT,
|
| 25 | ClientboundCommandSuggestionsPacket::length,
|
| 26 | ClientboundCommandSuggestionsPacket.Entry.STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 27 | ClientboundCommandSuggestionsPacket::suggestions,
|
| 28 | ClientboundCommandSuggestionsPacket::new
|
| 29 | );
|
| 30 |
|
| 31 | public ClientboundCommandSuggestionsPacket(final int id, final Suggestions suggestions) {
|
| 32 | this(
|
| 33 | id,
|
| 34 | suggestions.getRange().getStart(),
|
| 35 | suggestions.getRange().getLength(),
|
| 36 | suggestions.getList()
|
| 37 | .stream()
|
| 38 | .map(
|
| 39 | suggestion -> new ClientboundCommandSuggestionsPacket.Entry(
|
| 40 | suggestion.getText(), Optional.ofNullable(suggestion.getTooltip()).map(ComponentUtils::fromMessage)
|
| 41 | )
|
| 42 | )
|
| 43 | .toList()
|
| 44 | );
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public PacketType<ClientboundCommandSuggestionsPacket> type() {
|
| 49 | return GamePacketTypes.CLIENTBOUND_COMMAND_SUGGESTIONS;
|
| 50 | }
|
| 51 |
|
| 52 | public void handle(final ClientGamePacketListener listener) {
|
| 53 | listener.handleCommandSuggestions(this);
|
| 54 | }
|
| 55 |
|
| 56 | public Suggestions toSuggestions() {
|
| 57 | StringRange range = StringRange.between(this.start, this.start + this.length);
|
| 58 | return new Suggestions(range, this.suggestions.stream().map(entry -> new Suggestion(range, entry.text(), entry.tooltip().orElse(null))).toList());
|
| 59 | }
|
| 60 |
|
| 61 | public record Entry(String text, Optional<Component> tooltip) {
|
| 62 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundCommandSuggestionsPacket.Entry> STREAM_CODEC = StreamCodec.composite(
|
| 63 | ByteBufCodecs.STRING_UTF8,
|
| 64 | ClientboundCommandSuggestionsPacket.Entry::text,
|
| 65 | ComponentSerialization.TRUSTED_OPTIONAL_STREAM_CODEC,
|
| 66 | ClientboundCommandSuggestionsPacket.Entry::tooltip,
|
| 67 | ClientboundCommandSuggestionsPacket.Entry::new
|
| 68 | );
|
| 69 | }
|
| 70 | }
|
| 71 |
|