Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Taskeren committed Oct 1, 2023
1 parent f92dfbf commit 4cfe662
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 66 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ publishing {
create<MavenPublication>("maven") {
groupId = "com.github.taskeren"
artifactId = "brigadierX"
version = "1.3.0"
version = "1.3.1"

from(components["java"])
}
Expand Down
159 changes: 94 additions & 65 deletions src/main/kotlin/cn/taskeren/brigadierx/BrigadierX.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("unused")
@file:Suppress("unused", "DeprecatedCallableAddReplaceWith")

package cn.taskeren.brigadierx

Expand All @@ -10,87 +10,116 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.builder.RequiredArgumentBuilder
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)
fun <S> newLiteralArgBuilder(name: String): LiteralArgumentBuilder<S> = LiteralArgumentBuilder.literal(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) }
@Deprecated("Removal",level = DeprecationLevel.WARNING)
fun <S> newLiteralArgBuilder(name: String, func: LiteralArgumentBuilder<S>.() -> Unit): LiteralArgumentBuilder<S> =
LiteralArgumentBuilder.literal<S>(name).apply { func.invoke(this) }

/**
* 注册指令
* Register a command.
*
* @param name 根命令名称
* @param func 传入根命令,进行命令注册
* @return Dispatcher
* @param name The name of the command.
* @param func The function to build the command.
*/
fun <S> CommandDispatcher<S>.register(name: String, func: LiteralArgumentBuilder<S>.() -> Unit): CommandDispatcher<S> {
val rootCommand = LiteralArgumentBuilder.literal<S>(name)
func.invoke(rootCommand)
this.register(rootCommand)
return this
}

fun <S> LiteralArgumentBuilder<S>.literal(name: String): LiteralArgumentBuilder<S> {
val subcommand = LiteralArgumentBuilder.literal<S>(name)
this.then(subcommand)
return subcommand
}

fun <S> LiteralArgumentBuilder<S>.literal(name: String, func: LiteralArgumentBuilder<S>.() -> Unit): LiteralArgumentBuilder<S> {
val subcommand = LiteralArgumentBuilder.literal<S>(name)
func.invoke(subcommand)
this.then(subcommand)
return subcommand
}

fun <S, T> RequiredArgumentBuilder<S, T>.literal(name: String): LiteralArgumentBuilder<S> {
val subcommand = LiteralArgumentBuilder.literal<S>(name)
this.then(subcommand)
return subcommand
val rootCommand = LiteralArgumentBuilder.literal<S>(name)
func.invoke(rootCommand)
this.register(rootCommand)
return this
}

fun <S, T> RequiredArgumentBuilder<S, T>.literal(name: String, func: LiteralArgumentBuilder<S>.() -> Unit): LiteralArgumentBuilder<S> {
val subcommand = LiteralArgumentBuilder.literal<S>(name)
func.invoke(subcommand)
this.then(subcommand)
return subcommand
/**
* Register a command.
*
* @param name The name of the command.
* @param func The function to build the command.
*/
fun <S> CommandDispatcher<S>.literal(
name: String,
func: (LiteralArgumentBuilder<S>.() -> Unit)? = null
): CommandDispatcher<S> {
val rootCommand = LiteralArgumentBuilder.literal<S>(name)
func?.invoke(rootCommand)
this.register(rootCommand)
return this
}

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)
return rab
/**
* Register a subcommand.
*
* @param name The name of the subcommand.
* @param func The function to build the subcommand.
*/
fun <S> LiteralArgumentBuilder<S>.literal(
name: String,
func: (LiteralArgumentBuilder<S>.() -> Unit)? = null
): LiteralArgumentBuilder<S> {
val subcommand = LiteralArgumentBuilder.literal<S>(name)
func?.invoke(subcommand)
this.then(subcommand)
return subcommand
}

fun <S, T> LiteralArgumentBuilder<S>.argument(name: String, type: ArgumentType<T>, func: RequiredArgumentBuilder<S, T>.() -> Unit): RequiredArgumentBuilder<S, T> {
val rab = RequiredArgumentBuilder.argument<S, T>(name, type)
func.invoke(rab)
this.then(rab)
return rab
/**
* Register a subcommand.
*
* @param name The name of the subcommand.
* @param func The function to build the subcommand.
*/
fun <S, T> RequiredArgumentBuilder<S, T>.literal(
name: String,
func: (LiteralArgumentBuilder<S>.() -> Unit)? = null
): LiteralArgumentBuilder<S> {
val subcommand = LiteralArgumentBuilder.literal<S>(name)
func?.invoke(subcommand)
this.then(subcommand)
return subcommand
}

fun <S, T1, T2> RequiredArgumentBuilder<S, T1>.argument(name: String, type: ArgumentType<T2>): RequiredArgumentBuilder<S, T2> {
val rab = RequiredArgumentBuilder.argument<S, T2>(name, type)
this.then(rab)
return rab
/**
* Register a subcommand.
*
* @param name The name of the subcommand.
* @param func The function to build the subcommand.
*/
fun <S, T> LiteralArgumentBuilder<S>.argument(
name: String,
type: ArgumentType<T>,
func: (RequiredArgumentBuilder<S, T>.() -> Unit)? = null
): RequiredArgumentBuilder<S, T> {
val rab = RequiredArgumentBuilder.argument<S, T>(name, type)
func?.invoke(rab)
this.then(rab)
return rab
}

fun <S, T1, T2> RequiredArgumentBuilder<S, T1>.argument(name: String, type: ArgumentType<T2>, func: RequiredArgumentBuilder<S, T2>.() -> Unit): RequiredArgumentBuilder<S, T2> {
val rab = RequiredArgumentBuilder.argument<S, T2>(name, type)
func.invoke(rab)
this.then(rab)
return rab
/**
* Register a subcommand.
*
* @param name The name of the subcommand.
* @param func The function to build the subcommand.
*/
fun <S, T1, T2> RequiredArgumentBuilder<S, T1>.argument(
name: String,
type: ArgumentType<T2>,
func: (RequiredArgumentBuilder<S, T2>.() -> Unit)? = null
): RequiredArgumentBuilder<S, T2> {
val rab = RequiredArgumentBuilder.argument<S, T2>(name, type)
func?.invoke(rab)
this.then(rab)
return rab
}

fun <S, T: ArgumentBuilder<S, T>, R> ArgumentBuilder<S, T>.executesX(func: (CommandContext<S>) -> R) {
this.executes {
func.invoke(it)
Command.SINGLE_SUCCESS
}
/**
* Register the execute function.
*
* @param func The function to execute.
*/
fun <S, T : ArgumentBuilder<S, T>, R> ArgumentBuilder<S, T>.executesX(func: (ctx: CommandContext<S>) -> R) {
this.executes {
func.invoke(it)
Command.SINGLE_SUCCESS
}
}

0 comments on commit 4cfe662

Please sign in to comment.