Migrate to Configurate
- Code improvements
This commit is contained in:
parent
5221b66140
commit
f1bc19ac3d
36
pom.xml
36
pom.xml
@ -38,42 +38,6 @@
|
||||
<target>16</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.1-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>de.leonhard</pattern>
|
||||
<shadedPattern>com.glyart.authmevelocity.libs.simplixstorage</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/maven/</exclude>
|
||||
<exclude>META-INF/*.MF</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
|
@ -39,10 +39,9 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.simplix-softworks</groupId>
|
||||
<artifactId>SimplixStorage</artifactId>
|
||||
<version>3.2.3</version>
|
||||
<scope>compile</scope>
|
||||
<groupId>org.spongepowered</groupId>
|
||||
<artifactId>configurate-yaml</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.games647</groupId>
|
||||
@ -59,6 +58,42 @@
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.2.1-SNAPSHOT</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.3.1-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>org.spongepowered</pattern>
|
||||
<shadedPattern>com.glyart.authmevelocity.libs.configurate</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/maven/</exclude>
|
||||
<exclude>META-INF/*.MF</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
@ -6,12 +6,12 @@ import com.glyart.authmevelocity.proxy.listener.ProxyListener;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import de.leonhard.storage.Yaml;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@ -20,14 +20,15 @@ import java.util.UUID;
|
||||
public class AuthMeVelocityPlugin {
|
||||
private static ProxyServer proxy;
|
||||
private final Logger logger;
|
||||
private static Yaml config = new Yaml("config", "plugins/AuthmeVelocity");
|
||||
private final Path pluginDirectory;
|
||||
|
||||
protected static final Set<UUID> loggedPlayers = Collections.synchronizedSet(new HashSet<UUID>());
|
||||
|
||||
@Inject
|
||||
public AuthMeVelocityPlugin(ProxyServer server, Logger logger) {
|
||||
public AuthMeVelocityPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
|
||||
proxy = server;
|
||||
this.logger = logger;
|
||||
this.pluginDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@ -38,18 +39,15 @@ public class AuthMeVelocityPlugin {
|
||||
if(proxy.getPluginManager().getPlugin("fastlogin").isPresent()){
|
||||
proxy.getEventManager().register(this, new FastLoginListener(proxy));
|
||||
}
|
||||
AuthMeConfig.defaultConfig();
|
||||
AuthMeConfig.loadConfig(pluginDirectory, logger);
|
||||
logger.info("-- AuthMeVelocity enabled --");
|
||||
logger.info("AuthServers: " + config.getList("authservers"));
|
||||
if(config.getBoolean("teleport.send-to-server-after-login")){
|
||||
logger.info("LobbyServers: " + config.getList("teleport.servers"));
|
||||
var config = AuthMeConfig.getConfig();
|
||||
logger.info("AuthServers: {}", config.getAuthServers());
|
||||
if(config.sendToServer()){
|
||||
logger.info("LobbyServers: {}", config.getTeleportServers());
|
||||
}
|
||||
}
|
||||
|
||||
public static Yaml getConfig(){
|
||||
return config;
|
||||
}
|
||||
|
||||
protected static ProxyServer getProxy(){
|
||||
return proxy;
|
||||
}
|
||||
|
@ -48,4 +48,6 @@ public class AuthmeVelocityAPI {
|
||||
.filter(predicate)
|
||||
.forEach(player -> AuthMeVelocityPlugin.loggedPlayers.remove(player.getUniqueId()));
|
||||
}
|
||||
|
||||
private AuthmeVelocityAPI(){}
|
||||
}
|
||||
|
@ -1,28 +1,67 @@
|
||||
package com.glyart.authmevelocity.proxy.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
import de.leonhard.storage.Yaml;
|
||||
public class AuthMeConfig {
|
||||
public static void loadConfig(Path path, Logger logger){
|
||||
File configFile = new File(path.toFile(), "config.yml");
|
||||
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
|
||||
.defaultOptions(opts -> opts.shouldCopyDefaults(true))
|
||||
.file(configFile)
|
||||
.build();
|
||||
|
||||
public interface AuthMeConfig {
|
||||
public static void defaultConfig(){
|
||||
Yaml config = AuthMeVelocityPlugin.getConfig();
|
||||
config.setDefault(
|
||||
"authservers",
|
||||
List.of(
|
||||
"auth1",
|
||||
"auth2"
|
||||
));
|
||||
config.setDefault(
|
||||
"teleport.send-to-server-after-login",
|
||||
false);
|
||||
config.setDefault(
|
||||
"teleport.servers",
|
||||
List.of(
|
||||
"lobby1",
|
||||
"lobby2"
|
||||
));
|
||||
try {
|
||||
CommentedConfigurationNode node = loader.load();
|
||||
config = node.get(Config.class);
|
||||
node.set(Config.class, config);
|
||||
loader.save(node);
|
||||
} catch (ConfigurateException exception){
|
||||
logger.error("Could not load configuration: {}", exception.getMessage());
|
||||
}
|
||||
}
|
||||
@ConfigSerializable
|
||||
public static class Config {
|
||||
|
||||
@Comment("List of authservers")
|
||||
private Set<String> authservers = Set.of(
|
||||
"auth1",
|
||||
"auth2"
|
||||
);
|
||||
|
||||
@Comment("Send each player to another server on login?")
|
||||
private boolean sendToServerOnLogin = false;
|
||||
|
||||
@Comment("List of teleport to servers")
|
||||
private List<String> teleportServers = List.of(
|
||||
"lobby1",
|
||||
"lobby2"
|
||||
);
|
||||
|
||||
public Set<String> getAuthServers(){
|
||||
return authservers;
|
||||
}
|
||||
|
||||
public boolean sendToServer(){
|
||||
return sendToServerOnLogin;
|
||||
}
|
||||
|
||||
public List<String> getTeleportServers(){
|
||||
return teleportServers;
|
||||
}
|
||||
}
|
||||
private static Config config;
|
||||
public static Config getConfig(){
|
||||
return config;
|
||||
}
|
||||
private AuthMeConfig(){}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.glyart.authmevelocity.proxy.listener;
|
||||
|
||||
import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin;
|
||||
import com.glyart.authmevelocity.proxy.AuthmeVelocityAPI;
|
||||
import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
|
||||
import com.glyart.authmevelocity.proxy.event.ProxyLoginEvent;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
@ -25,39 +25,41 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class ProxyListener {
|
||||
private final ProxyServer server;
|
||||
private final ProxyServer proxy;
|
||||
private final Logger logger;
|
||||
private final Random rm;
|
||||
private AuthMeConfig.Config config;
|
||||
|
||||
public ProxyListener(ProxyServer server, Logger logger) {
|
||||
this.server = server;
|
||||
public ProxyListener(ProxyServer proxy, Logger logger) {
|
||||
this.proxy = proxy;
|
||||
this.logger = logger;
|
||||
this.rm = new Random();
|
||||
this.config = AuthMeConfig.getConfig();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onPluginMessage(final PluginMessageEvent event) {
|
||||
if (!(event.getSource() instanceof ServerConnection)) return;
|
||||
|
||||
if (!event.getIdentifier().getId().equals("authmevelocity:main")) return;
|
||||
if (!(event.getSource() instanceof ServerConnection) || !event.getIdentifier().getId().equals("authmevelocity:main"))
|
||||
return;
|
||||
|
||||
ByteArrayDataInput input = event.dataAsDataStream();
|
||||
String sChannel = input.readUTF();
|
||||
if (!sChannel.equals("LOGIN")) return;
|
||||
|
||||
String user = input.readUTF();
|
||||
Optional<Player> optionalPlayer = server.getPlayer(UUID.fromString(user));
|
||||
Optional<Player> optionalPlayer = proxy.getPlayer(UUID.fromString(user));
|
||||
if (!optionalPlayer.isPresent()) return;
|
||||
|
||||
Player loggedPlayer = optionalPlayer.get();
|
||||
if (!AuthmeVelocityAPI.isLogged(loggedPlayer)){
|
||||
AuthmeVelocityAPI.addPlayer(loggedPlayer);
|
||||
|
||||
RegisteredServer loginServer = loggedPlayer.getCurrentServer().get().getServer();
|
||||
server.getEventManager().fireAndForget(new ProxyLoginEvent(loggedPlayer, loginServer));
|
||||
if(AuthMeVelocityPlugin.getConfig().getBoolean("teleport.send-to-server-after-login")){
|
||||
Random rm = new Random();
|
||||
List<String> serverList = AuthMeVelocityPlugin.getConfig().getStringList("teleport.servers");
|
||||
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 = server.getServer(randomServer);
|
||||
Optional<RegisteredServer> optionalServer = proxy.getServer(randomServer);
|
||||
if(optionalServer.isPresent()){
|
||||
RegisteredServer serverToSend = optionalServer.get();
|
||||
try{
|
||||
@ -92,7 +94,7 @@ public class ProxyListener {
|
||||
|
||||
Optional<ServerConnection> server = player.getCurrentServer();
|
||||
boolean isAuthServer = server.isPresent() &&
|
||||
AuthMeVelocityPlugin.getConfig().getList("authservers").contains(server.get().getServerInfo().getName());
|
||||
config.getAuthServers().contains(server.get().getServerInfo().getName());
|
||||
|
||||
if (isAuthServer) {
|
||||
event.setResult(CommandExecuteEvent.CommandResult.forwardToServer());
|
||||
@ -107,7 +109,7 @@ public class ProxyListener {
|
||||
if (AuthmeVelocityAPI.isLogged(player)) return;
|
||||
|
||||
Optional<ServerConnection> server = player.getCurrentServer();
|
||||
if (server.isPresent() && AuthMeVelocityPlugin.getConfig().getList("authservers").contains(server.get().getServerInfo().getName())) {
|
||||
if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -119,7 +121,7 @@ public class ProxyListener {
|
||||
if (AuthmeVelocityAPI.isLogged(event.getPlayer())) return;
|
||||
|
||||
Optional<RegisteredServer> server = event.getResult().getServer();
|
||||
if (server.isPresent() && AuthMeVelocityPlugin.getConfig().getList("authservers").contains(server.get().getServerInfo().getName())) {
|
||||
if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user