From 2308fd373403241adbde264917d9755180de1300 Mon Sep 17 00:00:00 2001 From: powercas_gamer Date: Sat, 2 Dec 2023 16:55:45 +0100 Subject: [PATCH] add bstats --- .../core/config/ConfigManager.kt | 39 ++++++++++ .../velocity/PluginLoader.java | 76 +++++++++++++++++++ .../mizule/timetriggeredperms/velocity/TTP.kt | 65 +++------------- .../velocity/listener/LuckPermsListener.kt | 6 +- 4 files changed, 129 insertions(+), 57 deletions(-) create mode 100644 core/src/main/kotlin/dev/mizule/timetriggeredperms/core/config/ConfigManager.kt create mode 100644 velocity/src/main/java/dev/mizule/timetriggeredperms/velocity/PluginLoader.java diff --git a/core/src/main/kotlin/dev/mizule/timetriggeredperms/core/config/ConfigManager.kt b/core/src/main/kotlin/dev/mizule/timetriggeredperms/core/config/ConfigManager.kt new file mode 100644 index 0000000..b9c6ce2 --- /dev/null +++ b/core/src/main/kotlin/dev/mizule/timetriggeredperms/core/config/ConfigManager.kt @@ -0,0 +1,39 @@ +package dev.mizule.timetriggeredperms.core.config + +import org.spongepowered.configurate.kotlin.extensions.get +import org.spongepowered.configurate.kotlin.objectMapperFactory +import org.spongepowered.configurate.yaml.NodeStyle +import org.spongepowered.configurate.yaml.YamlConfigurationLoader +import java.nio.file.Path +import kotlin.io.path.exists + +class ConfigManager { + + companion object { + + fun loadConfig(path: Path): Config { + val configLoader = YamlConfigurationLoader.builder() + .path(path) + .nodeStyle(NodeStyle.BLOCK) + .indent(2) + .defaultOptions { options -> + options.shouldCopyDefaults(true) + options.serializers { builder -> + builder.registerAnnotatedObjects(objectMapperFactory()) + } + } + .build() + var configNode = configLoader.load() + var config = requireNotNull(configNode.get()) { + "Could not read configuration" + } + + if (!path.exists()) { + configNode.set(config) // update the backing node to add defaults + configLoader.save(configNode) + } + + return config + } + } +} \ No newline at end of file diff --git a/velocity/src/main/java/dev/mizule/timetriggeredperms/velocity/PluginLoader.java b/velocity/src/main/java/dev/mizule/timetriggeredperms/velocity/PluginLoader.java new file mode 100644 index 0000000..a79c68b --- /dev/null +++ b/velocity/src/main/java/dev/mizule/timetriggeredperms/velocity/PluginLoader.java @@ -0,0 +1,76 @@ +package dev.mizule.timetriggeredperms.velocity; + +import com.google.inject.Inject; +import com.velocitypowered.api.event.PostOrder; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.plugin.Dependency; +import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.annotation.DataDirectory; +import com.velocitypowered.api.proxy.ProxyServer; +import dev.mizule.timetriggeredperms.core.TTPPlugin; +import dev.mizule.timetriggeredperms.core.config.Config; +import org.bstats.velocity.Metrics; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import xyz.jpenilla.gremlin.runtime.DependencyCache; +import xyz.jpenilla.gremlin.runtime.DependencyResolver; +import xyz.jpenilla.gremlin.runtime.DependencySet; +import xyz.jpenilla.gremlin.runtime.platformsupport.VelocityClasspathAppender; + +import java.nio.file.Path; + +@Plugin( + name = "TimeTriggeredPerms", + id = "timetriggeredperms", + version = "@version@", + authors = "powercas_gamer", + dependencies = {@Dependency(id = "luckperms")} +) +public class PluginLoader implements TTPPlugin { + + final Path path; + final Logger logger; + final ProxyServer server; + final Metrics.Factory metricsFactory; + + public static PluginLoader instance; + TTP ttp; + + @Inject + public PluginLoader(@DataDirectory Path dataPath, Logger logger, ProxyServer proxy, Metrics.Factory metricsFactory) { + this.path = dataPath; + this.logger = logger; + this.server = proxy; + this.metricsFactory = metricsFactory; + instance = this; + + } + + @Subscribe(order = PostOrder.LATE) + public void onInit(final ProxyInitializeEvent event) { + var deps = DependencySet.readDefault(this.getClass().getClassLoader()); + var cache = new DependencyCache(path.resolve("libraries")); + try (DependencyResolver resolver = new DependencyResolver(logger)) { + var files = resolver.resolve(deps, cache).jarFiles(); + VelocityClasspathAppender appender = new VelocityClasspathAppender(server, PluginLoader.this); + appender.append(files); + } + cache.cleanup(); + Metrics metrics = metricsFactory.make(this, 20405); + + this.ttp = new TTP(logger, server, path); + this.ttp.enable(); + } + + @Override + public @NotNull Config config() { + return this.ttp.config(); + } + + @Override + public PluginLoader plugin() { + return this; + } + +} \ No newline at end of file diff --git a/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/TTP.kt b/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/TTP.kt index 75f8024..3b48d6d 100644 --- a/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/TTP.kt +++ b/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/TTP.kt @@ -10,79 +10,36 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory import com.velocitypowered.api.proxy.ProxyServer import dev.mizule.timetriggeredperms.core.TTPPlugin import dev.mizule.timetriggeredperms.core.config.Config -import dev.mizule.timetriggeredperms.paper.listener.LuckPermsListener +import dev.mizule.timetriggeredperms.core.config.ConfigManager +import dev.mizule.timetriggeredperms.velocity.listener.LuckPermsListener +import org.bstats.velocity.Metrics import org.slf4j.Logger -import org.spongepowered.configurate.kotlin.extensions.get -import org.spongepowered.configurate.kotlin.objectMapperFactory -import org.spongepowered.configurate.yaml.NodeStyle -import org.spongepowered.configurate.yaml.YamlConfigurationLoader import xyz.jpenilla.gremlin.runtime.DependencyCache import xyz.jpenilla.gremlin.runtime.DependencyResolver import xyz.jpenilla.gremlin.runtime.DependencySet -import xyz.jpenilla.gremlin.runtime.platformsupport.PaperClasspathAppender import xyz.jpenilla.gremlin.runtime.platformsupport.VelocityClasspathAppender import java.nio.file.Path -import java.util.* -import kotlin.io.path.exists -@Plugin( - name = "TimeTriggeredPerms", - id = "timetriggeredperms", - version = "@version@", - authors = ["powercas_gamer"], - dependencies = [Dependency(id = "luckperms", optional = false)] -) class TTP @Inject constructor( logger: Logger, - @DataDirectory val dataPath: Path, val proxy: ProxyServer, -) : TTPPlugin { - - init { - val deps = DependencySet.readDefault(this.javaClass.classLoader) - val cache = DependencyCache(dataPath.resolve("libraries")) - DependencyResolver(logger).use { downloader -> - VelocityClasspathAppender(this.proxy, this).append(downloader.resolve(deps, cache).jarFiles()) - } - cache.cleanup() - } + val dataPath: Path +) : TTPPlugin { private val configPath = dataPath.resolve("permissions.yml") - private val pluginId = 20405 - - val configLoader = YamlConfigurationLoader.builder() - .path(configPath) - .nodeStyle(NodeStyle.BLOCK) - .indent(2) - .defaultOptions { options -> - options.shouldCopyDefaults(true) - options.serializers { builder -> - builder.registerAnnotatedObjects(objectMapperFactory()) - } - } - .build() - - var configNode = configLoader.load() - var config = requireNotNull(configNode.get()) { - "Could not read configuration" - } + private lateinit var config: Config - @Subscribe(order = PostOrder.LATE) - fun onProxyInitialize(event: ProxyInitializeEvent) { - if (!configPath.exists()) { - configNode.set(config) // update the backing node to add defaults - configLoader.save(configNode) - } -// Metrics(this, pluginId) - LuckPermsListener(this) + fun enable() { + this.config = ConfigManager.loadConfig(configPath) + LuckPermsListener(PluginLoader.instance) } override fun config(): Config { return config } - override fun plugin(): TTP { - return this + override fun plugin(): PluginLoader { + return PluginLoader.instance; } } \ No newline at end of file diff --git a/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/listener/LuckPermsListener.kt b/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/listener/LuckPermsListener.kt index 3bc1504..e60115a 100644 --- a/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/listener/LuckPermsListener.kt +++ b/velocity/src/main/kotlin/dev/mizule/timetriggeredperms/velocity/listener/LuckPermsListener.kt @@ -22,19 +22,19 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package dev.mizule.timetriggeredperms.paper.listener +package dev.mizule.timetriggeredperms.velocity.listener import com.google.inject.Inject import com.velocitypowered.api.proxy.ProxyServer import dev.mizule.timetriggeredperms.core.TTPPlugin import dev.mizule.timetriggeredperms.core.config.PermissionThing import dev.mizule.timetriggeredperms.core.listener.AbstractLuckPermsListener -import dev.mizule.timetriggeredperms.velocity.TTP +import dev.mizule.timetriggeredperms.velocity.PluginLoader import net.luckperms.api.event.node.NodeRemoveEvent import net.luckperms.api.model.user.User import net.luckperms.api.node.types.PermissionNode -class LuckPermsListener(private val plugin: TTPPlugin) : AbstractLuckPermsListener(plugin) { +class LuckPermsListener(private val plugin: TTPPlugin) : AbstractLuckPermsListener(plugin) { @Inject lateinit var proxy: ProxyServer