feat: Add basic API for server links (#1353)
* feat: add basic server links API * refactor: more precondition checking on links * refactor: remove whitespace * refactor: adjust method order, JDs in ServerLink * refactor: remove "throws" from constructors * refactor: add @NotNull annotations * refactor: requested changes * refactor: just use `List#copyOf`
This commit is contained in:
@@ -55,6 +55,7 @@ import com.velocitypowered.api.proxy.player.ResourcePackInfo;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import com.velocitypowered.api.util.GameProfile;
|
||||
import com.velocitypowered.api.util.ModInfo;
|
||||
import com.velocitypowered.api.util.ServerLink;
|
||||
import com.velocitypowered.proxy.VelocityServer;
|
||||
import com.velocitypowered.proxy.adventure.VelocityBossBarImplementation;
|
||||
import com.velocitypowered.proxy.connection.MinecraftConnection;
|
||||
@@ -83,6 +84,7 @@ import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
|
||||
import com.velocitypowered.proxy.protocol.packet.chat.PlayerChatCompletionPacket;
|
||||
import com.velocitypowered.proxy.protocol.packet.chat.builder.ChatBuilderFactory;
|
||||
import com.velocitypowered.proxy.protocol.packet.chat.legacy.LegacyChatPacket;
|
||||
import com.velocitypowered.proxy.protocol.packet.config.ClientboundServerLinksPacket;
|
||||
import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket;
|
||||
import com.velocitypowered.proxy.protocol.packet.title.GenericTitlePacket;
|
||||
import com.velocitypowered.proxy.protocol.util.ByteBufDataOutput;
|
||||
@@ -1059,6 +1061,22 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
|
||||
}, connection.eventLoop());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerLinks(final @NotNull List<ServerLink> links) {
|
||||
Preconditions.checkNotNull(links, "links");
|
||||
Preconditions.checkArgument(
|
||||
this.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_21),
|
||||
"Player version must be at least 1.21 to be able to set server links");
|
||||
|
||||
if (connection.getState() != StateRegistry.PLAY
|
||||
&& connection.getState() != StateRegistry.CONFIG) {
|
||||
throw new IllegalStateException("Can only send server links in CONFIGURATION or PLAY protocol");
|
||||
}
|
||||
|
||||
connection.write(new ClientboundServerLinksPacket(List.copyOf(links).stream()
|
||||
.map(l -> new ClientboundServerLinksPacket.ServerLink(l, getProtocolVersion())).toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCustomChatCompletions(@NotNull Collection<String> completions) {
|
||||
Preconditions.checkNotNull(completions, "completions");
|
||||
|
@@ -18,6 +18,7 @@
|
||||
package com.velocitypowered.proxy.protocol.packet.config;
|
||||
|
||||
import com.velocitypowered.api.network.ProtocolVersion;
|
||||
import com.velocitypowered.api.util.ServerLink;
|
||||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
|
||||
import com.velocitypowered.proxy.protocol.MinecraftPacket;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolUtils;
|
||||
@@ -66,6 +67,13 @@ public class ClientboundServerLinksPacket implements MinecraftPacket {
|
||||
}
|
||||
|
||||
public record ServerLink(int id, ComponentHolder displayName, String url) {
|
||||
|
||||
public ServerLink(com.velocitypowered.api.util.ServerLink link, ProtocolVersion protocolVersion) {
|
||||
this(link.getBuiltInType().map(Enum::ordinal).orElse(-1),
|
||||
link.getCustomLabel().map(c -> new ComponentHolder(protocolVersion, c)).orElse(null),
|
||||
link.getUrl().toString());
|
||||
}
|
||||
|
||||
private static ServerLink read(ByteBuf buf, ProtocolVersion version) {
|
||||
if (buf.readBoolean()) {
|
||||
return new ServerLink(ProtocolUtils.readVarInt(buf), null, ProtocolUtils.readString(buf));
|
||||
|
Reference in New Issue
Block a user