Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.

Commit 8f9023c

Browse files
author
DaRacci
committed
refactor: Plugin and Plugin Service
1 parent 188a48b commit 8f9023c

File tree

5 files changed

+134
-91
lines changed

5 files changed

+134
-91
lines changed

Minix-API/src/main/kotlin/dev/racci/minix/api/extension/ExtensionSkeleton.kt

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dev.racci.minix.api.plugin.MinixPlugin
77
import dev.racci.minix.api.plugin.logger.MinixLogger
88
import kotlinx.collections.immutable.ImmutableSet
99
import kotlinx.coroutines.CoroutineScope
10+
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
1011
import org.koin.core.qualifier.Qualifier
1112
import kotlin.reflect.KClass
1213

@@ -35,6 +36,7 @@ interface ExtensionSkeleton<P : MinixPlugin> : WithPlugin<P>, Qualifier {
3536
val name: String
3637

3738
/** The MinixLogger instance from the plugin. */
39+
@get:ScheduledForRemoval(inVersion = "4.0.0")
3840
@Deprecated("Use the WithPlugin extension instead.", ReplaceWith("WithPlugin<*>.log", "dev.racci.minix.api.extensions.ExPlugin"))
3941
val log: MinixLogger
4042

Minix-API/src/main/kotlin/dev/racci/minix/api/plugin/MinixPlugin.kt

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package dev.racci.minix.api.plugin
33
import dev.racci.minix.api.annotations.MappedPlugin
44
import dev.racci.minix.api.annotations.MinixDsl
55
import dev.racci.minix.api.data.PluginUpdater
6-
import dev.racci.minix.api.plugin.logger.MinixLogger
76
import dev.racci.minix.api.services.PluginService
7+
import dev.racci.minix.api.updater.Version
88
import dev.racci.minix.api.utils.safeCast
9-
import org.bstats.bukkit.Metrics
109
import org.bukkit.plugin.java.JavaPlugin
1110
import org.jetbrains.annotations.ApiStatus
11+
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
1212
import org.koin.core.component.get
1313
import kotlin.reflect.KClass
1414
import kotlin.reflect.full.findAnnotation
@@ -20,16 +20,20 @@ import kotlin.reflect.full.isSubclassOf
2020
* systems of Minix.
2121
*/
2222
abstract class MinixPlugin : JavaPlugin(), SusPlugin {
23-
@get:ApiStatus.Internal // Public to be used within the plugin service impl
23+
@get:ApiStatus.Internal
24+
// Public to be used within the plugin service impl
25+
@Deprecated("Useless and uses unneeded resources.")
26+
@get:ScheduledForRemoval(inVersion = "4.0.0")
2427
val annotation by lazy { this::class.findAnnotation<MappedPlugin>() }
2528

26-
override val bStatsId: Int? by lazy { annotation?.bStatsId.takeIf { it != -1 } }
27-
override val bindToKClass: KClass<out MinixPlugin>? by lazy { annotation?.bindToKClass.takeIf { it?.isSubclassOf(MinixPlugin::class) == true }.safeCast() }
29+
final override val log get() = get<PluginService>()[this].log
30+
final override val bStatsId = this::class.findAnnotation<MappedPlugin>()?.bStatsId.takeIf { it != -1 }
31+
final override val metrics get() = get<PluginService>()[this].metrics
32+
final override val version by lazy { Version(description.version) }
2833

29-
val log: MinixLogger get() = get<PluginService>()[this].log
30-
val metrics: Metrics? get() = get<PluginService>()[this].metrics
34+
override val updater: PluginUpdater? = null
3135

32-
open val updater: PluginUpdater? = null
36+
override val bindToKClass = this::class.findAnnotation<MappedPlugin>()?.bindToKClass?.takeIf { it.isSubclassOf(MinixPlugin::class) }.safeCast<KClass<out MinixPlugin>>()
3337

3438
@ApiStatus.Internal
3539
@ApiStatus.NonExtendable
@@ -50,11 +54,15 @@ abstract class MinixPlugin : JavaPlugin(), SusPlugin {
5054
}
5155

5256
@MinixDsl
57+
@Deprecated("Use the MappedPlugin annotation instead")
58+
@ScheduledForRemoval(inVersion = "4.0.0")
5359
protected suspend fun extensions(builder: suspend ExtensionsBuilder.() -> Unit) {
5460
builder(get<PluginService>()[this].extensionsBuilder)
5561
}
5662

5763
@MinixDsl
64+
@Deprecated("Use the MappedPlugin annotation instead")
65+
@ScheduledForRemoval(inVersion = "4.0.0")
5866
inner class ExtensionsBuilder {
5967

6068
@MinixDsl
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
11
package dev.racci.minix.api.plugin
22

3+
import dev.racci.minix.api.data.PluginUpdater
4+
import dev.racci.minix.api.plugin.logger.MinixLogger
5+
import dev.racci.minix.api.updater.Version
6+
import org.apiguardian.api.API
7+
import org.bstats.bukkit.Metrics
38
import org.bukkit.plugin.Plugin
9+
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
410
import org.koin.core.component.KoinComponent
511
import kotlin.reflect.KClass
612

13+
@API(status = API.Status.MAINTAINED, since = "1.0.0")
714
interface SusPlugin : Plugin, KoinComponent {
815

16+
/** The plugin's [MinixLogger] instance. */
17+
val log: MinixLogger
18+
19+
/** The plugin's bStats ID. */
920
val bStatsId: Int?
21+
22+
/** The plugin's bStats metrics instance. */
23+
val metrics: Metrics?
24+
25+
/** The plugin's version instance. */
26+
val version: Version
27+
28+
/** The plugin's auto-updater. */
29+
val updater: PluginUpdater?
30+
31+
@Deprecated("Use the MappedPlugin annotation instead.")
32+
@get:ScheduledForRemoval(inVersion = "4.0.0")
1033
val bindToKClass: KClass<out MinixPlugin>?
1134

1235
/**
1336
* This is called when the server picks up your Plugin and has begun loading it.
1437
*/
15-
suspend fun handleLoad() {}
38+
suspend fun handleLoad() = Unit
1639

1740
/**
1841
* This is called Once the Plugin is ready to accept and register
1942
* events, commands etc.
2043
*/
21-
suspend fun handleEnable() {}
44+
suspend fun handleEnable() = Unit
2245

2346
/**
2447
* This will be called after your Plugin is done loading
2548
* and Minix has finished its loading process for your Plugin.
2649
*/
27-
suspend fun handleAfterLoad() {}
50+
suspend fun handleAfterLoad() = Unit
2851

29-
/**
30-
* This will be called after your Plugin is finished enabling,
31-
* and Minix and finished all of the enable logic for your plugin.
32-
*/
33-
suspend fun handleAfterEnable() {}
52+
/** This is called after [handleEnable] and after plugin service has finished processing the plugin. */
53+
suspend fun handleAfterEnable() = Unit
3454

3555
/**
3656
* This is triggered when your Plugin is being disabled by the Server,
3757
* Please use this to clean up your Plugin to not leak resources.
3858
*/
39-
suspend fun handleDisable() {}
59+
suspend fun handleDisable() = Unit
4060
}

Minix-API/src/main/kotlin/dev/racci/minix/api/plugin/logger/MinixLogger.kt

+27-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import com.github.ajalt.mordant.rendering.TextAlign
55
import com.github.ajalt.mordant.rendering.TextColors
66
import com.github.ajalt.mordant.rendering.Whitespace
77
import com.github.ajalt.mordant.terminal.Terminal
8+
import dev.racci.minix.api.annotations.MinixInternal
89
import dev.racci.minix.api.exceptions.LevelConversionException
910
import dev.racci.minix.api.exceptions.NoTraceException
1011
import dev.racci.minix.api.utils.kotlin.fromOrdinal
1112
import dev.racci.minix.api.utils.kotlin.toSafeString
1213
import dev.racci.minix.api.utils.unsafeCast
14+
import kotlinx.atomicfu.AtomicBoolean
1315
import kotlinx.atomicfu.AtomicRef
1416
import kotlinx.atomicfu.atomic
1517
import org.apache.logging.log4j.spi.StandardLevel
@@ -19,9 +21,28 @@ import kotlin.reflect.KFunction
1921
import kotlin.reflect.full.declaredMemberFunctions
2022
import kotlin.reflect.jvm.isAccessible
2123

24+
@OptIn(MinixInternal::class)
2225
@API(status = API.Status.EXPERIMENTAL, since = "3.2.0")
2326
abstract class MinixLogger {
2427

28+
@MinixInternal
29+
private val atomicLock: AtomicBoolean = atomic(false)
30+
31+
@MinixInternal
32+
private val atomicLevel: AtomicRef<LoggingLevel>
33+
34+
val level get() = atomicLevel.value
35+
36+
@MinixInternal
37+
fun lockLevel() {
38+
this.atomicLock.compareAndSet(expect = false, update = true)
39+
}
40+
41+
@MinixInternal
42+
fun unlockLevel() {
43+
this.atomicLock.compareAndSet(expect = true, update = false)
44+
}
45+
2546
constructor(level: LoggingLevel) {
2647
this.atomicLevel = atomic(level)
2748
}
@@ -38,20 +59,21 @@ abstract class MinixLogger {
3859
this.atomicLevel = atomic(LoggingLevel.fromSlF4(level))
3960
}
4061

41-
private val atomicLevel: AtomicRef<LoggingLevel>
42-
val level get() = atomicLevel.value
43-
4462
/**
4563
* @param level The [LoggingLevel] to test.
4664
* @return `true` if the given [LoggingLevel] can display.
4765
*/
4866
fun isEnabled(level: LoggingLevel): Boolean = level.ordinal <= this.level.ordinal
4967

5068
/**
69+
* Sets the [LoggingLevel] of this logger.
70+
* If [atomicLock] is `true` this will not change the level.
71+
*
5172
* @param level the [LoggingLevel] to log at.
5273
* @return The previous [LoggingLevel] and makes a trace log, or if the same just returns.
5374
*/
5475
fun setLevel(level: LoggingLevel): LoggingLevel {
76+
if (this.atomicLock.value) return this.level
5577
if (level == this.level) return level
5678

5779
val previous = this.atomicLevel.getAndSet(level)
@@ -130,7 +152,7 @@ abstract class MinixLogger {
130152
msg: () -> Any?
131153
): RuntimeException {
132154
log(FormattedMessage(msg, scope, LoggingLevel.FATAL, t, TextColors.brightRed))
133-
return NoTraceException()
155+
return NoTraceException(cause = t)
134156
}
135157

136158
open fun trace(
@@ -176,7 +198,7 @@ abstract class MinixLogger {
176198
msg: String? = null
177199
): RuntimeException {
178200
log(FormattedMessage(msg, scope, LoggingLevel.FATAL, t, TextColors.brightRed))
179-
return NoTraceException()
201+
return NoTraceException(cause = t)
180202
}
181203

182204
/**

0 commit comments

Comments
 (0)