Skip to content

Commit 98b7dd6

Browse files
authored
Merge pull request #50 from SpigotMC/master
[pull] master from SpigotMC:master
2 parents bacd008 + e62fc6c commit 98b7dd6

File tree

4 files changed

+111
-20
lines changed

4 files changed

+111
-20
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package net.md_5.bungee.api;
2+
3+
import lombok.AccessLevel;
4+
import lombok.Data;
5+
import lombok.NonNull;
6+
import lombok.RequiredArgsConstructor;
7+
import net.md_5.bungee.api.chat.BaseComponent;
8+
9+
/**
10+
* Represents a server link which may be sent to the client.
11+
*/
12+
@Data
13+
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
14+
public final class ServerLink
15+
{
16+
17+
/**
18+
* The links type.
19+
*
20+
* Note: This value is nullable, if null, label is non-null.
21+
*/
22+
private final LinkType type;
23+
24+
/**
25+
* The label for the link.
26+
*
27+
* Note: This value is nullable, if null, type is non-null.
28+
*/
29+
private final BaseComponent label;
30+
31+
/**
32+
* The URL that is displayed.
33+
*/
34+
@NonNull
35+
private final String url;
36+
37+
/**
38+
* Creates a link with a specified type and URL.
39+
*
40+
* @param type the type of the link
41+
* @param url the URL to be displayed
42+
*/
43+
public ServerLink(@NonNull LinkType type, @NonNull String url)
44+
{
45+
this.type = type;
46+
this.label = null;
47+
this.url = url;
48+
}
49+
50+
/**
51+
* Creates a link with a label and URL.
52+
*
53+
* @param label the label to be displayed
54+
* @param url the URL to be displayed
55+
*/
56+
public ServerLink(@NonNull BaseComponent label, @NonNull String url)
57+
{
58+
this.type = null;
59+
this.label = label;
60+
this.url = url;
61+
}
62+
63+
public enum LinkType
64+
{
65+
66+
REPORT_BUG,
67+
COMMUNITY_GUIDELINES,
68+
SUPPORT,
69+
STATUS,
70+
FEEDBACK,
71+
COMMUNITY,
72+
WEBSITE,
73+
FORUMS,
74+
NEWS,
75+
ANNOUNCEMENTS;
76+
}
77+
}

api/src/main/java/net/md_5/bungee/api/connection/ProxiedPlayer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.md_5.bungee.api.connection;
22

3+
import java.util.List;
34
import java.util.Locale;
45
import java.util.Map;
56
import java.util.UUID;
@@ -8,6 +9,7 @@
89
import net.md_5.bungee.api.ChatMessageType;
910
import net.md_5.bungee.api.CommandSender;
1011
import net.md_5.bungee.api.ServerConnectRequest;
12+
import net.md_5.bungee.api.ServerLink;
1113
import net.md_5.bungee.api.SkinConfiguration;
1214
import net.md_5.bungee.api.Title;
1315
import net.md_5.bungee.api.chat.BaseComponent;
@@ -411,4 +413,16 @@ public enum MainHand
411413
*/
412414
@ApiStatus.Experimental
413415
void showDialog(Dialog dialog);
416+
417+
/**
418+
* Sends server links to the player.
419+
*
420+
* Note: The links already sent to the player will be overwritten. Also, the
421+
* backend server is able to override links sent by the proxy.
422+
*
423+
* @param serverLinks the server links to send
424+
* @throws IllegalStateException if the player's version is not at least
425+
* 1.21
426+
*/
427+
void sendServerLinks(List<ServerLink> serverLinks);
414428
}

protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerLinks.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco
2727
links = new Link[ len ];
2828
for ( int i = 0; i < len; i++ )
2929
{
30-
Either<LinkType, BaseComponent> type;
30+
Either<Integer, BaseComponent> type;
3131
if ( buf.readBoolean() )
3232
{
33-
type = Either.left( LinkType.values()[readVarInt( buf )] );
33+
type = Either.left( readVarInt( buf ) );
3434
} else
3535
{
3636
type = Either.right( readBaseComponent( buf, protocolVersion ) );
@@ -47,11 +47,11 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc
4747
writeVarInt( links.length, buf );
4848
for ( Link link : links )
4949
{
50-
Either<LinkType, BaseComponent> type = link.getType();
50+
Either<Integer, BaseComponent> type = link.getType();
5151
if ( type.isLeft() )
5252
{
5353
buf.writeBoolean( true );
54-
writeVarInt( type.getLeft().ordinal(), buf );
54+
writeVarInt( type.getLeft(), buf );
5555
} else
5656
{
5757
buf.writeBoolean( false );
@@ -67,26 +67,11 @@ public void handle(AbstractPacketHandler handler) throws Exception
6767
handler.handle( this );
6868
}
6969

70-
public enum LinkType
71-
{
72-
73-
REPORT_BUG,
74-
COMMUNITY_GUIDELINES,
75-
SUPPORT,
76-
STATUS,
77-
FEEDBACK,
78-
COMMUNITY,
79-
WEBSITE,
80-
FORUMS,
81-
NEWS,
82-
ANNOUNCEMENTS;
83-
}
84-
8570
@Data
8671
public static class Link
8772
{
8873

89-
private final Either<LinkType, BaseComponent> type;
74+
private final Either<Integer, BaseComponent> type;
9075
private final String url;
9176
}
9277
}

proxy/src/main/java/net/md_5/bungee/UserConnection.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Collections;
1515
import java.util.HashSet;
1616
import java.util.LinkedList;
17+
import java.util.List;
1718
import java.util.Locale;
1819
import java.util.Map;
1920
import java.util.Objects;
@@ -29,6 +30,7 @@
2930
import net.md_5.bungee.api.ChatMessageType;
3031
import net.md_5.bungee.api.ProxyServer;
3132
import net.md_5.bungee.api.ServerConnectRequest;
33+
import net.md_5.bungee.api.ServerLink;
3234
import net.md_5.bungee.api.SkinConfiguration;
3335
import net.md_5.bungee.api.Title;
3436
import net.md_5.bungee.api.chat.BaseComponent;
@@ -59,6 +61,7 @@
5961
import net.md_5.bungee.protocol.packet.Kick;
6062
import net.md_5.bungee.protocol.packet.PlayerListHeaderFooter;
6163
import net.md_5.bungee.protocol.packet.PluginMessage;
64+
import net.md_5.bungee.protocol.packet.ServerLinks;
6265
import net.md_5.bungee.protocol.packet.SetCompression;
6366
import net.md_5.bungee.protocol.packet.ShowDialog;
6467
import net.md_5.bungee.protocol.packet.ShowDialogDirect;
@@ -860,4 +863,16 @@ public void showDialog(Dialog dialog)
860863

861864
unsafe.sendPacket( new ShowDialog( Either.right( dialog ) ) );
862865
}
866+
867+
@Override
868+
public void sendServerLinks(List<ServerLink> serverLinks)
869+
{
870+
Preconditions.checkState( getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_21, "Server links are only supported in 1.21 and above" );
871+
872+
ServerLinks.Link[] links = serverLinks.stream()
873+
.map( link -> new ServerLinks.Link( link.getType() != null ? Either.left( link.getType().ordinal() ) : Either.right( link.getLabel() ), link.getUrl() ) )
874+
.toArray( ServerLinks.Link[]::new );
875+
876+
unsafe.sendPacket( new ServerLinks( links ) );
877+
}
863878
}

0 commit comments

Comments
 (0)