Skip to content

Commit

Permalink
Merge pull request #32 from mani1232/master
Browse files Browse the repository at this point in the history
Update code structure and deps
  • Loading branch information
Matt-MX authored Jun 28, 2024
2 parents fdc34fc + 32ce24a commit 3578a8a
Show file tree
Hide file tree
Showing 85 changed files with 349 additions and 321 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.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.

29 changes: 11 additions & 18 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
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.kotlinJvm) apply true
alias(libs.plugins.shadow) apply true
`maven-publish`
}

repositories {
mavenCentral()
}

dependencies {
// compileOnly(kotlin("reflect"))
implementation(kotlin("reflect"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10")
compileOnly(libs.paper.api)
compileOnly(libs.placeholder.api)
implementation(libs.kotlin.reflect)
compileOnly(libs.kotlin.stdlib)
}

tasks.test {
Expand All @@ -24,8 +19,8 @@ version = rootProject.version

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

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
kotlin {
jvmToolchain(JavaVersion.VERSION_17.ordinal)
}

tasks {
Expand All @@ -38,15 +33,13 @@ tasks {
//// it.path.startsWith("kotlin") && !it.path.contains("reactive")
// it.name.startsWith("kotlin")
// }
archiveBaseName.set("ktgui")
mergeServiceFiles()
}
}

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

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
withJavadocJar()
withSourcesJar()
}
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/kotlin/com/mattmx/ktgui/GuiManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object GuiManager : Listener {
if (existingCommand != null) {
Bukkit.getPluginCommand(command.name)?.setExecutor(command)
} else {
if(!isInitialized()) {
if (!isInitialized()) {
throw RuntimeException("Unregistered commands are unsupported when GuiManager not initialised! Call GuiManager.init")
}

Expand Down Expand Up @@ -89,7 +89,8 @@ object GuiManager : Listener {
val aliasesField = SimpleCommandMap::class.java.getDeclaredField("aliases")
aliasesField.setAccessible(true)
knownAliases = aliasesField.get(cmdMap) as MutableSet<String?>
} catch (e: NoSuchFieldException) {}
} catch (e: NoSuchFieldException) {
}

event<PluginDisableEvent>(cmdPlugin) {
if (this.plugin == cmdPlugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AnimatedBossBar(var updateEvery: Long? = null) : LegacyBossBarBuilder() {
private lateinit var task: BukkitTask
var update: ((AnimatedBossBar) -> Unit)? = null

fun update(builder: AnimatedBossBar.() -> Unit) : AnimatedBossBar {
fun update(builder: AnimatedBossBar.() -> Unit): AnimatedBossBar {
this.update = builder
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ open class LegacyBossBarBuilder {
inline fun bossBar(builder: LegacyBossBarBuilder.() -> Unit) = bossBarDsl(LegacyBossBarBuilder(), builder)

@Deprecated("Simply use the BossBar class in adventure.", ReplaceWith("net.kyori.adventure.bossbar.BossBar"))
inline fun animatedBossBar(period: Long? = null, builder: AnimatedBossBar.() -> Unit) = bossBarDsl(AnimatedBossBar(period), builder)
inline fun animatedBossBar(period: Long? = null, builder: AnimatedBossBar.() -> Unit) =
bossBarDsl(AnimatedBossBar(period), builder)

@Deprecated("Simply use the BossBar class in adventure.", ReplaceWith("net.kyori.adventure.bossbar.BossBar"))
inline fun <T : LegacyBossBarBuilder> bossBarDsl(instance: T, builder: T.() -> Unit) : T {
inline fun <T : LegacyBossBarBuilder> bossBarDsl(instance: T, builder: T.() -> Unit): T {
builder(instance)
instance.build()
return instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mattmx.ktgui.commands

import com.mattmx.ktgui.scheduling.not
import com.mattmx.ktgui.utils.not
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ open class SimpleCommandBuilder(
constructor(name: String) : this(name, null)
constructor(name: String, vararg alias: String) : this(name, null, *alias)

infix fun permission(permission: String) : SimpleCommandBuilder {
infix fun permission(permission: String): SimpleCommandBuilder {
this.permission = permission
return this
}

infix fun alias(alias: String) : SimpleCommandBuilder {
infix fun alias(alias: String): SimpleCommandBuilder {
this.aliases.add(alias)
return this
}

fun noPermissions(cb: CommandInvocation.() -> Unit) : SimpleCommandBuilder {
fun noPermissions(cb: CommandInvocation.() -> Unit): SimpleCommandBuilder {
noPermissions = cb
return this
}

infix fun subCommand(commandBuilder: SimpleCommandBuilder) : SimpleCommandBuilder {
infix fun subCommand(commandBuilder: SimpleCommandBuilder): SimpleCommandBuilder {
subCommands.add(commandBuilder)
return this
}

infix fun hasPermission(executor: CommandSender) : Boolean {
infix fun hasPermission(executor: CommandSender): Boolean {
// todo check for subcommand permissions
return permission == null || executor.hasPermission(permission!!)
}

fun onCooldown(executes: CommandInvocation.() -> Unit) : SimpleCommandBuilder {
fun onCooldown(executes: CommandInvocation.() -> Unit): SimpleCommandBuilder {
this.cooldownCallback = executes
return this
}
Expand All @@ -76,7 +76,7 @@ open class SimpleCommandBuilder(
this.cooldownCallback?.invoke(invocation)
}

fun executes(execute: CommandInvocation.() -> Unit) : SimpleCommandBuilder {
fun executes(execute: CommandInvocation.() -> Unit): SimpleCommandBuilder {
this.execute = execute
return this
}
Expand All @@ -85,7 +85,7 @@ open class SimpleCommandBuilder(
this.execute = block
}

fun unknownSubcommand(unknown: CommandInvocation.() -> Unit) : SimpleCommandBuilder {
fun unknownSubcommand(unknown: CommandInvocation.() -> Unit): SimpleCommandBuilder {
this.unknown = unknown
return this
}
Expand All @@ -106,11 +106,11 @@ open class SimpleCommandBuilder(
this.suggests = suggest
}

fun allAliases() : List<String> {
fun allAliases(): List<String> {
return aliases.toMutableList() + name
}

fun getSuggestions(invocation: CommandInvocation) : List<String> {
fun getSuggestions(invocation: CommandInvocation): List<String> {
suggests?.also {
return it(invocation) ?: listOf()
} ?: run {
Expand All @@ -125,7 +125,7 @@ open class SimpleCommandBuilder(
return listOf()
}

infix fun suggestSubCommands(value: Boolean) : SimpleCommandBuilder {
infix fun suggestSubCommands(value: Boolean): SimpleCommandBuilder {
this.suggestSubCommands = value
return this
}
Expand All @@ -134,7 +134,7 @@ open class SimpleCommandBuilder(
if (isInConfig) {
Bukkit.getPluginCommand(name)?.setExecutor(DummyCommandExecutor(this))
} else {
if(!GuiManager.isInitialized()) {
if (!GuiManager.isInitialized()) {
throw RuntimeException("Unregistered commands are unsupported when GuiManager not initialised! Call GuiManager.init")
}
val cmdMapField = Bukkit.getServer().javaClass.getDeclaredField("commandMap")
Expand All @@ -150,11 +150,12 @@ open class SimpleCommandBuilder(
val aliasesField = SimpleCommandMap::class.java.getDeclaredField("aliases")
aliasesField.setAccessible(true)
knownAliases = aliasesField.get(cmdMap) as MutableSet<String?>
} catch (e: NoSuchFieldException) {}
} catch (e: NoSuchFieldException) {
}
val prefix = GuiManager.owningPlugin.name.lowercase()

event<PluginDisableEvent>(plugin = GuiManager.owningPlugin) {
if(plugin == GuiManager.owningPlugin) {
if (plugin == GuiManager.owningPlugin) {
synchronized(cmdMap) {
knownCommands.remove(name)
knownCommands.remove("$prefix:$name")
Expand All @@ -171,15 +172,16 @@ open class SimpleCommandBuilder(
}
}
}
fun couldBeCommand(arg: String) : Boolean {

fun couldBeCommand(arg: String): Boolean {
return name.startsWith(arg) || aliases.any { it.startsWith(arg) }
}

fun isCommand(arg: String) : Boolean {
fun isCommand(arg: String): Boolean {
return name == arg || aliases.any { it == arg }
}

fun getCommand(args: List<String>) : SimpleCommandBuilder? {
fun getCommand(args: List<String>): SimpleCommandBuilder? {
if (args.isEmpty()) return this
subCommands.forEach { cmd ->
if (cmd.isCommand(args[0])) {
Expand All @@ -202,21 +204,21 @@ class CommandInvocation(
val player: Player
get() = source as Player

fun player() : Player {
fun player(): Player {
return source as Player
}

operator fun get(index: Int) : String? {
operator fun get(index: Int): String? {
if (index >= args.size) return null
return args[index]
}

fun isNotEmpty() : Boolean = args.isNotEmpty()
fun isEmpty() : Boolean = args.isEmpty()
fun isNotEmpty(): Boolean = args.isNotEmpty()
fun isEmpty(): Boolean = args.isEmpty()
}

@Deprecated("No longer considered a 'simple command'", ReplaceWith("rawCommand"))
inline fun simpleCommand(cmd: (SimpleCommandBuilder.() -> Unit)) : SimpleCommandBuilder {
inline fun simpleCommand(cmd: (SimpleCommandBuilder.() -> Unit)): SimpleCommandBuilder {
val cmdB = SimpleCommandBuilder()
cmd(cmdB)
return cmdB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class Argument<S : CommandSender, T, V>(

fun description() = description

fun getDefaultSuggestions() : List<String>? {
fun getDefaultSuggestions(): List<String>? {
// todo need to invoke [suggests] with fake [CommandContext]
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ open class CommandContext<S : CommandSender>(
operator fun <T, V> ReadOnlyProperty<T, V>.getValue(
thisRef: T,
property: KProperty<*>
) : V {
): V {
return map[property.name] as V
}

fun <T, V> getValue(argument: Argument<S, T, V>) : V? {
fun <T, V> getValue(argument: Argument<S, T, V>): V? {
return if (::map.isInitialized) map[argument.id] as V? else null
}

Expand Down
5 changes: 4 additions & 1 deletion api/src/main/kotlin/com/mattmx/ktgui/commands/alpha/tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.mattmx.ktgui.commands.alpha

import kotlin.concurrent.thread

fun <S : CommandSender, T> KtCommandBuilder<S>.argument(description: String? = null, suggests: (CommandContext<S>.() -> List<String>)? = null) =
fun <S : CommandSender, T> KtCommandBuilder<S>.argument(
description: String? = null,
suggests: (CommandContext<S>.() -> List<String>)? = null
) =
Argument<S, T, String>(ArgumentType.REQUIRED_SINGLE, { c -> c.args[0] }, suggests).apply {
this withDescription description
this@argument.expectedArguments += this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mattmx.ktgui.commands.declarative.arg

import com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilder
import com.mattmx.ktgui.commands.declarative.invocation.InvalidArgContext
import com.mattmx.ktgui.commands.declarative.invocation.SuggestionInvocation
import com.mattmx.ktgui.commands.declarative.syntax.VariableType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ class Parser(
val optional = matchToken(SyntaxKind.QUESTION)
val closeDiamondBraces = matchToken(SyntaxKind.CLOSE_DIAMOND)

return VariableDeclarationSyntax(openDiamondBraces, varName, colon, type, ellipsis, optional, closeDiamondBraces)
return VariableDeclarationSyntax(
openDiamondBraces,
varName,
colon,
type,
ellipsis,
optional,
closeDiamondBraces
)
}

private fun parseCommand(): CommandDeclarationSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.mattmx.ktgui.commands.declarative.syntax

abstract class SyntaxNode {

abstract fun kind() : SyntaxKind
abstract fun kind(): SyntaxKind

open fun children() : List<SyntaxNode> {
open fun children(): List<SyntaxNode> {
val children = arrayListOf<SyntaxNode>()
// default impl of children, you should override this.
for (member in this.javaClass.declaredFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ class VariableDeclarationSyntax(

fun getType() = VariableType(typeToken.text!!, ellipsisToken.text != null, optional.text != null)

override fun children() = listOf(openDiamondBracesToken, varNameToken, colonToken, typeToken, closeDiamondBracesToken)
override fun children() =
listOf(openDiamondBracesToken, varNameToken, colonToken, typeToken, closeDiamondBracesToken)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fun interface CommandSuggestion<V> {
fun getLastArgSuggestion(invocation: SuggestionInvocation<*>) = getSuggestion(invocation)
?.filter { it.startsWith((invocation.rawArgs.lastOrNull() ?: ""), true) }

fun getValue(argumentString: String) : V? {
fun getValue(argumentString: String): V? {
return argumentString as V?
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mattmx.ktgui.commands.suggestions.impl

import com.mattmx.ktgui.commands.suggestions.CommandSuggestion
import com.mattmx.ktgui.commands.declarative.invocation.SuggestionInvocation
import com.mattmx.ktgui.commands.suggestions.CommandSuggestion
import org.bukkit.Bukkit
import org.bukkit.entity.Player

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mattmx.ktgui.components

import com.mattmx.ktgui.components.screen.GuiScreen
import com.mattmx.ktgui.components.signal.SignalListener
import com.mattmx.ktgui.components.signal.SignalOwner

class EffectBlock<T : GuiScreen>(
val owner: T,
Expand Down
Loading

0 comments on commit 3578a8a

Please sign in to comment.