Add forced host check

This commit is contained in:
Evert Prants 2024-09-30 20:38:41 +03:00
parent 82d6c04d58
commit a92638c097
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 37 additions and 2 deletions

View File

@ -6,7 +6,7 @@ plugins {
sourceCompatibility = JavaVersion.VERSION_17
group 'com.actualplayer'
version '1.2.4'
version '1.3.0'
description 'A way for the user to login to the server he last connected to.'
repositories {

View File

@ -4,7 +4,6 @@ import com.actualplayer.rememberme.handlers.*;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
@ -12,6 +11,7 @@ import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import lombok.Getter;
import net.luckperms.api.LuckPerms;
@ -19,6 +19,7 @@ import net.luckperms.api.LuckPermsProvider;
import org.slf4j.Logger;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionException;
@ -70,6 +71,10 @@ public class RememberMe {
@Subscribe
public void onServerChooseEvent(PlayerChooseInitialServerEvent chooseServerEvent) {
if (wasDirectConnection(chooseServerEvent)) {
return;
}
// Ignore plugin when user has notransfer permission
if (!chooseServerEvent.getPlayer().hasPermission("rememberme.notransfer")) {
handler.getLastServerName(chooseServerEvent.getPlayer().getUniqueId()).thenAcceptAsync(lastServerName -> {
@ -89,6 +94,36 @@ public class RememberMe {
@Subscribe
public void onServerChange(ServerConnectedEvent serverConnectedEvent) {
if (getPlayerForcedHost(serverConnectedEvent.getPlayer()).isPresent()) {
// was a direct connection, do not save the last server name
return;
}
handler.setLastServerName(serverConnectedEvent.getPlayer().getUniqueId(), serverConnectedEvent.getServer().getServerInfo().getName());
}
public boolean wasDirectConnection(PlayerChooseInitialServerEvent chooseServerEvent) {
var connectedToHost = getPlayerForcedHost(chooseServerEvent.getPlayer());
if (connectedToHost.isEmpty()) {
return false;
}
getLogger().info("Player {} connected to a forced host ({})", chooseServerEvent.getPlayer().getUsername(), connectedToHost.get());
return true;
}
public Optional<String> getPlayerForcedHost(Player player) {
var overrides = getServer().getConfiguration().getForcedHosts();
var connectedTo = player.getVirtualHost();
if (connectedTo.isEmpty()) {
return Optional.empty();
}
var connectedToHost = connectedTo.get().getHostName();
if (!overrides.containsKey(connectedToHost)) {
return Optional.empty();
}
return Optional.of(connectedToHost);
}
}