API changes

- Removed static methods and singletons
This commit is contained in:
4drian3d 2022-02-12 10:31:50 -05:00
parent 5fead6090c
commit 47c7e157c9
11 changed files with 76 additions and 71 deletions

View File

@ -1,6 +1,7 @@
package com.glyart.authmevelocity.proxy; package com.glyart.authmevelocity.proxy;
import com.glyart.authmevelocity.proxy.config.AuthMeConfig; import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
import com.glyart.authmevelocity.proxy.config.AuthMeConfig.Config;
import com.glyart.authmevelocity.proxy.listener.FastLoginListener; import com.glyart.authmevelocity.proxy.listener.FastLoginListener;
import com.glyart.authmevelocity.proxy.listener.PluginMessageListener; import com.glyart.authmevelocity.proxy.listener.PluginMessageListener;
import com.glyart.authmevelocity.proxy.listener.ProxyListener; import com.glyart.authmevelocity.proxy.listener.ProxyListener;
@ -11,12 +12,12 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -24,30 +25,29 @@ public class AuthMeVelocityPlugin {
private final 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; private final AuthmeVelocityAPI api;
Config config = null;
protected static final Set<UUID> loggedPlayers = Collections.synchronizedSet(new HashSet<>()); protected final Set<UUID> loggedPlayers = Collections.<UUID>synchronizedSet(new HashSet<>());
@Inject @Inject
public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) { public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) {
plugin = this;
this.proxy = proxy; this.proxy = proxy;
this.logger = logger; this.logger = logger;
this.pluginDirectory = dataDirectory; this.pluginDirectory = dataDirectory;
this.api = new AuthmeVelocityAPI(this);
} }
@Subscribe @Subscribe
public void onProxyInitialize(ProxyInitializeEvent event) { public void onProxyInitialization(ProxyInitializeEvent event) {
AuthMeConfig.loadConfig(pluginDirectory, logger); this.config = Objects.requireNonNull(new AuthMeConfig().loadConfig(pluginDirectory, logger), "configuration cannot be null");
@NotNull var config = AuthMeConfig.getConfig();
proxy.getChannelRegistrar().register( proxy.getChannelRegistrar().register(MinecraftChannelIdentifier.create("authmevelocity", "main"));
MinecraftChannelIdentifier.create("authmevelocity", "main")); proxy.getEventManager().register(this, new ProxyListener(config, api));
proxy.getEventManager().register(this, new ProxyListener(config)); proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api));
proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config));
if(proxy.getPluginManager().isLoaded("fastlogin")){ if(proxy.getPluginManager().isLoaded("fastlogin")){
proxy.getEventManager().register(this, new FastLoginListener(proxy)); proxy.getEventManager().register(this, new FastLoginListener(proxy, api));
} }
logger.info("-- AuthMeVelocity enabled --"); logger.info("-- AuthMeVelocity enabled --");
@ -61,7 +61,7 @@ public class AuthMeVelocityPlugin {
return this.proxy; return this.proxy;
} }
public static AuthMeVelocityPlugin getInstance(){ public AuthmeVelocityAPI getAPI(){
return plugin; return this.api;
} }
} }

View File

