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
This commit is contained in:
Camotoy 2022-06-09 21:40:54 -04:00
parent f1a12d1feb
commit 691d674f01
No known key found for this signature in database
GPG Key ID: 7EEFB66FE798081F
3 changed files with 113 additions and 2 deletions

View File

@ -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;
}
}
}

View File

@ -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;

View File

@ -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);