Skip to content

Commit

Permalink
Merge pull request #121 from SpigotBasics/command-factory
Browse files Browse the repository at this point in the history
Added CommandFactory, refactored command registration
  • Loading branch information
mfnalex authored Feb 13, 2024
2 parents 4c6ff11 + be43c3d commit 09de5b9
Show file tree
Hide file tree
Showing 67 changed files with 776 additions and 241 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ It is not often updated, so better create your own using `gradlew dokkaHtml` or
## Note to Contributors
Contributions are always welcome from anyone!


## Create a new module
Modules can be written in Java or Kotlin. To create a new module, use the `createModule` task.
It will ask you which language (`kotlin` or `java`) you want to use, asks you for the module name, which should be `[a-z0-9_-]+`.
Expand All @@ -59,3 +58,10 @@ It will ask you which language (`kotlin` or `java`) you want to use, asks you fo
- Fixes the "cannot get locale" error message when using ACF on modern Spigot versions.
- [Our Fork](https://github.com/SpigotBasics/acf) | [Original](https://github.com/aikar/commands)
- EDIT: FIXED in 0.5.1-SNAPSHOT as of 25th Jan 2024-->

## Debug information
Debug output can be enabled by setting the `BASICS_DEBUG_LEVEL` environment variable to 1 or higher, for example:

```shell
BASICS_DEBUG_LEVEL=99 java -jar spigot.jar
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.spigotbasics.core.command

import com.github.spigotbasics.core.command.raw.RawCommandContext
import com.github.spigotbasics.core.command.raw.RawTabCompleter
import com.github.spigotbasics.core.logger.BasicsLoggerFactory
import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.MessageFactory
Expand All @@ -19,7 +21,7 @@ import java.util.logging.Level
class BasicsCommand internal constructor(
var info: CommandInfo,
private var executor: BasicsCommandExecutor?,
private var tabCompleter: BasicsTabCompleter?,
private var tabCompleter: RawTabCompleter?,
val coreMessages: CoreMessages,
val messageFactory: MessageFactory,
) :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.spigotbasics.core.command

import com.github.spigotbasics.core.command.raw.RawCommandContext
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.spigotbasics.core.command

import com.github.spigotbasics.core.command.raw.RawCommandContext
import com.github.spigotbasics.core.command.raw.RawTabCompleter
import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.MessageFactory
import com.github.spigotbasics.core.module.BasicsModule

abstract class BasicsCommandExecutor(
val coreMessages: CoreMessages,
val messageFactory: MessageFactory,
) : BasicsCommandContextHandler(), BasicsTabCompleter {
) : BasicsCommandContextHandler(), RawTabCompleter {
constructor(module: BasicsModule) : this(module.coreMessages, module.messageFactory)

abstract fun execute(context: RawCommandContext): CommandResult?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.github.spigotbasics.core.command

import com.github.spigotbasics.core.logger.BasicsLoggerFactory
import org.bukkit.command.Command
import org.bukkit.command.SimpleCommandMap
import java.util.logging.Level

// TODO: Fix updateCommandsToPlayers logic
// 1. Must not be called async
Expand Down Expand Up @@ -67,8 +70,28 @@ class BasicsCommandManager(private val serverCommandMap: SimpleCommandMap) {
}
}

companion object {
private val logger = BasicsLoggerFactory.getCoreLogger(BasicsCommandManager::class)

private val simpleCommandMap_knownCommands =
try {
val field = SimpleCommandMap::class.java.getDeclaredField("knownCommands")
field.isAccessible = true
field
} catch (e: Exception) {
logger.log(Level.SEVERE, "Failed to get SimpleCommandMap.knownCommands field", e)
null
}
}

private fun removeFromServerCommandMap(command: BasicsCommand) {
// serverCommandMap.commands.remove(command)
try {
@Suppress("UNCHECKED_CAST")
(simpleCommandMap_knownCommands?.get(serverCommandMap) as? MutableMap<String, Command>)?.let { knownCommands ->
knownCommands.entries.removeIf { it.value == command }
}
} catch (e: Exception) {
}
command.disableExecutor()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.spigotbasics.core.command
import com.github.spigotbasics.core.messages.Message
import org.bukkit.permissions.Permission

// TODO: Executor does not belong into CommandInfo
// TODO: createCommandInfo in BasicsModule
// TODO: Permission message not shown when permission is missing, but "no command found" - make this configurable?
// That'd require not setting any permission on the actual Command, but only checking it in the executor (BasicsCommand#execute)
Expand All @@ -14,6 +13,4 @@ data class CommandInfo(
val description: String?,
val usage: String,
val aliases: List<String>,
// val executor: (CommandSender, BasicsCommand, String, List<String>) -> Boolean
// val executor: BasicsCommandExecutor
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.spigotbasics.core.command

import com.github.spigotbasics.core.command.raw.RawCommandContext
import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.Message
import org.bukkit.entity.Player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.bukkit.entity.Player
import org.bukkit.util.StringUtil
import java.util.Collections

object TabCompleter {
object TabCompleters {
fun getPlayers(
sender: CommandSender,
string: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.spigotbasics.core.command.factory

import com.github.spigotbasics.core.command.BasicsCommandManager
import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.MessageFactory
import org.bukkit.permissions.Permission

class CommandFactory(
private val messageFactory: MessageFactory,
private val coreMessages: CoreMessages,
private val commandManager: BasicsCommandManager,
) {
fun rawCommandBuilder(
name: String,
permission: Permission,
): RawCommandBuilder {
return RawCommandBuilder(
messageFactory = messageFactory,
coreMessages = coreMessages,
commandManager = commandManager,
name = name,
permission = permission,
)
}

fun parsedCommandBuilder(
name: String,
permission: Permission,
): ParsedCommandBuilderFactory {
return ParsedCommandBuilderFactory(
messageFactory = messageFactory,
coreMessages = coreMessages,
commandManager = commandManager,
name = name,
permission = permission,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.spigotbasics.core.command.factory

import com.github.spigotbasics.core.command.BasicsCommandManager
import com.github.spigotbasics.core.command.ParsedCommandBuilder
import com.github.spigotbasics.core.command.parsed.MapCommandContext
import com.github.spigotbasics.core.command.parsed.ParsedCommandContext
import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.MessageFactory
import org.bukkit.permissions.Permission

class ParsedCommandBuilderFactory(
private val messageFactory: MessageFactory,
private val coreMessages: CoreMessages,
private val commandManager: BasicsCommandManager,
private val name: String,
private val permission: Permission,
) {
fun <T : ParsedCommandContext> context(): ParsedCommandBuilder<T> {
return ParsedCommandBuilder(
messageFactory = messageFactory,
coreMessages = coreMessages,
commandManager = commandManager,
name = name,
permission = permission,
)
}

fun mapContext(): ParsedCommandBuilder<MapCommandContext> {
return ParsedCommandBuilder(
messageFactory = messageFactory,
coreMessages = coreMessages,
commandManager = commandManager,
name = name,
permission = permission,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.github.spigotbasics.core.command
package com.github.spigotbasics.core.command.factory

import com.github.spigotbasics.core.command.BasicsCommand
import com.github.spigotbasics.core.command.BasicsCommandExecutor
import com.github.spigotbasics.core.command.BasicsCommandManager
import com.github.spigotbasics.core.command.CommandInfo
import com.github.spigotbasics.core.command.CommandResult
import com.github.spigotbasics.core.command.raw.RawCommandContext
import com.github.spigotbasics.core.command.raw.RawTabCompleter
import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.Message
import com.github.spigotbasics.core.module.BasicsModule
import com.github.spigotbasics.core.messages.MessageFactory
import org.bukkit.permissions.Permission

class BasicsCommandBuilder(
private val module: BasicsModule,
class RawCommandBuilder(
private val messageFactory: MessageFactory,
private val coreMessages: CoreMessages,
private val commandManager: BasicsCommandManager,
private val name: String,
private val permission: Permission,
) {
private var permissionMessage: Message = module.plugin.messages.noPermission
private var permissionMessage: Message = coreMessages.noPermission
private var description: String? = null
private var usage: String = ""
private var aliases: List<String> = emptyList()
private var executor: BasicsCommandExecutor? = null
private var tabCompleter: BasicsTabCompleter? = null
private var tabCompleter: RawTabCompleter? = null

fun description(description: String) = apply { this.description = description }

Expand All @@ -24,18 +34,20 @@ class BasicsCommandBuilder(
this.usage = usage
}

fun permissionMessage(permissionMessage: Message) = apply { this.permissionMessage = permissionMessage }
@Deprecated("Ignored by Bukkit")
private fun permissionMessage(permissionMessage: Message) = apply { this.permissionMessage = permissionMessage }

@Deprecated("Will be registered automatically in the future")
fun aliases(aliases: List<String>) = apply { this.aliases = aliases }

fun executor(executor: BasicsCommandExecutor) = apply { this.executor = executor }

fun tabCompleter(tabCompleter: BasicsTabCompleter) = apply { this.tabCompleter = tabCompleter }
fun tabCompleter(tabCompleter: RawTabCompleter) = apply { this.tabCompleter = tabCompleter }

fun executor(executor: (RawCommandContext) -> CommandResult?) =
apply {
this.executor =
object : BasicsCommandExecutor(module) {
object : BasicsCommandExecutor(coreMessages, messageFactory) {
override fun execute(context: RawCommandContext): CommandResult? {
return executor(context)
}
Expand All @@ -45,7 +57,7 @@ class BasicsCommandBuilder(
fun tabCompleter(tabCompleter: (RawCommandContext) -> MutableList<String>?) =
apply {
this.tabCompleter =
object : BasicsTabCompleter {
object : RawTabCompleter {
override fun tabComplete(context: RawCommandContext): MutableList<String>? {
return tabCompleter(context)
}
Expand All @@ -54,7 +66,7 @@ class BasicsCommandBuilder(

fun register(): BasicsCommand {
val command = build()
module.commandManager.registerCommand(command)
commandManager.registerCommand(command)
return command
}

Expand All @@ -72,8 +84,8 @@ class BasicsCommandBuilder(
info = info,
executor = executor ?: error("Executor must be set"),
tabCompleter = tabCompleter ?: executor,
coreMessages = module.plugin.messages,
messageFactory = module.plugin.messageFactory,
coreMessages = coreMessages,
messageFactory = messageFactory,
)
}
}
Loading

0 comments on commit 09de5b9

Please sign in to comment.