Convert PlayerChooseInitialServerEvent into a resulted event

This commit is contained in:
Shane Freeder
2026-05-07 11:56:26 +01:00
parent ad8de4361c
commit aa9e02d79c
2 changed files with 74 additions and 15 deletions

View File

@@ -8,11 +8,17 @@
package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NonNull;
/**
* Fired when a player has finished the login process, and we need to choose the first server
@@ -21,10 +27,10 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* {@link KickedFromServerEvent} as normal.
*/
@AwaitingEvent
public class PlayerChooseInitialServerEvent {
public class PlayerChooseInitialServerEvent implements ResultedEvent<PlayerChooseInitialServerEvent.Result> {
private final Player player;
private @Nullable RegisteredServer initialServer;
private @NotNull Result result;
/**
* Constructs a PlayerChooseInitialServerEvent.
@@ -34,7 +40,7 @@ public class PlayerChooseInitialServerEvent {
*/
public PlayerChooseInitialServerEvent(Player player, @Nullable RegisteredServer initialServer) {
this.player = Preconditions.checkNotNull(player, "player");
this.initialServer = initialServer;
this.result = Allowed.of(initialServer);
}
public Player getPlayer() {
@@ -42,8 +48,11 @@ public class PlayerChooseInitialServerEvent {
}
public Optional<RegisteredServer> getInitialServer() {
if (result instanceof Allowed(RegisteredServer initialServer)) {
return Optional.ofNullable(initialServer);
}
return Optional.empty();
}
/**
* Sets the new initial server.
@@ -51,14 +60,62 @@ public class PlayerChooseInitialServerEvent {
* @param server the initial server the player should connect to
*/
public void setInitialServer(@Nullable RegisteredServer server) {
this.initialServer = server;
if (server == null) {
this.result = Denied.of();
return;
}
this.result = Allowed.of(server);
}
@Override
public String toString() {
return "PlayerChooseInitialServerEvent{"
+ "player=" + player
+ ", initialServer=" + initialServer
+ ", result=" + result
+ '}';
}
@Override
public @NonNull Result getResult() {
return this.result;
}
@Override
public void setResult(final Result result) {
this.result = Preconditions.checkNotNull(result, "result");
}
public sealed interface Result extends ResultedEvent.Result permits Allowed, Denied {
}
public record Allowed(@NotNull RegisteredServer initialServer) implements Result {
public Allowed {
Preconditions.checkNotNull(initialServer, "initialServer");
}
public static @NotNull Result of(final RegisteredServer initialServer) {
return new Allowed(initialServer);
}
@Override
public boolean isAllowed() {
return true;
}
}
public record Denied(@Nullable Component reason) implements Result {
public static @NotNull Result of(@Nullable Component reason) {
return new Denied(reason);
}
public static @NotNull Result of() {
return new Denied(null);
}
@Override
public boolean isAllowed() {
return false;
}
}
}

View File

@@ -262,14 +262,16 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
new PlayerChooseInitialServerEvent(player, initialFromConfig.orElse(null));
return server.getEventManager().fire(event).thenRunAsync(() -> {
Optional<RegisteredServer> toTry = event.getInitialServer();
if (toTry.isEmpty()) {
player.disconnect0(
Component.translatable("velocity.error.no-available-servers", NamedTextColor.RED),
final PlayerChooseInitialServerEvent.Result result = event.getResult();
if (event.getResult() instanceof PlayerChooseInitialServerEvent.Denied(Component reason)) {
player.disconnect0(Objects.requireNonNullElse(reason,
Component.translatable("velocity.error.no-available-servers", NamedTextColor.RED)),
true);
//noinspection UnnecessaryReturnStatement
return;
} else if (result instanceof PlayerChooseInitialServerEvent.Allowed(RegisteredServer initialServer)) {
player.createConnectionRequest(initialServer).fireAndForget();
}
player.createConnectionRequest(toTry.get()).fireAndForget();
}, mcConnection.eventLoop());
}