Skip to content

Commit 5773bcb

Browse files
committed
Fix WrappedServerPing#getPlayers in 1.21.10
Fixes #3570
1 parent 468082c commit 5773bcb

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
group = "net.dmulloy2"
1515
description = "Provides access to the Minecraft protocol"
1616

17-
val mcVersion = "1.21.9"
17+
val mcVersion = "1.21.10"
1818
val isSnapshot = version.toString().endsWith("-SNAPSHOT")
1919
val commitHash = System.getenv("COMMIT_SHA") ?: ""
2020
val isCI = commitHash.isNotEmpty()

src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
* @author Kristian
3737
*/
3838
public final class MinecraftVersion implements Comparable<MinecraftVersion>, Serializable {
39+
/**
40+
* Version 1.21.10 - hotfix for 1.21.9
41+
*/
42+
public static final MinecraftVersion v1_21_10 = new MinecraftVersion("1.21.10");
43+
3944
/**
4045
* Version 1.21.9 - the copper age drop
4146
*/
@@ -174,7 +179,7 @@ public final class MinecraftVersion implements Comparable<MinecraftVersion>, Ser
174179
/**
175180
* The latest release version of minecraft.
176181
*/
177-
public static final MinecraftVersion LATEST = v1_21_9;
182+
public static final MinecraftVersion LATEST = v1_21_10;
178183

179184
// used when serializing
180185
private static final long serialVersionUID = -8695133558996459770L;

src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
88
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
99
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
10+
import com.comphenix.protocol.reflect.instances.DefaultInstances;
11+
import com.comphenix.protocol.reflect.instances.MinecraftGenerator;
1012
import com.comphenix.protocol.utility.MinecraftProtocolVersion;
1113
import com.comphenix.protocol.utility.MinecraftReflection;
1214
import com.comphenix.protocol.utility.MinecraftVersion;
@@ -23,6 +25,7 @@ public final class ServerPingRecord implements ServerPingImpl {
2325
private static Class<?> SERVER_PING;
2426
private static Class<?> PLAYER_SAMPLE_CLASS;
2527
private static Class<?> SERVER_DATA_CLASS;
28+
private static Class<?> NAME_AND_ID_CLASS;
2629

2730
private static Class<?> GSON_CLASS;
2831
private static MethodAccessor GSON_TO_JSON;
@@ -62,7 +65,31 @@ private static void initialize() {
6265
SAMPLE_WRAPPER = AutoWrapper.wrap(PlayerSample.class, PLAYER_SAMPLE_CLASS);
6366
FAVICON_WRAPPER = AutoWrapper.wrap(Favicon.class, MinecraftReflection.getMinecraftClass("network.protocol.status.ServerPing$a", "network.protocol.status.ServerStatus$Favicon"));
6467

65-
PROFILE_LIST_CONVERTER = BukkitConverters.getListConverter(BukkitConverters.getWrappedGameProfileConverter());
68+
if (MinecraftVersion.v1_21_10.atOrAbove()) {
69+
NAME_AND_ID_CLASS = MinecraftReflection.getMinecraftClass("server.players.NameAndId");
70+
NAME_AND_ID_WRAPPER = AutoWrapper.wrap(NameAndId.class, NAME_AND_ID_CLASS);
71+
72+
PROFILE_LIST_CONVERTER = BukkitConverters.getListConverter(new EquivalentConverter<>() {
73+
@Override
74+
public Object getGeneric(WrappedGameProfile specific) {
75+
NameAndId wrapper = new NameAndId(specific.getUUID(), specific.getName());
76+
return NAME_AND_ID_WRAPPER.getGeneric(wrapper);
77+
}
78+
79+
@Override
80+
public WrappedGameProfile getSpecific(Object generic) {
81+
NameAndId wrapper = NAME_AND_ID_WRAPPER.getSpecific(generic);
82+
return new WrappedGameProfile(wrapper.id, wrapper.name);
83+
}
84+
85+
@Override
86+
public Class<WrappedGameProfile> getSpecificType() {
87+
return WrappedGameProfile.class;
88+
}
89+
});
90+
} else {
91+
PROFILE_LIST_CONVERTER = BukkitConverters.getListConverter(BukkitConverters.getWrappedGameProfileConverter());
92+
}
6693

6794
DEFAULT_DESCRIPTION = WrappedChatComponent.fromLegacyText("A Minecraft Server");
6895

@@ -124,11 +151,24 @@ public Favicon() {
124151
}
125152
}
126153

127-
private static AutoWrapper<PlayerSample> SAMPLE_WRAPPER;
154+
public static final class NameAndId {
155+
public UUID id;
156+
public String name;
128157

129-
private static AutoWrapper<ServerData> DATA_WRAPPER;
158+
public NameAndId(UUID id, String name) {
159+
this.id = id;
160+
this.name = name;
161+
}
130162

163+
public NameAndId() {
164+
this(MinecraftGenerator.SYS_UUID, "");
165+
}
166+
}
167+
168+
private static AutoWrapper<PlayerSample> SAMPLE_WRAPPER;
169+
private static AutoWrapper<ServerData> DATA_WRAPPER;
131170
private static AutoWrapper<Favicon> FAVICON_WRAPPER;
171+
private static AutoWrapper<NameAndId> NAME_AND_ID_WRAPPER;
132172

133173
private WrappedChatComponent description;
134174
private PlayerSample playerSample;

src/test/java/com/comphenix/protocol/wrappers/WrappedServerPingTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.comphenix.protocol.wrappers;
22

33
import java.io.IOException;
4+
import java.util.List;
45
import java.util.Optional;
6+
import java.util.UUID;
57

68
import com.comphenix.protocol.BukkitInitialization;
79
import com.comphenix.protocol.PacketType;
@@ -11,6 +13,10 @@
1113

1214
import com.google.common.io.Resources;
1315
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
16+
import net.minecraft.network.chat.Component;
17+
import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket;
18+
import net.minecraft.network.protocol.status.ServerStatus;
19+
import net.minecraft.server.players.NameAndId;
1420
import org.junit.jupiter.api.BeforeAll;
1521
import org.junit.jupiter.api.Disabled;
1622
import org.junit.jupiter.api.Test;
@@ -77,6 +83,25 @@ public void fullTest() throws IOException {
7783
assertArrayEquals(copy.getData(), roundTrip.getFavicon().getData());
7884
}
7985

86+
@Test
87+
public void testGetPlayers() {
88+
ClientboundStatusResponsePacket packet = new ClientboundStatusResponsePacket(new ServerStatus(
89+
Component.literal("A Minecraft Server"),
90+
Optional.of(new ServerStatus.Players(2, 2, List.of(
91+
new NameAndId(UUID.randomUUID(), "Player1"),
92+
new NameAndId(UUID.randomUUID(), "Player2")
93+
))),
94+
Optional.empty(),
95+
Optional.empty(),
96+
false
97+
));
98+
99+
PacketContainer container = PacketContainer.fromPacket(packet);
100+
101+
WrappedServerPing wrappedServerStatus = container.getServerPings().read(0);
102+
assertEquals(2, wrappedServerStatus.getPlayers().size());
103+
}
104+
80105
@Test
81106
public void testDefaultData() {
82107
PacketContainer packet = new PacketContainer(PacketType.Status.Server.SERVER_INFO);

0 commit comments

Comments
 (0)