diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 18:29:00 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 18:29:00 +0100 |
commit | a9c19ff5cfbf6d99d46cc38d0e294275cdc7f61e (patch) | |
tree | f30590798caa58811210a60596b5c36a7356aa98 | |
parent | cb3fb4771a824ec13e3da1647e3278f3ebb9a5bc (diff) |
Work on some commands
7 files changed, 181 insertions, 33 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt b/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt index ac389ca..1f5eedb 100644 --- a/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt +++ b/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt @@ -15,9 +15,7 @@ import org.slf4j.LoggerFactory import java.io.File val logger = LoggerFactory.getLogger("ParcelsPlugin") - private inline val plogger get() = logger -const val debugging = true class ParcelsPlugin : JavaPlugin() { lateinit var optionsFile: File; private set @@ -31,6 +29,7 @@ class ParcelsPlugin : JavaPlugin() { private var cmdDispatcher: ICommandDispatcher? = null override fun onEnable() { + plogger.info("Debug enabled: ${plogger.isDebugEnabled}") if (!init()) { Bukkit.getPluginManager().disablePlugin(this) } diff --git a/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt b/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt new file mode 100644 index 0000000..21ff1d7 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt @@ -0,0 +1,43 @@ +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.parcels2.ParcelOwner +import io.dico.parcels2.ParcelsPlugin +import io.dico.parcels2.util.hasAdminManage +import io.dico.parcels2.util.parcelLimit +import io.dico.parcels2.util.uuid +import org.bukkit.entity.Player +import org.bukkit.plugin.Plugin +import java.lang.reflect.Method + +abstract class AbstractParcelCommands(val plugin: ParcelsPlugin): ICommandReceiver.Factory { + + override fun getPlugin(): Plugin = plugin + override fun getReceiver(context: ExecutionContext, target: Method, cmdName: String): ICommandReceiver { + return getParcelCommandReceiver(plugin.worlds, context, target, cmdName) + } + + protected inline val worlds get() = plugin.worlds + + protected fun error(message: String): Nothing { + throw CommandException(message) + } + + protected fun checkConnected(action: String) { + if (!plugin.storage.isConnected) error("Parcels cannot $action right now because of a database error") + } + + protected suspend fun checkParcelLimit(player: Player) { + if (player.hasAdminManage) return + val numOwnedParcels = plugin.storage.getNumParcels(ParcelOwner(uuid = player.uuid)).await() + + val limit = player.parcelLimit + if (numOwnedParcels >= limit) { + error("You have enough plots for now") + } + } + +} + diff --git a/src/main/kotlin/io/dico/parcels2/command/CommandRequirement.kt b/src/main/kotlin/io/dico/parcels2/command/CommandRequirement.kt index f4b5c73..5dd8270 100644 --- a/src/main/kotlin/io/dico/parcels2/command/CommandRequirement.kt +++ b/src/main/kotlin/io/dico/parcels2/command/CommandRequirement.kt @@ -1,17 +1,18 @@ package io.dico.parcels2.command -import io.dico.dicore.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.logger 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.javaType import kotlin.reflect.jvm.jvmErasure import kotlin.reflect.jvm.kotlinFunction @@ -24,7 +25,10 @@ annotation class ParcelRequire(val admin: Boolean = false, val owner: Boolean = annotation class SuspensionTimeout(val millis: Int) open class WorldScope(val world: ParcelWorld) : ICommandReceiver -open class ParcelScope(val parcel: Parcel) : WorldScope(parcel.world) +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 diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt new file mode 100644 index 0000000..7e27d87 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt @@ -0,0 +1,46 @@ +package io.dico.parcels2.command + +import io.dico.dicore.command.annotation.Cmd +import io.dico.dicore.command.annotation.Desc +import io.dico.parcels2.ParcelsPlugin +import org.bukkit.OfflinePlayer +import org.bukkit.entity.Player + +class ParcelAddCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { + + @Cmd("allow", aliases = ["add", "permit"]) + @Desc("Allows a player to build on this parcel", + shortVersion = "allows a player to build on this parcel") + @ParcelRequire(owner = true) + fun ParcelScope.cmdAllow(sender: Player, player: OfflinePlayer): Any? { + TODO() + } + + @Cmd("disallow", aliases = ["remove", "forbid"]) + @Desc("Disallows a player to build on this parcel,", + "they won't be allowed to anymore", + shortVersion = "disallows a player to build on this parcel") + @ParcelRequire(owner = true) + fun ParcelScope.cmdDisallow(sender: Player, player: OfflinePlayer): Any? { + TODO() + } + + @Cmd("ban", aliases = ["deny"]) + @Desc("Bans a player from this parcel,", + "making them unable to enter", + shortVersion = "bans a player from this parcel") + @ParcelRequire(owner = true) + fun ParcelScope.cmdBan(sender: Player, player: OfflinePlayer): Any? { + TODO() + } + + @Cmd("unban", aliases = ["undeny"]) + @Desc("Unbans a player from this parcel,", + "they will be able to enter it again", + shortVersion = "unbans a player from this parcel") + @ParcelRequire(owner = true) + fun ParcelScope.cmdUnban(sender: Player, player: OfflinePlayer): Any? { + TODO() + } + +}
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt index e06357f..964e282 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt @@ -3,7 +3,7 @@ package io.dico.parcels2.command import io.dico.dicore.command.CommandBuilder import io.dico.dicore.command.ICommandDispatcher import io.dico.parcels2.ParcelsPlugin -import io.dico.parcels2.debugging +import io.dico.parcels2.logger @Suppress("UsePropertyAccessSyntax") fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { @@ -11,16 +11,25 @@ fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { return CommandBuilder() .addParameterType(false, ParcelParameterType(plugin.worlds)) .addParameterType(true, ParcelHomeParameterType(plugin.worlds)) + .group("parcel", "plot", "plots", "p") .registerCommands(ParcelCommands(plugin)) + .registerCommands(ParcelAddCommands(plugin)) + + .group("option") + .apply { ParcelOptionCommands.setGroupDescription(this) } + .registerCommands(ParcelOptionCommands(plugin)) + .parent() + .putDebugCommands(plugin) + .parent() .getDispatcher() //@formatter:on } private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuilder { - if (!debugging) return this + if (!logger.isDebugEnabled) return this //@formatter:off return group("debug", "d") .registerCommands(DebugCommands(plugin)) diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt index ac09e4a..3e5c27a 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt @@ -1,47 +1,27 @@ 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.annotation.Cmd import io.dico.dicore.command.annotation.Desc import io.dico.dicore.command.annotation.RequireParameters import io.dico.parcels2.ParcelOwner import io.dico.parcels2.ParcelsPlugin import io.dico.parcels2.command.NamedParcelDefaultValue.FIRST_OWNED -import io.dico.parcels2.logger import io.dico.parcels2.storage.getParcelBySerializedValue +import io.dico.parcels2.util.hasAdminManage import io.dico.parcels2.util.hasParcelHomeOthers -import io.dico.parcels2.util.parcelLimit import io.dico.parcels2.util.uuid import org.bukkit.entity.Player -import org.bukkit.plugin.Plugin -import java.lang.reflect.Method //@Suppress("unused") -class ParcelCommands(val plugin: ParcelsPlugin) : ICommandReceiver.Factory { - private inline val worlds get() = plugin.worlds - - override fun getPlugin(): Plugin = plugin - override fun getReceiver(context: ExecutionContext, target: Method, cmdName: String): ICommandReceiver { - return getParcelCommandReceiver(plugin.worlds, context, target, cmdName) - } - - private fun error(message: String): Nothing { - throw CommandException(message) - } +class ParcelCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { @Cmd("auto") @Desc("Finds the unclaimed parcel nearest to origin,", "and gives it to you", shortVersion = "sets you up with a fresh, unclaimed parcel") suspend fun WorldScope.cmdAuto(player: Player): Any? { - val numOwnedParcels = plugin.storage.getNumParcels(ParcelOwner(uuid = player.uuid)).await() - - val limit = player.parcelLimit - if (numOwnedParcels >= limit) { - error("You have enough plots for now") - } + checkConnected("be claimed") + checkParcelLimit(player) val parcel = world.nextEmptyParcel() ?: error("This world is full, please ask an admin to upsize it") @@ -86,8 +66,16 @@ class ParcelCommands(val plugin: ParcelsPlugin) : ICommandReceiver.Factory { @Cmd("claim") @Desc("If this parcel is unowned, makes you the owner", shortVersion = "claims this parcel") - fun ParcelScope.cmdClaim(player: Player) { + suspend fun ParcelScope.cmdClaim(player: Player): Any? { + checkConnected("be claimed") + parcel.owner.takeIf { !player.hasAdminManage }?.let { + error(if (it.matches(player)) "You already own this parcel" else "This parcel is not available") + } + checkParcelLimit(player) + parcel.owner = ParcelOwner(uuid = player.uuid, name = player.name) + return "Enjoy your new parcel!" } + }
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelOptionCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelOptionCommands.kt new file mode 100644 index 0000000..ad5bb30 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelOptionCommands.kt @@ -0,0 +1,59 @@ +package io.dico.parcels2.command + +import io.dico.dicore.command.CommandBuilder +import io.dico.dicore.command.Validate +import io.dico.dicore.command.annotation.Cmd +import io.dico.dicore.command.annotation.Desc +import io.dico.dicore.command.annotation.RequireParameters +import io.dico.parcels2.Parcel +import io.dico.parcels2.ParcelsPlugin +import org.bukkit.entity.Player +import kotlin.reflect.KMutableProperty + +class ParcelOptionCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { + @Cmd("inputs") + @Desc("Sets whether players who are not allowed to", + "build here can use levers, buttons,", + "pressure plates, tripwire or redstone ore", + shortVersion = "allows using inputs") + @RequireParameters(0) + fun ParcelScope.cmdInputs(player: Player, enabled: Boolean?): Any? { + return runOptionCommand(player, Parcel::allowInteractInputs, enabled, "using levers, buttons, etc.") + } + + @Cmd("inventory") + @Desc("Sets whether players who are not allowed to", + "build here can interact with inventories", + shortVersion = "allows editing inventories") + fun ParcelScope.cmdInventory(player: Player, enabled: Boolean?): Any? { + return runOptionCommand(player, Parcel::allowInteractInventory, enabled, "interaction with inventories") + } + + private inline val Boolean.enabledWord get() = if (this) "enabled" else "disabled" + private fun ParcelScope.runOptionCommand(player: Player, + property: KMutableProperty<Boolean>, + enabled: Boolean?, + desc: String): Any? { + checkConnected("have their options changed") + val current = property.getter.call(parcel) + if (enabled == null) { + val word = if (current) "" else "not " + return "This parcel does ${word}allow $desc" + } + + checkCanManage(player, "change its options") + Validate.isTrue(current != enabled, "That option was already ${enabled.enabledWord}") + property.setter.call(parcel, enabled) + return "That option is now ${enabled.enabledWord}" + } + + companion object { + private const val descShort = "changes interaction options for this parcel" + private val desc = arrayOf("Sets whether players who are not allowed to", "build here can interact with certain things.") + + fun setGroupDescription(builder: CommandBuilder) { + builder.setGroupDescription(descShort, *desc) + } + } + +}
\ No newline at end of file |