Skip to content

Commit

Permalink
undo changing of target server if it fails (fixes an issue with skip-…
Browse files Browse the repository at this point in the history
…queue-server-if-possible)
  • Loading branch information
ajgeiss0702 committed May 12, 2024
1 parent 9589b9a commit 3f05f5b
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.*;

@Plugin(
id = "ajqueue",
Expand Down Expand Up @@ -154,7 +151,19 @@ public void onLeave(DisconnectEvent e) {

@Subscribe
public void onKick(KickedFromServerEvent e) {
if(!e.getPlayer().getCurrentServer().isPresent()) return; // if the player is kicked on initial join, we dont care


if(!e.getPlayer().getCurrentServer().isPresent()) {
if(changedTargetPlayers.containsKey(e.getPlayer())) {
// If the player failed to connect to the server we changed them to, redirect them to the original server
e.setResult(
KickedFromServerEvent.RedirectPlayer
.create(changedTargetPlayers.get(e.getPlayer()))
);
} else {
return; // if the player is kicked on initial join, we don't care
}
}
Optional<Component> reasonOptional = e.getServerKickReason();
main.getEventHandler().onServerKick(
new VelocityPlayer(e.getPlayer()),
Expand All @@ -165,6 +174,8 @@ public void onKick(KickedFromServerEvent e) {
);
}

private final WeakHashMap<Player, RegisteredServer> changedTargetPlayers = new WeakHashMap<>();

@Subscribe
public void onPreConnect(ServerPreConnectEvent e) {
RegisteredServer to = e.getResult().getServer().orElse(null);
Expand All @@ -175,6 +186,8 @@ public void onPreConnect(ServerPreConnectEvent e) {

if(newServer == null) return;

changedTargetPlayers.put(e.getPlayer(), to); // save the original server so we can revert it if it fails

e.setResult(ServerPreConnectEvent.ServerResult.allowed((RegisteredServer) newServer.getHandle()));
}

Expand Down

0 comments on commit 3f05f5b

Please sign in to comment.