diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChooseInitialServerEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChooseInitialServerEvent.java index 73d41ecdcd..8a17c2df01 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChooseInitialServerEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChooseInitialServerEvent.java @@ -12,6 +12,7 @@ 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; /** @@ -25,6 +26,7 @@ public class PlayerChooseInitialServerEvent { private final Player player; private @Nullable RegisteredServer initialServer; + private @Nullable Component reason; /** * Constructs a PlayerChooseInitialServerEvent. @@ -35,6 +37,7 @@ public class PlayerChooseInitialServerEvent { public PlayerChooseInitialServerEvent(Player player, @Nullable RegisteredServer initialServer) { this.player = Preconditions.checkNotNull(player, "player"); this.initialServer = initialServer; + this.reason = null; } public Player getPlayer() { @@ -54,6 +57,20 @@ public void setInitialServer(@Nullable RegisteredServer server) { this.initialServer = server; } + public Optional getReason() { + return Optional.ofNullable(reason); + } + + /** + * Sets a custom disconnect reason for the player. + * Passing {@code null} will show the default reason. + * + * @param reason the disconnect reason to show to the player + */ + public void setReason(@Nullable Component reason) { + this.reason = reason; + } + @Override public String toString() { return "PlayerChooseInitialServerEvent{" diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java index 0aea8d7786..f1e5ef7c12 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java @@ -262,9 +262,13 @@ private CompletableFuture connectToInitialServer(ConnectedPlayer player) { return server.getEventManager().fire(event).thenRunAsync(() -> { Optional toTry = event.getInitialServer(); if (toTry.isEmpty()) { - player.disconnect0( - Component.translatable("velocity.error.no-available-servers", NamedTextColor.RED), - true); + if (event.getReason().isPresent()) { + player.disconnect0(event.getReason().get(), true); + } else { + player.disconnect0( + Component.translatable("velocity.error.no-available-servers", NamedTextColor.RED), + true); + } return; } player.createConnectionRequest(toTry.get()).fireAndForget();