diff --git a/build.gradle.kts b/build.gradle.kts index 09f0dbd..426da76 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ repositories { allprojects { group = "pl.spcode.navauth" - version = "0.1.4-SNAPSHOT" + version = "0.1.5-SNAPSHOT" } tasks.register("formatAll") { diff --git a/navauth-common/build.gradle.kts b/navauth-common/build.gradle.kts index 18e659e..57d95ae 100644 --- a/navauth-common/build.gradle.kts +++ b/navauth-common/build.gradle.kts @@ -6,8 +6,8 @@ dependencies { api(project(":navauth-api")) // EternalCode Multification - api("com.eternalcode:multification-core:1.2.2") - api("com.eternalcode:multification-okaeri:1.2.2") + api("com.eternalcode:multification-core:1.2.3") + api("com.eternalcode:multification-okaeri:1.2.3") implementation("com.google.inject:guice:7.0.0") api("com.google.code.gson:gson:2.13.2") @@ -17,7 +17,7 @@ dependencies { api("com.j256.ormlite:ormlite-jdbc:6.1") // config - api("eu.okaeri:okaeri-configs-yaml-snakeyaml:6.0.0-beta.3") + api("eu.okaeri:okaeri-configs-yaml-snakeyaml:6.0.0-beta.27") // crypto implementation("at.favre.lib:bcrypt:0.10.2") diff --git a/navauth-velocity/src/main/java/pl/spcode/navauth/velocity/Bootstrap.java b/navauth-velocity/src/main/java/pl/spcode/navauth/velocity/Bootstrap.java index 0a562a2..83275e8 100644 --- a/navauth-velocity/src/main/java/pl/spcode/navauth/velocity/Bootstrap.java +++ b/navauth-velocity/src/main/java/pl/spcode/navauth/velocity/Bootstrap.java @@ -34,13 +34,18 @@ "Minecraft login plugin built for speed, security, and seamless player authentication.", authors = {"Navio1430"}) public class Bootstrap { - @Inject private Logger logger; - @Inject private Injector injector; - @Subscribe - void onProxyInitialization(final ProxyInitializeEvent event) { + private static NavAuthVelocity instance; + + @Inject + private Bootstrap(Logger logger, Injector injector) { logger.info("Bootstrapping velocity plugin..."); - NavAuthVelocity instance = injector.getInstance(NavAuthVelocity.class); - instance.init(event, this); + instance = injector.getInstance(NavAuthVelocity.class); + instance.init(); + } + + @Subscribe + private void onProxyInitializeEvent(ProxyInitializeEvent event) { + instance.onProxyInitializeEvent(this); } } diff --git a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/NavAuthVelocity.kt b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/NavAuthVelocity.kt index eab56e9..f6817eb 100644 --- a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/NavAuthVelocity.kt +++ b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/NavAuthVelocity.kt @@ -24,7 +24,6 @@ import com.google.inject.Key import com.google.inject.Singleton import com.velocitypowered.api.command.CommandSource import com.velocitypowered.api.event.Subscribe -import com.velocitypowered.api.event.proxy.ProxyInitializeEvent import com.velocitypowered.api.event.proxy.ProxyShutdownEvent import com.velocitypowered.api.plugin.annotation.DataDirectory import com.velocitypowered.api.proxy.ProxyServer @@ -68,26 +67,19 @@ constructor( val proxyServer: ProxyServer, @param:DataDirectory val dataDirectory: Path, val metricsFactory: Metrics.Factory, -) { +) : VelocityPluginProvider { private val logger: Logger = LoggerFactory.getLogger(NavAuthVelocity::class.java) - lateinit var pluginInstance: Bootstrap + // chicken or egg problem, unfortunately + var pluginInstance: Bootstrap? = null lateinit var injector: Injector lateinit var liteCommands: LiteCommands - fun init(event: ProxyInitializeEvent, pluginInstance: Bootstrap) { + fun init() { try { logger.info("Initializing NavAuth plugin...") - this.pluginInstance = pluginInstance - - // initialize bstats - val pluginId = 28777 - metricsFactory.make(pluginInstance, pluginId) - - // register self as listener because of the shutdown event - proxyServer.eventManager.register(pluginInstance, this) val generalConfigModule = YamlConfigModule( @@ -113,7 +105,7 @@ constructor( EventsModule(), VelocityMultificationsModule(velocityViewerProvider), VelocityCommandsModule(), - SchedulerModule(pluginInstance, proxyServer.scheduler), + SchedulerModule(this, proxyServer.scheduler), HttpClientModule(), DataPersistenceModule(), ServicesModule(), @@ -128,6 +120,19 @@ constructor( val apiImpl = injector.getInstance(NavAuthApiImpl::class.java) NavAuthAPI.setAPIInstance(apiImpl) + } catch (ex: Exception) { + logger.error("Could not initialize NavAuth plugin, shutting down the server...", ex) + proxyServer.shutdown(Component.text("NavAuth initialization failure")) + } + } + + fun onProxyInitializeEvent(pluginInstance: Bootstrap) { + try { + this.pluginInstance = pluginInstance + + // initialize bstats + val pluginId = 28777 + metricsFactory.make(pluginInstance, pluginId) registerListeners(injector) registerCommands(injector) @@ -165,16 +170,24 @@ constructor( } fun registerListeners(injector: Injector) { + // register self as listener because of the shutdown event + proxyServer.eventManager.register(pluginInstance, this) + val listeners = VelocityListenersRegistry.getWithInjection(injector) listeners.forEach { proxyServer.eventManager.register(pluginInstance, it) } } @Suppress("UNNECESSARY_SAFE_CALL") @Subscribe - fun shutdown(proxyShutdownEvent: ProxyShutdownEvent) { + fun shutdown(event: ProxyShutdownEvent) { injector?.getInstance(DatabaseManager::class.java)?.closeConnections() liteCommands?.unregister() logger.info("Goodbye!") } + + override fun provideInstance(): Bootstrap { + require(pluginInstance != null) { "plugin was not initialized yet" } + return pluginInstance!! + } } diff --git a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/VelocityPluginProvider.kt b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/VelocityPluginProvider.kt new file mode 100644 index 0000000..980db74 --- /dev/null +++ b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/VelocityPluginProvider.kt @@ -0,0 +1,23 @@ +/* + * NavAuth + * Copyright © 2026 Oliwier Fijas (Navio1430) + * + * NavAuth is free software; You can redistribute it and/or modify it under the terms of: + * the GNU Affero General Public License version 3 as published by the Free Software Foundation. + * + * NavAuth is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with NavAuth. If not, see + * and navigate to version 3 of the GNU Affero General Public License. + * + */ + +package pl.spcode.navauth.velocity + +interface VelocityPluginProvider { + fun provideInstance(): Bootstrap +} diff --git a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/module/SchedulerModule.kt b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/module/SchedulerModule.kt index 7e7a76e..d2ef143 100644 --- a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/module/SchedulerModule.kt +++ b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/module/SchedulerModule.kt @@ -20,12 +20,13 @@ package pl.spcode.navauth.velocity.module import com.google.inject.AbstractModule import com.velocitypowered.api.scheduler.Scheduler -import pl.spcode.navauth.velocity.Bootstrap +import pl.spcode.navauth.velocity.VelocityPluginProvider import pl.spcode.navauth.velocity.scheduler.NavAuthScheduler -class SchedulerModule(val plugin: Bootstrap, val scheduler: Scheduler) : AbstractModule() { +class SchedulerModule(val pluginProvider: VelocityPluginProvider, val scheduler: Scheduler) : + AbstractModule() { override fun configure() { - bind(NavAuthScheduler::class.java).toInstance(NavAuthScheduler(plugin, scheduler)) + bind(NavAuthScheduler::class.java).toInstance(NavAuthScheduler(pluginProvider, scheduler)) } } diff --git a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/scheduler/NavAuthScheduler.kt b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/scheduler/NavAuthScheduler.kt index 676d04d..e9baaaf 100644 --- a/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/scheduler/NavAuthScheduler.kt +++ b/navauth-velocity/src/main/kotlin/pl/spcode/navauth/velocity/scheduler/NavAuthScheduler.kt @@ -21,15 +21,18 @@ package pl.spcode.navauth.velocity.scheduler import com.velocitypowered.api.scheduler.ScheduledTask import com.velocitypowered.api.scheduler.Scheduler import java.util.function.Consumer -import pl.spcode.navauth.velocity.Bootstrap +import pl.spcode.navauth.velocity.VelocityPluginProvider -class NavAuthScheduler(private val plugin: Bootstrap, private val scheduler: Scheduler) { +class NavAuthScheduler( + private val pluginProvider: VelocityPluginProvider, + private val scheduler: Scheduler, +) { fun buildTask(runnable: Runnable): Scheduler.TaskBuilder { - return scheduler.buildTask(plugin, runnable) + return scheduler.buildTask(pluginProvider.provideInstance(), runnable) } fun buildTask(consumer: Consumer): Scheduler.TaskBuilder { - return scheduler.buildTask(plugin, consumer) + return scheduler.buildTask(pluginProvider.provideInstance(), consumer) } }