@ -1,9 +1,9 @@
package com.glyart.authmevelocity.proxy; package com.glyart.authmevelocity.proxy;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate; import java.util.function.Predicate;
import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
@ -11,17 +11,21 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* APi provided to interact with logged players * API provided to interact with logged players
*/ */
public class AuthmeVelocityAPI { public final class AuthmeVelocityAPI {
private final AuthMeVelocityPlugin plugin;
AuthmeVelocityAPI(AuthMeVelocityPlugin plugin){
this.plugin = plugin;
}
/** /**
* 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(@NotNull Player player){ public boolean isLogged(@NotNull Player player){
final UUID playerUUID = player.getUniqueId(); final UUID playerUUID = player.getUniqueId();
return AuthMeVelocityPlugin.loggedPlayers.contains(playerUUID); return plugin.loggedPlayers.contains(playerUUID);
} }
/** /**
@ -29,9 +33,9 @@ public class AuthmeVelocityAPI {
* @param player the new logged player * @param player the new logged player
* @return if the player was succesfully added * @return if the player was succesfully added
*/ */
public static boolean addPlayer(@NotNull Player player){ public boolean addPlayer(@NotNull Player player){
final UUID playerUUID = player.getUniqueId(); final UUID playerUUID = player.getUniqueId();
return AuthMeVelocityPlugin.loggedPlayers.add(playerUUID); return plugin.loggedPlayers.add(playerUUID);
} }
/** /**
@ -39,20 +43,17 @@ public class AuthmeVelocityAPI {
* @param player the unlogged player * @param player the unlogged player
* @return if the player was succesfully removed * @return if the player was succesfully removed
*/ */
public static boolean removePlayer(@NotNull Player player){ public boolean removePlayer(@NotNull Player player){
final UUID playerUUID = player.getUniqueId(); final UUID playerUUID = player.getUniqueId();
return AuthMeVelocityPlugin.loggedPlayers.remove(playerUUID); return plugin.loggedPlayers.remove(playerUUID);
} }
/** /**
* 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(@NotNull Predicate<Player> predicate){ public void removePlayerIf(@NotNull Predicate<Player> predicate){
AuthMeVelocityPlugin.loggedPlayers.stream() plugin.loggedPlayers.removeIf(uuid -> predicate.test(plugin.getProxy().getPlayer(uuid).orElseThrow()));
.map(uuid -> AuthMeVelocityPlugin.getInstance().getProxy().getPlayer(uuid).orElseThrow())
.filter(predicate)
.forEach(player -> AuthMeVelocityPlugin.loggedPlayers.remove(player.getUniqueId()));
} }
/** /**
@ -60,8 +61,8 @@ public class AuthmeVelocityAPI {
* @param player the player * @param player the player
* @return if the player is on a login server * @return if the player is on a login server
*/ */
public static boolean isInAuthServer(@NotNull Player player){ public boolean isInAuthServer(@NotNull Player player){
var connection = player.getCurrentServer(); Optional<ServerConnection> connection = player.getCurrentServer();
return connection.isPresent() && isAuthServer(connection.get()); return connection.isPresent() && isAuthServer(connection.get());
} }
@ -70,8 +71,8 @@ public class AuthmeVelocityAPI {
* @param server the server * @param server the server
* @return if the server is a login server * @return if the server is a login server
*/ */
public static boolean isAuthServer(@NotNull RegisteredServer server){ public boolean isAuthServer(@NotNull RegisteredServer server){
return AuthMeConfig.getConfig().getAuthServers().contains(server.getServerInfo().getName()); return plugin.config.getAuthServers().contains(server.getServerInfo().getName());
} }
/** /**
@ -79,9 +80,7 @@ public class AuthmeVelocityAPI {
* @param connection the connection * @param connection the connection
* @return if the connection is made from a login server * @return if the connection is made from a login server
*/ */
public static boolean isAuthServer(@NotNull ServerConnection connection){ public boolean isAuthServer(@NotNull ServerConnection connection){
return AuthMeConfig.getConfig().getAuthServers().contains(connection.getServerInfo().getName()); return plugin.config.getAuthServers().contains(connection.getServerInfo().getName());
} }
private AuthmeVelocityAPI(){}
} }

View File

@ -2,6 +2,7 @@ package com.glyart.authmevelocity.proxy.config;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,15 +14,13 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment; import org.spongepowered.configurate.objectmapping.meta.Comment;
public class AuthMeConfig { public class AuthMeConfig {
private static final String HEADER = "AuthmeVelocity Proxy\n\nOriginal Developer: xQuickGlare\nCurrent Developer: 4drian3d"; public Config loadConfig(@NotNull Path path, @NotNull Logger logger){
private static final HoconConfigurationLoader.Builder configBuilder = HoconConfigurationLoader.builder() Path configPath = Objects.requireNonNull(path).resolve("config.conf");
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts .defaultOptions(opts -> opts
.shouldCopyDefaults(true) .shouldCopyDefaults(true)
.header(HEADER) .header("AuthmeVelocity Proxy\n\nOriginal Developer: xQuickGlare\nCurrent Developer: 4drian3d")
); )
public static void loadConfig(@NotNull Path path, @NotNull Logger logger){
Path configPath = path.resolve("config.conf");
final HoconConfigurationLoader loader = configBuilder
.path(configPath) .path(configPath)
.build(); .build();
@ -30,8 +29,10 @@ public class AuthMeConfig {
config = node.get(Config.class); config = node.get(Config.class);
node.set(Config.class, config); node.set(Config.class, config);
loader.save(node); loader.save(node);
return config;
} catch (ConfigurateException exception){ } catch (ConfigurateException exception){
logger.error("Could not load configuration: {}", exception.getMessage()); logger.error("Could not load configuration: {}", exception.getMessage());
return null;
} }
} }
@ -103,9 +104,8 @@ public class AuthMeConfig {
return this.blockedCommandMessage; return this.blockedCommandMessage;
} }
} }
private static Config config; private Config config = null;
public static Config getConfig(){ public Config getConfig(){
return config; return config;
} }
private AuthMeConfig(){}
} }

