@@ -2,10 +2,11 @@ package com.mineinabyss.idofront.features
22
33import com.github.shynixn.mccoroutine.bukkit.registerSuspendingEvents
44import com.mineinabyss.features.*
5- import com.mineinabyss.idofront.commands.brigadier.IdoRootCommand
6- import com.mineinabyss.idofront.commands.brigadier.RootIdoCommands
7- import com.mineinabyss.idofront.commands.brigadier.commands
5+ import com.mineinabyss.idofront.commands.brigadier.*
86import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
7+ import com.mineinabyss.idofront.messaging.error
8+ import com.mineinabyss.idofront.messaging.success
9+ import com.mineinabyss.idofront.plugin.Plugins
910import com.mineinabyss.idofront.plugin.unregisterListeners
1011import kotlinx.coroutines.CoroutineScope
1112import kotlinx.coroutines.Job
@@ -34,6 +35,14 @@ fun FeatureDI.listeners(vararg listeners: Listener) {
3435 }
3536}
3637
38+
39+ fun FeatureBuilder.FeatureDependenciesBuilder.plugins (vararg names : String ) {
40+ condition {
41+ val notEnabled = names.filterNot { Plugins .isEnabled(it) }
42+ require(notEnabled.isEmpty()) { " Plugin dependencies not found: $notEnabled " }
43+ }
44+ }
45+
3746fun FeatureDI.task (job : Job ) {
3847 val scope = instance<CoroutineScope >()
3948 scope.launch { job.join() }
@@ -53,9 +62,47 @@ fun FeatureBuilder.commands(block: context(DICommandContext) RootIdoCommands.()
5362 }
5463}
5564
65+ val MainCommandFeature = feature(" Main Command" ) {
66+ onLoad {
67+ plugin.commands {
68+ val main = get<MainCommand >()
69+ val manager = get<FeatureManager >()
70+ main.names.invoke {
71+ description = main.description
72+ permission = main.permission
73+ main.subcommands.forEach { subcommand ->
74+ val feature = manager.getNamed(subcommand.featureName) ? : error(" Feature name not found: ${subcommand.featureName} " )
75+ val context = DICommandContext (manager, feature)
76+ subcommand.create(context, this )
77+ }
78+
79+ if (main.reloadCommandName != null ) {
80+ main.reloadCommandName {
81+ permission = main.reloadCommandPermission
82+
83+ executes {
84+ manager.reloadAll()
85+ }
86+
87+ executes.args(
88+ " feature" to Args .string().oneOf { manager.loaded.map { it.name }.toList() }
89+ ) { featureName ->
90+ if (manager.reload(manager.getNamed(featureName) ? : fail(" Feature $featureName not found" ))) {
91+ sender.success(" Reloaded feature $featureName " )
92+ } else {
93+ sender.error(" Failed to reload feature $featureName " )
94+ }
95+ }
96+ }
97+ }
98+ }
99+ }
100+ }
101+ }
102+
56103fun FeatureBuilder.mainCommand (block : context(DICommandContext ) IdoRootCommand .() -> Unit ) {
57104 onLoad {
58- instance<MainCommand >().subcommand(block)
105+ instance<MainCommand >().subcommand(name, block)
59106 }
60107}
61108
@@ -64,10 +111,16 @@ data class MainCommand(
64111 val description : String? ,
65112 val reloadCommandName : String? = null ,
66113 val reloadCommandPermission : String? = null ,
114+ val reloadableFeatures : List <Feature <* >>? = null ,
67115 val permission : String? = null ,
68116) {
69- internal val subcommands = mutableListOf< context( DICommandContext ) IdoRootCommand .() -> Unit > ()
70- fun subcommand (block : context(DICommandContext ) IdoRootCommand .() -> Unit ) {
71- subcommands + = block
117+ internal val subcommands = mutableListOf<Subcommand >()
118+ fun subcommand (featureName : String , block : context(DICommandContext ) IdoRootCommand .() -> Unit ) {
119+ subcommands + = Subcommand (featureName, block)
72120 }
73121}
122+
123+ data class Subcommand (
124+ val featureName : String ,
125+ val create : context(DICommandContext ) IdoRootCommand .() -> Unit ,
126+ )
0 commit comments