Skip to content

Commit

Permalink
refactor: basically recode this entire system
Browse files Browse the repository at this point in the history
  • Loading branch information
StillLutto committed Dec 8, 2024
1 parent d868b18 commit f98a0cd
Show file tree
Hide file tree
Showing 114 changed files with 831 additions and 1,467 deletions.
10 changes: 5 additions & 5 deletions api/src/main/kotlin/com/undefined/stellar/StellarCommand.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.undefined.stellar

import com.undefined.stellar.exception.UnsupportedVersionException
import com.undefined.stellar.listener.StellarListener
import com.undefined.stellar.manager.CommandManager
import com.undefined.stellar.util.NMSVersion
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.annotations.ApiStatus

class StellarCommand(name: String, description: String = "", vararg aliases: String = arrayOf()) : AbstractStellarCommand<StellarCommand>(name, description) {

Expand All @@ -18,16 +17,17 @@ class StellarCommand(name: String, description: String = "", vararg aliases: Str
}

override fun register(plugin: JavaPlugin) {
StellarCommands.commands.add(this)
CommandManager.initialize(plugin)
val registrar = CommandManager.registrars[NMSVersion.version] ?: throw UnsupportedVersionException()
registrar.register(this)
CommandManager.initialize(plugin)
StellarCommands.commands.add(this)
}

companion object {
@ApiStatus.Internal
fun parseAndReturnCancelled(sender: CommandSender, input: String): Boolean {
val registrar = CommandManager.registrars[NMSVersion.version] ?: throw UnsupportedVersionException()
return registrar.parseAndReturnCancelled(sender, input)
return registrar.handleCommandFailure(sender, input)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.undefined.stellar

import com.undefined.stellar.data.HideDefaultFailureMessages
import com.undefined.stellar.data.execution.CustomStellarExecution
import com.undefined.stellar.data.requirement.PermissionStellarRequirement
import com.undefined.stellar.data.argument.CommandContext
import com.undefined.stellar.data.execution.StellarExecution
import com.undefined.stellar.data.execution.StellarRunnable
import com.undefined.stellar.data.requirement.PermissionStellarRequirement
import com.undefined.stellar.data.requirement.StellarRequirement
import com.undefined.stellar.sub.SubCommandHandler
import com.undefined.stellar.sub.custom.CustomSubCommand
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.annotations.ApiStatus

Expand All @@ -21,22 +19,14 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
override fun getBase(): AbstractStellarCommand<*> = this

@ApiStatus.Internal val aliases: MutableList<String> = mutableListOf()
@ApiStatus.Internal val failureMessages: MutableList<Component> = mutableListOf()
@ApiStatus.Internal val globalFailureMessages: MutableList<Component> = mutableListOf()
@ApiStatus.Internal val failureExecutions: MutableList<CustomStellarExecution<*, Any>> = mutableListOf()
@ApiStatus.Internal var hideDefaultFailureMessages: HideDefaultFailureMessages = HideDefaultFailureMessages(false, false)
@ApiStatus.Internal private val _requirements: MutableList<StellarRequirement<*>> = mutableListOf()
val requirements: List<StellarRequirement<*>>
get() {
if (this is CustomSubCommand<*>)
_requirements.addFirst(
StellarRequirement(CommandSender::class) { this@AbstractStellarCommand.requirement() }
)
return _requirements
}
@ApiStatus.Internal val permissionRequirements: MutableList<PermissionStellarRequirement> = mutableListOf()
@ApiStatus.Internal val executions: MutableList<StellarExecution<*>> = mutableListOf()
@ApiStatus.Internal val runnables: MutableList<StellarRunnable<*>> = mutableListOf()
@ApiStatus.Internal open val failureMessages: MutableList<Component> = mutableListOf()
@ApiStatus.Internal open val globalFailureMessages: MutableList<Component> = mutableListOf()
@ApiStatus.Internal open val failureExecutions: MutableList<StellarExecution<*>> = mutableListOf()
@ApiStatus.Internal open var hideDefaultFailureMessages: HideDefaultFailureMessages = HideDefaultFailureMessages(false, false)
@ApiStatus.Internal open val requirements: MutableList<StellarRequirement<*>> = mutableListOf()
@ApiStatus.Internal open val permissionRequirements: MutableList<PermissionStellarRequirement> = mutableListOf()
@ApiStatus.Internal open val executions: MutableList<StellarExecution<*>> = mutableListOf()
@ApiStatus.Internal open val runnables: MutableList<StellarRunnable<*>> = mutableListOf()

fun addAlias(name: String): T {
aliases.add(name)
Expand Down Expand Up @@ -64,12 +54,13 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
}

inline fun <reified C : CommandSender> addRequirement(noinline requirement: C.() -> Boolean): T {
addRequirement(StellarRequirement(C::class, requirement))
addRequirement(StellarRequirement(requirement))
return this as T
}

fun addRequirement(requirement: StellarRequirement<*>) {
_requirements.add(requirement)
fun addRequirement(requirement: StellarRequirement<*>): T {
requirements.add(requirement)
return this as T
}

fun addFailureMessage(message: String): T {
Expand All @@ -92,9 +83,8 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
return this as T
}

@Suppress("UNCHECKED_CAST")
inline fun <reified C : CommandSender> addFailureExecution(noinline execution: C.(String) -> Unit): T {
failureExecutions.add(CustomStellarExecution(C::class, execution) as CustomStellarExecution<*, Any>)
inline fun <reified C : CommandSender> addFailureExecution(noinline execution: CommandContext<C>.() -> Unit): T {
failureExecutions.add(StellarExecution(execution))
return this as T
}

Expand All @@ -103,13 +93,13 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
return this as T
}

inline fun <reified C : CommandSender> addExecution(noinline execution: C.() -> Unit): T {
executions.add(StellarExecution(C::class, execution))
inline fun <reified C : CommandSender> addExecution(noinline execution: CommandContext<C>.() -> Unit): T {
executions.add(StellarExecution(execution))
return this as T
}

inline fun <reified C : CommandSender> alwaysRun(noinline execution: C.() -> Boolean): T {
runnables.add(StellarRunnable(C::class, execution))
inline fun <reified C : CommandSender> addRunnable(noinline execution: CommandContext<C>.() -> Boolean): T {
runnables.add(StellarRunnable(execution))
return this as T
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.undefined.stellar

import org.jetbrains.annotations.ApiStatus

object StellarCommands {
@ApiStatus.Internal
val commands: MutableList<AbstractStellarCommand<*>> = mutableListOf()
@ApiStatus.Internal
fun getStellarCommand(command: String): AbstractStellarCommand<*>? = commands.firstOrNull { it.name == command || it.aliases.contains(command) }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package com.undefined.stellar.data.argument

import java.util.*
import com.undefined.stellar.exception.ArgumentCastMismatchException
import org.bukkit.command.CommandSender

typealias CommandNode = LinkedList<Any?>
typealias CommandNode = HashMap<String, (CommandContext<CommandSender>) -> Any?>

@Suppress("UNCHECKED_CAST", "UNUSED")
class CommandContext<T : CommandSender>(val arguments: CommandNode, val source: T, val input: String) {

inline fun <reified T> getArgument(name: String): T {
val argument = arguments[name] ?: throw NoSuchElementException("No argument with the name $name")
val context = this as CommandContext<CommandSender>
return argument(context) as? T
?: throw ArgumentCastMismatchException("Argument of index $name cannot be cast into ${T::class.simpleName}!")
}

inline fun <reified T> getArgument(index: Int): T =
arguments.values.toList()[index](this as CommandContext<CommandSender>) as? T
?: throw ArgumentCastMismatchException("Argument of index $index cannot be cast into ${T::class.simpleName}!")

operator fun get(index: Int) = arguments.values.toList()[index](this as CommandContext<CommandSender>)
operator fun get(name: String) = (arguments[name]
?: throw NoSuchElementException("No argument with the name $name"))(this as CommandContext<CommandSender>)

class CommandContext(val arguments: CommandNode, val input: String) {
fun <T> getSubCommand(int: Int): T = arguments[int] as T
operator fun get(int: Int) = arguments[int]
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.undefined.stellar.data.execution

import com.undefined.stellar.data.argument.CommandContext
import org.bukkit.command.CommandSender

data class ReturnableStellarExecution<C : CommandSender, V : Any?>(val execution: CommandContext<C>.(V) -> Unit) {
operator fun invoke(context: CommandContext<CommandSender>, value: V) {
execution(context as? CommandContext<C> ?: return, value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.undefined.stellar.data.execution

import com.undefined.stellar.data.argument.CommandContext
import org.bukkit.command.CommandSender

data class ReturnableStellarRunnable<C : CommandSender, V : Any?>(val runnable: CommandContext<C>.(V) -> Boolean) {
operator fun invoke(context: CommandContext<CommandSender>, value: V): Boolean {
return runnable(context as? CommandContext<C> ?: return true, value)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.undefined.stellar.data.execution

import com.undefined.stellar.data.argument.CommandContext
import org.bukkit.command.CommandSender
import kotlin.reflect.KClass
import kotlin.reflect.safeCast

data class StellarExecution<T : CommandSender>(val kClass: KClass<T>, val execution: T.() -> Unit) {
fun run(sender: CommandSender) {
execution.invoke(kClass.safeCast(sender) ?: return)
data class StellarExecution<T : CommandSender>(val execution: CommandContext<T>.() -> Unit) {
operator fun invoke(context: CommandContext<CommandSender>) {
execution(context as? CommandContext<T> ?: return)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.undefined.stellar.data.execution

import com.undefined.stellar.data.argument.CommandContext
import org.bukkit.command.CommandSender
import kotlin.reflect.KClass
import kotlin.reflect.safeCast

data class StellarRunnable<T : CommandSender>(val kClass: KClass<T>, val execution: T.() -> Boolean) {
fun run(sender: CommandSender): Boolean {
return execution.invoke(kClass.safeCast(sender) ?: return false)
data class StellarRunnable<C : CommandSender>(val execution: CommandContext<C>.() -> Boolean) {
fun run(context: CommandContext<CommandSender>): Boolean {
return execution(context as? CommandContext<C> ?: return true)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.undefined.stellar.data.requirement

import org.bukkit.command.CommandSender
import kotlin.reflect.KClass
import kotlin.reflect.safeCast

data class StellarRequirement<T : CommandSender>(val kClass: KClass<T>, val execution: T.() -> Boolean) {
fun run(sender: CommandSender): Boolean {
return execution.invoke(kClass.safeCast(sender) ?: return false)
data class StellarRequirement<C : CommandSender>(val execution: C.() -> Boolean) {
operator fun invoke(sender: CommandSender): Boolean {
return execution(sender as? C ?: return true)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.undefined.stellar.data.suggestion

import com.undefined.stellar.data.argument.CommandContext
import org.bukkit.command.CommandSender
import kotlin.reflect.KClass
import kotlin.reflect.safeCast

data class StellarSuggestion<T : CommandSender>(val kClass: KClass<T>, val suggestion: T.(input: String) -> List<Suggestion>) {
fun get(sender: CommandSender, input: String): List<Suggestion> {
return suggestion(kClass.safeCast(sender) ?: return listOf(), input)
}
data class StellarSuggestion<C : CommandSender>(val suggestion: CommandContext<C>.() -> List<Suggestion>) {
fun get(context: CommandContext<C>): List<Suggestion> = suggestion(context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.undefined.stellar.exception

class ArgumentCastMismatchException(message: String) : RuntimeException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.undefined.stellar.exception

class LiteralArgumentMismatchException : RuntimeException("This argument is of type Literal, thus you cannot get it's value!")
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,76 @@ package com.undefined.stellar.sub

import com.undefined.stellar.AbstractStellarCommand
import com.undefined.stellar.data.argument.CommandContext
import com.undefined.stellar.data.suggestion.StellarSuggestion
import com.undefined.stellar.data.suggestion.Suggestion
import org.bukkit.command.CommandSender
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.annotations.ApiStatus

abstract class AbstractStellarSubCommand<T>(val parent: AbstractStellarCommand<*>, name: String) : AbstractStellarCommand<T>(name) {

@ApiStatus.Internal open val suggestions: MutableList<StellarSuggestion<*>> = mutableListOf()

fun addSuggestion(text: String, tooltip: String): T {
addSuggestion(Suggestion(text, tooltip))
return this as T
}

fun addSuggestion(suggestion: Suggestion): T {
suggestions.add(StellarSuggestion<CommandSender> { listOf(suggestion) })
return this as T
}

fun addSuggestions(list: List<Suggestion>): T {
suggestions.add(StellarSuggestion<CommandSender> { list })
return this as T
}

fun addSuggestions(vararg list: Suggestion): T {
suggestions.add(StellarSuggestion<CommandSender> { list.toList() })
return this as T
}

fun addSuggestionsWithoutTooltip(list: List<String>): T {
suggestions.add(StellarSuggestion<CommandSender> { list.map { Suggestion(it, "") } })
return this as T
}

fun addSuggestions(vararg list: String): T {
suggestions.add(StellarSuggestion<CommandSender> { list.map { Suggestion(it, "") } })
return this as T
}

fun setSuggestions(vararg suggestion: Suggestion): T {
suggestions.clear()
suggestions.add(StellarSuggestion<CommandSender> { suggestion.toList() })
return this as T
}

fun setSuggestions(vararg suggestion: String): T {
suggestions.clear()
suggestions.add(StellarSuggestion<CommandSender> { suggestion.map { Suggestion(it, "") } })
return this as T
}

fun setSuggestions(suggestion: List<Suggestion>): T {
suggestions.clear()
suggestions.add(StellarSuggestion<CommandSender> { suggestion.toList() })
return this as T
}

fun setSuggestionsWithoutTooltip(suggestion: List<String>): T {
suggestions.clear()
suggestions.add(StellarSuggestion<CommandSender> { suggestion.map { Suggestion(it, "") } })
return this as T
}

inline fun <reified C : CommandSender> addSuggestion(noinline suggestion: CommandContext<C>.() -> List<Suggestion>): T {
suggestions.add(StellarSuggestion(suggestion))
return this as T
}

override fun getBase(): AbstractStellarCommand<*> = parent.getBase()
override fun register(plugin: JavaPlugin) = parent.register(plugin)

}
Loading

0 comments on commit f98a0cd

Please sign in to comment.