Skip to content

Commit

Permalink
fix: incorrect casting in a lot of data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
StillLutto committed Dec 13, 2024
1 parent df31397 commit f6eade9
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.annotations.ApiStatus
@ApiStatus.Internal
object CommandManager {
val registrars: Map<String, AbstractCommandRegistrar> = mapOf(
"1.20.4" to com.undefined.stellar.v1_20_4.CommandRegistrar,
"1.20.5" to com.undefined.stellar.v1_20_6.CommandRegistrar,
"1.20.6" to com.undefined.stellar.v1_20_6.CommandRegistrar,
"1.21" to com.undefined.stellar.v1_21.CommandRegistrar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
}

inline fun <reified C : CommandSender> addRequirement(noinline requirement: C.() -> Boolean): T =
addRequirements(StellarRequirement(requirement))
addRequirements(StellarRequirement(C::class, requirement))

fun setFailureMessages(messages: List<Component>): T {
failureMessages.clear()
Expand Down Expand Up @@ -158,7 +158,7 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
addGlobalFailureMessages(messages.toList())

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

Expand All @@ -173,7 +173,7 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
}

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

Expand All @@ -183,7 +183,7 @@ abstract class AbstractStellarCommand<T>(val name: String, var description: Stri
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,56 @@ abstract class AbstractStellarArgument<T>(val parent: AbstractStellarCommand<*>,
}

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

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

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

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

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

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

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

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

fun setSuggestionsWithoutTooltip(suggestion: List<String>): T {
suggestions.clear()
suggestions.add(StellarSuggestion<CommandSender> { suggestion.map { Suggestion(it, "") } })
suggestions.add(StellarSuggestion(CommandSender::class) { 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))
suggestions.add(StellarSuggestion(C::class, suggestion))
return this as T
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ abstract class CustomArgument<T>(
override val arguments: MutableList<AbstractStellarArgument<*>>
get() = (super.arguments + getArgumentsList()).toMutableList()
override val failureExecutions: MutableList<StellarExecution<*>>
get() = (super.failureExecutions + StellarExecution {
get() = (super.failureExecutions + StellarExecution(CommandSender::class) {
failureExecution(this, arguments.values.last())
}).toMutableList()
override val requirements: MutableList<StellarRequirement<*>>
get() = (super.requirements + StellarRequirement<CommandSender> { requirement() }).toMutableList()
get() = (super.requirements + StellarRequirement(CommandSender::class) { requirement() }).toMutableList()
override val executions: MutableList<StellarExecution<*>>
get() = (super.executions + StellarExecution {
get() = (super.executions + StellarExecution(CommandSender::class) {
execution(this, this.arguments.values.last())
}).toMutableList()
override val runnables: MutableList<StellarRunnable<*>>
get() = (super.runnables + StellarRunnable {
get() = (super.runnables + StellarRunnable(CommandSender::class) {
runnable(this, this[name])
}).toMutableList()
override val suggestions: MutableList<StellarSuggestion<*>>
get() = (super.suggestions + StellarSuggestion {
get() = (super.suggestions + StellarSuggestion(CommandSender::class) {
listSuggestions(this)
}).toMutableList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,56 @@ class GreedyWordArgument {
}

fun addSuggestion(suggestion: Suggestion): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion<CommandSender> { listOf(suggestion) })
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { listOf(suggestion) })
return this
}

fun addSuggestions(list: List<Suggestion>): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion<CommandSender> { list })
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { list })
return this
}

fun addSuggestions(vararg list: Suggestion): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion<CommandSender> { list.toList() })
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { list.toList() })
return this
}

fun addSuggestionsWithoutGreedyWordArgumentooltip(list: List<String>): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion<CommandSender> { list.map { Suggestion(it, "") } })
fun addSuggestionsWithoutGreedyWordArgumenTooltip(list: List<String>): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { list.map { Suggestion(it, "") } })
return this
}

fun addSuggestions(vararg list: String): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion<CommandSender> { list.map { Suggestion(it, "") } })
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { list.map { Suggestion(it, "") } })
return this
}

fun setSuggestions(vararg suggestion: Suggestion): GreedyWordArgument {
suggestions.clear()
suggestions.add(GreedyStellarSuggestion<CommandSender> { suggestion.toList() })
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { suggestion.toList() })
return this
}

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

fun setSuggestions(suggestion: List<Suggestion>): GreedyWordArgument {
suggestions.clear()
suggestions.add(GreedyStellarSuggestion<CommandSender> { suggestion.toList() })
suggestions.add(GreedyStellarSuggestion(CommandSender::class) { suggestion.toList() })
return this
}

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

inline fun <reified C : CommandSender> addSuggestion(noinline suggestion: GreedyCommandContext<C>.() -> List<Suggestion>): GreedyWordArgument {
suggestions.add(GreedyStellarSuggestion(suggestion))
suggestions.add(GreedyStellarSuggestion(C::class, suggestion))
return this
}

Expand All @@ -79,7 +79,7 @@ class GreedyWordArgument {
}

inline fun <reified C : CommandSender> addExecution(noinline execution: GreedyCommandContext<C>.() -> Unit): GreedyWordArgument {
executions.add(GreedyStellarExecution(execution))
executions.add(GreedyStellarExecution(C::class, execution))
return this
}

Expand All @@ -89,7 +89,7 @@ class GreedyWordArgument {
}

