mirror of
https://github.com/PaperMC/Velocity.git
synced 2026-06-21 09:47:44 +02:00
Convert PlayerChooseInitialServerEvent into a resulted event
This commit is contained in:
@@ -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,10 +27,10 @@ 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.
|
||||||
@@ -34,7 +40,7 @@ public class PlayerChooseInitialServerEvent {
|
|||||||
*/
|
*/
|
||||||
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,8 +48,11 @@ public class PlayerChooseInitialServerEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<RegisteredServer> getInitialServer() {
|
public Optional<RegisteredServer> getInitialServer() {
|
||||||
|
if (result instanceof Allowed(RegisteredServer initialServer)) {
|
||||||
return Optional.ofNullable(initialServer);
|
return Optional.ofNullable(initialServer);
|
||||||
}
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the new initial server.
|
* Sets the new initial server.
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user