diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 23:31:24 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 23:31:24 +0100 |
commit | 2c1aee89f6bbc696f55a0952bf26c295611e5e3a (patch) | |
tree | ee74596b472127577ba5df0f2badfcc3911a88da /src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt | |
parent | 2a726e1b618b38e635b249d7d09000ec2ea9ac17 (diff) |
Refactor and reformat commands
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt new file mode 100644 index 0000000..5dd8270 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt @@ -0,0 +1,61 @@ +package io.dico.parcels2.command + +import io.dico.dicore.command.CommandException +import io.dico.dicore.command.ExecutionContext +import io.dico.dicore.command.ICommandReceiver +import io.dico.dicore.command.Validate +import io.dico.parcels2.Parcel +import io.dico.parcels2.ParcelWorld +import io.dico.parcels2.Worlds +import io.dico.parcels2.util.hasAdminManage +import io.dico.parcels2.util.uuid +import org.bukkit.entity.Player +import java.lang.reflect.Method +import kotlin.reflect.full.extensionReceiverParameter +import kotlin.reflect.full.findAnnotation +import kotlin.reflect.jvm.jvmErasure +import kotlin.reflect.jvm.kotlinFunction + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +annotation class ParcelRequire(val admin: Boolean = false, val owner: Boolean = false) + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +annotation class SuspensionTimeout(val millis: Int) + +open class WorldScope(val world: ParcelWorld) : ICommandReceiver +open class ParcelScope(val parcel: Parcel) : WorldScope(parcel.world) { + fun checkCanManage(player: Player, action: String) = Validate.isTrue(player.hasAdminManage || parcel.isOwner(player.uuid), + "You must own this parcel to $action") +} + +fun getParcelCommandReceiver(worlds: Worlds, context: ExecutionContext, method: Method, cmdName: String): ICommandReceiver { + val player = context.sender as Player + val function = method.kotlinFunction!! + val receiverType = function.extensionReceiverParameter!!.type + val require = function.findAnnotation<ParcelRequire>() + val admin = require?.admin == true + val owner = require?.owner == true + + return when (receiverType.jvmErasure) { + ParcelScope::class -> ParcelScope(worlds.getParcelRequired(player, admin, owner)) + WorldScope::class -> WorldScope(worlds.getWorldRequired(player, admin)) + else -> throw InternalError("Invalid command receiver type") + } +} + +fun Worlds.getWorldRequired(player: Player, admin: Boolean = false): ParcelWorld { + if (admin) Validate.isTrue(player.hasAdminManage, "You must have admin rights to use that command") + return getWorld(player.world) + ?: throw CommandException("You must be in a parcel world to use that command") +} + +fun Worlds.getParcelRequired(player: Player, admin: Boolean = false, own: Boolean = false): Parcel { + val parcel = getWorldRequired(player, admin = admin).parcelAt(player) + ?: throw CommandException("You must be in a parcel to use that command") + if (own) Validate.isTrue(parcel.isOwner(player.uuid) || player.hasAdminManage, + "You must own this parcel to use that command") + return parcel +} + |