From f4fc07768b1d0cacf2a1586d1e20705b1f63a12a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 11 May 2021 06:40:22 -0400 Subject: [PATCH] Simplify event result structure and add some convenience methods --- .../event/connection/PluginMessageEvent.java | 37 +----- .../connection/PluginMessageEventImpl.java | 8 +- .../event/player/KickedFromServerEvent.java | 27 +++++ .../api/event/player/PreLoginEvent.java | 111 ++---------------- .../api/event/player/PreLoginEventImpl.java | 23 +++- .../event/player/ServerPreConnectEvent.java | 4 + .../client/LoginSessionHandler.java | 12 +- 7 files changed, 74 insertions(+), 148 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/event/connection/PluginMessageEvent.java b/api/src/main/java/com/velocitypowered/api/event/connection/PluginMessageEvent.java index 74b8155f0..154c1e701 100644 --- a/api/src/main/java/com/velocitypowered/api/event/connection/PluginMessageEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/connection/PluginMessageEvent.java @@ -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 { +public interface PluginMessageEvent extends ResultedEvent { ChannelMessageSource source(); @@ -34,36 +34,11 @@ public interface PluginMessageEvent extends ResultedEvent { +public interface PreLoginEvent extends ResultedEvent { 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 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)); } } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PreLoginEventImpl.java b/api/src/main/java/com/velocitypowered/api/event/player/PreLoginEventImpl.java index 37e2e0fae..72e254a95 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PreLoginEventImpl.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PreLoginEventImpl.java @@ -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"); } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java index 89840bf6b..bd5cc7daf 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java @@ -37,6 +37,10 @@ public interface ServerPreConnectEvent extends ResultedEvent { if (mcConnection.isClosed()) { @@ -188,16 +189,15 @@ public class LoginSessionHandler implements MinecraftSessionHandler { return; } - PreLoginComponentResult result = event.result(); - Optional disconnectReason = result.denialReason(); + ComponentResult result = event.result(); + Optional 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);