Skip to content

Commit

Permalink
Merge pull request #129 from SpigotBasics/executor-per-path
Browse files Browse the repository at this point in the history
Allow CommandContextExecutors per ArgumentPath!
  • Loading branch information
mfnalex authored Feb 15, 2024
2 parents 3642a3f + a72c481 commit b18cb91
Show file tree
Hide file tree
Showing 20 changed files with 149 additions and 25 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ Debug output can be enabled by setting the `BASICS_DEBUG_LEVEL` environment vari

```shell
BASICS_DEBUG_LEVEL=99 java -jar spigot.jar
```
```

You can also change the debug level on the fly using `/basicsdebug setdebugloglevel <number>`.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ArgumentPath<T : CommandContext>(
val arguments: List<Pair<String, CommandArgument<*>>>,
val permission: List<Permission> = emptyList(),
private val contextBuilder: (Map<String, Any?>) -> T,
val ownExecutor: CommandContextExecutor<T>? = null,
) {
companion object {
val logger = BasicsLoggerFactory.getCoreLogger(ArgumentPath::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ import org.bukkit.command.CommandSender
import org.bukkit.permissions.Permission

class ParsedCommandExecutor<T : CommandContext>(
private val executor: CommandContextExecutor<T>,
private val executor: CommandContextExecutor<T>?,
private val paths: List<ArgumentPath<T>>,
) {
companion object {
private val logger = BasicsLoggerFactory.getCoreLogger(ParsedCommandExecutor::class)
}

init {

for (path in paths) {
if (path.ownExecutor == null && executor == null) {
throw IllegalArgumentException(
"Attempting to create a ParsedCommandExecutor with a path that has no executor and no default " +
"executor was provided. Path: $path",
)
}
}

logger.debug(10, "ParsedCommandExecutor created with paths: ${paths.size}")
}

Expand Down Expand Up @@ -78,7 +88,10 @@ class ParsedCommandExecutor<T : CommandContext>(
when (val result = path.parse(sender, input)) {
is ParseResult.Success -> {
logger.debug(10, "Path.parse == ParseResult.Success: $result")
executor.execute(sender, result.context)

val actualExecutor = path.ownExecutor ?: executor ?: error("No executor found for path: $path")

actualExecutor.execute(sender, result.context)
// logger.debug(10,"Command executed successfully.")
logger.debug(10, "Command executed successfully.")
return Either.Left(CommandResult.SUCCESS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.github.spigotbasics.core.command.parsed.arguments

import com.github.spigotbasics.core.Basics
import com.github.spigotbasics.core.messages.Message
import org.bukkit.command.CommandSender

open class IntArg(name: String) : CommandArgument<Int>(name) {
override fun parse(
sender: CommandSender,
value: String,
): Int? = value.toIntOrNull()

override fun errorMessage(
sender: CommandSender,
value: String,
): Message {
return Basics.messages.invalidValueForArgumentMustBeInteger(name, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class IntRangeArg(name: String, private val min: () -> Int, private val max: ()
sender: CommandSender,
value: String,
): Message {
return Basics.messages.invalidValueForArgumentNumberNotInRange(name, value.toIntOrNull() ?: 0, min(), max())
val given = value.toIntOrNull() ?: return Basics.messages.invalidValueForArgumentMustBeInteger(name, value)
return Basics.messages.invalidValueForArgumentNumberNotInRange(name, given, min(), max())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.spigotbasics.common.leftOrNull
import com.github.spigotbasics.core.Basics
import com.github.spigotbasics.core.extensions.partialMatches
import com.github.spigotbasics.core.messages.Message
import com.github.spigotbasics.core.permission.CorePermissions
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
Expand All @@ -13,10 +14,10 @@ class PlayersArg(name: String) : CommandArgument<List<Player>>(name) {
private enum class ErrorType {
NOT_FOUND,
NO_PERMISSION_SELECTORS,
SELECTOR_INCUDES_ENTITIES,
SELECTOR_INCLUDES_ENTITIES,
}

private val selectorPermission = Basics.permissions.useSelectors
private val selectorPermission = CorePermissions.useSelectors

override fun parse(
sender: CommandSender,
Expand Down Expand Up @@ -58,7 +59,7 @@ class PlayersArg(name: String) : CommandArgument<List<Player>>(name) {
val selected = Bukkit.selectEntities(sender, value)
val players = selected.filterIsInstance<Player>()
if (selected.size != players.size) {
return Either.Left(ErrorType.SELECTOR_INCUDES_ENTITIES)
return Either.Left(ErrorType.SELECTOR_INCLUDES_ENTITIES)
}
if (players.isEmpty()) {
return Either.Left(ErrorType.NOT_FOUND)
Expand All @@ -73,7 +74,7 @@ class PlayersArg(name: String) : CommandArgument<List<Player>>(name) {
return when (get(sender, value).leftOrNull()) {
ErrorType.NOT_FOUND -> Basics.messages.playerNotFound(value)
ErrorType.NO_PERMISSION_SELECTORS -> Basics.messages.noPermission(selectorPermission)
ErrorType.SELECTOR_INCUDES_ENTITIES -> Basics.messages.selectorIncludesEntities(value)
ErrorType.SELECTOR_INCLUDES_ENTITIES -> Basics.messages.selectorIncludesEntities(value)
else -> super.errorMessage(sender, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.spigotbasics.core.command.parsed.dsl.argumentpathbuilder

import com.github.spigotbasics.core.command.parsed.AnySender
import com.github.spigotbasics.core.command.parsed.ArgumentPath
import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.PlayerSender
import com.github.spigotbasics.core.command.parsed.SenderType
import com.github.spigotbasics.core.command.parsed.arguments.CommandArgument
Expand All @@ -13,6 +14,7 @@ abstract class ArgumentPathBuilder<T : CommandContext> {
protected var senderType: SenderType<*> = AnySender
protected var arguments = emptyList<Pair<String, CommandArgument<*>>>()
protected var permissions = emptyList<Permission>()
protected var pathExecutor: CommandContextExecutor<T>? = null

private fun senderType(senderType: SenderType<*>) = apply { this.senderType = senderType }

Expand All @@ -29,5 +31,7 @@ abstract class ArgumentPathBuilder<T : CommandContext> {
this.arguments = argumentBuilder.build()
}

fun executor(pathExecutor: CommandContextExecutor<T>) = apply { this.pathExecutor = pathExecutor }

abstract fun build(): ArgumentPath<T>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ class GenericArgumentPathBuilder<T : CommandContext> : ArgumentPathBuilder<T>()
arguments,
permissions,
contextBuilder ?: error("Context builder not set"),
pathExecutor,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ class MapArgumentPathBuilder : ArgumentPathBuilder<MapContext>() {
senderType,
arguments,
permissions,
) { args -> MapContext(args) }
{ args -> MapContext(args) },
pathExecutor,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ open class ParsedCommandBuilder<T : CommandContext>(
fun build(): BasicsCommand {
val command =
ParsedCommandExecutor(
parsedExecutor ?: error("parsedExecutor must be set"),
parsedExecutor,
argumentPaths,
)
val actualExecutor = createActualExecutor(command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class CoreMessages(context: ConfigInstantiationContext) : SavedConfig(context) {
.tagUnparsed("value", givenValue)
}

fun invalidValueForArgumentMustBeInteger(
argumentName: String,
givenValue: String,
): Message {
return getMessage("invalid-value-for-argument-must-be-integer")
.tagUnparsed("argument", argumentName)
.tagUnparsed("value", givenValue)
}

fun invalidValueForArgumentNumberNotInRange(
argumentName: String,
givenValue: Int,
Expand All @@ -66,9 +75,9 @@ class CoreMessages(context: ConfigInstantiationContext) : SavedConfig(context) {
): Message {
return getMessage("invalid-value-for-argument-number-not-in-range")
.tagUnparsed("argument", argumentName)
.tagParsed("value", givenValue.toString())
.tagParsed("min", min.toString())
.tagParsed("max", max.toString())
.tagUnparsed("value", givenValue.toString())
.tagUnparsed("min", min.toString())
.tagUnparsed("max", max.toString())
}

fun missingArgument(name: String) = getMessage("missing-value-for-argument").tagParsed("argument", name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.github.spigotbasics.core.permission

class CorePermissions(private val permissionManager: BasicsPermissionManager) {
import com.github.spigotbasics.core.logger.BasicsLoggerFactory

object CorePermissions {
private val pm = BasicsPermissionManager(BasicsLoggerFactory.getCoreLogger(CorePermissions::class))

val useSelectors =
permissionManager.createSimplePermission(
pm.createSimplePermission(
"basics.selectors",
"Allows using selectors (@p, etc)",
)
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ unknown-option: "<error><red>Unknown option: </red><color:#FF8888><#option></col

# Tags: <#argument> <#value>
invalid-value-for-argument: "<error><red>Invalid value for <bright_red><#argument></bright_red>: <bright_red><#value></bright_red></red>"
invalid-value-for-argument-must-be-integer: "<error><red>Invalid value for <bright_red><#argument></bright_red>: Must be an integer, was <dark_red><#value></dark_red>!</red>"

# Tags: <#argument> <#value> <#min> <#max>
invalid-value-for-argument-number-not-in-range: "<error><red>Invalid value for <bright_red><#argument></bright_red>: Must be between <bright_red><#min></bright_red> and <bright_red><#max></bright_red>, was <dark_red><#value></dark_red>!</red>"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package com.github.spigotbasics.modules.basicscore

import com.github.spigotbasics.core.command.parsed.arguments.IntRangeArg
import com.github.spigotbasics.core.module.AbstractBasicsModule
import com.github.spigotbasics.core.module.loader.ModuleInstantiationContext
import com.github.spigotbasics.modules.basicscore.commands.PrintPermissionsCommand
import com.github.spigotbasics.modules.basicscore.commands.SetDebugLogLevelCommand
import org.bukkit.permissions.PermissionDefault

class BasicsCoreModule(context: ModuleInstantiationContext) : AbstractBasicsModule(context) {
val permission =
val modulePermission =
permissionManager.createSimplePermission(
"basics.admin.module",
"Allows managing Basics modules",
PermissionDefault.OP,
)

val debugPermission =
permissionManager.createSimplePermission(
"basics.admin.debug",
"Allows debugging Basics",
PermissionDefault.OP,
)

override fun onEnable() {
commandFactory.parsedCommandBuilder("module", permission)
commandFactory.parsedCommandBuilder("module", modulePermission)
.mapContext {
usage = "<command> [module]"

Expand Down Expand Up @@ -92,5 +102,26 @@ class BasicsCoreModule(context: ModuleInstantiationContext) : AbstractBasicsModu
}
.executor(ModuleCommand(this))
.register()

commandFactory.parsedCommandBuilder("basicsdebug", debugPermission)
.mapContext {
usage = "<command>"

path {
arguments {
sub("printpermissions")
}
executor(PrintPermissionsCommand())
}

path {
arguments {
sub("setdebugloglevel")
named("level", IntRangeArg("Log Level", { 0 }, { 999_999_999 }))
}
executor(SetDebugLogLevelCommand())
}
}
.register()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.spigotbasics.modules.basicscore.commands

import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.context.MapContext
import org.bukkit.command.CommandSender

class DebugFallbackExecutor : CommandContextExecutor<MapContext> {
override fun execute(
sender: CommandSender,
context: MapContext,
) {
throw IllegalStateException("This command should never be executed")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.spigotbasics.modules.basicscore.commands

import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.context.MapContext
import org.bukkit.command.CommandSender

class PrintPermissionsCommand : CommandContextExecutor<MapContext> {
override fun execute(
sender: CommandSender,
context: MapContext,
) {
for (permission in sender.server.pluginManager.permissions.filter { it.name.startsWith("basics.") }.sortedBy { it.name }) {
sender.sendMessage(permission.name + " - " + permission.description + " - " + permission.default.toString())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.spigotbasics.modules.basicscore.commands

import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.context.MapContext
import com.github.spigotbasics.core.logger.BasicsLogger
import org.bukkit.command.CommandSender

class SetDebugLogLevelCommand : CommandContextExecutor<MapContext> {
override fun execute(
sender: CommandSender,
context: MapContext,
) {
BasicsLogger.debugLogLevel = context["level"] as Int
sender.sendMessage("Debug log level set to ${context["level"]}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BasicsWorkbenchModule(context: ModuleInstantiationContext) : AbstractBasic
override fun onEnable() {
commandFactory.rawCommandBuilder("craftingtable", permissionCraftingTable)
.description("Opens a crafting table")
.aliases(listOf("workbench"))
// .aliases(listOf("workbench"))
.executor(WorkbenchExecutor())
.register()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.github.spigotbasics.core.messages.CoreMessages
import com.github.spigotbasics.core.messages.MessageFactory
import com.github.spigotbasics.core.messages.tags.TagResolverFactory
import com.github.spigotbasics.core.module.manager.ModuleManager
import com.github.spigotbasics.core.permission.BasicsPermissionManager
import com.github.spigotbasics.core.permission.CorePermissions
import com.github.spigotbasics.core.playerdata.CorePlayerData
import com.github.spigotbasics.core.playerdata.CorePlayerDataListener
Expand Down Expand Up @@ -50,7 +49,7 @@ class BasicsPluginImpl : JavaPlugin(), BasicsPlugin {

private val logger = BasicsLoggerFactory.getCoreLogger(this::class)

override val permissions = CorePermissions(BasicsPermissionManager(logger))
override val permissions = CorePermissions
override val moduleFolder = File(dataFolder, "modules")
override val moduleManager = ModuleManager(this, server, moduleFolder)
override val tagResolverFactory: TagResolverFactory = TagResolverFactory(facade)
Expand Down Expand Up @@ -138,7 +137,7 @@ class BasicsPluginImpl : JavaPlugin(), BasicsPlugin {
server.pluginManager.registerEvents(modulePlayerDataLoader, this)
server.pluginManager.registerEvents(PlayerCommandListSendListener(facade.getCommandMap(server.pluginManager)), this)

getCommand("basicsdebug")?.setExecutor(BasicsDebugCommand(this))
// getCommand("basicsdebug")?.setExecutor(BasicsDebugCommand(this))
}

private fun initializeHeavyClasses() {
Expand Down
8 changes: 4 additions & 4 deletions plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ main: com.github.spigotbasics.plugin.BasicsPluginImpl
api-version: "1.13"
authors: [ "https://github.com/SpigotBasics/basics/graphs/contributors" ]
commands:
basicsdebug:
description: "Debugging commands for Basics"
permission: basics.debug
usage: "/<command> [start <interval>|stop]"
# basicsdebug:
# description: "Debugging commands for Basics"
# permission: basics.debug
# usage: "/<command> [start <interval>|stop]"

0 comments on commit b18cb91

Please sign in to comment.