API and code improvements
This commit is contained in:
parent
f1bc19ac3d
commit
ec314e54b9
@ -18,15 +18,17 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class AuthMeVelocityPlugin {
|
||||
private static ProxyServer proxy;
|
||||
private final ProxyServer proxy;
|
||||
private final Logger logger;
|
||||
private final Path pluginDirectory;
|
||||
private static AuthMeVelocityPlugin plugin;
|
||||
|
||||
protected static final Set<UUID> loggedPlayers = Collections.synchronizedSet(new HashSet<UUID>());
|
||||
|
||||
@Inject
|
||||
public AuthMeVelocityPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
|
||||
proxy = server;
|
||||
public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) {
|
||||
plugin = this;
|
||||
this.proxy = proxy;
|
||||
this.logger = logger;
|
||||
this.pluginDirectory = dataDirectory;
|
||||
}
|
||||
@ -48,7 +50,11 @@ public class AuthMeVelocityPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
protected static ProxyServer getProxy(){
|
||||
protected ProxyServer getProxy(){
|
||||
return proxy;
|
||||
}
|
||||
|
||||
public static AuthMeVelocityPlugin getInstance(){
|
||||
return plugin;
|
||||
}
|
||||
}
|
||||
|
@ -5,13 +5,15 @@ import java.util.function.Predicate;
|
||||
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AuthmeVelocityAPI {
|
||||
/**
|
||||
* Check if the player is logged in or not
|
||||
* @param player the player
|
||||
* @return if the player is logged in or not
|
||||
*/
|
||||
public static boolean isLogged(Player player){
|
||||
public static boolean isLogged(@NotNull Player player){
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
return AuthMeVelocityPlugin.loggedPlayers.contains(playerUUID);
|
||||
}
|
||||
@ -19,19 +21,18 @@ public class AuthmeVelocityAPI {
|
||||
/**
|
||||
* Adds a player to the list of logged in players
|
||||
* @param player the new logged player
|
||||
* @return if the player was succesfully added
|
||||
*/
|
||||
public static void addPlayer(Player player){
|
||||
public static boolean addPlayer(@NotNull Player player){
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
if(!AuthmeVelocityAPI.isLogged(player)){
|
||||
AuthMeVelocityPlugin.loggedPlayers.add(playerUUID);
|
||||
}
|
||||
return AuthMeVelocityPlugin.loggedPlayers.add(playerUUID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a player from the list of logged-in players
|
||||
* @param player the unlogged player
|
||||
*/
|
||||
public static void removePlayer(Player player){
|
||||
public static void removePlayer(@NotNull Player player){
|
||||
final UUID playerUUID = player.getUniqueId();
|
||||
if(AuthmeVelocityAPI.isLogged(player)){
|
||||
AuthMeVelocityPlugin.loggedPlayers.remove(playerUUID);
|
||||
@ -42,9 +43,9 @@ public class AuthmeVelocityAPI {
|
||||
* Removes players who meet the established condition
|
||||
* @param predicate the condition
|
||||
*/
|
||||
public static void removePlayerIf(Predicate<Player> predicate){
|
||||
public static void removePlayerIf(@NotNull Predicate<Player> predicate){
|
||||
AuthMeVelocityPlugin.loggedPlayers.stream()
|
||||
.map(uuid -> AuthMeVelocityPlugin.getProxy().getPlayer(uuid).orElse(null))
|
||||
.map(uuid -> AuthMeVelocityPlugin.getInstance().getProxy().getPlayer(uuid).orElseThrow())
|
||||
.filter(predicate)
|
||||
.forEach(player -> AuthMeVelocityPlugin.loggedPlayers.remove(player.getUniqueId()));
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
@ -13,7 +14,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
public class AuthMeConfig {
|
||||
public static void loadConfig(Path path, Logger logger){
|
||||
public static void loadConfig(@NotNull Path path, @NotNull Logger logger){
|
||||
File configFile = new File(path.toFile(), "config.yml");
|
||||
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
|
||||
.defaultOptions(opts -> opts.shouldCopyDefaults(true))
|
||||
|
@ -16,8 +16,6 @@ public class FastLoginListener {
|
||||
@Subscribe
|
||||
public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){
|
||||
Optional<Player> autoLoginPlayer = server.getPlayer(event.getProfile().getName());
|
||||
if(autoLoginPlayer.isPresent()){
|
||||
AuthmeVelocityAPI.addPlayer(autoLoginPlayer.get());
|
||||
}
|
||||
autoLoginPlayer.ifPresent(AuthmeVelocityAPI::addPlayer);
|
||||
}
|
||||
}
|
||||
|
@ -51,17 +51,14 @@ public class ProxyListener {
|
||||
if (!optionalPlayer.isPresent()) return;
|
||||
|
||||
Player loggedPlayer = optionalPlayer.get();
|
||||
if (!AuthmeVelocityAPI.isLogged(loggedPlayer)){
|
||||
AuthmeVelocityAPI.addPlayer(loggedPlayer);
|
||||
|
||||
if (AuthmeVelocityAPI.addPlayer(loggedPlayer)){
|
||||
RegisteredServer loginServer = loggedPlayer.getCurrentServer().orElseThrow().getServer();
|
||||
proxy.getEventManager().fireAndForget(new ProxyLoginEvent(loggedPlayer, loginServer));
|
||||
if(config.sendToServer()){
|
||||
List<String> serverList = config.getTeleportServers();
|
||||
String randomServer = serverList.get(rm.nextInt(serverList.size()));
|
||||
Optional<RegisteredServer> optionalServer = proxy.getServer(randomServer);
|
||||
if(optionalServer.isPresent()){
|
||||
RegisteredServer serverToSend = optionalServer.get();
|
||||
optionalServer.ifPresentOrElse(serverToSend -> {
|
||||
try{
|
||||
if(!loggedPlayer.createConnectionRequest(serverToSend).connect().get().isSuccessful()){
|
||||
logger.info("Unable to connect the player {} to the server {}",
|
||||
@ -74,9 +71,7 @@ public class ProxyListener {
|
||||
serverToSend.getServerInfo().getName(),
|
||||
exception);
|
||||
}
|
||||
} else{
|
||||
logger.info("The server {} does not exist", randomServer);
|
||||
}
|
||||
}, () -> logger.info("The server {} does not exist", randomServer));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -88,15 +83,12 @@ public class ProxyListener {
|
||||
|
||||
@Subscribe
|
||||
public void onCommandExecute(final CommandExecuteEvent event) {
|
||||
if (!(event.getCommandSource() instanceof Player player)) return;
|
||||
|
||||
if (AuthmeVelocityAPI.isLogged(player)) return;
|
||||
if (!(event.getCommandSource() instanceof Player player) || AuthmeVelocityAPI.isLogged(player))
|
||||
return;
|
||||
|
||||
Optional<ServerConnection> server = player.getCurrentServer();
|
||||
boolean isAuthServer = server.isPresent() &&
|
||||
config.getAuthServers().contains(server.get().getServerInfo().getName());
|
||||
|
||||
if (isAuthServer) {
|
||||
if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) {
|
||||
event.setResult(CommandExecuteEvent.CommandResult.forwardToServer());
|
||||
} else {
|
||||
event.setResult(CommandExecuteEvent.CommandResult.denied());
|
||||
@ -130,7 +122,7 @@ public class ProxyListener {
|
||||
|
||||
@Subscribe
|
||||
public void onTabComplete(TabCompleteEvent event){
|
||||
Player player = event.getPlayer();
|
||||
final Player player = event.getPlayer();
|
||||
if (!AuthmeVelocityAPI.isLogged(player)){
|
||||
event.getSuggestions().clear();
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
"version":"${project.version}",
|
||||
"url":"https://github.com/4drian3d/AuthMeVelocity",
|
||||
"description":"This plugin adds the support for AuthMeReloaded to Velocity.",
|
||||
"authors":["xQuickGlare"],
|
||||
"authors":["xQuickGlare", "4drian3d"],
|
||||
"dependencies":[
|
||||
{
|
||||
"id":"fastlogin",
|
||||
|
Loading…
x
Reference in New Issue
Block a user