feat: Implement Spigot Compatibility (not tested)
fix: Fixed possible NullPointerException in case the configuration could not be loaded
This commit is contained in:
parent
a9dc37584c
commit
47d4b9cb77
@ -10,7 +10,6 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly("org.spongepowered:configurate-hocon:4.1.2")
|
compileOnly("org.spongepowered:configurate-hocon:4.1.2")
|
||||||
compileOnly("org.slf4j:slf4j-api:2.0.6")
|
|
||||||
compileOnly("net.byteflux:libby-core:1.1.5")
|
compileOnly("net.byteflux:libby-core:1.1.5")
|
||||||
compileOnly("net.kyori:adventure-api:4.12.0")
|
compileOnly("net.kyori:adventure-api:4.12.0")
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package me.adrianed.authmevelocity.common.configuration;
|
package me.adrianed.authmevelocity.common.configuration;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionException;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||||
import org.spongepowered.configurate.ConfigurateException;
|
import org.spongepowered.configurate.ConfigurateException;
|
||||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||||
@ -11,38 +11,29 @@ public class ConfigurationContainer<C> {
|
|||||||
private C config;
|
private C config;
|
||||||
private final HoconConfigurationLoader loader;
|
private final HoconConfigurationLoader loader;
|
||||||
private final Class<C> clazz;
|
private final Class<C> clazz;
|
||||||
private final Logger logger;
|
|
||||||
|
|
||||||
public ConfigurationContainer(
|
public ConfigurationContainer(
|
||||||
final C config,
|
final C config,
|
||||||
final Class<C> clazz,
|
final Class<C> clazz,
|
||||||
final HoconConfigurationLoader loader,
|
final HoconConfigurationLoader loader
|
||||||
final Logger logger
|
|
||||||
) {
|
) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.loader = loader;
|
this.loader = loader;
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
this.logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CompletableFuture<Boolean> reload() {
|
|
||||||
return this.safeReload();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public C get() {
|
public C get() {
|
||||||
return this.config;
|
return this.config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<Boolean> safeReload() {
|
public CompletableFuture<Void> reload() {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.runAsync(() -> {
|
||||||
C newConfig = null;
|
C newConfig = null;
|
||||||
try {
|
try {
|
||||||
final CommentedConfigurationNode node = loader.load();
|
final CommentedConfigurationNode node = loader.load();
|
||||||
newConfig = node.get(clazz);
|
newConfig = node.get(clazz);
|
||||||
return true;
|
|
||||||
} catch (ConfigurateException exception) {
|
} catch (ConfigurateException exception) {
|
||||||
logger.error("Could not load config.conf file", exception);
|
throw new CompletionException("Could not load config.conf file", exception);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
if (newConfig != null) {
|
if (newConfig != null) {
|
||||||
config = newConfig;
|
config = newConfig;
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
package me.adrianed.authmevelocity.common.configuration;
|
package me.adrianed.authmevelocity.common.configuration;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||||
import org.spongepowered.configurate.ConfigurateException;
|
|
||||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||||
|
|
||||||
public final class Loader {
|
public final class Loader {
|
||||||
private Loader() {}
|
private Loader() {}
|
||||||
public static <C> ConfigurationContainer<C> loadMainConfig(Path path, Class<C> clazz, Logger logger) {
|
public static <C> ConfigurationContainer<C> loadMainConfig(Path path, Class<C> clazz) throws IOException {
|
||||||
path = path.resolve("config.conf");
|
path = path.resolve("config.conf");
|
||||||
final boolean firstCreation = Files.notExists(path);
|
final boolean firstCreation = Files.notExists(path);
|
||||||
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
|
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
|
||||||
@ -24,19 +22,14 @@ public final class Loader {
|
|||||||
.path(path)
|
.path(path)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final C config;
|
|
||||||
try {
|
|
||||||
final CommentedConfigurationNode node = loader.load();
|
|
||||||
config = node.get(clazz);
|
|
||||||
if (firstCreation) {
|
|
||||||
node.set(clazz, config);
|
|
||||||
loader.save(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (ConfigurateException exception){
|
final CommentedConfigurationNode node = loader.load();
|
||||||
logger.error("Could not load config.conf file", exception);
|
final C config = node.get(clazz);
|
||||||
return null;
|
if (firstCreation) {
|
||||||
|
node.set(clazz, config);
|
||||||
|
loader.save(node);
|
||||||
}
|
}
|
||||||
return new ConfigurationContainer<>(config, clazz, loader, logger);
|
|
||||||
|
return new ConfigurationContainer<>(config, clazz, loader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
compileOnly(project(":authmevelocity-common"))
|
compileOnly(project(":authmevelocity-common"))
|
||||||
compileOnly(project(":authmevelocity-api-paper"))
|
compileOnly(project(":authmevelocity-api-paper"))
|
||||||
compileOnly("io.papermc.paper:paper-api:1.19.1-R0.1-SNAPSHOT")
|
compileOnly("io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT")
|
||||||
compileOnly("fr.xephi:authme:5.6.0-SNAPSHOT")
|
compileOnly("fr.xephi:authme:5.6.0-SNAPSHOT")
|
||||||
compileOnly("com.github.4drian3d:MiniPlaceholders:1.3.1")
|
compileOnly("com.github.4drian3d:MiniPlaceholders:1.3.1")
|
||||||
shadow("net.byteflux:libby-bukkit:1.1.5")
|
shadow("net.byteflux:libby-bukkit:1.1.5")
|
||||||
|
@ -17,6 +17,8 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import net.byteflux.libby.BukkitLibraryManager;
|
import net.byteflux.libby.BukkitLibraryManager;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public final class AuthMeVelocityPlugin extends JavaPlugin {
|
public final class AuthMeVelocityPlugin extends JavaPlugin {
|
||||||
private static final String CHANNEL = "authmevelocity:main";
|
private static final String CHANNEL = "authmevelocity:main";
|
||||||
|
|
||||||
@ -24,11 +26,15 @@ public final class AuthMeVelocityPlugin extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
final LibsManager libraries
|
new LibsManager(new BukkitLibraryManager(this)).loadLibraries();
|
||||||
= new LibsManager(new BukkitLibraryManager(this));
|
|
||||||
libraries.loadLibraries();
|
|
||||||
|
|
||||||
this.config = Loader.loadMainConfig(getDataFolder().toPath(), PaperConfiguration.class, getSLF4JLogger());
|
try {
|
||||||
|
this.config = Loader.loadMainConfig(getDataFolder().toPath(), PaperConfiguration.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
getLogger().log(Level.SEVERE, "Could not load config.conf file", e);
|
||||||
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL);
|
this.getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL);
|
||||||
this.getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, new MessageListener(this));
|
this.getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, new MessageListener(this));
|
||||||
@ -38,7 +44,7 @@ public final class AuthMeVelocityPlugin extends JavaPlugin {
|
|||||||
AuthmePlaceholders.getExpansion().register();
|
AuthmePlaceholders.getExpansion().register();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.getSLF4JLogger().info("AuthMeVelocity enabled");
|
this.getLogger().info("AuthMeVelocity enabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -46,10 +52,11 @@ public final class AuthMeVelocityPlugin extends JavaPlugin {
|
|||||||
this.getServer().getMessenger().unregisterOutgoingPluginChannel(this, CHANNEL);
|
this.getServer().getMessenger().unregisterOutgoingPluginChannel(this, CHANNEL);
|
||||||
this.getServer().getMessenger().unregisterIncomingPluginChannel(this, CHANNEL);
|
this.getServer().getMessenger().unregisterIncomingPluginChannel(this, CHANNEL);
|
||||||
|
|
||||||
this.getSLF4JLogger().info("AuthmeVelocity disabled");
|
this.getLogger().info("AuthMeVelocity disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessageToProxy(final Player player, @NotNull MessageType type, @NotNull String playername) {
|
public void sendMessageToProxy(final Player player, @NotNull MessageType type, @NotNull String playername) {
|
||||||
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||||
out.writeUTF(type.toString());
|
out.writeUTF(type.toString());
|
||||||
out.writeUTF(playername);
|
out.writeUTF(playername);
|
||||||
@ -63,13 +70,9 @@ public final class AuthMeVelocityPlugin extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigurationContainer<PaperConfiguration> config() {
|
|
||||||
return this.config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void logDebug(String debug) {
|
public void logDebug(String debug) {
|
||||||
if (config.get().debug()) {
|
if (config.get().debug()) {
|
||||||
getSLF4JLogger().info("[DEBUG] {}", debug);
|
getLogger().log(Level.INFO, "[DEBUG] {}", debug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import fr.xephi.authme.events.RegisterEvent;
|
|||||||
import fr.xephi.authme.events.UnregisterByAdminEvent;
|
import fr.xephi.authme.events.UnregisterByAdminEvent;
|
||||||
import fr.xephi.authme.events.UnregisterByPlayerEvent;
|
import fr.xephi.authme.events.UnregisterByPlayerEvent;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@ -27,9 +28,13 @@ public final class AuthMeListener implements Listener {
|
|||||||
final Player player = event.getPlayer();
|
final Player player = event.getPlayer();
|
||||||
plugin.logDebug("LoginEvent | Start");
|
plugin.logDebug("LoginEvent | Start");
|
||||||
|
|
||||||
if (new PreSendLoginEvent(player).callEvent()) {
|
// I hate this, but... Spigot compatibility ¯\_(ツ)_/¯
|
||||||
|
final var preSendLoginEvent = new PreSendLoginEvent(player);
|
||||||
|
Bukkit.getPluginManager().callEvent(preSendLoginEvent);
|
||||||
|
|
||||||
|
if (!preSendLoginEvent.isCancelled()) {
|
||||||
plugin.sendMessageToProxy(player, MessageType.LOGIN, player.getName());
|
plugin.sendMessageToProxy(player, MessageType.LOGIN, player.getName());
|
||||||
plugin.getSLF4JLogger().info("LoginEvent | PreSendLoginEvent allowed");
|
plugin.getLogger().info("LoginEvent | PreSendLoginEvent allowed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ package me.adrianed.authmevelocity.paper.listeners;
|
|||||||
import com.google.common.io.ByteArrayDataInput;
|
import com.google.common.io.ByteArrayDataInput;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
||||||
|
import me.adrianed.authmevelocity.common.MessageType;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -35,9 +37,9 @@ public class MessageListener implements PluginMessageListener {
|
|||||||
if ("main".equals(subChannel)) {
|
if ("main".equals(subChannel)) {
|
||||||
plugin.logDebug("PluginMessage | Main Subchannel");
|
plugin.logDebug("PluginMessage | Main Subchannel");
|
||||||
final String msg = input.readUTF();
|
final String msg = input.readUTF();
|
||||||
if ("LOGIN".equals(msg)) {
|
if (MessageType.LOGIN.toString().equals(msg)) {
|
||||||
plugin.logDebug("PluginMessage | Login Message");
|
plugin.logDebug("PluginMessage | Login Message");
|
||||||
new LoginByProxyEvent(player).callEvent();
|
Bukkit.getPluginManager().callEvent(new LoginByProxyEvent(player));
|
||||||
AuthMeApi.getInstance().forceLogin(player);
|
AuthMeApi.getInstance().forceLogin(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,13 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI {
|
|||||||
logger, pluginDirectory, proxy.getPluginManager(), this));
|
logger, pluginDirectory, proxy.getPluginManager(), this));
|
||||||
libraries.loadLibraries();
|
libraries.loadLibraries();
|
||||||
|
|
||||||
this.config = Loader.loadMainConfig(pluginDirectory, ProxyConfiguration.class, logger);
|
try {
|
||||||
|
this.config = Loader.loadMainConfig(pluginDirectory, ProxyConfiguration.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Could not load config.conf file", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logDebug("Loaded plugin libraries");
|
logDebug("Loaded plugin libraries");
|
||||||
|
|
||||||
final int pluginId = 16128;
|
final int pluginId = 16128;
|
||||||
@ -105,21 +111,21 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI {
|
|||||||
).forEach(listener ->
|
).forEach(listener ->
|
||||||
proxy.getEventManager().register(this, listener));
|
proxy.getEventManager().register(this, listener));
|
||||||
|
|
||||||
boolean fastlogin = proxy.getPluginManager().isLoaded("fastlogin");
|
final boolean fastlogin = proxy.getPluginManager().isLoaded("fastlogin");
|
||||||
metrics.addCustomChart(new SimplePie("fastlogin_compatibility", () -> Boolean.toString(fastlogin)));
|
metrics.addCustomChart(new SimplePie("fastlogin_compatibility", () -> Boolean.toString(fastlogin)));
|
||||||
if (fastlogin) {
|
if (fastlogin) {
|
||||||
logDebug("Register FastLogin compatibility");
|
logDebug("Register FastLogin compatibility");
|
||||||
proxy.getEventManager().register(this, new FastLoginListener(proxy, this));
|
proxy.getEventManager().register(this, new FastLoginListener(proxy, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean miniplaceholders = proxy.getPluginManager().isLoaded("miniplaceholders");
|
final boolean miniplaceholders = proxy.getPluginManager().isLoaded("miniplaceholders");
|
||||||
metrics.addCustomChart(new SimplePie("miniplaceholders_compatibility", () -> Boolean.toString(miniplaceholders)));
|
metrics.addCustomChart(new SimplePie("miniplaceholders_compatibility", () -> Boolean.toString(miniplaceholders)));
|
||||||
if (miniplaceholders) {
|
if (miniplaceholders) {
|
||||||
logDebug("Register MiniPlaceholders compatibility");
|
logDebug("Register MiniPlaceholders compatibility");
|
||||||
AuthMePlaceholders.getExpansion(this).register();
|
AuthMePlaceholders.getExpansion(this).register();
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthmeCommand.register(this, proxy.getCommandManager());
|
AuthmeCommand.register(this, proxy.getCommandManager(), logger);
|
||||||
|
|
||||||
this.sendInfoMessage();
|
this.sendInfoMessage();
|
||||||
}
|
}
|
||||||
|
@ -10,25 +10,28 @@ import com.velocitypowered.api.command.CommandSource;
|
|||||||
|
|
||||||
import me.adrianed.authmevelocity.velocity.AuthMeVelocityPlugin;
|
import me.adrianed.authmevelocity.velocity.AuthMeVelocityPlugin;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public class AuthmeCommand {
|
public class AuthmeCommand {
|
||||||
private AuthmeCommand() {}
|
private AuthmeCommand() {}
|
||||||
|
|
||||||
public static void register(AuthMeVelocityPlugin plugin, CommandManager manager) {
|
public static void register(AuthMeVelocityPlugin plugin, CommandManager manager, Logger logger) {
|
||||||
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder.<CommandSource>literal("authmevelocity")
|
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder.<CommandSource>literal("authmevelocity")
|
||||||
.requires(src -> src.hasPermission("authmevelocity.commands"))
|
.requires(src -> src.hasPermission("authmevelocity.commands"))
|
||||||
.then(LiteralArgumentBuilder.<CommandSource>literal("reload")
|
.then(LiteralArgumentBuilder.<CommandSource>literal("reload")
|
||||||
.executes(cmd -> {
|
.executes(cmd -> {
|
||||||
CommandSource source = cmd.getSource();
|
CommandSource source = cmd.getSource();
|
||||||
plugin.config().reload().thenAcceptAsync(result -> {
|
plugin.config().reload().handleAsync((v, ex) -> {
|
||||||
if(result) {
|
if (ex == null) {
|
||||||
plugin.sendInfoMessage();
|
plugin.sendInfoMessage();
|
||||||
source.sendMessage(MiniMessage.miniMessage().deserialize(
|
source.sendMessage(MiniMessage.miniMessage().deserialize(
|
||||||
"<aqua>AuthmeVelocity <green>has been successfully reloaded"));
|
"<aqua>AuthmeVelocity <green>has been successfully reloaded"));
|
||||||
} else {
|
} else {
|
||||||
source.sendMessage(MiniMessage.miniMessage().deserialize(
|
source.sendMessage(MiniMessage.miniMessage().deserialize(
|
||||||
"<dark_red>There was an error while reloading the configuration. <red>Check the server console"));
|
"<dark_red>There was an error while reloading the configuration. <red>Check the server console"));
|
||||||
|
logger.error(ex.getMessage(), ex.getCause());
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
return Command.SINGLE_SUCCESS;
|
return Command.SINGLE_SUCCESS;
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user