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 | |
parent | 2a726e1b618b38e635b249d7d09000ec2ea9ac17 (diff) |
Refactor and reformat commands
12 files changed, 94 insertions, 104 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt b/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt index 21ff1d7..2cee99a 100644 --- a/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt @@ -12,7 +12,7 @@ import org.bukkit.entity.Player import org.bukkit.plugin.Plugin import java.lang.reflect.Method -abstract class AbstractParcelCommands(val plugin: ParcelsPlugin): ICommandReceiver.Factory { +abstract class AbstractParcelCommands(val plugin: ParcelsPlugin) : ICommandReceiver.Factory { override fun getPlugin(): Plugin = plugin override fun getReceiver(context: ExecutionContext, target: Method, cmdName: String): ICommandReceiver { diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsAddedStatus.kt index 02e9123..adc8e60 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelAddCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsAddedStatus.kt @@ -8,7 +8,7 @@ import io.dico.parcels2.util.hasAdminManage import org.bukkit.OfflinePlayer import org.bukkit.entity.Player -class ParcelAddCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { +class CommandsAddedStatus(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { @Cmd("allow", aliases = ["add", "permit"]) @Desc("Allows a player to build on this parcel", diff --git a/src/main/kotlin/io/dico/parcels2/command/CommandsAdmin.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsAdmin.kt new file mode 100644 index 0000000..2fe18ed --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsAdmin.kt @@ -0,0 +1,7 @@ +package io.dico.parcels2.command + +import io.dico.parcels2.ParcelsPlugin + +class CommandsAdmin(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { + +}
\ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/command/DebugCommands.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsDebug.kt index 24a61c5..8f7f6ba 100644 --- a/src/main/kotlin/io/dico/parcels2/command/DebugCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsDebug.kt @@ -6,7 +6,7 @@ import io.dico.parcels2.ParcelsPlugin import org.bukkit.Bukkit import org.bukkit.entity.Player -class DebugCommands(val plugin: ParcelsPlugin) { +class CommandsDebug(val plugin: ParcelsPlugin) { @Cmd("reloadoptions") fun reloadOptions() { diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt index 3e5c27a..7308a00 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsGeneral.kt @@ -13,7 +13,7 @@ import io.dico.parcels2.util.uuid import org.bukkit.entity.Player //@Suppress("unused") -class ParcelCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { +class CommandsGeneral(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { @Cmd("auto") @Desc("Finds the unclaimed parcel nearest to origin,", diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelOptionCommands.kt b/src/main/kotlin/io/dico/parcels2/command/CommandsParcelOptions.kt index ad5bb30..7f900d5 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelOptionCommands.kt +++ b/src/main/kotlin/io/dico/parcels2/command/CommandsParcelOptions.kt @@ -10,7 +10,7 @@ import io.dico.parcels2.ParcelsPlugin import org.bukkit.entity.Player import kotlin.reflect.KMutableProperty -class ParcelOptionCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { +class CommandsParcelOptions(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { @Cmd("inputs") @Desc("Sets whether players who are not allowed to", "build here can use levers, buttons,", diff --git a/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt b/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt deleted file mode 100644 index bb7add9..0000000 --- a/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt +++ /dev/null @@ -1,85 +0,0 @@ -package io.dico.parcels2.command - -import io.dico.dicore.command.parameter.ArgumentBuffer -import io.dico.dicore.command.parameter.Parameter -import io.dico.dicore.command.parameter.type.ParameterConfig -import io.dico.dicore.command.parameter.type.ParameterType -import io.dico.parcels2.ParcelWorld -import io.dico.parcels2.Worlds -import io.dico.parcels2.util.isValid -import org.bukkit.Bukkit -import org.bukkit.OfflinePlayer -import org.bukkit.command.CommandSender -import org.bukkit.entity.Player - -class NamedParcelTarget(val world: ParcelWorld, val player: OfflinePlayer, val index: Int) - -@Target(AnnotationTarget.VALUE_PARAMETER) -@Retention(AnnotationRetention.RUNTIME) -annotation class NamedParcelDefault(val value: NamedParcelDefaultValue) - -enum class NamedParcelDefaultValue { - FIRST_OWNED, - NULL -} - -class NamedParcelTargetConfig : ParameterConfig<NamedParcelDefault, - NamedParcelDefaultValue>(NamedParcelDefault::class.java) { - - override fun toParameterInfo(annotation: NamedParcelDefault): NamedParcelDefaultValue { - return annotation.value - } -} - -class ParcelHomeParameterType(val worlds: Worlds) : ParameterType<NamedParcelTarget, - NamedParcelDefaultValue>(NamedParcelTarget::class.java, NamedParcelTargetConfig()) { - - val regex = Regex.fromLiteral("((.+)->)?(.+)|((.+):([0-9]+))") - - private fun requirePlayer(sender: CommandSender, parameter: Parameter<*, *>): Player { - if (sender !is Player) invalidInput(parameter, "console cannot omit the player name") - return sender - } - - @Suppress("UsePropertyAccessSyntax") - private fun getOfflinePlayer(input: String, parameter: Parameter<*, *>) = Bukkit.getOfflinePlayer(input) - ?.takeIf { it.isValid } - ?: invalidInput(parameter, "do not know who $input is") - - override fun parse(parameter: Parameter<NamedParcelTarget, NamedParcelDefaultValue>, - sender: CommandSender, buffer: ArgumentBuffer): NamedParcelTarget { - val matchResult = regex.matchEntire(buffer.next()) - ?: invalidInput(parameter, "must be a player, index, or player:index (/${regex.pattern}/)") - - val world = worlds.getTargetWorld(matchResult.groupValues[2], sender, parameter) - - matchResult.groupValues[3].takeUnless { it.isEmpty() }?.let { - // first group was matched, it's a player or an int - it.toIntOrNull()?.let { - requirePlayer(sender, parameter) - return NamedParcelTarget(world, sender as Player, it) - } - - return NamedParcelTarget(world, getOfflinePlayer(it, parameter), 0) - } - - val player = getOfflinePlayer(matchResult.groupValues[5], parameter) - val index = matchResult.groupValues[6].toIntOrNull() - ?: invalidInput(parameter, "couldn't parse int") - - return NamedParcelTarget(world, player, index) - } - - override fun getDefaultValue(parameter: Parameter<NamedParcelTarget, NamedParcelDefaultValue>, - sender: CommandSender, buffer: ArgumentBuffer): NamedParcelTarget? { - if (parameter.paramInfo == NamedParcelDefaultValue.NULL) { - return null - } - - val world = worlds.getTargetWorld(null, sender, parameter) - val player = requirePlayer(sender, parameter) - return NamedParcelTarget(world, player, 0) - } - -} - diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelAdminCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelAdminCommands.kt deleted file mode 100644 index 79e4474..0000000 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelAdminCommands.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.dico.parcels2.command - -import io.dico.parcels2.ParcelsPlugin - -class ParcelAdminCommands(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) { - -}
\ 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 3cd6e1c..284c6d4 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt @@ -14,16 +14,16 @@ fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { .addParameterType(true, ParcelHomeParameterType(plugin.worlds)) .group("parcel", "plot", "plots", "p") - .registerCommands(ParcelCommands(plugin)) - .registerCommands(ParcelAddCommands(plugin)) + .registerCommands(CommandsGeneral(plugin)) + .registerCommands(CommandsAddedStatus(plugin)) .group("option") - .apply { ParcelOptionCommands.setGroupDescription(this) } - .registerCommands(ParcelOptionCommands(plugin)) + .apply { CommandsParcelOptions.setGroupDescription(this) } + .registerCommands(CommandsParcelOptions(plugin)) .parent() .group("admin", "a") - .registerCommands(ParcelAdminCommands(plugin)) + .registerCommands(CommandsAdmin(plugin)) .parent() .putDebugCommands(plugin) @@ -37,7 +37,7 @@ private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuild if (!logger.isDebugEnabled) return this //@formatter:off return group("debug", "d") - .registerCommands(DebugCommands(plugin)) + .registerCommands(CommandsDebug(plugin)) .parent() //@formatter:on } diff --git a/src/main/kotlin/io/dico/parcels2/command/CommandRequirement.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt index 5dd8270..5dd8270 100644 --- a/src/main/kotlin/io/dico/parcels2/command/CommandRequirement.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandReceivers.kt diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt index 726be81..ab97023 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt @@ -3,10 +3,14 @@ package io.dico.parcels2.command import io.dico.dicore.command.CommandException import io.dico.dicore.command.parameter.ArgumentBuffer import io.dico.dicore.command.parameter.Parameter +import io.dico.dicore.command.parameter.type.ParameterConfig import io.dico.dicore.command.parameter.type.ParameterType import io.dico.parcels2.Parcel import io.dico.parcels2.ParcelWorld import io.dico.parcels2.Worlds +import io.dico.parcels2.util.isValid +import org.bukkit.Bukkit +import org.bukkit.OfflinePlayer import org.bukkit.command.CommandSender import org.bukkit.entity.Player @@ -44,3 +48,75 @@ class ParcelParameterType(val worlds: Worlds) : ParameterType<Parcel, Void>(Parc } } + + +class NamedParcelTarget(val world: ParcelWorld, val player: OfflinePlayer, val index: Int) + +@Target(AnnotationTarget.VALUE_PARAMETER) +@Retention(AnnotationRetention.RUNTIME) +annotation class NamedParcelDefault(val value: NamedParcelDefaultValue) + +enum class NamedParcelDefaultValue { + FIRST_OWNED, + NULL +} + +class NamedParcelTargetConfig : ParameterConfig<NamedParcelDefault, + NamedParcelDefaultValue>(NamedParcelDefault::class.java) { + + override fun toParameterInfo(annotation: NamedParcelDefault): NamedParcelDefaultValue { + return annotation.value + } +} + +class ParcelHomeParameterType(val worlds: Worlds) : ParameterType<NamedParcelTarget, + NamedParcelDefaultValue>(NamedParcelTarget::class.java, NamedParcelTargetConfig()) { + + val regex = Regex.fromLiteral("((.+)->)?(.+)|((.+):([0-9]+))") + + private fun requirePlayer(sender: CommandSender, parameter: Parameter<*, *>): Player { + if (sender !is Player) invalidInput(parameter, "console cannot omit the player name") + return sender + } + + @Suppress("UsePropertyAccessSyntax") + private fun getOfflinePlayer(input: String, parameter: Parameter<*, *>) = Bukkit.getOfflinePlayer(input) + ?.takeIf { it.isValid } + ?: invalidInput(parameter, "do not know who $input is") + + override fun parse(parameter: Parameter<NamedParcelTarget, NamedParcelDefaultValue>, + sender: CommandSender, buffer: ArgumentBuffer): NamedParcelTarget { + val matchResult = regex.matchEntire(buffer.next()) + ?: invalidInput(parameter, "must be a player, index, or player:index (/${regex.pattern}/)") + + val world = worlds.getTargetWorld(matchResult.groupValues[2], sender, parameter) + + matchResult.groupValues[3].takeUnless { it.isEmpty() }?.let { + // first group was matched, it's a player or an int + it.toIntOrNull()?.let { + requirePlayer(sender, parameter) + return NamedParcelTarget(world, sender as Player, it) + } + + return NamedParcelTarget(world, getOfflinePlayer(it, parameter), 0) + } + + val player = getOfflinePlayer(matchResult.groupValues[5], parameter) + val index = matchResult.groupValues[6].toIntOrNull() + ?: invalidInput(parameter, "couldn't parse int") + + return NamedParcelTarget(world, player, index) + } + + override fun getDefaultValue(parameter: Parameter<NamedParcelTarget, NamedParcelDefaultValue>, + sender: CommandSender, buffer: ArgumentBuffer): NamedParcelTarget? { + if (parameter.paramInfo == NamedParcelDefaultValue.NULL) { + return null + } + + val world = worlds.getTargetWorld(null, sender, parameter) + val player = requirePlayer(sender, parameter) + return NamedParcelTarget(world, player, 0) + } + +} diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt index 5b2d25d..456893b 100644 --- a/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelsChatController.kt @@ -5,5 +5,4 @@ import io.dico.dicore.command.chat.AbstractChatController class ParcelsChatController : AbstractChatController() { - }
\ No newline at end of file |