Skip to content

Commit 4fc8464

Browse files
committed
CommandAPI.kt refactor.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 910d559 commit 4fc8464

File tree

1 file changed

+33
-1
lines changed
  • src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/commands

1 file changed

+33
-1
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/commands/CommandAPI.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
package com.mairwunnx.projectessentials.core.api.v1.commands
44

5+
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI
6+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.CommandEventData
7+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.ModuleCoreEventType
8+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
9+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderType
510
import com.mojang.brigadier.CommandDispatcher
611
import com.mojang.brigadier.arguments.BoolArgumentType
712
import com.mojang.brigadier.arguments.IntegerArgumentType
@@ -11,12 +16,16 @@ import net.minecraft.command.CommandSource
1116
import net.minecraft.command.arguments.EntityArgument
1217
import net.minecraft.entity.Entity
1318
import net.minecraft.entity.player.ServerPlayerEntity
19+
import org.apache.logging.log4j.LogManager
1420

1521
/**
1622
* Class for interacting with command api.
1723
* @since 2.0.0-SNAPSHOT.1.
1824
*/
1925
object CommandAPI {
26+
private val logger = LogManager.getLogger()
27+
private var commands = listOf<ICommand>()
28+
2029
private lateinit var dispatcher: CommandDispatcher<CommandSource>
2130
private var dispatcherAssigned = false
2231

@@ -48,7 +57,7 @@ object CommandAPI {
4857
* @return all installed and checked commands.
4958
* @since 2.0.0-SNAPSHOT.1.
5059
*/
51-
fun getAllCommands() = CommandProcessor.getCommands()
60+
fun getCommands() = commands
5261

5362
/**
5463
* Remove already registered command with
@@ -248,4 +257,27 @@ object CommandAPI {
248257
context: CommandContext<CommandSource>,
249258
argumentName: String
250259
): MutableCollection<ServerPlayerEntity> = EntityArgument.getPlayers(context, argumentName)
260+
261+
internal fun registerAll() {
262+
ProviderAPI.getProvidersByType(ProviderType.Command).forEach {
263+
val clazz = it.getDeclaredField("INSTANCE").get(null) as ICommand
264+
ModuleEventAPI.fire(
265+
ModuleCoreEventType.OnCommandClassProcessing, CommandEventData(clazz)
266+
)
267+
logger.debug(
268+
"Command taken! ${clazz.javaClass.simpleName}, name: ${clazz.name}, aliases: ${clazz.aliases}"
269+
)
270+
commands = commands + clazz
271+
register(clazz)
272+
ModuleEventAPI.fire(
273+
ModuleCoreEventType.OnCommandClassProcessed, CommandEventData(clazz)
274+
)
275+
}
276+
}
277+
278+
private fun register(command: ICommand) =
279+
logger.info("Starting registering command ${command.name}").also {
280+
command.initialize()
281+
command.register(getDispatcher())
282+
}
251283
}

0 commit comments

Comments
 (0)