Skip to content

Commit

Permalink
impl runServer task, better libs storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Jul 25, 2024
1 parent 8e8ef5d commit 70e51ea
Show file tree
Hide file tree
Showing 28 changed files with 371 additions and 315 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
/build/classes/kotlin/main/
/build/classes/kotlin/test/
/api/build
/plugin/build
/plugin/build
run/
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions .idea/modules/ktgui.main.iml

This file was deleted.

Empty file.
2 changes: 1 addition & 1 deletion api/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Program Files\Java\jdk-17\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\api\build\libs\ktgui-2.4.2-alpha-dev-all.jar D:\PC\Projects\KtBukkitGui\api\build\libs\api-2.4.2-alpha.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 3172.69 ms.
Finished after 2880.70 ms.
43 changes: 17 additions & 26 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
id("com.github.johnrengelman.shadow") version "7.0.0"
alias(libs.plugins.paperweight) apply true
alias(libs.plugins.kotlinJvm) apply true
alias(libs.plugins.shadow) apply true
`maven-publish`
}

val paper_version: String by rootProject

repositories {
mavenCentral()
}

dependencies {
// compileOnly(kotlin("reflect"))
shadow(implementation(kotlin("reflect"))!!)
// implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10")

paperweight.paperDevBundle(paper_version)
}
paperweight.paperDevBundle(libs.versions.paperApi.get())
compileOnly(libs.placeholder.api)

tasks.test {
useJUnitPlatform()
compileOnly(libs.kotlin.stdlib)
implementation(libs.kotlin.reflect)
}

version = rootProject.version

sourceSets["main"].resources.srcDir("src/resources/")

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}

tasks {
test {
useJUnitPlatform()
}

build {
dependsOn(shadowJar)
}

shadowJar {
mergeServiceFiles()
archiveBaseName.set("ktgui")
}

assemble {
dependsOn(reobfJar)
dependsOn("reobfJar")
}
}

tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
archiveBaseName.set("ktgui")
mergeServiceFiles()
kotlin {
jvmToolchain(17)
}

