From 691d674f018ff25231a6bd018401e6bfc04e4e23 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Thu, 9 Jun 2022 21:40:54 -0400 Subject: [PATCH] Use PaperServerListPingEvent when available We need to support this or else events that exclusively use PaperServerListPingEvent will not see our call. Fixes https://github.com/GeyserMC/Geyser/issues/3003 --- .../spigot/GeyserPaperPingPassthrough.java | 105 ++++++++++++++++++ .../spigot/GeyserSpigotPingPassthrough.java | 2 +- .../platform/spigot/GeyserSpigotPlugin.java | 8 +- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java new file mode 100644 index 000000000..cbe063931 --- /dev/null +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.platform.spigot; + +import com.destroystokyo.paper.event.server.PaperServerListPingEvent; +import com.destroystokyo.paper.network.StatusClient; +import com.destroystokyo.paper.profile.PlayerProfile; +import org.bukkit.Bukkit; +import org.geysermc.geyser.network.MinecraftProtocol; +import org.geysermc.geyser.ping.GeyserPingInfo; +import org.geysermc.geyser.ping.IGeyserPingPassthrough; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.net.InetSocketAddress; + +/** + * This class is used if possible, so listeners listening for PaperServerListPingEvent exclusively have their changes + * applied. + */ +public final class GeyserPaperPingPassthrough implements IGeyserPingPassthrough { + private final GeyserSpigotLogger logger; + + public GeyserPaperPingPassthrough(GeyserSpigotLogger logger) { + this.logger = logger; + } + + @Nullable + @Override + public GeyserPingInfo getPingInformation(InetSocketAddress inetSocketAddress) { + 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 = new PaperServerListPingEvent(new GeyserStatusClient(inetSocketAddress), + Bukkit.getMotd(), Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers(), Bukkit.getVersion(), + MinecraftProtocol.getJavaProtocolVersion(), null); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + // We have to send a ping, so not really sure what else to do here. + return null; + } + + GeyserPingInfo.Players players; + if (event.shouldHidePlayers()) { + players = new GeyserPingInfo.Players(1, 0); + } else { + players = new GeyserPingInfo.Players(event.getMaxPlayers(), event.getNumPlayers()); + } + + GeyserPingInfo geyserPingInfo = new GeyserPingInfo(event.getMotd(), players, + new GeyserPingInfo.Version(Bukkit.getVersion(), MinecraftProtocol.getJavaProtocolVersion())); + + if (!event.shouldHidePlayers()) { + for (PlayerProfile profile : event.getPlayerSample()) { + geyserPingInfo.getPlayerList().add(profile.getName()); + } + } + + return geyserPingInfo; + } catch (Exception e) { + logger.debug("Error while getting Paper ping passthrough: " + e); + return null; + } + } + + private record GeyserStatusClient(InetSocketAddress address) implements StatusClient { + @Override + public @NotNull InetSocketAddress getAddress() { + return address; + } + + @Override + public int getProtocolVersion() { + return MinecraftProtocol.getJavaProtocolVersion(); + } + + @Override + public @Nullable InetSocketAddress getVirtualHost() { + return null; + } + } +} diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java index 19153b750..ae01190c0 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java @@ -30,8 +30,8 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.util.CachedServerIcon; -import org.geysermc.geyser.ping.GeyserPingInfo; import org.geysermc.geyser.network.MinecraftProtocol; +import org.geysermc.geyser.ping.GeyserPingInfo; import org.geysermc.geyser.ping.IGeyserPingPassthrough; import javax.annotation.Nonnull; diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java index 4b41ad569..cee38228c 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java @@ -167,8 +167,14 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { if (geyserConfig.isLegacyPingPassthrough()) { this.geyserSpigotPingPassthrough = GeyserLegacyPingPassthrough.init(geyser); } else { - this.geyserSpigotPingPassthrough = new GeyserSpigotPingPassthrough(geyserLogger); + try { + Class.forName("com.destroystokyo.paper.event.server.PaperServerListPingEvent"); + this.geyserSpigotPingPassthrough = new GeyserPaperPingPassthrough(geyserLogger); + } catch (ClassNotFoundException e) { + this.geyserSpigotPingPassthrough = new GeyserSpigotPingPassthrough(geyserLogger); + } } + geyserLogger.debug("Spigot ping passthrough type: " + (this.geyserSpigotPingPassthrough == null ? null : this.geyserSpigotPingPassthrough.getClass())); this.geyserCommandManager = new GeyserSpigotCommandManager(geyser);