mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-12-19 23:09:29 +00:00
Fix issues with paper ping passthrough
This commit is contained in:
@@ -37,6 +37,7 @@ import org.geysermc.geyser.ping.GeyserPingInfo;
|
|||||||
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
|
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,7 +45,10 @@ import java.net.InetSocketAddress;
|
|||||||
* applied.
|
* applied.
|
||||||
*/
|
*/
|
||||||
public final class GeyserPaperPingPassthrough implements IGeyserPingPassthrough {
|
public final class GeyserPaperPingPassthrough implements IGeyserPingPassthrough {
|
||||||
private static final Constructor<PaperServerListPingEvent> OLD_CONSTRUCTOR = ReflectedNames.getOldPaperPingConstructor();
|
private static final Constructor<PaperServerListPingEvent> EVENT_CONSTRUCTOR = ReflectedNames.paperServerListPingEventConstructor();
|
||||||
|
// https://jd.papermc.io/paper/1.19.2/com/destroystokyo/paper/event/server/PaperServerListPingEvent.html
|
||||||
|
private static final boolean CHAT_PREVIEWS = EVENT_CONSTRUCTOR.getParameters()[2].getType() == boolean.class;
|
||||||
|
private static final Method MOTD_COMPONENT_GETTER = ReflectedNames.motdGetter();
|
||||||
|
|
||||||
private final GeyserSpigotLogger logger;
|
private final GeyserSpigotLogger logger;
|
||||||
|
|
||||||
@@ -57,17 +61,14 @@ public final class GeyserPaperPingPassthrough implements IGeyserPingPassthrough
|
|||||||
@Override
|
@Override
|
||||||
public GeyserPingInfo getPingInformation(InetSocketAddress inetSocketAddress) {
|
public GeyserPingInfo getPingInformation(InetSocketAddress inetSocketAddress) {
|
||||||
try {
|
try {
|
||||||
// We'd rather *not* use deprecations here, but unfortunately any Adventure class would be relocated at
|
|
||||||
// runtime because we still have to shade in our own Adventure class. For now.
|
|
||||||
PaperServerListPingEvent event;
|
PaperServerListPingEvent event;
|
||||||
if (OLD_CONSTRUCTOR != null) {
|
if (CHAT_PREVIEWS) {
|
||||||
// 1.19, removed in 1.19.4
|
event = EVENT_CONSTRUCTOR.newInstance(new GeyserStatusClient(inetSocketAddress),
|
||||||
event = OLD_CONSTRUCTOR.newInstance(new GeyserStatusClient(inetSocketAddress),
|
MOTD_COMPONENT_GETTER.invoke(null), false, Bukkit.getOnlinePlayers().size(),
|
||||||
Bukkit.getMotd(), Bukkit.getOnlinePlayers().size(),
|
|
||||||
Bukkit.getMaxPlayers(), Bukkit.getVersion(), GameProtocol.getJavaProtocolVersion(), null);
|
Bukkit.getMaxPlayers(), Bukkit.getVersion(), GameProtocol.getJavaProtocolVersion(), null);
|
||||||
} else {
|
} else {
|
||||||
event = new PaperServerListPingEvent(new GeyserStatusClient(inetSocketAddress),
|
event = EVENT_CONSTRUCTOR.newInstance(new GeyserStatusClient(inetSocketAddress),
|
||||||
Bukkit.getMotd(), Bukkit.getOnlinePlayers().size(),
|
MOTD_COMPONENT_GETTER.invoke(null), Bukkit.getOnlinePlayers().size(),
|
||||||
Bukkit.getMaxPlayers(), Bukkit.getVersion(), GameProtocol.getJavaProtocolVersion(), null);
|
Bukkit.getMaxPlayers(), Bukkit.getVersion(), GameProtocol.getJavaProtocolVersion(), null);
|
||||||
}
|
}
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
|||||||
@@ -26,12 +26,12 @@
|
|||||||
package org.geysermc.geyser.platform.spigot;
|
package org.geysermc.geyser.platform.spigot;
|
||||||
|
|
||||||
import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
|
import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
|
||||||
import com.destroystokyo.paper.network.StatusClient;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.server.ServerListPingEvent;
|
import org.bukkit.event.server.ServerListPingEvent;
|
||||||
import org.bukkit.util.CachedServerIcon;
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,8 +42,9 @@ public final class ReflectedNames {
|
|||||||
static boolean checkPaperPingEvent() {
|
static boolean checkPaperPingEvent() {
|
||||||
try {
|
try {
|
||||||
Class.forName("com.destroystokyo.paper.event.server.PaperServerListPingEvent");
|
Class.forName("com.destroystokyo.paper.event.server.PaperServerListPingEvent");
|
||||||
|
paperServerListPingEventConstructor();
|
||||||
return true;
|
return true;
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (Throwable ignored) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,18 +53,27 @@ public final class ReflectedNames {
|
|||||||
return getConstructor(ServerListPingEvent.class, InetAddress.class, String.class, boolean.class, int.class, int.class) != null;
|
return getConstructor(ServerListPingEvent.class, InetAddress.class, String.class, boolean.class, int.class, int.class) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static @Nullable Constructor<PaperServerListPingEvent> getOldPaperPingConstructor() {
|
// Ugly workaround that's necessary due to relocation of adventure components
|
||||||
if (getConstructor(PaperServerListPingEvent.class, StatusClient.class, String.class, int.class,
|
static Method motdGetter() {
|
||||||
int.class, String.class, int.class, CachedServerIcon.class) != null) {
|
try {
|
||||||
// @NotNull StatusClient client, @NotNull String motd, int numPlayers, int maxPlayers,
|
return Bukkit.class.getMethod("motd");
|
||||||
// @NotNull String version, int protocolVersion, @Nullable CachedServerIcon favicon
|
} catch (Throwable e) {
|
||||||
// New constructor is present
|
throw new RuntimeException("Could not find component motd method! Please report this issue.", e);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
// @NotNull StatusClient client, @NotNull String motd, boolean shouldSendChatPreviews, int numPlayers, int maxPlayers,
|
}
|
||||||
// @NotNull String version, int protocolVersion, @Nullable CachedServerIcon favicon
|
|
||||||
return getConstructor(PaperServerListPingEvent.class, StatusClient.class, String.class, boolean.class, int.class, int.class,
|
@SuppressWarnings("unchecked")
|
||||||
String.class, int.class, CachedServerIcon.class);
|
static Constructor<PaperServerListPingEvent> paperServerListPingEventConstructor() {
|
||||||
|
var constructors = PaperServerListPingEvent.class.getConstructors();
|
||||||
|
for (var constructor : constructors) {
|
||||||
|
// We want to get the constructor with the adventure component motd, but without referencing the
|
||||||
|
// component class as that's relocated
|
||||||
|
if (constructor.getParameters()[1].getType() != String.class) {
|
||||||
|
return (Constructor<PaperServerListPingEvent>) constructor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalStateException("Could not find component motd method!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user