generated from UndefinedCreations/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(class-commands): add class commands
- Loading branch information
1 parent
e665990
commit 8d8f868
Showing
3 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
api/src/main/kotlin/com/undefined/stellar/BaseStellarCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.undefined.stellar | ||
|
||
import org.bukkit.plugin.java.JavaPlugin | ||
|
||
abstract class BaseStellarCommand(val name: String, val description: String = "", val permissions: List<String> = listOf()) { | ||
|
||
var hasInitializedArguments = false | ||
private set | ||
private var hasBeenRegistered = false | ||
|
||
val command: StellarCommand by lazy { | ||
setup().apply { addRequirements(*permissions.toTypedArray()) } | ||
} | ||
|
||
private fun initializeArguments() { | ||
if (hasInitializedArguments) return | ||
hasInitializedArguments = true | ||
for (argument in arguments()) command.addArgument(argument.getFullArgument()) | ||
} | ||
|
||
abstract fun setup(): StellarCommand | ||
open fun arguments(): List<StellarArgument> = listOf() | ||
|
||
fun createCommand(init: StellarCommand.() -> Unit): StellarCommand { | ||
val command = StellarCommand(name, permissions) | ||
command.setDescription(description) | ||
command.init() | ||
return command | ||
} | ||
|
||
fun getFullCommand(): StellarCommand { | ||
initializeArguments() | ||
return command | ||
} | ||
|
||
fun register(plugin: JavaPlugin) { | ||
if (hasBeenRegistered) return | ||
getFullCommand().register(plugin) | ||
hasBeenRegistered = true | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
api/src/main/kotlin/com/undefined/stellar/StellarArgument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.undefined.stellar | ||
|
||
import com.undefined.stellar.argument.AbstractStellarArgument | ||
|
||
abstract class StellarArgument(val type: AbstractStellarArgument<*>, val permissions: List<String>) { | ||
|
||
constructor(type: AbstractStellarArgument<*>, vararg permissions: String) : this(type, permissions.toList()) | ||
|
||
val argument: AbstractStellarArgument<*> by lazy { | ||
setup().apply { addRequirements(*permissions.toTypedArray()) } | ||
} | ||
|
||
abstract fun setup(): AbstractStellarArgument<*> | ||
open fun arguments(): List<StellarArgument> = listOf() | ||
|
||
fun createArgument(init: AbstractStellarArgument<*>.() -> Unit): AbstractStellarArgument<*> = type.apply { init() } | ||
|
||
fun getFullArgument(): AbstractStellarArgument<*> { | ||
val command = setup() | ||
command.addRequirements(*permissions.toTypedArray()) | ||
for (argument in arguments()) command.addArgument(argument.getFullArgument()) | ||
return command | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
package com.undefined.stellar | ||
|
||
import com.undefined.stellar.argument.types.misc.UUIDArgument | ||
import org.bukkit.entity.Player | ||
import org.bukkit.plugin.java.JavaPlugin | ||
|
||
class TestCommand : BaseStellarCommand("test", "description") { | ||
override fun setup() = createCommand { | ||
addExecution<Player> { | ||
sender.sendMessage("hi!") | ||
} | ||
} | ||
|
||
override fun arguments(): List<StellarArgument> = listOf(TestArgument(this.command)) | ||
} | ||
|
||
class TestArgument(parent: AbstractStellarCommand<*>) : StellarArgument(UUIDArgument(parent, "sub")) { | ||
override fun setup() = createArgument { | ||
addExecution<Player> { | ||
sender.sendMessage("Hello there! Argument.") | ||
} | ||
} | ||
} | ||
|
||
class Main : JavaPlugin() { | ||
|
||
override fun onEnable() { | ||
val main = StellarCommand("test") | ||
main.addGreedyStringArgument("args") | ||
.addWordSuggestions(1, "test") | ||
.register(this) | ||
val command = TestCommand() | ||
command.register(this) | ||
println(command.name) | ||
} | ||
|
||
} |