8000 adding Kotlin DSL by DerEchtePilz · Pull Request #357 · CommandAPI/CommandAPI · GitHub
[go: up one dir, main page]

Skip to content

adding Kotlin DSL #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding command requirement support and execution of commands with no …
…arguments
  • Loading branch information
DerEchtePilz committed Oct 28, 2022
commit 5431b571d93750d508a81f107ea5659b1fa1dcc5
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.bukkit.entity.Player
import java.util.function.Predicate

inline fun command(name: String, tree: CommandTree.() -> Unit = {}) = CommandTree(name).apply(tree).register()
inline fun command(name: String, requirement: Predicate<CommandSender>, tree: CommandTree.() -> Unit = {}) = CommandTree(name).withRequirement(requirement).apply(tree).register()

// CommandTree start
inline fun CommandTree.argument(base: Argument<*>, block: ArgumentTree.() -> Unit = {}): CommandTree = then(base.apply(block))
Expand Down Expand Up @@ -72,7 +73,7 @@ inline fun CommandTree.entityTypeArgument(nodeName: String, block: ArgumentTree.

// Scoreboard arguments
inline fun CommandTree.scoreHolderArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): CommandTree = then(ScoreHolderArgument<String>(nodeName).apply(block))
inline fun <T: ScoreHolderType> CommandTree.scoreHolderArgument(nodeName: String, scoreHolderType: T, block: ArgumentTree.() -> Unit = {}): CommandTree = then(ScoreHolderArgument<T>(nodeName, scoreHolderType).apply(block))
inline fun <T : ScoreHolderType> CommandTree.scoreHolderArgument(nodeName: String, scoreHolderType: T, block: ArgumentTree.() -> Unit = {}): CommandTree = then(ScoreHolderArgument<T>(nodeName, scoreHolderType).apply(block))
inline fun CommandTree.scoreboardSlotArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): CommandTree = then(ScoreboardSlotArgument(nodeName).apply(block))
inline fun CommandTree.objectiveArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): CommandTree = then(ObjectiveArgument(nodeName).apply(block))
inline fun CommandTree.objectiveCriteriaArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): CommandTree = then(ObjectiveCriteriaArgument(nodeName).apply(block))
Expand Down Expand Up @@ -165,7 +166,7 @@ inline fun ArgumentTree.entityTypeArgument(nodeName: String, block: ArgumentTree

// Scoreboard arguments
inline fun ArgumentTree.scoreHolderArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(ScoreHolderArgument<String>(nodeName).apply(block))
inline fun <T: ScoreHolderType> ArgumentTree.scoreHolderArgument(nodeName: String, scoreHolderType: T, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(ScoreHolderArgument<T>(nodeName, scoreHolderType).apply(block))
inline fun <T : ScoreHolderType> ArgumentTree.scoreHolderArgument(nodeName: String, scoreHolderType: T, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(ScoreHolderArgument<T>(nodeName, scoreHolderType).apply(block))
inline fun ArgumentTree.scoreboardSlotArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(ScoreboardSlotArgument(nodeName).apply(block))
inline fun ArgumentTree.objectiveArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(ObjectiveArgument(nodeName).apply(block))
inline fun ArgumentTree.objectiveCriteriaArgument(nodeName: String, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(ObjectiveCriteriaArgument(nodeName).apply(block))
Expand Dow 8000 n Expand Up @@ -203,6 +204,15 @@ inline fun ArgumentTree.multiLiteralArgument(vararg literals: String, block: Arg
inline fun CommandTree.requirement(base: Argument<*>, predicate: Predicate<CommandSender>, block: ArgumentTree.() -> Unit = {}): CommandTree = then(base.withRequirement(predicate).apply(block))
inline fun ArgumentTree.requirement(base: Argument<*>, predicate: Predicate<CommandSender>, block: ArgumentTree.() -> Unit = {}): ArgumentTree = then(base.withRequirement(predicate).apply(block))

// CommandTree execution
fun CommandTree.anyExecutor(any: (CommandSender, Array<Any>) -> Unit) = Executions().any(any).executes(this)
fun CommandTree.playerExecutor(player: (Player, Array<Any>) -> Unit) = Executions().player(player).executes(this)
fun CommandTree.consoleExecutor(console: (ConsoleCommandSender, Array<Any>) -> Unit) = Executions().console(console).executes(this)
fun CommandTree.commandBlockExecutor(block: (BlockCommandSender, Array<Any>) -> Unit) = Executions().block(block).executes(this)
fun CommandTree.proxyExecutor(proxy: (ProxiedCommandSender, Array<Any>) -> Unit) = Executions().proxy(proxy).executes(this)
fun CommandTree.nativeExecutor(native: (NativeProxyCommandSender, Array<Any>) -> Unit) = Executions().native(native).executes(this)

// ArgumentTree execution
fun ArgumentTree.anyExecutor(any: (CommandSender, Array<Any>) -> Unit) = Executions().any(any).executes(this)
fun ArgumentTree.playerExecutor(player: (Player, Array<Any>) -> Unit) = Executions().player(player).executes(this)
fun ArgumentTree.consoleExecutor(console: (ConsoleCommandSender, Array<Any>) -> Unit) = Executions().console(console).executes(this)
Expand Down Expand Up @@ -288,4 +298,43 @@ class Executions {
return
}
}

fun executes(tree: CommandTree) {
if (any != null) {
tree.executes(CommandExecutor { sender, args ->
any?.invoke(sender, args)
})
return
}
if (player != null) {
tree.executesPlayer(PlayerCommandExecutor { player, args ->
this.player?.invoke(player, args)
})
return
}
if (console != null) {
tree.executesConsole(ConsoleCommandExecutor { console, args ->
this.console?.invoke(console, args)
})
return
}
if (block != null) {
tree.executesCommandBlock(CommandBlockCommandExecutor { block, args ->
this.block?.invoke(block, args)
})
return
}
if (proxy != null) {
tree.executesProxy(ProxyCommandExecutor { proxy, args ->
this.proxy?.invoke(proxy, args)
})
return
}
if (native != null) {
tree.executesNative(NativeCommandExecutor { native, args ->
this.native?.invoke(native, args)
})
return
}
}
}
8 changes: 8 additions & 0 deletions commandapi-kotlin/src/test/kotlin/Examples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ command("sendMessageTo") {
}
}
/* ANCHOR_END: dslSendMessageToCommandRequirement */

/* ANCHOR: dslCommandRequirements */
command("commandRequirement", {sender: CommandSender -> (sender is Player) && sender.isOp}) {
playerExecutor { player, _ ->
player.sendMessage("This command can only be executed by players who are server operators.")
}
}
/* ANCHOR_END: dslCommandRequirements */
}

fun moreExamples() {
Expand Down
14 changes: 14 additions & 0 deletions docssrc/src/kotlinusedsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ Below, the `sendMessageTo` command is adding a broadcast option which should onl

</div>

### Adding requirements to commands

Previously, we've taken a look at how to restrict arguments to certain players by using requirements.

You can also restrict the use of a whole command by using requirements:

<div class="multi-pre">

```kotlin,Kotlin
{{#include ../../commandapi-kotlin/src/test/kotlin/Examples.kt:dslCommandRequirements}}
```

</div>

-----

## More examples
Expand Down
0