Skip to content

monun/kommand

Repository files navigation

Kommand

Java Kotlin Gradle Maven Central GitHub YouTube

Command DSL for PaperMC Plugin (Brigadier)


  • Features

    • 명령어 분기 지원
    • 인수 분석(Parsing)
    • 명령어 제안 지원 (TabComplete)
  • Supported minecraft versions

    • 1.17.1
    • 1.18
    • 1.18.1
    • 1.18.2
    • 1.19
    • 1.19.1
    • 1.19.2
    • 1.19.3
    • 1.19.4
    • 1.20
    • 1.20.1

Paper에는 명령어 실행을 위한 프레임워크가 없습니다.
다른 프레임워크를 사용하지 않고 독립형으로 명령어 실행 코드를 작성시 다음과 같은 패턴으로 작성하게 됩니다.

class CommandDispatcher : CommandExecutor {
    ...
    override fun onCommand(...) {
        val commandName = args[0]

        if ("first".equals(commandName, true)) {
            // first 명령 처리
        } else if ("second".equals(commandName, true)) {
            // second 명령 처리
        } else {
            // Error
        }
        //when 문으로도 구현 가능
    }
}
...

위와 같은 코드는 명령문이 적을땐 간결하지만, 따라붙는 인수나 상수가 늘어나면 코드가 매우 지저분해지고 디버깅에도 어려움을 겪게됩니다.

그래서 명령 처리는 신경쓰지 않고 최종 실행코드만 작성 할 수 있는 프레임워크가 필요해졌습니다.

Kommand는 위와 같은 문제를 해결하고 명령문을 보다 직관적인 코드작성을 위한 DSL을 제공합니다.


아래와 같은 명령 체계가 있다고 생각해봅시다.

  • user
    • create <username>
    • modify <user>
      • name <newName>
      • tag <newTag>

다음 코드는 Kommand의 DSL을 사용한 코드입니다.

//in JavaPlugin
kommand {
    register("user") {
        then("create") {
            then("name" to string()) { //"name"이라는 이름의 String을 요청합니다.
                executes { context ->
                    val name: String by context
                    createUser(name) //명령어 실행 함수를 통해 실행
                }
            }
        }
        // 빠른 명령 작성 - then 함수와 동일
        "modify"("user" to dynamic { ... }) { // 동적 인수를 요청합니다.
            "name"("newName" to string()) {
                executes { context ->
                    val user: User by context
                    val newName: String = it["newName"]
                    setUserName(user, newName)
                }
            }
            "tag"("newTag" to string()) {
                executes {
                    //함수 인수에 의한 타입 추론
                    setUserTag(it["user"], it["newTag"])
                }
            }
        }
    }
}

분기문을 직접 작성하지 않고도 명령어를 실행 할 수 있는 코드가 완성되었습니다.

사용자가 명령문을 알맞게 작성하면 executes블록 내의 코드가 실행됩니다.


Gradle

repositories {
    mavenCentral()
}
dependencies {
    implementation("io.github.monun:kommand-api:<version>")
}

plugin.yml

name: ...
version: ...
main: ...
libraries:
  - io.github.monun:kommand-core:<version>

NOTE

  • 라이센스는 GPL-3.0이며 변경 혹은 삭제를 금합니다.

Contributors

  • patrick-choe
    • maven central 배포
    • 기본 Argument 일부 작성
    • mojang map 이용 환경 구축
  • Jhyub
    • DslMarker를 이용한 DSL 규칙 향상
  • pikokr
    • 1.18.2 지원

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages