Skip to content

Commit 913ea9e

Browse files
committed
refactor: Tidy up feature refactor
1 parent 0f1c8d5 commit 913ea9e

13 files changed

Lines changed: 210 additions & 113 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ try to keep MAJOR changes in sync with Minecraft updates.
1212
- New `idofront-services` module for interfaces implemented by this plugin using Bukkit's ServiceManager API
1313
- Start a changelog based on Keep a Changelog
1414
- `Services.register` helper function
15+
- New **command argument** helpers like offline player, options, etc...
1516

1617
### Changed
1718

18-
- SerializableItemStack uses a new service for letting other plugins register custom item types instead of manually
19+
- **Feature system** has been reworked as a DSL built around Koin for dependency injection
20+
- **SerializableItemStack** uses a new service for letting other plugins register custom item types instead of manually
1921
adding support for them
2022

2123
## [1.0.0] - 2025-08-12

idofront-commands/src/main/kotlin/com/mineinabyss/idofront/commands/brigadier/IdoRootCommand.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import org.bukkit.plugin.Plugin
88
class IdoRootCommand(
99
initial: LiteralArgumentBuilder<CommandSourceStack>,
1010
name: String,
11+
description: String?,
1112
val aliases: List<String>,
1213
plugin: Plugin,
13-
) : IdoCommand(initial, name, plugin, parentPermission = null)
14+
) : IdoCommand(initial, name, plugin, parentPermission = null) {
15+
init {
16+
this.description = description
17+
}
18+
}

idofront-commands/src/main/kotlin/com/mineinabyss/idofront/commands/brigadier/RootIdoCommands.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ class RootIdoCommands(
1515
rootCommands += IdoRootCommand(
1616
Commands.literal(this),
1717
this,
18+
description,
1819
aliases,
1920
plugin,
20-
).apply {
21-
this.description = description
22-
}.apply(init)
21+
).apply(init)
2322
}
2423

2524
/** Creates a new subcommand with aliases via a [Commands.literal] argument. */

idofront-features/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ plugins {
1111
dependencies {
1212
api(libs.koin.core)
1313
compileOnly(libs.kotlinx.serialization.json)
14+
compileOnly(libs.minecraft.mccoroutine)
1415
implementation(projects.idofrontUtil)
1516
implementation(projects.idofrontCommands)
1617
implementation(projects.idofrontLogging)

idofront-features/src/main/kotlin/com/mineinabyss/idofront/features/CommandHelpers.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

idofront-features/src/main/kotlin/com/mineinabyss/idofront/features/Configurable.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.mineinabyss.idofront.features
22

3+
import org.koin.core.Koin
34
import org.koin.core.module.Module
45
import org.koin.dsl.ScopeDSL
56

6-
data class FeatureDependencies(
7-
val features: List<Feature>,
8-
val plugins: List<String>,
9-
)
10-
117
data class Feature(
128
val name: String,
139
val dependencies: FeatureDependencies,
1410
val globalModule: Module.() -> Unit,
1511
val scopedModule: ScopeDSL.() -> Unit,
12+
val onLoad: Koin.() -> Unit,
1613
val onEnable: FeatureCreate.() -> Unit,
1714
val onDisable: FeatureCreate.() -> Unit,
1815
)

idofront-features/src/main/kotlin/com/mineinabyss/idofront/features/FeatureBuilder.kt

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.mineinabyss.idofront.features
22

33
import com.mineinabyss.idofront.commands.brigadier.IdoRootCommand
4+
import com.mineinabyss.idofront.commands.brigadier.RootIdoCommands
5+
import com.mineinabyss.idofront.commands.brigadier.commands
46
import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
7+
import org.bukkit.plugin.Plugin
58
import org.koin.core.Koin
69
import org.koin.core.module.Module
7-
import org.koin.core.scope.Scope
810
import org.koin.dsl.ScopeDSL
911

1012
class FeatureBuilder(
@@ -15,6 +17,7 @@ class FeatureBuilder(
1517
private var scopedModule: ScopeDSL.() -> Unit = {}
1618
private var onEnable: FeatureCreate.() -> Unit = {}
1719
private var onDisable: FeatureCreate.() -> Unit = {}
20+
private val onLoad: MutableList<Koin.() -> Unit> = mutableListOf()
1821

1922
class FeatureDependenciesBuilder() {
2023
private val features = mutableListOf<Feature>()
@@ -30,15 +33,6 @@ class FeatureBuilder(
3033
fun build() = FeatureDependencies(features.toList(), plugins.toList())
3134
}
3235

33-
fun build(): Feature = Feature(
34-
name = name,
35-
dependencies = dependencies,
36-
globalModule = globalModule,
37-
scopedModule = scopedModule,
38-
onEnable = onEnable,
39-
onDisable = onDisable,
40-
)
41-
4236
fun dependsOn(block: FeatureDependenciesBuilder.() -> Unit) {
4337
dependencies = FeatureDependenciesBuilder().apply(block).build()
4438
}
@@ -56,15 +50,25 @@ class FeatureBuilder(
5650
//
5751
// }
5852

59-
fun commands(block: context(Scope) IdoRootCommand.() -> Unit) {
53+
fun commands(block: context(Koin) RootIdoCommands.() -> Unit) {
54+
onLoad {
55+
get<Plugin>().commands {
56+
block(this@onLoad, this)
57+
}
58+
}
59+
}
6060

61+
fun mainCommand(block: context(Koin) IdoRootCommand.() -> Unit) {
62+
onLoad {
63+
get<MainCommand>().subcommand(block)
64+
}
6165
}
6266

63-
fun mainCommand(block: context(Scope) IdoRootCommand.() -> Unit) {
64-
// global.get<MainCommand>().root.block()
65-
TODO()
67+
fun onLoad(block: Koin.() -> Unit) {
68+
onLoad += block
6669
}
6770

71+
6872
fun onEnable(block: FeatureCreate.() -> Unit) {
6973
onEnable = block
7074
}
@@ -73,13 +77,27 @@ class FeatureBuilder(
7377
onDisable = block
7478
}
7579

76-
context(command: IdoCommandContext, scope: Scope)
77-
inline fun <reified T: Any> get(): T {
78-
return scope.get<T>()
80+
context(command: IdoCommandContext, koin: Koin)
81+
inline fun <reified T : Any> get(): T {
82+
val manager = featureManager
83+
return manager.getScope(manager.getFeature(name)!!).get<T>()
7984
}
8085

81-
context(command: IdoCommandContext, scope: Scope)
82-
val featureManager: FeatureManager get() {
83-
return scope.get<FeatureManager>()
84-
}
86+
context(command: IdoCommandContext, koin: Koin)
87+
val featureManager: FeatureManager
88+
get() {
89+
return koin.get<FeatureManager>()
90+
}
91+
92+
fun build(): Feature = Feature(
93+
name = name,
94+
dependencies = dependencies,
95+
globalModule = globalModule,
96+
scopedModule = scopedModule,
97+
onLoad = {
98+
onLoad.forEach { it() }
99+
},
100+
onEnable = onEnable,
101+
onDisable = onDisable,
102+
)
85103
}

idofront-features/src/main/kotlin/com/mineinabyss/idofront/features/FeatureDSL.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.mineinabyss.idofront.features
22

3+
import com.github.shynixn.mccoroutine.bukkit.registerSuspendingEvents
34
import com.mineinabyss.idofront.commands.brigadier.IdoRootCommand
45
import com.mineinabyss.idofront.plugin.listeners
56
import com.mineinabyss.idofront.plugin.unregisterListeners
67
import org.bukkit.event.Listener
78
import org.bukkit.plugin.Plugin
9+
import org.koin.core.Koin
810
import org.koin.core.scope.Scope
911

1012
@DslMarker
1113
annotation class FeatureDSLMarker
1214

13-
1415
@FeatureDSLMarker
1516
interface FeatureDSL
1617

@@ -19,8 +20,15 @@ fun feature(name: String, block: FeatureBuilder.() -> Unit): Feature {
1920
}
2021

2122
data class MainCommand(
22-
val root: IdoRootCommand,
23-
)
23+
val names: List<String>,
24+
val description: String?,
25+
val permission: String?,
26+
) {
27+
internal val subcommands = mutableListOf<context(Koin) IdoRootCommand.() -> Unit>()
28+
fun subcommand(block: context(Koin) IdoRootCommand.() -> Unit) {
29+
subcommands += block
30+
}
31+
}
2432

2533
class FeatureCreate(val scope: Scope) : FeatureDSL {
2634
private val plugin = scope.get<Plugin>()
@@ -29,7 +37,9 @@ class FeatureCreate(val scope: Scope) : FeatureDSL {
2937

3038
fun listeners(vararg listeners: Listener) {
3139
this.listeners += listeners
32-
plugin.listeners(*listeners)
40+
for (listener in listeners) {
41+
plugin.server.pluginManager.registerSuspendingEvents(listener, plugin)
42+
}
3343
}
3444

3545
inline fun <reified T : Any> get(): T {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.mineinabyss.idofront.features
2+
3+
data class FeatureDependencies(
4+
val features: List<Feature>,
5+
val plugins: List<String>,
6+
)

0 commit comments

Comments
 (0)