Skip to content

Commit bab42da

Browse files
committed
feat: Event updates
1 parent 83b3207 commit bab42da

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎build.gradle.kts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "eu.sporedev"
10-
version = "10.2"
10+
version = "10.2.1"
1111

1212
repositories {
1313
mavenCentral()

‎src/main/kotlin/me/clearedSpore/sporeAPI/SporePlugin.kt‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import co.aikar.commands.MessageKeys
55
import co.aikar.commands.PaperCommandManager
66
import me.clearedSpore.sporeAPI.annotation.RegisterCommand
77
import me.clearedSpore.sporeAPI.annotation.RegisterListener
8+
import me.clearedSpore.sporeAPI.annotation.SubscribeEvents
89
import me.clearedSpore.sporeAPI.bossbar.BossBarManager
910
import me.clearedSpore.sporeAPI.command.SporeCommand
1011
import me.clearedSpore.sporeAPI.command.SporeCommandManager
@@ -131,6 +132,9 @@ open class SporePlugin : JavaPlugin() {
131132
Logger.info("Scanning for listeners...")
132133
scanAndRegisterListeners()
133134

135+
Logger.info("Scanning for event subscribers...")
136+
scanAndRegisterEventSubscribers()
137+
134138
Logger.info("Registering module commands...")
135139

136140
var moduleCount = 0
@@ -288,6 +292,38 @@ open class SporePlugin : JavaPlugin() {
288292
Logger.info("Auto-registered $count listener(s)")
289293
}
290294

295+
private fun scanAndRegisterEventSubscribers() {
296+
val methods = SporeScanner.methodsAnnotatedWith(this, SubscribeEvents::class.java)
297+
var count = 0
298+
299+
methods.forEach { method ->
300+
try {
301+
if (method.parameterCount != 0) {
302+
Logger.warn(
303+
"Method ${method.declaringClass.simpleName}#${method.name} is annotated with " +
304+
"@SubscribeEvents but takes parameters, so it cannot be auto-invoked"
305+
)
306+
return@forEach
307+
}
308+
309+
val instance = SporeScanner.objectInstanceOrNull(method.declaringClass)
310+
?: method.declaringClass.getDeclaredConstructor().apply { isAccessible = true }.newInstance()
311+
312+
method.isAccessible = true
313+
method.invoke(instance)
314+
count++
315+
316+
} catch (e: Exception) {
317+
IncidentReporter.report(
318+
"event.subscribe", e,
319+
details = mapOf("class" to method.declaringClass.name, "method" to method.name)
320+
)
321+
}
322+
}
323+
324+
Logger.info("Auto-invoked $count event subscriber method(s)")
325+
}
326+
291327
private fun kotlinObjectInstanceOrNull(clazz: Class<*>): Any? {
292328
return try {
293329
val field = clazz.getDeclaredField("INSTANCE")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.clearedSpore.sporeAPI.annotation
2+
3+
// Copyright (c) 2025 ClearedSpore
4+
// Licensed under the MIT License. See LICENSE file in the project root for details.
5+
6+
7+
@Target(AnnotationTarget.FUNCTION)
8+
@Retention(AnnotationRetention.RUNTIME)
9+
annotation class SubscribeEvents

‎src/main/kotlin/me/clearedSpore/sporeAPI/scan/SporeScanner.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ object SporeScanner {
2727
fun annotatedWith(plugin: JavaPlugin, annotation: Class<out Annotation>): Set<Class<*>> =
2828
forPlugin(plugin).getTypesAnnotatedWith(annotation)
2929

30+
fun methodsAnnotatedWith(plugin: JavaPlugin, annotation: Class<out Annotation>): Set<java.lang.reflect.Method> =
31+
forPlugin(plugin).getMethodsAnnotatedWith(annotation)
32+
3033
fun invalidate(plugin: JavaPlugin) {
3134
cache.remove(plugin.name)
3235
}
@@ -53,7 +56,7 @@ object SporeScanner {
5356
configureVfs()
5457

5558
val builder = ConfigurationBuilder()
56-
.setScanners(Scanners.TypesAnnotated, Scanners.SubTypes)
59+
.setScanners(Scanners.TypesAnnotated, Scanners.SubTypes, Scanners.MethodsAnnotated)
5760
.addClassLoaders(plugin.javaClass.classLoader)
5861

5962
val jarUrl = jarUrlOf(plugin)

0 commit comments

Comments
 (0)