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; package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent; import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional; import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable; 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 * Fired when a player has finished the login process, and we need to choose the first server
@@ -21,20 +27,20 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* {@link KickedFromServerEvent} as normal. * {@link KickedFromServerEvent} as normal.
*/ */
@AwaitingEvent @AwaitingEvent
public class PlayerChooseInitialServerEvent { public class PlayerChooseInitialServerEvent implements ResultedEvent<PlayerChooseInitialServerEvent.Result> {
private final Player player; private final Player player;
private @Nullable RegisteredServer initialServer; private @NotNull Result result;
/** /**
* Constructs a PlayerChooseInitialServerEvent. * Constructs a PlayerChooseInitialServerEvent.
* *
* @param player the player that was connected * @param player the player that was connected
* @param initialServer the initial server selected, may be {@code null} * @param initialServer the initial server selected, may be {@code null}
*/ */
public PlayerChooseInitialServerEvent(Player player, @Nullable RegisteredServer initialServer) { public PlayerChooseInitialServerEvent(Player player, @Nullable RegisteredServer initialServer) {
this.player = Preconditions.checkNotNull(player, "player"); this.player = Preconditions.checkNotNull(player, "player");
this.initialServer = initialServer; this.result = Allowed.of(initialServer);
} }
public Player getPlayer() { public Player getPlayer() {
@@ -42,7 +48,10 @@ public class PlayerChooseInitialServerEvent {
} }
public Optional<RegisteredServer> getInitialServer() { public Optional<RegisteredServer> getInitialServer() {
return Optional.ofNullable(initialServer); if (result instanceof Allowed(RegisteredServer initialServer)) {
return Optional.ofNullable(initialServer);
}
return Optional.empty();
} }
/** /**
@@ -51,14 +60,62 @@ public class PlayerChooseInitialServerEvent {
* @param server the initial server the player should connect to * @param server the initial server the player should connect to
*/ */
public void setInitialServer(@Nullable RegisteredServer server) { public void setInitialServer(@Nullable RegisteredServer server) {
this.initialServer = server; if (server == null) {
this.result = Denied.of();
return;
}
this.result = Allowed.of(server);
} }
@Override @Override
public String toString() { public String toString() {
return "PlayerChooseInitialServerEvent{" return "PlayerChooseInitialServerEvent{"
+ "player=" + player + "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)); new PlayerChooseInitialServerEvent(player, initialFromConfig.orElse(null));
return server.getEventManager().fire(event).thenRunAsync(() -> { return server.getEventManager().fire(event).thenRunAsync(() -> {
Optional<RegisteredServer> toTry = event.getInitialServer(); final PlayerChooseInitialServerEvent.Result result = event.getResult();
if (toTry.isEmpty()) { if (event.getResult() instanceof PlayerChooseInitialServerEvent.Denied(Component reason)) {
player.disconnect0( player.disconnect0(Objects.requireNonNullElse(reason,
Component.translatable("velocity.error.no-available-servers", NamedTextColor.RED), Component.translatable("velocity.error.no-available-servers", NamedTextColor.RED)),
true); true);
//noinspection UnnecessaryReturnStatement
return; return;
} else if (result instanceof PlayerChooseInitialServerEvent.Allowed(RegisteredServer initialServer)) {
player.createConnectionRequest(initialServer).fireAndForget();
} }
player.createConnectionRequest(toTry.get()).fireAndForget();
}, mcConnection.eventLoop()); }, mcConnection.eventLoop());
} }