View File

@ -1,12 +1,12 @@
package com.glyart.authmevelocity.proxy.config; package com.glyart.authmevelocity.proxy.config;
import com.glyart.authmevelocity.proxy.config.AuthMeConfig.Config;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
public class ConfigUtils { public class ConfigUtils {
public static void sendBlockedMessage(Player player){ public static void sendBlockedMessage(Player player, Config config){
var config = AuthMeConfig.getConfig();
String blockedMessage = config.getCommandsConfig().getBlockedMessage(); String blockedMessage = config.getCommandsConfig().getBlockedMessage();
if(!blockedMessage.isBlank()){ if(!blockedMessage.isBlank()){
player.sendMessage( player.sendMessage(

View File

@ -7,11 +7,13 @@ import com.velocitypowered.api.proxy.ProxyServer;
public class FastLoginListener { public class FastLoginListener {
private final ProxyServer server; private final ProxyServer server;
public FastLoginListener(ProxyServer server){ private final AuthmeVelocityAPI api;
public FastLoginListener(ProxyServer server, AuthmeVelocityAPI api){
this.server = server; this.server = server;
this.api = api;
} }
@Subscribe @Subscribe
public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){ public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){
server.getPlayer(event.getProfile().getName()).ifPresent(AuthmeVelocityAPI::addPlayer); server.getPlayer(event.getProfile().getName()).ifPresent(api::addPlayer);
} }
} }

View File

@ -25,13 +25,15 @@ public class PluginMessageListener {
private final ProxyServer proxy; private final ProxyServer proxy;
private final Logger logger; private final Logger logger;
private final Random rm; private final Random rm;
private AuthMeConfig.Config config; private final AuthMeConfig.Config config;
private final AuthmeVelocityAPI api;
public PluginMessageListener(@NotNull ProxyServer proxy, @NotNull Logger logger, @NotNull AuthMeConfig.Config config) { public PluginMessageListener(@NotNull ProxyServer proxy, @NotNull Logger logger, @NotNull AuthMeConfig.Config config, AuthmeVelocityAPI api) {
this.proxy = proxy; this.proxy = proxy;
this.logger = logger; this.logger = logger;
this.rm = new Random(); this.rm = new Random();
this.config = config; this.config = config;
this.api = api;
} }
@Subscribe @Subscribe
@ -49,13 +51,13 @@ public class PluginMessageListener {
final Player loggedPlayer = connection.getPlayer(); final Player loggedPlayer = connection.getPlayer();
switch(sChannel){ switch(sChannel){
case "LOGIN" : case "LOGIN" :
if (AuthmeVelocityAPI.addPlayer(loggedPlayer)){ if (api.addPlayer(loggedPlayer)){
createServerConnectionRequest(loggedPlayer, config, proxy, logger, connection); createServerConnectionRequest(loggedPlayer, config, proxy, logger, connection);
} }
continuation.resume(); continuation.resume();
break; break;
case "LOGOUT": case "LOGOUT":
if(AuthmeVelocityAPI.removePlayer(loggedPlayer)){ if(api.removePlayer(loggedPlayer)){
proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(loggedPlayer)); proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(loggedPlayer));
} }
continuation.resume(); continuation.resume();

View File

@ -18,14 +18,16 @@ import org.jetbrains.annotations.NotNull;
public class ProxyListener { public class ProxyListener {
private AuthMeConfig.Config config; private AuthMeConfig.Config config;
private final AuthmeVelocityAPI api;
public ProxyListener(@NotNull AuthMeConfig.Config config) { public ProxyListener(@NotNull AuthMeConfig.Config config, AuthmeVelocityAPI api) {
this.config = config; this.config = config;
this.api = api;
} }
@Subscribe @Subscribe
public void onDisconnect(final DisconnectEvent event) { public void onDisconnect(final DisconnectEvent event) {
AuthmeVelocityAPI.removePlayer(event.getPlayer()); api.removePlayer(event.getPlayer());
} }
@Subscribe @Subscribe
@ -37,19 +39,19 @@ public class ProxyListener {
Player player = ((Player)event.getCommandSource()); Player player = ((Player)event.getCommandSource());
if(AuthmeVelocityAPI.isLogged(player)){ if(api.isLogged(player)){
continuation.resume(); continuation.resume();
return; return;
} }
if(AuthmeVelocityAPI.isInAuthServer(player)){ if(api.isInAuthServer(player)){
String command = AuthmeUtils.getFirstArgument(event.getCommand()); String command = AuthmeUtils.getFirstArgument(event.getCommand());
if(!config.getCommandsConfig().getAllowedCommands().contains(command)){ if(!config.getCommandsConfig().getAllowedCommands().contains(command)){
ConfigUtils.sendBlockedMessage(player); ConfigUtils.sendBlockedMessage(player, config);
event.setResult(CommandExecuteEvent.CommandResult.denied()); event.setResult(CommandExecuteEvent.CommandResult.denied());
} }
} else { } else {
ConfigUtils.sendBlockedMessage(player); ConfigUtils.sendBlockedMessage(player, config);
event.setResult(CommandExecuteEvent.CommandResult.denied()); event.setResult(CommandExecuteEvent.CommandResult.denied());
} }
continuation.resume(); continuation.resume();
@ -57,20 +59,20 @@ public class ProxyListener {
@Subscribe @Subscribe
public void onPlayerChat(final PlayerChatEvent event) { public void onPlayerChat(final PlayerChatEvent event) {
if (!AuthmeVelocityAPI.isLogged(event.getPlayer())) { if (!api.isLogged(event.getPlayer())) {
event.setResult(PlayerChatEvent.ChatResult.denied()); event.setResult(PlayerChatEvent.ChatResult.denied());
} }
} }
@Subscribe @Subscribe
public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) { public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) {
if (AuthmeVelocityAPI.isLogged(event.getPlayer())){ if (api.isLogged(event.getPlayer())){
continuation.resume(); continuation.resume();
return; return;
} }
event.getResult().getServer().ifPresent(server -> { event.getResult().getServer().ifPresent(server -> {
if(!AuthmeVelocityAPI.isAuthServer(server)){ if(!api.isAuthServer(server)){
event.setResult(ServerPreConnectEvent.ServerResult.denied()); event.setResult(ServerPreConnectEvent.ServerResult.denied());
} }
}); });
@ -79,7 +81,7 @@ public class ProxyListener {
@Subscribe @Subscribe
public EventTask onTabComplete(TabCompleteEvent event){ public EventTask onTabComplete(TabCompleteEvent event){
if (!AuthmeVelocityAPI.isLogged(event.getPlayer())){ if (!api.isLogged(event.getPlayer())){
return EventTask.async(() -> event.getSuggestions().clear()); return EventTask.async(() -> event.getSuggestions().clear());
} }
return null; return null;

View File

@ -32,7 +32,7 @@
<dependency> <dependency>
<groupId>io.papermc.paper</groupId> <groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId> <artifactId>paper-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version> <version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull;
public class PreSendLoginEvent extends PlayerEvent implements Cancellable { public class PreSendLoginEvent extends PlayerEvent implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList(); private static final HandlerList HANDLERS = new HandlerList();
private boolean isCancelled; private boolean isCancelled = false;
public PreSendLoginEvent(@NotNull final Player player) { public PreSendLoginEvent(@NotNull final Player player) {
super(player); super(player);
@ -16,7 +16,7 @@ public class PreSendLoginEvent extends PlayerEvent implements Cancellable {
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
return isCancelled; return this.isCancelled;
} }
@Override @Override

View File

@ -24,7 +24,7 @@ public class AuthMeListener implements Listener {
public void onLogin(final LoginEvent event) { public void onLogin(final LoginEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
PreSendLoginEvent preSendLoginEvent = new PreSendLoginEvent(player); PreSendLoginEvent preSendLoginEvent = new PreSendLoginEvent(player);
if(!preSendLoginEvent.callEvent()){ if(preSendLoginEvent.callEvent()){
plugin.sendMessageToProxy(player, MessageType.LOGIN); plugin.sendMessageToProxy(player, MessageType.LOGIN);
} }
} }

View File

@ -3,4 +3,4 @@ author: xQuickGlare
version: ${project.version} version: ${project.version}
main: com.glyart.authmevelocity.spigot.AuthMeVelocityPlugin main: com.glyart.authmevelocity.spigot.AuthMeVelocityPlugin
depend: [AuthMe] depend: [AuthMe]
api-version: 1.16 api-version: 1.15