Skip to content

Commit 64e1e7d

Browse files
committed
Fix some issues with server ping in 1.19.4
1 parent 05fa147 commit 64e1e7d

File tree

6 files changed

+56
-39
lines changed

6 files changed

+56
-39
lines changed

src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,15 @@ public boolean inject() {
209209
return false;
210210
}
211211

212+
String anchorHandler = "decoder";
213+
if (MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove()) {
214+
anchorHandler = "unbundler";
215+
}
216+
212217
// inject our handlers
213218
this.wrappedChannel.pipeline().addAfter("encoder", WIRE_PACKET_ENCODER_NAME, WIRE_PACKET_ENCODER);
214219
this.wrappedChannel.pipeline().addAfter(
215-
"decoder",
220+
anchorHandler,
216221
INTERCEPTOR_NAME,
217222
new InboundPacketInterceptor(this, this.channelListener));
218223

src/main/java/com/comphenix/protocol/reflect/accessors/DefaultFieldAccessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.lang.reflect.Field;
55
import java.lang.reflect.Modifier;
66

7+
import com.google.common.base.Preconditions;
8+
79
final class DefaultFieldAccessor implements FieldAccessor {
810

911
private final Field field;
@@ -13,9 +15,9 @@ final class DefaultFieldAccessor implements FieldAccessor {
1315
private final MethodHandle getter;
1416

1517
public DefaultFieldAccessor(Field field, MethodHandle setter, MethodHandle getter, boolean staticField) {
16-
this.field = field;
17-
this.setter = setter;
18-
this.getter = getter;
18+
this.field = Preconditions.checkNotNull(field, "field");
19+
this.setter = Preconditions.checkNotNull(setter, "setter");
20+
this.getter = Preconditions.checkNotNull(getter, "getter");
1921
this.staticField = staticField;
2022
}
2123

src/main/java/com/comphenix/protocol/reflect/accessors/MethodHandleHelper.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.lang.reflect.Modifier;
1212
import java.util.logging.Level;
1313

14+
import com.google.common.base.Preconditions;
15+
1416
final class MethodHandleHelper {
1517

1618
private static final Lookup LOOKUP;
@@ -51,29 +53,35 @@ private MethodHandleHelper() {
5153
}
5254

5355
public static MethodAccessor getMethodAccessor(Method method) {
56+
Preconditions.checkNotNull(method, "method");
57+
5458
try {
5559
MethodHandle unreflected = LOOKUP.unreflect(method);
5660
boolean staticMethod = Modifier.isStatic(method.getModifiers());
5761

5862
MethodHandle generified = convertToGeneric(unreflected, staticMethod, false);
5963
return new DefaultMethodAccessor(method, generified, staticMethod);
60-
} catch (IllegalAccessException exception) {
61-
throw new IllegalStateException("Unable to access method " + method);
64+
} catch (IllegalAccessException ex) {
65+
throw new IllegalStateException("Unable to access method " + method, ex);
6266
}
6367
}
6468

6569
public static ConstructorAccessor getConstructorAccessor(Constructor<?> constructor) {
70+
Preconditions.checkNotNull(constructor, "constructor");
71+
6672
try {
6773
MethodHandle unreflected = LOOKUP.unreflectConstructor(constructor);
6874
MethodHandle generified = convertToGeneric(unreflected, false, true);
6975

7076
return new DefaultConstrutorAccessor(constructor, generified);
71-
} catch (IllegalAccessException exception) {
72-
throw new IllegalStateException("Unable to access constructor " + constructor);
77+
} catch (IllegalAccessException ex) {
78+
throw new IllegalStateException("Unable to access constructor " + constructor, ex);
7379
}
7480
}
7581

7682
public static FieldAccessor getFieldAccessor(Field field) {
83+
Preconditions.checkNotNull(field, "field");
84+
7785
try {
7886
boolean staticField = Modifier.isStatic(field.getModifiers());
7987

@@ -97,10 +105,18 @@ public static FieldAccessor getFieldAccessor(Field field) {
97105
setter = setter.asType(VIRTUAL_FIELD_SETTER);
98106
}
99107

108+
if (getter == null) {
109+
throw new IllegalStateException("Unable to access field " + field + ". Could not find getter");
110+
}
111+
112+
if (setter == null) {
113+
throw new IllegalStateException("Unable to access field " + field + ". Could not find setter");
114+
}
115+
100116
return new DefaultFieldAccessor(field, setter, getter, staticField);
101-
} catch (IllegalAccessException | NoSuchFieldException exception) {
117+
} catch (IllegalAccessException | NoSuchFieldException ex) {
102118
// NoSuchFieldException can never happen, the field always exists
103-
throw new IllegalStateException("Unable to access field " + field, exception);
119+
throw new IllegalStateException("Unable to access field " + field, ex);
104120
}
105121
}
106122

src/main/java/com/comphenix/protocol/wrappers/AutoWrapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
2121
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
2222
import com.google.common.base.Defaults;
23+
import com.google.common.base.Preconditions;
2324

2425
import java.lang.reflect.Modifier;
2526
import java.util.Arrays;
@@ -88,6 +89,8 @@ public AutoWrapper<T> field(int index, EquivalentConverter converter) {
8889
}
8990

9091
public T wrap(Object nmsObject) {
92+
Preconditions.checkNotNull(nmsObject);
93+
9194
T instance;
9295

9396
try {
@@ -114,6 +117,8 @@ public T wrap(Object nmsObject) {
114117
}
115118

116119
public Object unwrap(Object wrapper) {
120+
Preconditions.checkNotNull(wrapper);
121+
117122
// ensures that all accessors are present
118123
computeFieldAccessors();
119124
computeNmsConstructorAccess();
@@ -139,14 +144,14 @@ private void computeFieldAccessors() {
139144
nmsAccessors = Arrays
140145
.stream(nmsClass.getDeclaredFields())
141146
.filter(field -> !Modifier.isStatic(field.getModifiers()))
142-
.map(field -> Accessors.getFieldAccessor(field))
147+
.map(Accessors::getFieldAccessor)
143148
.toArray(FieldAccessor[]::new);
144149
}
145150

146151
if (wrapperAccessors == null) {
147152
wrapperAccessors = Arrays
148153
.stream(wrapperClass.getDeclaredFields())
149-
.map(field -> Accessors.getFieldAccessor(field))
154+
.map(Accessors::getFieldAccessor)
150155
.toArray(FieldAccessor[]::new);
151156
}
152157
}

src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ private WrappedServerPing(Object handle) {
6868
this.impl = newImpl(handle);
6969
}
7070

71-
private ServerPingImpl newImpl() {
71+
private static ServerPingImpl newImpl() {
7272
if (MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove()) {
7373
return new ServerPingRecord();
7474
}
7575

7676
return new LegacyServerPing();
7777
}
7878

79-
private ServerPingImpl newImpl(Object handle) {
79+
private static ServerPingImpl newImpl(Object handle) {
8080
if (MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove()) {
81-
return new ServerPingRecord();
81+
return new ServerPingRecord(handle);
8282
}
8383

8484
return new LegacyServerPing(handle);
@@ -87,14 +87,14 @@ private ServerPingImpl newImpl(Object handle) {
8787
/**
8888
* Set the player count and player maximum to the default values.
8989
*/
90-
protected void resetPlayers() {
90+
private void resetPlayers() {
9191
impl.resetPlayers();
9292
}
9393

9494
/**
9595
* Reset the version string to the default state.
9696
*/
97-
protected void resetVersion() {
97+
private void resetVersion() {
9898
impl.resetVersion();
9999
}
100100

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ServerPingRecord implements ServerPingImpl {
2626

2727
private static ConstructorAccessor PING_CTOR;
2828

29-
private static boolean initialized;
29+
private static boolean initialized = false;
3030

3131
private static void initialize() {
3232
if (initialized) {
@@ -42,39 +42,28 @@ private static void initialize() {
4242

4343
PING_CTOR = Accessors.getConstructorAccessor(SERVER_PING.getConstructors()[0]);
4444

45-
DATA_WRAPPER = AutoWrapper
46-
.wrap(ServerData.class, SERVER_DATA_CLASS)
47-
.field(0, Converters.passthrough(String.class))
48-
.field(1, Converters.passthrough(int.class));
49-
50-
SAMPLE_WRAPPER = AutoWrapper
51-
.wrap(PlayerSample.class, PLAYER_SAMPLE_CLASS)
52-
.field(0, Converters.passthrough(int.class))
53-
.field(1, Converters.passthrough(int.class))
54-
.field(2, Converters.passthrough(Object.class));
55-
56-
FAVICON_WRAPPER = AutoWrapper
57-
.wrap(Favicon.class, MinecraftReflection.getMinecraftClass("network.protocol.status.ServerPing$a"))
58-
.field(0, Converters.passthrough(byte[].class));
45+
DATA_WRAPPER = AutoWrapper.wrap(ServerData.class, SERVER_DATA_CLASS);
46+
SAMPLE_WRAPPER = AutoWrapper.wrap(PlayerSample.class, PLAYER_SAMPLE_CLASS);
47+
FAVICON_WRAPPER = AutoWrapper.wrap(Favicon.class, MinecraftReflection.getMinecraftClass("network.protocol.status.ServerPing$a"));
5948

6049
DEFAULT_DESCRIPTION = WrappedChatComponent.fromLegacyText("A Minecraft Server");
6150
} catch (Exception ex) {
6251
ex.printStackTrace(); // TODO
6352
}
6453
}
6554

66-
private static class PlayerSample {
55+
public static final class PlayerSample {
6756
public int max;
6857
public int online;
6958
public Object sample;
7059
}
7160

72-
private static class ServerData {
61+
public static final class ServerData {
7362
public String name;
7463
public int protocol;
7564
}
7665

77-
private static class Favicon {
66+
public static final class Favicon {
7867
public byte[] iconBytes;
7968
}
8069

@@ -255,10 +244,10 @@ public void setPlayersVisible(boolean visible) {
255244

256245
@Override
257246
public Object getHandle() {
258-
Optional<Object> players = Optional.of(SAMPLE_WRAPPER.unwrap(playerSample));
259-
Optional<Object> version = Optional.of(DATA_WRAPPER.unwrap(serverData));
260-
Optional<Object> favHandle = Optional.of(FAVICON_WRAPPER.unwrap(favicon));
247+
Optional<Object> playersHandle = Optional.ofNullable(playerSample != null ? SAMPLE_WRAPPER.unwrap(playerSample) : null);
248+
Optional<Object> versionHandle = Optional.ofNullable(serverData != null ? DATA_WRAPPER.unwrap(serverData) : null);
249+
Optional<Object> favHandle = Optional.ofNullable(favicon != null ? FAVICON_WRAPPER.unwrap(favicon) : null);
261250

262-
return PING_CTOR.invoke(description, players, version, favHandle, enforceSafeChat);
251+
return PING_CTOR.invoke(description, playersHandle, versionHandle, favHandle, enforceSafeChat);
263252
}
264253
}

0 commit comments

Comments
 (0)