Skip to content

Commit

Permalink
Merge branch 'dev/3.0.0' of https://github.com/PaperMC/Velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jun 9, 2024
2 parents cbf6e0d + 2d015cd commit 79123d3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
package com.velocitypowered.api.event.proxy;

import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.InboundConnection;
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
Expand All @@ -20,28 +22,71 @@
* amount of ping packets a client can send.
*/
@AwaitingEvent
public final class ProxyPingEvent {
public final class ProxyPingEvent implements ResultedEvent<ResultedEvent.GenericResult> {

private final InboundConnection connection;
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.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() {
return connection;
return this.connection;
}

/**
* Get the ServerPing to send to the connection.
*
* @return the ServerPing to send
*/
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");
}

/**
* 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
public String toString() {
return "ProxyPingEvent{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ public boolean handle(LegacyPingPacket packet) {
this.pingReceived = true;
server.getServerListPingHandler().getInitialPing(this.inbound)
.thenCompose(ping -> server.getEventManager().fire(new ProxyPingEvent(inbound, ping)))
.thenAcceptAsync(event -> connection.closeWith(
LegacyDisconnect.fromServerPing(event.getPing(), packet.getVersion())),
connection.eventLoop())
.thenAcceptAsync(event -> {
if (event.getResult().isAllowed()) {
connection.closeWith(LegacyDisconnect.fromServerPing(event.getPing(), packet.getVersion()));
} else {
connection.close();
}
}, connection.eventLoop())
.exceptionally((ex) -> {
logger.error("Exception while handling legacy ping {}", packet, ex);
return null;
Expand All @@ -95,10 +99,14 @@ public boolean handle(StatusRequestPacket packet) {
.thenCompose(ping -> server.getEventManager().fire(new ProxyPingEvent(inbound, ping)))
.thenAcceptAsync(
(event) -> {
final StringBuilder json = new StringBuilder();
VelocityServer.getPingGsonInstance(connection.getProtocolVersion())
.toJson(event.getPing(), json);
connection.write(new StatusResponsePacket(json));
if (event.getResult().isAllowed()) {
final StringBuilder json = new StringBuilder();
VelocityServer.getPingGsonInstance(connection.getProtocolVersion())
.toJson(event.getPing(), json);
connection.write(new StatusResponsePacket(json));
} else {
connection.close();
}
},
connection.eventLoop())
.exceptionally((ex) -> {
Expand Down

0 comments on commit 79123d3

Please sign in to comment.