java {
Expand Down
5 changes: 4 additions & 1 deletion api/src/main/kotlin/com/mattmx/ktgui/GuiManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ object GuiManager : Listener {
lateinit var owningPlugin: JavaPlugin

fun init(plugin: JavaPlugin): Boolean {
pluginCache.cacheInstance(plugin::class.java, plugin)
if (initialized) {
pluginCache.cacheInstance(plugin::class.java, plugin)
return false
}

initialized = true
owningPlugin = plugin
// todo probably should change this
Scheduling.plugin = plugin

Bukkit.getPluginManager().registerEvents(this, plugin)
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ val ChainCommandBuilder.command
get() = build()

operator fun String.div(argument: Argument<*>) = ChainCommandBuilder(this).apply {
arguments.add(argument)
argument(argument)
}

@JvmName("div1")
operator fun String.div(argument: List<Argument<*>>) = ChainCommandBuilder(this).apply {
arguments.add(MultiArgument("multi-argument", *argument.toTypedArray()))
argument(MultiArgument("multi-argument", *argument.toTypedArray()))
}

operator fun String.div(s: String) = ChainCommandBuilder(this).apply {
Expand All @@ -57,5 +57,5 @@ operator fun String.div(subs: List<DeclarativeCommandBuilder>) = ChainCommandBui
operator fun DeclarativeCommandBuilder.plus(other: DeclarativeCommandBuilder) = listOf(this, other)

operator fun ChainCommandBuilder.div(argument: Argument<*>) = this.apply {
arguments.add(argument)
argument(argument)
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ open class DeclarativeCommandBuilder(
localArgumentSuggestions[name()] = suggest
}

fun getSuggestions(argument: Argument<*>?): CommandSuggestion<out Any?>? {
if (argument == null) return null
return localArgumentSuggestions[argument.name()]
?: argument.suggests.orElse(null)
fun getSuggestions(argument: Argument<*>?, invocation: StorageCommandContext<*>): Optional<Collection<String>> {
if (argument == null) return Optional.empty()
return Optional.ofNullable(
localArgumentSuggestions[argument.name()]?.getSuggestion(invocation)
?: argument.getSuggestions(invocation).orElse(null)
)
}

inline operator fun ChainCommandBuilder.invoke(block: DeclarativeSubCommandBuilder.() -> Unit) =
Expand Down Expand Up @@ -234,9 +236,9 @@ open class DeclarativeCommandBuilder(

if (result.isEmpty() && !arg.isRequired()) continue

val thisArgSuggestions = getSuggestions(arg)?.getLastArgSuggestion(context)
if (thisArgSuggestions != null) {
suggestedArgs.addAll(thisArgSuggestions)
val thisArgSuggestions = getSuggestions(arg, context)
if (thisArgSuggestions.isPresent) {
suggestedArgs.addAll(thisArgSuggestions.get())
}
}

Expand All @@ -253,17 +255,17 @@ open class DeclarativeCommandBuilder(
infix fun register(localObject: Any) = apply {
val permissionNodePrefix = buildAutomaticPermissions.orElse(null)
if (permissionNodePrefix != null) {
registerPermissionWithPrefixRecursively(permissionNodePrefix)
registerPermissionWithPrefixRecursively(permissionNodePrefix, 0)
}

val wrapper = DeclarativeCommandWrapper(name, this)
GuiManager.registerCommand(localObject::class.javaObjectType, wrapper)
}

fun registerPermissionWithPrefixRecursively(prefix: String) {
val finalPrefix = "$prefix.$name"
fun registerPermissionWithPrefixRecursively(prefix: String, depth: Int) {
val finalPrefix = if (depth == 0) name else "$prefix.$name"
for (sub in subcommands) {
sub.registerPermissionWithPrefixRecursively(finalPrefix)
sub.registerPermissionWithPrefixRecursively(finalPrefix, depth + 1)
}
registerPermissionWithPrefix(finalPrefix)
}
Expand Down Expand Up @@ -385,116 +387,14 @@ open class DeclarativeCommandBuilder(
val runnableContext =
RunnableCommandContext(context.sender, context.alias, context.rawArgs, argumentValues)

if (runs.isEmpty()) return

val executor = runs.entries.firstOrNull { (clazz, _) ->
clazz.isAssignableFrom(context.sender.javaClass)
}
?: return context.reply(!"&cThis command can only be ran by ${runs.values.joinToString("/") { "${it.javaClass.simpleName}s" }}.")
?: return context.reply(!"&cThis command can only be ran by ${runs.keys.joinToString(", ") { "${it.javaClass.simpleName}s" }}.")

executor.value.invoke(runnableContext)

// while (providedArgumentIndex < context.rawArgs.size) {
// val arg = context.rawArgs[providedArgumentIndex]
//
// // Check sub-commands
// // todo could match multiple subcommands
// val cmd = subcommands.firstOrNull { it.nameEquals(arg) }
// if (cmd != null) {
// return cmd.invoke(
// context.clone(context.rawArgs.subList(1, context.rawArgs.size))
// )
// }
//
// // Check arguments
// val expectedArg = expectedArguments.getOrNull(expectedArgumentIndex)
// if (expectedArg != null) {
// // Get full argument string using consumer
// val consumed =
// expectedArg.consumer.consume(context.rawArgs.subList(providedArgumentIndex, context.rawArgs.size))
// providedArgumentIndex += consumed.size
//
// // If the value is empty and IS required
// if (expectedArg.isRequired() && consumed.isEmpty()) {
// val missingArgContext =
// InvalidArgContext(context.sender, context.alias, context.rawArgs, expectedArg, null)
//
// // Try and invoke this specific missing arg
// if (expectedArg.invokeMissing(missingArgContext)) {
// return
// } else if (expectedArg.invokeInvalid(missingArgContext)) {
// return
// }
//
// // Invoke the global missing or invalid
// if (missing.isPresent) {
// missing.get().invoke(missingArgContext)
// } else if (invalid.isPresent) {
// invalid.get().invoke(missingArgContext)
// }
//
// return
// } else {
// var isValid = expectedArg.validate(consumed)
//
// val stringValue = consumed.joinToString(" ")
// val actualValue = expectedArg.getValueOfString(this, context, consumed)
//
// isValid = isValid && (expectedArg.isOptional() || actualValue != null)
//
// if (!isValid) {
// val invalidArgumentContext =
// InvalidArgContext(context.sender, context.alias, context.rawArgs, expectedArg, stringValue)
//
// if (expectedArg.invokeInvalid(invalidArgumentContext)) {
// return
// }
//
// invalid.ifPresent { it.invoke(invalidArgumentContext) }
// return
// }
//
// argumentValues[expectedArg.name()] = expectedArg.createContext(stringValue, actualValue)
//
// expectedArgumentIndex++
// }
// } else {
// if (unknownCommand.isPresent) {
// unknownCommand.get().invoke(context)
// } else {
// context.reply(!"&cUnknown sub-command or arguments.")
// }
// return
// }
// }
//
// val missing = expectedArguments.filter { arg ->
// !argumentValues.containsKey(arg.name())
// }
//
// if (missing.isNotEmpty()) {
// missing.forEach { arg ->
// val missingArgContext =
// InvalidArgContext(context.sender, context.alias, context.rawArgs, arg, null)
//
// // Try and invoke this specific missing arg
// if (arg.invokeMissing(missingArgContext)) {
// return
// } else if (arg.invokeInvalid(missingArgContext)) {
// return
// }
// }
// return
// }
//
// val runnableContext =
// RunnableCommandContext(context.sender, context.alias, context.rawArgs, argumentValues)
//
// val executor =
// runs.entries.firstOrNull { (clazz, _) ->
// clazz.isAssignableFrom(context.sender.javaClass)
// }
// ?: return context.reply(!"&cThis command can only be ran by ${runs.values.joinToString("/") { "${it.javaClass.simpleName}s" }}.")
//
// executor.value.invoke(runnableContext)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.mattmx.ktgui.commands.declarative
import com.mattmx.ktgui.commands.declarative.invocation.StorageCommandContext
import com.mattmx.ktgui.commands.declarative.invocation.SuggestionInvocation
import com.mattmx.ktgui.commands.wrapper.CommandWrapper
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import java.util.*

class DeclarativeCommandWrapper(
name: String,
val builder: DeclarativeCommandBuilder
) : CommandWrapper(name) {
val cachedUsage = builder.getUsage()

override fun execute(sender: CommandSender, commandLabel: String, args: Array<out String>): Boolean {
val context = StorageCommandContext(sender, commandLabel, args.toList())
Expand All @@ -33,4 +35,16 @@ class DeclarativeCommandWrapper(
return builder.permission.orElse(null)?.invoke(context) ?: false
}

override fun getAliases(): MutableList<String> {
return builder.aliases.toMutableList()
}

override fun getDescription(): String {
return builder.description
}

override fun getUsage() : String {
return cachedUsage
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ open class Argument<T : Any>(
return !invalidCallback.isEmpty()
}

open fun getSuggestions() {
TODO()
open fun getSuggestions(invocation: StorageCommandContext<*>): Optional<Collection<String>> {
return Optional.ofNullable(suggests.orElse(null)?.getLastArgSuggestion(invocation))
}

open fun getDefaultSuggestions(): Collection<String>? {
Expand Down
Loading

0 comments on commit 70e51ea

Please sign in to comment.