Minestom/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java

86 lines
3.1 KiB
Java
Raw Normal View History

2020-04-24 03:25:58 +02:00
package net.minestom.server.network.packet.server.play;
2020-04-11 17:21:53 +02:00
import net.kyori.adventure.text.Component;
2021-03-12 16:33:19 +01:00
import net.minestom.server.adventure.ComponentHolder;
import net.minestom.server.network.packet.server.ComponentHoldingServerPacket;
2020-04-24 03:25:58 +02:00
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
2021-02-06 18:58:52 +01:00
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
2021-11-30 17:49:41 +01:00
import net.minestom.server.utils.binary.Writeable;
import org.jetbrains.annotations.NotNull;
2021-11-30 17:49:41 +01:00
import org.jetbrains.annotations.Nullable;
2020-04-11 17:21:53 +02:00
2021-03-12 16:33:19 +01:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.UnaryOperator;
2021-11-30 17:49:41 +01:00
public record TabCompletePacket(int transactionId, int start, int length,
@NotNull List<Match> matches) implements ComponentHoldingServerPacket {
public TabCompletePacket {
matches = List.copyOf(matches);
}
2020-04-11 17:21:53 +02:00
2021-11-30 17:49:41 +01:00
public TabCompletePacket(BinaryReader reader) {
this(reader.readVarInt(), reader.readVarInt(), reader.readVarInt(), reader.readVarIntList(Match::new));
2021-02-06 18:58:52 +01:00
}
2020-04-11 17:21:53 +02:00
@Override
public void write(@NotNull BinaryWriter writer) {
2020-04-11 17:21:53 +02:00
writer.writeVarInt(transactionId);
writer.writeVarInt(start);
writer.writeVarInt(length);
2021-11-30 17:49:41 +01:00
writer.writeVarIntList(matches, BinaryWriter::write);
2021-02-06 18:58:52 +01:00
}
2020-04-11 17:21:53 +02:00
@Override
public int getId() {
return ServerPacketIdentifier.TAB_COMPLETE;
}
2021-03-12 16:33:19 +01:00
@Override
public @NotNull Collection<Component> components() {
if (matches.isEmpty()) return List.of();
2021-11-30 17:49:41 +01:00
List<Component> components = new ArrayList<>(matches.size());
for (Match match : matches) {
if (match.tooltip != null) {
components.add(match.tooltip);
2021-03-12 16:33:19 +01:00
}
}
2021-11-30 17:49:41 +01:00
return components;
2021-03-12 16:33:19 +01:00
}
@Override
public @NotNull ServerPacket copyWithOperator(@NotNull UnaryOperator<Component> operator) {
2021-11-30 17:49:41 +01:00
if (matches.isEmpty()) return this;
final List<Match> updatedMatches = matches.stream().map(match -> match.copyWithOperator(operator)).toList();
return new TabCompletePacket(transactionId, start, length, updatedMatches);
2021-03-12 16:33:19 +01:00
2021-11-30 17:49:41 +01:00
}
2021-03-12 16:33:19 +01:00
2021-11-30 17:49:41 +01:00
public record Match(@NotNull String match,
@Nullable Component tooltip) implements Writeable, ComponentHolder<Match> {
public Match(BinaryReader reader) {
this(reader.readSizedString(), reader.readBoolean() ? reader.readComponent() : null);
2021-03-12 16:33:19 +01:00
}
2021-11-30 17:49:41 +01:00
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(match);
writer.writeBoolean(tooltip != null);
if (tooltip != null) writer.writeComponent(tooltip);
}
2021-03-12 16:33:19 +01:00
@Override
public @NotNull Collection<Component> components() {
return tooltip != null ? List.of(tooltip) : List.of();
2021-03-12 16:33:19 +01:00
}
@Override
public @NotNull Match copyWithOperator(@NotNull UnaryOperator<Component> operator) {
2021-11-30 17:49:41 +01:00
return tooltip != null ? new Match(match, operator.apply(tooltip)) : this;
2021-03-12 16:33:19 +01:00
}
2020-04-11 17:21:53 +02:00
}
}