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/CommandsGeneral.kt | |
parent | 2a726e1b618b38e635b249d7d09000ec2ea9ac17 (diff) |
Refactor and reformat commands
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt new file mode 100644 index 0000000..7308a00 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt @@ -0,0 +1,81 @@ +package io.dico.parcels2.command + +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.storage.getParcelBySerializedValue +import io.dico.parcels2.util.hasAdminManage +import io.dico.parcels2.util.hasParcelHomeOthers +import io.dico.parcels2.util.uuid +import org.bukkit.entity.Player + +//@Suppress("unused") +class CommandsGeneral(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? { + checkConnected("be claimed") + checkParcelLimit(player) + + val parcel = world.nextEmptyParcel() + ?: error("This world is full, please ask an admin to upsize it") + parcel.owner = ParcelOwner(uuid = player.uuid) + player.teleport(parcel.homeLocation) + return "Enjoy your new parcel!" + } + + @Cmd("info", aliases = ["i"]) + @Desc("Displays general information", + "about the parcel you're on", + shortVersion = "displays information about this parcel") + fun ParcelScope.cmdInfo(player: Player) = parcel.infoString + + @Cmd("home", aliases = ["h"]) + @Desc("Teleports you to your parcels,", + "unless another player was specified.", + "You can specify an index number if you have", + "more than one parcel", + shortVersion = "teleports you to parcels") + @RequireParameters(0) + suspend fun cmdHome(player: Player, + @NamedParcelDefault(FIRST_OWNED) target: NamedParcelTarget): Any? { + if (player !== target.player && !player.hasParcelHomeOthers) { + error("You do not have permission to teleport to other people's parcels") + } + + val ownedParcelsResult = plugin.storage.getOwnedParcels(ParcelOwner(uuid = target.player.uuid)).await() + + val uuid = target.player.uuid + val ownedParcels = ownedParcelsResult + .map { worlds.getParcelBySerializedValue(it) } + .filter { it != null && it.world == target.world && it.owner?.uuid == uuid } + + val targetMatch = ownedParcels.getOrNull(target.index) + ?: error("The specified parcel could not be matched") + + player.teleport(targetMatch.homeLocation) + return "" + } + + @Cmd("claim") + @Desc("If this parcel is unowned, makes you the owner", + shortVersion = "claims this parcel") + 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 |