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

View File

@ -1,9 +1,9 @@
package com.glyart.authmevelocity.proxy;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
@ -11,17 +11,21 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
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
* @param player the player
* @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();
return AuthMeVelocityPlugin.loggedPlayers.contains(playerUUID);
return plugin.loggedPlayers.contains(playerUUID);
}
/**
@ -29,9 +33,9 @@ public class AuthmeVelocityAPI {
* @param player the new logged player
* @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();
return AuthMeVelocityPlugin.loggedPlayers.add(playerUUID);
return plugin.loggedPlayers.add(playerUUID);
}
/**
@ -39,20 +43,17 @@ public class AuthmeVelocityAPI {
* @param player the unlogged player
* @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();
return AuthMeVelocityPlugin.loggedPlayers.remove(playerUUID);
return plugin.loggedPlayers.remove(playerUUID);
}
/**
* Removes players who meet the established condition
* @param predicate the condition
*/
public static void removePlayerIf(@NotNull Predicate<Player> predicate){
AuthMeVelocityPlugin.loggedPlayers.stream()
.map(uuid -> AuthMeVelocityPlugin.getInstance().getProxy().getPlayer(uuid).orElseThrow())
.filter(predicate)
.forEach(player -> AuthMeVelocityPlugin.loggedPlayers.remove(player.getUniqueId()));
public void removePlayerIf(@NotNull Predicate<Player> predicate){
plugin.loggedPlayers.removeIf(uuid -> predicate.test(plugin.getProxy().getPlayer(uuid).orElseThrow()));
}
/**
@ -60,8 +61,8 @@ public class AuthmeVelocityAPI {
* @param player the player
* @return if the player is on a login server
*/
public static boolean isInAuthServer(@NotNull Player player){
var connection = player.getCurrentServer();
public boolean isInAuthServer(@NotNull Player player){
Optional<ServerConnection> connection = player.getCurrentServer();
return connection.isPresent() && isAuthServer(connection.get());
}
@ -70,8 +71,8 @@ public class AuthmeVelocityAPI {
* @param server the server
* @return if the server is a login server
*/
public static boolean isAuthServer(@NotNull RegisteredServer server){
return AuthMeConfig.getConfig().getAuthServers().contains(server.getServerInfo().getName());
public boolean isAuthServer(@NotNull RegisteredServer server){
return plugin.config.getAuthServers().contains(server.getServerInfo().getName());
}
/**
@ -79,9 +80,7 @@ public class AuthmeVelocityAPI {
* @param connection the connection
* @return if the connection is made from a login server
*/
public static boolean isAuthServer(@NotNull ServerConnection connection){
return AuthMeConfig.getConfig().getAuthServers().contains(connection.getServerInfo().getName());
public boolean isAuthServer(@NotNull ServerConnection connection){
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.util.List;
import java.util.Objects;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
@ -13,15 +14,13 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
public class AuthMeConfig {
private static final String HEADER = "AuthmeVelocity Proxy\n\nOriginal Developer: xQuickGlare\nCurrent Developer: 4drian3d";
private static final HoconConfigurationLoader.Builder configBuilder = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts
.shouldCopyDefaults(true)
.header(HEADER)
);
public static void loadConfig(@NotNull Path path, @NotNull Logger logger){
Path configPath = path.resolve("config.conf");
final HoconConfigurationLoader loader = configBuilder
public Config loadConfig(@NotNull Path path, @NotNull Logger logger){
Path configPath = Objects.requireNonNull(path).resolve("config.conf");
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts
.shouldCopyDefaults(true)
.header("AuthmeVelocity Proxy\n\nOriginal Developer: xQuickGlare\nCurrent Developer: 4drian3d")
)
.path(configPath)
.build();
@ -30,8 +29,10 @@ public class AuthMeConfig {
config = node.get(Config.class);
node.set(Config.class, config);
loader.save(node);
return config;
} catch (ConfigurateException exception){
logger.error("Could not load configuration: {}", exception.getMessage());
return null;
}
}
@ -103,9 +104,8 @@ public class AuthMeConfig {
return this.blockedCommandMessage;
}
}
private static Config config;
public static Config getConfig(){
private Config config = null;
public Config getConfig(){
return config;
}
private AuthMeConfig(){}
}

View File

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

View File

@ -7,11 +7,13 @@ import com.velocitypowered.api.proxy.ProxyServer;
public class FastLoginListener {
private final ProxyServer server;
public FastLoginListener(ProxyServer server){
private final AuthmeVelocityAPI api;
public FastLoginListener(ProxyServer server, AuthmeVelocityAPI api){
this.server = server;
this.api = api;
}
@Subscribe
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 Logger logger;
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.logger = logger;
this.rm = new Random();
this.config = config;
this.api = api;
}
@Subscribe
@ -49,13 +51,13 @@ public class PluginMessageListener {
final Player loggedPlayer = connection.getPlayer();
switch(sChannel){
case "LOGIN" :
if (AuthmeVelocityAPI.addPlayer(loggedPlayer)){
if (api.addPlayer(loggedPlayer)){
createServerConnectionRequest(loggedPlayer, config, proxy, logger, connection);
}
continuation.resume();
break;
case "LOGOUT":
if(AuthmeVelocityAPI.removePlayer(loggedPlayer)){
if(api.removePlayer(loggedPlayer)){
proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(loggedPlayer));
}
continuation.resume();

View File

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

View File

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

View File

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

View File

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

View File

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