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,20 +27,20 @@ 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.
*
* @param player the player that was connected
* @param player the player that was connected
* @param initialServer the initial server selected, may be {@code null}
*/
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,7 +48,10 @@ public class PlayerChooseInitialServerEvent {
}
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
*/
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
+ '}';
+ "player=" + player
+ ", 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;
}
}
}