Skip to content

Commit

Permalink
Expose protocol version for status pings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephan-gh committed Apr 22, 2023
1 parent 8e4597a commit 22f6fbb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/spongepowered/api/MinecraftVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public interface MinecraftVersion extends Comparable<MinecraftVersion> {
*/
String name();

/**
* Gets the protocol version of this Minecraft version.
*
* @implNote This should be interpreted together with {@link #isLegacy()},
* since legacy clients use a different protocol version numbering scheme.
* @return The protocol version
* @see <a href="https://minecraft.fandom.com/wiki/Protocol_version">Protocol version (Minecraft Wiki)</a>
*/
int protocolVersion();

/**
* Returns whether this version is an older version that doesn't support
* all of the features in {@link StatusResponse}. These versions are only
Expand All @@ -62,4 +72,13 @@ public interface MinecraftVersion extends Comparable<MinecraftVersion> {
* @return The data version
*/
OptionalInt dataVersion();

@Override
default int compareTo(MinecraftVersion o) {
final int result = Boolean.compare(this.isLegacy(), o.isLegacy());
if (result != 0) {
return result;
}
return this.protocolVersion() - o.protocolVersion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.MinecraftVersion;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.network.status.Favicon;
Expand Down Expand Up @@ -92,6 +93,9 @@ interface Response extends StatusResponse {
*/
void setHidePlayers(boolean hide);

@Override
Version version();

/**
* Sets the {@link Favicon} to display on the client.
*
Expand Down Expand Up @@ -130,6 +134,33 @@ interface Players extends StatusResponse.Players {
@Override
List<GameProfile> profiles();
}

/**
* Represents the information about the version of the server, sent
* after the {@link ClientPingServerEvent}.
*/
@NoFactoryMethod
interface Version extends MinecraftVersion {

/**
* Sets the name of the version of the server. This is usually
* displayed on the client if the server is using an incompatible
* protocol version.
*
* @param name The new display name of the server version
*/
void setName(String name);

/**
* Sets the server protocol version reported to the client.
* Modifying this will change if the client sees the server as
* incompatible or not, forcing it to display the {@link #name()}.
*
* @param protocolVersion The new server protocol version
* @see <a href="https://minecraft.fandom.com/wiki/Protocol_version">Protocol version (Minecraft Wiki)</a>
*/
void setProtocolVersion(int protocolVersion);
}
}

}

0 comments on commit 22f6fbb

Please sign in to comment.