inline fun <reified C : CommandSender> addRunnable(noinline runnable: GreedyCommandContext<C>.() -> Boolean): GreedyWordArgument {
runnables.add(GreedyStellarRunnable(runnable))
runnables.add(GreedyStellarRunnable(C::class, runnable))
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender
typealias GreedyCommandNode = List<String>

@Suppress("UNCHECKED_CAST", "UNUSED")
class GreedyCommandContext<T : CommandSender>(val arguments: GreedyCommandNode, val source: T, val input: String) {
class GreedyCommandContext<T : CommandSender>(val arguments: GreedyCommandNode, val sender: T, val input: String) {
fun getArgument(index: Int): String = arguments[index]
operator fun get(index: Int) = arguments[index]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.undefined.stellar.data.execution

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

@Suppress("UNCHECKED_CAST")
data class GreedyStellarExecution<T : CommandSender>(val execution: GreedyCommandContext<T>.() -> Unit) {
data class GreedyStellarExecution<C : CommandSender>(val clazz: KClass<C>, val execution: GreedyCommandContext<C>.() -> Unit) {
operator fun invoke(context: GreedyCommandContext<CommandSender>) {
execution(context as? GreedyCommandContext<T> ?: return)
if (clazz.safeCast(context.sender) == null) return
execution(context as GreedyCommandContext<C>)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.undefined.stellar.data.execution

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

@Suppress("UNCHECKED_CAST")
data class GreedyStellarRunnable<C : CommandSender>(val execution: GreedyCommandContext<C>.() -> Boolean) {
data class GreedyStellarRunnable<C : CommandSender>(val clazz: KClass<C>, val execution: GreedyCommandContext<C>.() -> Boolean) {
operator fun invoke(context: GreedyCommandContext<CommandSender>): Boolean {
return execution(context as? GreedyCommandContext<C> ?: return true)
if (clazz.safeCast(context.sender) == null) return true
return execution(context as GreedyCommandContext<C>)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ 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

@Suppress("UNCHECKED_CAST")
data class StellarExecution<T : CommandSender>(val execution: CommandContext<T>.() -> Unit) {
data class StellarExecution<C : CommandSender>(val clazz: KClass<C>, val execution: CommandContext<C>.() -> Unit) {
operator fun invoke(context: CommandContext<CommandSender>) {
execution(context as? CommandContext<T> ?: return)
if (clazz.safeCast(context.sender) == null) return
execution(context as? CommandContext<C> ?: return)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ 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

@Suppress("UNCHECKED_CAST")
data class StellarRunnable<C : CommandSender>(val execution: CommandContext<C>.() -> Boolean) {
data class StellarRunnable<C : CommandSender>(val clazz: KClass<C>, val execution: CommandContext<C>.() -> Boolean) {
operator fun invoke(context: CommandContext<CommandSender>): Boolean {
if (clazz.safeCast(context.sender) == null) return true
return execution(context as? CommandContext<C> ?: return true)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.undefined.stellar.data.requirement

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

@Suppress("UNCHECKED_CAST")
data class StellarRequirement<C : CommandSender>(val execution: C.() -> Boolean) {
data class StellarRequirement<C : CommandSender>(val clazz: KClass<C>, val requirement: C.() -> Boolean) {
operator fun invoke(sender: CommandSender): Boolean {
return execution(sender as? C ?: return true)
return requirement(clazz.safeCast(sender) ?: return true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package com.undefined.stellar.data.suggestion

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

data class GreedyStellarSuggestion<C : CommandSender>(val suggestion: GreedyCommandContext<C>.() -> List<Suggestion>) {
fun get(context: GreedyCommandContext<C>): List<Suggestion> = suggestion(context)
data class GreedyStellarSuggestion<C : CommandSender>(val clazz: KClass<C>, val suggestion: GreedyCommandContext<C>.() -> List<Suggestion>) {
fun get(context: GreedyCommandContext<C>): List<Suggestion> {
if (clazz.safeCast(context.sender) == null) return listOf()
return suggestion(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ 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<C : CommandSender>(val suggestion: CommandContext<C>.() -> List<Suggestion>) {
fun get(context: CommandContext<C>): List<Suggestion> = suggestion(context)
data class StellarSuggestion<C : CommandSender>(val clazz: KClass<C>, val suggestion: CommandContext<C>.() -> List<Suggestion>) {
fun get(context: CommandContext<C>): List<Suggestion> {
if (clazz.safeCast(context.sender) == null) return listOf()
return suggestion(context)
}
}
6 changes: 3 additions & 3 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ val groupIdVar = "com.undefined"
val artifactIdVar = "stellar"

dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.5-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.20.4-R0.1-SNAPSHOT")

implementation(project(":api"))
implementation(project(":common"))
Expand All @@ -19,7 +19,7 @@ dependencies {
implementation(project(":v1_21_1:", "reobf"))
implementation(project(":v1_21_3:", "reobf"))
implementation(project(":v1_21_4:", "reobf"))
compileOnly(project(":v1_21_4"))
compileOnly(project(":v1_20_4"))
}

tasks {
Expand All @@ -36,7 +36,7 @@ tasks {
}

runServer {
minecraftVersion("1.20.5")
minecraftVersion("1.20.4")
jvmArgs("-Xmx2G")
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/kotlin/com/undefined/stellar/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Main : JavaPlugin() {
override fun onEnable() {
StellarCommand("test")
.addLocationArgument("location")
.addWolfVariantArgument("test")
.addStringArgument("string")
.addExecution<Player> {
sender.sendMessage(arguments.toList().withIndex().joinToString(", ") { "${it.index}: ${it.value.first}" })
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Stellar
version: '${version}'
main: com.undefined.stellar.Main
api-version: '1.20.6'
api-version: '1.20'
Loading

0 comments on commit f6eade9

Please sign in to comment.