13
0
geforkt von Mirrors/Velocity

Add support for canceling ProxyPingEvent (#1264)

* Added support for canceling ProxyPingEvent
Dieser Commit ist enthalten in:
Adrian 2024-06-08 14:28:33 -05:00 committet von GitHub
Ursprung 46cf9be297
Commit 2d015cdf5c
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
2 geänderte Dateien mit 65 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -8,9 +8,11 @@
package com.velocitypowered.api.event.proxy; package com.velocitypowered.api.event.proxy;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent; import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.InboundConnection;
import com.velocitypowered.api.proxy.server.ServerPing; import com.velocitypowered.api.proxy.server.ServerPing;
import org.jetbrains.annotations.NotNull;
/** /**
* This event is fired when a request for server information is sent by a remote client, or when the * This event is fired when a request for server information is sent by a remote client, or when the
@ -20,28 +22,71 @@ import com.velocitypowered.api.proxy.server.ServerPing;
* amount of ping packets a client can send. * amount of ping packets a client can send.
*/ */
@AwaitingEvent @AwaitingEvent
public final class ProxyPingEvent { public final class ProxyPingEvent implements ResultedEvent<ResultedEvent.GenericResult> {
private final InboundConnection connection; private final InboundConnection connection;
private ServerPing ping; private ServerPing ping;
private GenericResult result = GenericResult.allowed();
public ProxyPingEvent(InboundConnection connection, ServerPing ping) { public ProxyPingEvent(final InboundConnection connection, final ServerPing ping) {
this.connection = Preconditions.checkNotNull(connection, "connection"); this.connection = Preconditions.checkNotNull(connection, "connection");
this.ping = Preconditions.checkNotNull(ping, "ping"); this.ping = Preconditions.checkNotNull(ping, "ping");
} }
/**
* Obtain the connection to which the corresponding ServerPing will be sent.
*
* @return the connection that has sent the ServerPing request
*/
public InboundConnection getConnection() { public InboundConnection getConnection() {
return connection; return this.connection;
} }
/**
* Get the ServerPing to send to the connection.
*
* @return the ServerPing to send
*/
public ServerPing getPing() { public ServerPing getPing() {
return ping; return this.ping;
} }
public void setPing(ServerPing ping) { /**
* Sets the ServerPing to send to the connection.
*
* @param ping sets the ServerPing to send
*/
public void setPing(final @NotNull ServerPing ping) {
this.ping = Preconditions.checkNotNull(ping, "ping"); this.ping = Preconditions.checkNotNull(ping, "ping");
} }
/**
* Gets whether to avoid sending a ping response to the connection.
*
* @return if a ping response to the connection will be avoided
* @apiNote For the ProxyPingEvent executed to obtain the MOTD for the ServerData
* sent to players of versions higher than 1.19.1,
* the cancellation of this event will have no effect.
*/
@Override
public GenericResult getResult() {
return this.result;
}
/**
* Sets whether to avoid sending a ping response to the connection.
* This will automatically close the connection.
*
* @param result if a ping response to the connection will be avoided
* @apiNote For the ProxyPingEvent executed to obtain the MOTD for the ServerData
* sent to players of versions higher than 1.19.1,
* the cancellation of this event will have no effect.
*/
@Override
public void setResult(final @NotNull GenericResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}
@Override @Override
public String toString() { public String toString() {
return "ProxyPingEvent{" return "ProxyPingEvent{"

Datei anzeigen

@ -68,9 +68,13 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
this.pingReceived = true; this.pingReceived = true;
server.getServerListPingHandler().getInitialPing(this.inbound) server.getServerListPingHandler().getInitialPing(this.inbound)
.thenCompose(ping -> server.getEventManager().fire(new ProxyPingEvent(inbound, ping))) .thenCompose(ping -> server.getEventManager().fire(new ProxyPingEvent(inbound, ping)))
.thenAcceptAsync(event -> connection.closeWith( .thenAcceptAsync(event -> {
LegacyDisconnect.fromServerPing(event.getPing(), packet.getVersion())), if (event.getResult().isAllowed()) {
connection.eventLoop()) connection.closeWith(LegacyDisconnect.fromServerPing(event.getPing(), packet.getVersion()));
} else {
connection.close();
}
}, connection.eventLoop())
.exceptionally((ex) -> { .exceptionally((ex) -> {
logger.error("Exception while handling legacy ping {}", packet, ex); logger.error("Exception while handling legacy ping {}", packet, ex);
return null; return null;
@ -95,10 +99,14 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
.thenCompose(ping -> server.getEventManager().fire(new ProxyPingEvent(inbound, ping))) .thenCompose(ping -> server.getEventManager().fire(new ProxyPingEvent(inbound, ping)))
.thenAcceptAsync( .thenAcceptAsync(
(event) -> { (event) -> {
if (event.getResult().isAllowed()) {
final StringBuilder json = new StringBuilder(); final StringBuilder json = new StringBuilder();
VelocityServer.getPingGsonInstance(connection.getProtocolVersion()) VelocityServer.getPingGsonInstance(connection.getProtocolVersion())
.toJson(event.getPing(), json); .toJson(event.getPing(), json);
connection.write(new StatusResponsePacket(json)); connection.write(new StatusResponsePacket(json));
} else {
connection.close();
}
}, },
connection.eventLoop()) connection.eventLoop())
.exceptionally((ex) -> { .exceptionally((ex) -> {