Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add basic API for server links #1353

Merged
merged 9 commits into from
Jun 18, 2024
11 changes: 11 additions & 0 deletions api/src/main/java/com/velocitypowered/api/proxy/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 java.net.InetSocketAddress;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -461,4 +462,14 @@ default void openBook(@NotNull Book book) {
* @sinceMinecraft 1.20.5
*/
void requestCookie(Key key);

/**
* Send the player a list of custom links to display in their client's pause menu.
WiIIiam278 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param links an ordered list of {@link ServerLink}s to send to the player
* @throws IllegalArgumentException if the player is from a version lower than 1.21
* @since 3.3.0
* @sinceMinecraft 1.21
*/
void setServerLinks(@NotNull List<ServerLink> links);
}
93 changes: 93 additions & 0 deletions api/src/main/java/com/velocitypowered/api/util/ServerLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2021-2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.util;

import com.google.common.base.Preconditions;
import java.net.URI;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;

/**
* Represents a custom URL servers can show in player pause menus.
* Links can be of a built-in type or use a custom component text label.
*/
public final class ServerLink {

private @Nullable Type type;
private @Nullable Component label;
private final String link;

/**
* Construct a server link with a custom component label.
*
* @param label a custom component label to display
* @param link the URL to open when clicked
*/
public ServerLink(Component label, String link) {
WiIIiam278 marked this conversation as resolved.
Show resolved Hide resolved
this.label = Preconditions.checkNotNull(label, "label");
this.link = URI.create(link).toString();
WiIIiam278 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Construct a server link with a built-in type.
*
* @param type the {@link Type built-in type} of link
* @param link the URL to open when clicked
*/
public ServerLink(Type type, String link) {
WiIIiam278 marked this conversation as resolved.
Show resolved Hide resolved
this.type = Preconditions.checkNotNull(type, "type");
this.link = URI.create(link).toString();
}

/**
* Get the type of the server link.
*
* @return the type of the server link
*/
public Optional<Type> getBuiltInType() {
return Optional.ofNullable(type);
}

/**
* Get the custom component label of the server link.
*
* @return the custom component label of the server link
*/
public Optional<Component> getCustomLabel() {
return Optional.ofNullable(label);
}

/**
* Get the link URL.
*
* @return the link URL
*/
public String getLink() {
return link;
}

/**
* Built-in types of server links.
*
* @apiNote {@link Type#BUG_REPORT} links are shown on the connection error screen
*/
public enum Type {
BUG_REPORT,
COMMUNITY_GUIDELINES,
SUPPORT,
STATUS,
FEEDBACK,
COMMUNITY,
WEBSITE,
FORUMS,
NEWS,
ANNOUNCEMENTS
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.util.concurrent.CompletableFuture.completedFuture;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonObject;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.DisconnectEvent.LoginStatus;
Expand Down Expand Up @@ -54,6 +55,7 @@
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;
Expand Down Expand Up @@ -81,6 +83,7 @@
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;
Expand Down Expand Up @@ -1057,6 +1060,25 @@ public void requestCookie(final Key key) {
}, 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(ImmutableList.copyOf(links).stream()
WiIIiam278 marked this conversation as resolved.
Show resolved Hide resolved
.map(l -> new ClientboundServerLinksPacket.ServerLink(
l.getBuiltInType().map(Enum::ordinal).orElse(-1),
l.getCustomLabel().map(c -> new ComponentHolder(getProtocolVersion(), c)).orElse(null),
l.getLink())).toList()));
}

@Override
public void addCustomChatCompletions(@NotNull Collection<String> completions) {
Preconditions.checkNotNull(completions, "completions");
Expand Down