2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.network.packet.server.play;
|
2020-04-11 17:21:53 +02:00
|
|
|
|
2021-03-10 17:13:27 +01: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;
|
2020-08-19 20:34:21 +02:00
|
|
|
import net.minestom.server.utils.binary.BinaryWriter;
|
2020-10-25 22:19:27 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
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.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.UnaryOperator;
|
|
|
|
|
|
|
|
public class TabCompletePacket implements ComponentHoldingServerPacket {
|
2020-04-11 17:21:53 +02:00
|
|
|
|
|
|
|
public int transactionId;
|
|
|
|
public int start;
|
|
|
|
public int length;
|
|
|
|
public Match[] matches;
|
|
|
|
|
2021-02-06 18:58:52 +01:00
|
|
|
public TabCompletePacket() {
|
|
|
|
matches = new Match[0];
|
|
|
|
}
|
|
|
|
|
2020-04-11 17:21:53 +02:00
|
|
|
@Override
|
2020-10-25 22:19:27 +01:00
|
|
|
public void write(@NotNull BinaryWriter writer) {
|
2020-04-11 17:21:53 +02:00
|
|
|
writer.writeVarInt(transactionId);
|
|
|
|
writer.writeVarInt(start);
|
|
|
|
writer.writeVarInt(length);
|
|
|
|
|
|
|
|
writer.writeVarInt(matches.length);
|
|
|
|
for (Match match : matches) {
|
|
|
|
writer.writeSizedString(match.match);
|
|
|
|
writer.writeBoolean(match.hasTooltip);
|
|
|
|
if (match.hasTooltip)
|
2021-03-10 17:13:27 +01:00
|
|
|
writer.writeComponent(match.tooltip);
|
2020-04-11 17:21:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:58:52 +01:00
|
|
|
@Override
|
|
|
|
public void read(@NotNull BinaryReader reader) {
|
|
|
|
transactionId = reader.readVarInt();
|
|
|
|
start = reader.readVarInt();
|
|
|
|
length = reader.readVarInt();
|
|
|
|
|
|
|
|
int matchCount = reader.readVarInt();
|
|
|
|
matches = new Match[matchCount];
|
|
|
|
for (int i = 0; i < matchCount; i++) {
|
2021-06-11 16:19:17 +02:00
|
|
|
String match = reader.readSizedString();
|
2021-02-06 18:58:52 +01:00
|
|
|
boolean hasTooltip = reader.readBoolean();
|
2021-02-17 19:04:48 +01:00
|
|
|
Component tooltip = null;
|
2021-06-11 16:19:17 +02:00
|
|
|
if (hasTooltip) {
|
|
|
|
tooltip = reader.readComponent();
|
2021-02-06 18:58:52 +01:00
|
|
|
}
|
|
|
|
Match newMatch = new Match();
|
|
|
|
newMatch.match = match;
|
|
|
|
newMatch.hasTooltip = hasTooltip;
|
|
|
|
newMatch.tooltip = tooltip;
|
|
|
|
matches[i] = newMatch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 == null || matches.length == 0) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
} else {
|
|
|
|
List<Component> components = new ArrayList<>(matches.length);
|
|
|
|
for (Match match : matches) {
|
|
|
|
if (match.hasTooltip) {
|
|
|
|
components.add(match.tooltip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull ServerPacket copyWithOperator(@NotNull UnaryOperator<Component> operator) {
|
|
|
|
if (matches == null || matches.length == 0) {
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
TabCompletePacket packet = new TabCompletePacket();
|
|
|
|
packet.transactionId = transactionId;
|
|
|
|
packet.start = start;
|
|
|
|
packet.length = length;
|
|
|
|
packet.matches = new Match[matches.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < matches.length; i++) {
|
|
|
|
packet.matches[i] = matches[i].copyWithOperator(operator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Match implements ComponentHolder<Match> {
|
2020-04-11 17:21:53 +02:00
|
|
|
public String match;
|
|
|
|
public boolean hasTooltip;
|
2021-03-10 17:13:27 +01:00
|
|
|
public Component tooltip;
|
2021-03-12 16:33:19 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Collection<Component> components() {
|
|
|
|
if (hasTooltip) {
|
|
|
|
return Collections.singleton(tooltip);
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Match copyWithOperator(@NotNull UnaryOperator<Component> operator) {
|
|
|
|
if (hasTooltip) {
|
|
|
|
Match newMatch = new Match();
|
|
|
|
newMatch.match = match;
|
|
|
|
newMatch.hasTooltip = hasTooltip;
|
|
|
|
newMatch.tooltip = tooltip;
|
|
|
|
return newMatch;
|
|
|
|
} else {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 17:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|