Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -25,6 +26,7 @@ public class PlayerChooseInitialServerEvent {

private final Player player;
private @Nullable RegisteredServer initialServer;
private @Nullable Component reason;

/**
* Constructs a PlayerChooseInitialServerEvent.
Expand All @@ -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() {
Expand All @@ -54,6 +57,20 @@ public void setInitialServer(@Nullable RegisteredServer server) {
this.initialServer = server;
}

public Optional<Component> 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
*/
Comment on lines +65 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to detail the functionality of the method, something like: “if a valid server is not provided as the initial server, this message will be used to disconnect the player; if it is not present or null is used, the default message will be used”

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of allowing null here, maybe we just fire the event with the default message as initial reason?

public void setReason(@Nullable Component reason) {
this.reason = reason;
}

@Override
public String toString() {
return "PlayerChooseInitialServerEvent{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,13 @@ private CompletableFuture<Void> connectToInitialServer(ConnectedPlayer player) {
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),
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();
Expand Down