Skip to content

Commit 35ac14a

Browse files
authored
[ci skip] fix: either use new or old constructor of ServerListPingEvent (#689)
1 parent 41924f9 commit 35ac14a

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defaultTasks 'build', 'checkLicenses', 'test', 'jar'
2323

2424
allprojects {
2525

26-
def major = 3, minor = 4, patch = 4, versionType = "RELEASE"
26+
def major = 3, minor = 4, patch = 5, versionType = "SNAPSHOT"
2727

2828
group 'de.dytanic.cloudnet'
2929
version "$major.$minor.$patch-$versionType"

cloudnet-modules/cloudnet-bridge/src/main/java/de/dytanic/cloudnet/ext/bridge/bukkit/BukkitCloudNetBridgePlugin.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@
2525
import de.dytanic.cloudnet.ext.bridge.player.IPlayerManager;
2626
import de.dytanic.cloudnet.ext.bridge.server.BridgeServerHelper;
2727
import de.dytanic.cloudnet.wrapper.Wrapper;
28+
import java.lang.invoke.MethodHandle;
29+
import java.lang.invoke.MethodHandles;
30+
import java.lang.invoke.MethodType;
31+
import java.net.InetAddress;
2832
import java.net.InetSocketAddress;
33+
import java.util.function.Supplier;
34+
import java.util.logging.Level;
2935
import org.bukkit.Bukkit;
3036
import org.bukkit.event.HandlerList;
3137
import org.bukkit.event.server.ServerListPingEvent;
3238
import org.bukkit.plugin.java.JavaPlugin;
39+
import org.jetbrains.annotations.Nullable;
3340

3441
public final class BukkitCloudNetBridgePlugin extends JavaPlugin {
3542

43+
private Supplier<ServerListPingEvent> serverListPingEventConstructor;
44+
3645
@Override
3746
public synchronized void onEnable() {
3847
BukkitCloudNetHelper.init();
@@ -70,14 +79,12 @@ private void runFireServerListPingEvent() {
7079
boolean value = false;
7180

7281
try {
73-
ServerListPingEvent serverListPingEvent = new ServerListPingEvent(
74-
new InetSocketAddress("127.0.0.1", 53345).getAddress(),
75-
BridgeServerHelper.getMotd(),
76-
Bukkit.getOnlinePlayers().size(),
77-
BridgeServerHelper.getMaxPlayers()
78-
);
79-
Bukkit.getPluginManager().callEvent(serverListPingEvent);
82+
ServerListPingEvent serverListPingEvent = this.constructServerListPingEvent();
83+
if (serverListPingEvent == null) {
84+
return;
85+
}
8086

87+
Bukkit.getPluginManager().callEvent(serverListPingEvent);
8188
if (!serverListPingEvent.getMotd().equalsIgnoreCase(BridgeServerHelper.getMotd())) {
8289
hasToUpdate = true;
8390
BridgeServerHelper.setMotd(serverListPingEvent.getMotd());
@@ -106,4 +113,48 @@ private void runFireServerListPingEvent() {
106113
}
107114
}, 0, 10);
108115
}
116+
117+
private @Nullable ServerListPingEvent constructServerListPingEvent() {
118+
if (this.serverListPingEventConstructor == null) {
119+
try {
120+
// new method in 1.19 and above
121+
MethodHandle constructor = MethodHandles.publicLookup().findConstructor(
122+
ServerListPingEvent.class,
123+
MethodType.methodType(void.class, InetAddress.class, String.class, boolean.class, int.class, int.class));
124+
this.serverListPingEventConstructor = () -> {
125+
try {
126+
return (ServerListPingEvent) constructor.invokeExact(
127+
new InetSocketAddress("127.0.0.1", 53345).getAddress(),
128+
BridgeServerHelper.getMotd(),
129+
false,
130+
Bukkit.getOnlinePlayers().size(),
131+
BridgeServerHelper.getMaxPlayers());
132+
} catch (Throwable throwable) {
133+
// wait what
134+
this.getLogger().log(
135+
Level.SEVERE,
136+
"Unable to construct ServerListPingEvent using modern constructor; disabling event calling",
137+
throwable);
138+
this.serverListPingEventConstructor = () -> null;
139+
return null;
140+
}
141+
};
142+
} catch (IllegalAccessException exception) {
143+
// wait what
144+
this.serverListPingEventConstructor = () -> null;
145+
this.getLogger().log(
146+
Level.SEVERE,
147+
"Unable to access modern constructor of ServerListPingEvent; disabling event calling",
148+
exception);
149+
} catch (NoSuchMethodException exception) {
150+
// old method before 1.18
151+
this.serverListPingEventConstructor = () -> new ServerListPingEvent(
152+
new InetSocketAddress("127.0.0.1", 53345).getAddress(),
153+
BridgeServerHelper.getMotd(),
154+
Bukkit.getOnlinePlayers().size(),
155+
BridgeServerHelper.getMaxPlayers());
156+
}
157+
}
158+
return this.serverListPingEventConstructor.get();
159+
}
109160
}

0 commit comments

Comments
 (0)