Skip to content

Commit

Permalink
improve command api
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Dec 21, 2024
1 parent 5ec1eab commit d301e14
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package ru.astrainteractive.astralibs.command.api.argumenttype

import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import ru.astrainteractive.astralibs.command.api.exception.BukkitCommandException
import ru.astrainteractive.astralibs.command.api.exception.NoPlayerException

object OfflinePlayerArgument : ArgumentType<OfflinePlayer> {
override val key: String = "OfflinePlayerArgument"

override fun transform(value: String): OfflinePlayer {
val offlinePlayer = Bukkit.getOfflinePlayer(value)
if (offlinePlayer.name == null) throw BukkitCommandException.NoPlayerException(value)
if (offlinePlayer.name == null) throw NoPlayerException(value)
return offlinePlayer
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package ru.astrainteractive.astralibs.command.api.argumenttype

import org.bukkit.Bukkit
import org.bukkit.entity.Player
import ru.astrainteractive.astralibs.command.api.exception.BukkitCommandException
import ru.astrainteractive.astralibs.command.api.exception.NoPlayerException

data object OnlinePlayerArgument : ArgumentType<Player> {
override val key: String = "OnlinePlayerArgument"
override fun transform(value: String): Player {
return Bukkit.getOnlinePlayers()
.firstOrNull { it.name == value }
?: throw BukkitCommandException.NoPlayerException(value)
return Bukkit.getPlayer(value) ?: throw NoPlayerException(value)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.astrainteractive.astralibs.command.api.argumenttype

import org.bukkit.potion.PotionEffectType
import ru.astrainteractive.astralibs.command.api.exception.BukkitCommandException
import ru.astrainteractive.astralibs.command.api.exception.NoPotionEffectTypeException

data object PotionEffectTypeArgument : ArgumentType<PotionEffectType> {
override val key: String = "PotionEffectTypeArgument"
override fun transform(value: String): PotionEffectType {
return value.let(PotionEffectType::getByName)
?: throw BukkitCommandException.NoPotionEffectTypeException(value)
?: throw NoPotionEffectTypeException(value)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package ru.astrainteractive.astralibs.command.api.context

import ru.astrainteractive.astralibs.command.api.argumenttype.ArgumentType
import ru.astrainteractive.astralibs.command.api.exception.DefaultCommandException
import ru.astrainteractive.astralibs.command.api.exception.BadArgumentException
import ru.astrainteractive.astralibs.command.api.exception.NoPermissionException
import ru.astrainteractive.astralibs.permission.Permission

object BukkitCommandContextExt {
fun BukkitCommandContext.requirePermission(permission: Permission) {
if (sender.hasPermission(permission.value)) return
throw DefaultCommandException.NoPermissionException(permission)
throw NoPermissionException(permission)
}

fun <T : Any> BukkitCommandContext.requireArgument(index: Int, type: ArgumentType<T>): T {
val raw = args.getOrNull(index)
return raw?.let(type::transform) ?: throw DefaultCommandException.BadArgumentException(raw, type)
return raw?.let(type::transform) ?: throw BadArgumentException(raw, type)
}

fun <T : Any> BukkitCommandContext.findArgument(index: Int, type: ArgumentType<T>): T? {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ object PluginExt {
true
}
}

fun JavaPlugin.setCommandExecutor(
alias: String,
commandExecutor: CommandExecutor<BukkitCommandContext>,
) {
val javaCommand = getCommand(alias) ?: error("Command with alias $alias not found")
javaCommand.setExecutor { sender, bukkitCommand, label, args ->
val commandContext = BukkitCommandContext(
sender = sender,
command = bukkitCommand,
label = label,
args = args
)
commandExecutor.execute(commandContext)
true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.astrainteractive.astralibs.command.api.argumenttype

import ru.astrainteractive.astralibs.command.api.exception.DefaultCommandException
import ru.astrainteractive.astralibs.command.api.exception.ArgumentTypeException
import kotlin.jvm.Throws

interface ArgumentType<T : Any> {
val key: String

@Throws(DefaultCommandException.ArgumentTypeException::class)
@Throws(ArgumentTypeException::class)
fun transform(value: String): T

class Lambda<T : Any>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.astrainteractive.astralibs.command.api.argumenttype

import ru.astrainteractive.astralibs.command.api.exception.ArgumentTypeException
import kotlin.enums.EnumEntries

/**
* Enum argument is used to define enumeration of arguments
* @see EnumArgumentType
*/
interface EnumArgument {
/**
* This value will be checked for command argument
*/
val value: String
}

/**
* When your command is like /amarket <open|expired|players> - you can use enums
*/
class EnumArgumentType<T>(
private val entries: EnumEntries<T>
) : PrimitiveArgumentType<Enum<T>> where T : Enum<T>, T : EnumArgument {
override val key: String = "ENUM"

override fun transform(value: String): Enum<T> {
return entries.firstOrNull { entry -> entry.value.equals(value, ignoreCase = true) }
?.ordinal
?.let(entries::getOrNull)
?: throw ArgumentTypeException(key, value)
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package ru.astrainteractive.astralibs.command.api.argumenttype

import ru.astrainteractive.astralibs.command.api.exception.DefaultCommandException.ArgumentTypeException
import ru.astrainteractive.astralibs.command.api.exception.ArgumentTypeException

interface PrimitiveArgumentType<T : Any> : ArgumentType<T> {
data object Int : PrimitiveArgumentType<kotlin.Int> {
override val key: kotlin.String = "INT"
override fun transform(value: kotlin.String): kotlin.Int {
return value.toIntOrNull() ?: throw ArgumentTypeException(Double.key, value)
}
interface PrimitiveArgumentType<T : Any> : ArgumentType<T>

data object IntArgumentType : PrimitiveArgumentType<Int> {
override val key: String = "INT"
override fun transform(value: String): Int {
return value.toIntOrNull() ?: throw ArgumentTypeException(key, value)
}
}

data object String : PrimitiveArgumentType<kotlin.String> {
override val key: kotlin.String = "STRING"
override fun transform(value: kotlin.String): kotlin.String {
return value
}
data object StringArgumentType : PrimitiveArgumentType<String> {
override val key: String = "STRING"
override fun transform(value: String): String {
return value
}
}

data object Double : PrimitiveArgumentType<kotlin.Double> {
override val key: kotlin.String = "DOUBLE"
override fun transform(value: kotlin.String): kotlin.Double {
return value.toDoubleOrNull() ?: throw ArgumentTypeException(key, value)
}
data object DoubleArgumentType : PrimitiveArgumentType<Double> {
override val key: String = "DOUBLE"
override fun transform(value: String): Double {
return value.toDoubleOrNull() ?: throw ArgumentTypeException(key, value)
}
}

data object Boolean : PrimitiveArgumentType<kotlin.Boolean> {
override val key: kotlin.String = "BOOLEAN"
override fun transform(value: kotlin.String): kotlin.Boolean {
return value.toBooleanStrictOrNull() ?: throw ArgumentTypeException(Double.key, value)
}
data object BooleanArgumentType : PrimitiveArgumentType<Boolean> {
override val key: String = "BOOLEAN"
override fun transform(value: String): Boolean {
return value.toBooleanStrictOrNull() ?: throw ArgumentTypeException(key, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ package ru.astrainteractive.astralibs.command.api.exception

import ru.astrainteractive.astralibs.command.api.argumenttype.ArgumentType
import ru.astrainteractive.astralibs.permission.Permission
import ru.astrainteractive.astralibs.string.StringDesc

sealed class DefaultCommandException(message: String) : CommandException(message) {
class BadArgumentException(
wrongArgument: String?,
type: ArgumentType<*>
) : DefaultCommandException("Incompatible type $type for argument $wrongArgument")

class ArgumentTypeException(
key: String,
value: String
) : DefaultCommandException("Argument type $key could not parse $value")

class NoPermissionException(
permission: Permission
) : DefaultCommandException("No permission: $permission")
}
sealed class DefaultCommandException(message: String) : CommandException(message)

class BadArgumentException(
wrongArgument: String?,
type: ArgumentType<*>
) : DefaultCommandException("Incompatible type $type for argument $wrongArgument")

class ArgumentTypeException(
key: String,
value: String
) : DefaultCommandException("Argument type $key could not parse $value")

class NoPermissionException(
permission: Permission
) : DefaultCommandException("No permission: $permission")

class NoPlayerException(
name: String
) : DefaultCommandException("Player $name not found")

class NoPotionEffectTypeException(
name: String
) : DefaultCommandException("PotionEffectType $name not found")

class StringDescException(val stringDesc: StringDesc) : CommandException("Specific StringDesc exception: $stringDesc")
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import kotlinx.serialization.Serializable
sealed interface StringDesc {
val raw: String

/**
* Raw is used to convert raw text string into colored components
* example: #FF0000[Title] #00FF00This is #0000FFCOLORED #00FF00Text format!
*/
@JvmInline
@Serializable(with = RawStringDescSerializer::class)
value class Raw(override val raw: String) : StringDesc

/**
* Plain is used for plain text without any colors etc.
* example: Hello world, this is simple plain text format!
*/
@JvmInline
@Serializable(with = PlainStringDescSerializer::class)
value class Plain(override val raw: String) : StringDesc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object StringDescExt {
}

operator fun StringDesc.plus(other: StringDesc): StringDesc {
return when (other) {
return when (this) {
is StringDesc.Raw -> plus(other.raw)
is StringDesc.Plain -> plus(other.raw)
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ makeevrserg.java.ktarget=21
# Project
makeevrserg.project.name=AstraLibs
makeevrserg.project.group=ru.astrainteractive.astralibs
makeevrserg.project.version.string=3.17.1
makeevrserg.project.version.string=3.18.0
makeevrserg.project.description=Core utilities for spigot development
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ru.astrainteractive.astralibs.menu.slot.InventorySlot
*/
class MenuClickListener : ClickListener {
private val clicksMap = HashMap<Int, Click>()

override fun onClick(e: InventoryClickEvent) {
if (e.clickedInventory?.holder !is InventoryMenu) return
clicksMap[e.slot]?.invoke(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ abstract class Menu : InventoryHolder {

private val clickListener: ClickListener = MenuClickListener()

protected val menuScope: CoroutineScope = CoroutineFeature.Unconfined()

abstract val playerHolder: PlayerHolder

abstract val title: Component

open val childComponents: List<CoroutineScope> = emptyList()

val menuScope: CoroutineScope = CoroutineFeature.Unconfined()

/**
* This method called after inventory created and opened
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ru.astrainteractive.astralibs.menu.holder
import org.bukkit.entity.Player

/**
* Default implementatin of [PlayerHolder]
* Default implementation of [PlayerHolder]
*/
@JvmInline
value class DefaultPlayerHolder(override val player: Player) : PlayerHolder
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object InventorySlotBuilderExt {
*/
fun InventorySlot.Builder.setDisplayName(string: String): InventorySlot.Builder {
editMeta {
this.setDisplayName(string)
displayName(Component.text(string))
}
return this
}
Expand Down

0 comments on commit d301e14

Please sign in to comment.