diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:45:59 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:45:59 +0100 |
commit | 60503351a30a91985b95ee8aa64e163ef084b34b (patch) | |
tree | 493bc97dec1e75801b2a30b964b4c8235c14b836 /src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt | |
parent | 80def8901a168ba7be3f41d10147c083750771c6 (diff) |
Update parcels2 project
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt new file mode 100644 index 0000000..726be81 --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt @@ -0,0 +1,46 @@ +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.ParameterType +import io.dico.parcels2.Parcel +import io.dico.parcels2.ParcelWorld +import io.dico.parcels2.Worlds +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + +fun invalidInput(parameter: Parameter<*, *>, message: String): Nothing { + throw CommandException("invalid input for ${parameter.name}: $message") +} + +fun Worlds.getTargetWorld(input: String?, sender: CommandSender, parameter: Parameter<*, *>): ParcelWorld { + val worldName = input + ?.takeUnless { it.isEmpty() } + ?: (sender as? Player)?.world?.name + ?: invalidInput(parameter, "console cannot omit the world name") + + return getWorld(worldName) + ?: invalidInput(parameter, "$worldName is not a parcel world") +} + +class ParcelParameterType(val worlds: Worlds) : ParameterType<Parcel, Void>(Parcel::class.java) { + val regex = Regex.fromLiteral("((.+)->)?([0-9]+):([0-9]+)") + + override fun parse(parameter: Parameter<Parcel, Void>, sender: CommandSender, buffer: ArgumentBuffer): Parcel { + val matchResult = regex.matchEntire(buffer.next()) + ?: invalidInput(parameter, "must match (w->)?a:b (/${regex.pattern}/)") + + val world = worlds.getTargetWorld(matchResult.groupValues[2], sender, parameter) + + val x = matchResult.groupValues[3].toIntOrNull() + ?: invalidInput(parameter, "couldn't parse int") + + val z = matchResult.groupValues[4].toIntOrNull() + ?: invalidInput(parameter, "couldn't parse int") + + return world.parcelByID(x, z) + ?: invalidInput(parameter, "parcel id is out of range") + } + +} |