Skip to content

Commit

Permalink
Support Context Receivers for advanced usage
Browse files Browse the repository at this point in the history
Now supports things like this:
```
dispatcher {
  "literal_arg_1" {
    executesX {
      // ... do something
    }

    "typed_arg_1"(StringArgumentType.greedyString()) {
      executesX {
        val theString = StringArgumentType.getString(it, "typed_arg_1")
        println(theString)
      }
    }
  }
}
}
```
  • Loading branch information
Taskeren committed Oct 1, 2023
1 parent a1615aa commit f92dfbf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BrigadierX

[使用帮助/How to Use](https://nitu2003.github.io/brigadierX/index.html)
[使用帮助 / How to Use](https://taskeren.github.io/brigadierX/index.html)

## 导入项目 / Import Repository

Expand Down
16 changes: 11 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.0"
`maven-publish`
}

group = "com.github.taskeren"
version = "1.2-SNAPSHOT"
version = "1.3-SNAPSHOT"

repositories {
mavenCentral()
maven("https://libraries.minecraft.net")
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.21")

api("com.mojang:brigadier:1.0.18")

// JUnit5
Expand All @@ -25,12 +25,18 @@ tasks.test {
useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xcontext-receivers"
}
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = "com.github.taskeren"
artifactId = "brigadierX"
version = "1.2.2"
version = "1.3.0"

from(components["java"])
}
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "brigadierx"
rootProject.name = "brigadierX"
25 changes: 4 additions & 21 deletions src/main/kotlin/cn/taskeren/brigadierx/BrigadierX.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import com.mojang.brigadier.context.CommandContext
/**
* LiteralArgumentBuilder 构造方法语法糖
*/
@Deprecated("Removal", ReplaceWith("LiteralArgumentBuilder.literal<S>(name)"), DeprecationLevel.WARNING)
fun <S> newLiteralArgBuilder(name: String): LiteralArgumentBuilder<S> = LiteralArgumentBuilder.literal<S>(name)

/**
* LiteralArgumentBuilder 构造方法语法糖
*/
@Deprecated("Removal", ReplaceWith("LiteralArgumentBuilder.literal<S>(name).apply { func.invoke(this) }"), DeprecationLevel.WARNING)
fun <S> newLiteralArgBuilder(name: String, func: LiteralArgumentBuilder<S>.() -> Unit): LiteralArgumentBuilder<S> = LiteralArgumentBuilder.literal<S>(name).apply { func.invoke(this) }

/**
Expand All @@ -28,20 +30,14 @@ fun <S> newLiteralArgBuilder(name: String, func: LiteralArgumentBuilder<S>.() ->
* @return Dispatcher
*/
fun <S> CommandDispatcher<S>.register(name: String, func: LiteralArgumentBuilder<S>.() -> Unit): CommandDispatcher<S> {
val rootCommand = newLiteralArgBuilder<S>(name)
val rootCommand = LiteralArgumentBuilder.literal<S>(name)
func.invoke(rootCommand)
this.register(rootCommand)
return this
}

/* ==================================
*
* literal
*
* ================================== */

fun <S> LiteralArgumentBuilder<S>.literal(name: String): LiteralArgumentBuilder<S> {
val subcommand = newLiteralArgBuilder<S>(name)
val subcommand = LiteralArgumentBuilder.literal<S>(name)
this.then(subcommand)
return subcommand
}
Expand All @@ -66,12 +62,6 @@ fun <S, T> RequiredArgumentBuilder<S, T>.literal(name: String, func: LiteralArgu
return subcommand
}

/* ==================================
*
* argument
*
* ================================== */

fun <S, T> LiteralArgumentBuilder<S>.argument(name: String, type: ArgumentType<T>): RequiredArgumentBuilder<S, T> {
val rab = RequiredArgumentBuilder.argument<S, T>(name, type)
this.then(rab)
Expand All @@ -98,13 +88,6 @@ fun <S, T1, T2> RequiredArgumentBuilder<S, T1>.argument(name: String, type: Argu
return rab
}

/* ==================================
*
* Modified: executes
* executesX
*
* ================================== */

fun <S, T: ArgumentBuilder<S, T>, R> ArgumentBuilder<S, T>.executesX(func: (CommandContext<S>) -> R) {
this.executes {
func.invoke(it)
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/cn/taskeren/brigadierx/BrigadierXNew.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.taskeren.brigadierx

import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.builder.RequiredArgumentBuilder

operator fun <S> CommandDispatcher<S>.invoke(block: CommandDispatcher<S>.() -> Unit) = block(this)

context(CommandDispatcher<S>)
operator fun <S> String.invoke(block: LiteralArgumentBuilder<S>.() -> Unit) = register(this, block)

context(CommandDispatcher<*>, LiteralArgumentBuilder<S>)
operator fun <S> String.invoke(block: LiteralArgumentBuilder<S>.() -> Unit) = literal(this, block)

context(CommandDispatcher<*>, LiteralArgumentBuilder<S>)
operator fun <S, T> String.invoke(type: ArgumentType<T>, block: RequiredArgumentBuilder<S, T>.() -> Unit) =
argument(this, type, block)
42 changes: 42 additions & 0 deletions src/test/kotlin/cn/taskeren/sample/brigadier/TestNew.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.taskeren.sample.brigadier

import cn.taskeren.brigadierx.executesX
import cn.taskeren.brigadierx.invoke
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.StringArgumentType

fun main() {
TestNew().testCase1()
}

internal class TestNew {

fun testCase1() {
val dispatcher = CommandDispatcher<Any>()

dispatcher {
"help" {
"what" {

"what"(StringArgumentType.greedyString()) {
executesX {
val what = StringArgumentType.getString(it, "what")
println("Help What? $what")
}
}

executesX {
println("Idk what to help!")
}
}

executesX {
println("Help!")
}
}
}

dispatcher.execute(readlnOrNull(), object {})
}

}

0 comments on commit f92dfbf

Please sign in to comment.