3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Simplify event result structure and add some convenience methods

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-05-11 06:40:22 -04:00
Ursprung 1d867fce1c
Commit f4fc07768b
7 geänderte Dateien mit 74 neuen und 148 gelöschten Zeilen

Datei anzeigen

@ -20,7 +20,7 @@ import java.io.ByteArrayInputStream;
* This event is fired when a plugin message is sent to the proxy, either from a client ({@link
* Player}) or a server ({@link ServerConnection}).
*/
public interface PluginMessageEvent extends ResultedEvent<PluginMessageEvent.ForwardResult> {
public interface PluginMessageEvent extends ResultedEvent<ResultedEvent.GenericResult> {
ChannelMessageSource source();
@ -34,36 +34,11 @@ public interface PluginMessageEvent extends ResultedEvent<PluginMessageEvent.For
ByteArrayDataInput messageAsDataInput();
/**
* A result determining whether or not to forward this message on.
*/
public static final class ForwardResult implements Result {
private static final ForwardResult ALLOWED = new ForwardResult(true);
private static final ForwardResult DENIED = new ForwardResult(false);
private final boolean status;
private ForwardResult(boolean b) {
this.status = b;
}
@Override
public boolean isAllowed() {
return status;
}
@Override
public String toString() {
return status ? "forward to sink" : "handled message at proxy";
}
public static ForwardResult forward() {
return ALLOWED;
}
public static ForwardResult handled() {
return DENIED;
default void setHandled(boolean handled) {
if (handled) {
setResult(ResultedEvent.GenericResult.denied());
} else {
setResult(ResultedEvent.GenericResult.allowed());
}
}
}

Datei anzeigen

@ -28,7 +28,7 @@ public final class PluginMessageEventImpl implements PluginMessageEvent {
private final ChannelMessageSink target;
private final PluginChannelId identifier;
private final byte[] data;
private ForwardResult result;
private GenericResult result;
/**
* Creates a new instance.
@ -44,16 +44,16 @@ public final class PluginMessageEventImpl implements PluginMessageEvent {
this.target = Preconditions.checkNotNull(target, "target");
this.identifier = Preconditions.checkNotNull(identifier, "identifier");
this.data = Preconditions.checkNotNull(data, "data");
this.result = ForwardResult.forward();
this.result = GenericResult.allowed();
}
@Override
public ForwardResult result() {
public GenericResult result() {
return result;
}
@Override
public void setResult(ForwardResult result) {
public void setResult(GenericResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}

Datei anzeigen

@ -42,6 +42,33 @@ public interface KickedFromServerEvent extends
*/
boolean kickedDuringServerConnect();
/**
* Handles the event by disconnecting the player with the specified {@code reason}.
*
* @param reason the reason for disconnecting the player
*/
default void handleByDisconnecting(Component reason) {
setResult(DisconnectPlayer.create(reason));
}
/**
* Handles the event by falling back on the specified server.
*
* @param server the server to fall back to
*/
default void handleByConnectingToServer(RegisteredServer server) {
setResult(RedirectPlayer.create(server));
}
/**
* Handles the event by giving the player the specified {@code reason}.
*
* @param reason the reason for being kicked
*/
default void handleByNotifying(Component reason) {
setResult(Notify.create(reason));
}
/**
* Represents the base interface for {@link KickedFromServerEvent} results.
*/

Datei anzeigen

@ -7,124 +7,31 @@
package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.ResultedEvent.ComponentResult;
import com.velocitypowered.api.proxy.connection.InboundConnection;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* This event is fired when a player has initiated a connection with the proxy but before the proxy
* authenticates the player with Mojang or before the player's proxy connection is fully established
* (for offline mode).
*/
public interface PreLoginEvent extends ResultedEvent<PreLoginEvent.PreLoginComponentResult> {
public interface PreLoginEvent extends ResultedEvent<ComponentResult> {
InboundConnection connection();
String username();
/**
* Represents an "allowed/allowed with forced online\offline mode/denied" result with a reason
* allowed for denial.
*/
final class PreLoginComponentResult implements Result {
boolean onlineMode();
private static final PreLoginComponentResult ALLOWED = new PreLoginComponentResult(
Result.ALLOWED, null);
private static final PreLoginComponentResult FORCE_ONLINEMODE = new PreLoginComponentResult(
Result.FORCE_ONLINE, null);
private static final PreLoginComponentResult FORCE_OFFLINEMODE = new PreLoginComponentResult(
Result.FORCE_OFFLINE, null);
void setOnlineMode(boolean onlineMode);
private final Result result;
private final Component reason;
default void allow() {
setResult(ComponentResult.allowed());
}
private PreLoginComponentResult(Result result,
@Nullable Component reason) {
this.result = result;
this.reason = reason;
}
@Override
public boolean isAllowed() {
return result != Result.DISALLOWED;
}
public Optional<Component> denialReason() {
return Optional.ofNullable(reason);
}
public boolean isOnlineModeAllowed() {
return result == Result.FORCE_ONLINE;
}
public boolean isForceOfflineMode() {
return result == Result.FORCE_OFFLINE;
}
@Override
public String toString() {
switch (result) {
case ALLOWED:
return "allowed";
case FORCE_OFFLINE:
return "allowed with force offline mode";
case FORCE_ONLINE:
return "allowed with online mode";
default:
return "denied";
}
}
/**
* Returns a result indicating the connection will be allowed through the proxy.
*
* @return the allowed result
*/
public static PreLoginComponentResult allowed() {
return ALLOWED;
}
/**
* Returns a result indicating the connection will be allowed through the proxy, but the
* connection will be forced to use online mode provided that the proxy is in offline mode. This
* acts similarly to {@link #allowed()} on an online-mode proxy.
*
* @return the result
*/
public static PreLoginComponentResult forceOnlineMode() {
return FORCE_ONLINEMODE;
}
/**
* Returns a result indicating the connection will be allowed through the proxy, but the
* connection will be forced to use offline mode even when the proxy is running in online mode.
*
* @return the result
*/
public static PreLoginComponentResult forceOfflineMode() {
return FORCE_OFFLINEMODE;
}
/**
* Denies the login with the specified reason.
*
* @param reason the reason for disallowing the connection
* @return a new result
*/
public static PreLoginComponentResult denied(Component reason) {
Preconditions.checkNotNull(reason, "reason");
return new PreLoginComponentResult(Result.DISALLOWED, reason);
}
private enum Result {
ALLOWED,
FORCE_ONLINE,
FORCE_OFFLINE,
DISALLOWED
}
default void reject(Component component) {
setResult(ComponentResult.denied(component));
}
}

Datei anzeigen

@ -20,17 +20,20 @@ public final class PreLoginEventImpl implements PreLoginEvent {
private final InboundConnection connection;
private final String username;
private PreLoginComponentResult result;
private ComponentResult result;
private boolean onlineMode;
/**
* Creates a new instance.
* @param connection the connection logging into the proxy
* @param username the player's username
* @param onlineMode whether or not the connection is online mode
*/
public PreLoginEventImpl(InboundConnection connection, String username) {
public PreLoginEventImpl(InboundConnection connection, String username, boolean onlineMode) {
this.connection = Preconditions.checkNotNull(connection, "connection");
this.username = Preconditions.checkNotNull(username, "username");
this.result = PreLoginComponentResult.allowed();
this.onlineMode = onlineMode;
this.result = ComponentResult.allowed();
}
@Override
@ -44,12 +47,22 @@ public final class PreLoginEventImpl implements PreLoginEvent {
}
@Override
public PreLoginComponentResult result() {
public boolean onlineMode() {
return this.onlineMode;
}
@Override
public void setOnlineMode(boolean onlineMode) {
this.onlineMode = onlineMode;
}
@Override
public ComponentResult result() {
return result;
}
@Override
public void setResult(@NonNull PreLoginComponentResult result) {
public void setResult(@NonNull ComponentResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}

Datei anzeigen

@ -37,6 +37,10 @@ public interface ServerPreConnectEvent extends ResultedEvent<ServerPreConnectEve
*/
RegisteredServer originalTarget();
default void reject() {
setResult(ServerResult.DENIED);
}
/**
* Represents the result of the {@link ServerPreConnectEvent}.
*/

Datei anzeigen

@ -25,6 +25,7 @@ import static com.velocitypowered.proxy.util.EncryptionUtils.decryptRsa;
import static com.velocitypowered.proxy.util.EncryptionUtils.generateServerId;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent.ComponentResult;
import com.velocitypowered.api.event.permission.PermissionsSetupEventImpl;
import com.velocitypowered.api.event.player.DisconnectEvent.LoginStatus;
import com.velocitypowered.api.event.player.DisconnectEventImpl;
@ -35,7 +36,6 @@ import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent;
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEventImpl;
import com.velocitypowered.api.event.player.PostLoginEventImpl;
import com.velocitypowered.api.event.player.PreLoginEvent;
import com.velocitypowered.api.event.player.PreLoginEvent.PreLoginComponentResult;
import com.velocitypowered.api.event.player.PreLoginEventImpl;
import com.velocitypowered.api.permission.PermissionFunction;
import com.velocitypowered.api.proxy.server.RegisteredServer;
@ -180,7 +180,8 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
if (login == null) {
throw new IllegalStateException("No ServerLogin packet received yet.");
}
PreLoginEvent event = new PreLoginEventImpl(inbound, login.getUsername());
PreLoginEvent event = new PreLoginEventImpl(inbound, login.getUsername(),
server.configuration().isOnlineMode());
server.eventManager().fire(event)
.thenRunAsync(() -> {
if (mcConnection.isClosed()) {
@ -188,16 +189,15 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
return;
}
PreLoginComponentResult result = event.result();
Optional<Component> disconnectReason = result.denialReason();
ComponentResult result = event.result();
Optional<Component> disconnectReason = result.reason();
if (disconnectReason.isPresent()) {
// The component is guaranteed to be provided if the connection was denied.
inbound.disconnect(disconnectReason.get());
return;
}
if (!result.isForceOfflineMode() && (server.configuration().isOnlineMode() || result
.isOnlineModeAllowed())) {
if (event.onlineMode()) {
// Request encryption.
ClientboundEncryptionRequestPacket request = generateEncryptionRequest();
this.verify = Arrays.copyOf(request.getVerifyToken(), 4);