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