Skip to content

Commit c8b137e

Browse files
authored
Expose protocol version for status pings (#2456)
Historically we considered the Minecraft protocol versions as "implementation detail" that should not be exposed in SpongeAPI. However, since the addition of the status ping API 8 years ago the protocol version still exists exactly the same way, and there are use cases for checking it (identifying the exact client version) and modifying it (making the server appear as incompatible to clients). Right now plugins have to resort to using implementation-specific code for this, which is complicated and now hopelessly broken for api-10 (due to internal changes in the Minecraft code). Make it possible to check and modify the server version to fix this once and for all.
1 parent 8e4597a commit c8b137e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/org/spongepowered/api/MinecraftVersion.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public interface MinecraftVersion extends Comparable<MinecraftVersion> {
4444
*/
4545
String name();
4646

47+
/**
48+
* Gets the protocol version of this Minecraft version.
49+
*
50+
* @implNote This should be interpreted together with {@link #isLegacy()},
51+
* since legacy clients use a different protocol version numbering scheme.
52+
* @return The protocol version
53+
* @see <a href="https://minecraft.fandom.com/wiki/Protocol_version">Protocol version (Minecraft Wiki)</a>
54+
*/
55+
int protocolVersion();
56+
4757
/**
4858
* Returns whether this version is an older version that doesn't support
4959
* all of the features in {@link StatusResponse}. These versions are only
@@ -62,4 +72,13 @@ public interface MinecraftVersion extends Comparable<MinecraftVersion> {
6272
* @return The data version
6373
*/
6474
OptionalInt dataVersion();
75+
76+
@Override
77+
default int compareTo(MinecraftVersion o) {
78+
final int result = Boolean.compare(this.isLegacy(), o.isLegacy());
79+
if (result != 0) {
80+
return result;
81+
}
82+
return this.protocolVersion() - o.protocolVersion();
83+
}
6584
}

src/main/java/org/spongepowered/api/event/server/ClientPingServerEvent.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import net.kyori.adventure.text.Component;
2828
import org.checkerframework.checker.nullness.qual.Nullable;
29+
import org.spongepowered.api.MinecraftVersion;
2930
import org.spongepowered.api.event.Cancellable;
3031
import org.spongepowered.api.event.Event;
3132
import org.spongepowered.api.network.status.Favicon;
@@ -92,6 +93,9 @@ interface Response extends StatusResponse {
9293
*/
9394
void setHidePlayers(boolean hide);
9495

96+
@Override
97+
Version version();
98+
9599
/**
96100
* Sets the {@link Favicon} to display on the client.
97101
*
@@ -130,6 +134,33 @@ interface Players extends StatusResponse.Players {
130134
@Override
131135
List<GameProfile> profiles();
132136
}
137+
138+
/**
139+
* Represents the information about the version of the server, sent
140+
* after the {@link ClientPingServerEvent}.
141+
*/
142+
@NoFactoryMethod
143+
interface Version extends MinecraftVersion {
144+
145+
/**
146+
* Sets the name of the version of the server. This is usually
147+
* displayed on the client if the server is using an incompatible
148+
* protocol version.
149+
*
150+
* @param name The new display name of the server version
151+
*/
152+
void setName(String name);
153+
154+
/**
155+
* Sets the server protocol version reported to the client.
156+
* Modifying this will change if the client sees the server as
157+
* incompatible or not, forcing it to display the {@link #name()}.
158+
*
159+
* @param protocolVersion The new server protocol version
160+
* @see <a href="https://minecraft.fandom.com/wiki/Protocol_version">Protocol version (Minecraft Wiki)</a>
161+
*/
162+
void setProtocolVersion(int protocolVersion);
163+
}
133164
}
134165

135166
}

0 commit comments

Comments
 (0)