3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-09-29 14:40:21 +02: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 * This event is fired when a plugin message is sent to the proxy, either from a client ({@link
* Player}) or a server ({@link ServerConnection}). * Player}) or a server ({@link ServerConnection}).
*/ */
public interface PluginMessageEvent extends ResultedEvent<PluginMessageEvent.ForwardResult> { public interface PluginMessageEvent extends ResultedEvent<ResultedEvent.GenericResult> {
ChannelMessageSource source(); ChannelMessageSource source();
@ -34,36 +34,11 @@ public interface PluginMessageEvent extends ResultedEvent<PluginMessageEvent.For
ByteArrayDataInput messageAsDataInput(); ByteArrayDataInput messageAsDataInput();
/** default void setHandled(boolean handled) {
* A result determining whether or not to forward this message on. if (handled) {
*/ setResult(ResultedEvent.GenericResult.denied());
public static final class ForwardResult implements Result { } else {
setResult(ResultedEvent.GenericResult.allowed());
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;
} }
} }
} }

Datei anzeigen

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

Datei anzeigen

@ -42,6 +42,33 @@ public interface KickedFromServerEvent extends
*/ */
boolean kickedDuringServerConnect(); 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. * Represents the base interface for {@link KickedFromServerEvent} results.
*/ */

Datei anzeigen

@ -7,124 +7,31 @@
package com.velocitypowered.api.event.player; package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.ResultedEvent.ComponentResult;
import com.velocitypowered.api.proxy.connection.InboundConnection; import com.velocitypowered.api.proxy.connection.InboundConnection;
import java.util.Optional;
import net.kyori.adventure.text.Component; 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 * 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 * authenticates the player with Mojang or before the player's proxy connection is fully established
* (for offline mode). * (for offline mode).
*/ */
public interface PreLoginEvent extends ResultedEvent<PreLoginEvent.PreLoginComponentResult> { public interface PreLoginEvent extends ResultedEvent<ComponentResult> {
InboundConnection connection(); InboundConnection connection();
String username(); String username();
/** boolean onlineMode();
* Represents an "allowed/allowed with forced online\offline mode/denied" result with a reason
* allowed for denial.
*/
final class PreLoginComponentResult implements Result {
private static final PreLoginComponentResult ALLOWED = new PreLoginComponentResult( void setOnlineMode(boolean onlineMode);
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);
private final Result result; default void allow() {
private final Component reason; setResult(ComponentResult.allowed());
private PreLoginComponentResult(Result result,
@Nullable Component reason) {
this.result = result;
this.reason = reason;
} }
@Override default void reject(Component component) {
public boolean isAllowed() { setResult(ComponentResult.denied(component));
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
}
} }
} }

Datei anzeigen

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

Datei anzeigen

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