Implement /p home
This commit is contained in:
@@ -40,7 +40,7 @@ interface ParcelData {
|
|||||||
*/
|
*/
|
||||||
class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData {
|
class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData {
|
||||||
val id get() = "${pos.x}:${pos.z}"
|
val id get() = "${pos.x}:${pos.z}"
|
||||||
|
val homeLocation get() = world.generator.getHomeLocation(this)
|
||||||
|
|
||||||
var data: ParcelData = ParcelDataHolder(); private set
|
var data: ParcelData = ParcelDataHolder(); private set
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,9 @@ import io.dico.dicore.command.ICommandDispatcher
|
|||||||
import io.dico.dicore.command.parameter.ArgumentBuffer
|
import io.dico.dicore.command.parameter.ArgumentBuffer
|
||||||
import io.dico.dicore.command.parameter.IParameter
|
import io.dico.dicore.command.parameter.IParameter
|
||||||
import io.dico.dicore.command.parameter.type.ParameterType
|
import io.dico.dicore.command.parameter.type.ParameterType
|
||||||
import io.dico.parcels2.Parcel
|
import io.dico.parcels2.*
|
||||||
import io.dico.parcels2.ParcelsPlugin
|
import org.bukkit.Bukkit
|
||||||
import io.dico.parcels2.Worlds
|
import org.bukkit.OfflinePlayer
|
||||||
import io.dico.parcels2.debugging
|
|
||||||
import org.bukkit.command.CommandSender
|
import org.bukkit.command.CommandSender
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
|
|
||||||
@@ -18,6 +17,7 @@ fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher {
|
|||||||
//@formatter:off
|
//@formatter:off
|
||||||
return CommandBuilder()
|
return CommandBuilder()
|
||||||
.addParameterType(false, ParcelParameterType(plugin.worlds))
|
.addParameterType(false, ParcelParameterType(plugin.worlds))
|
||||||
|
.addParameterType(false, ParcelHomeParameterType(plugin.worlds))
|
||||||
.group("parcel", "plot", "plots", "p")
|
.group("parcel", "plot", "plots", "p")
|
||||||
.registerCommands(ParcelCommands(plugin))
|
.registerCommands(ParcelCommands(plugin))
|
||||||
.putDebugCommands(plugin)
|
.putDebugCommands(plugin)
|
||||||
@@ -35,25 +35,28 @@ private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuild
|
|||||||
//@formatter:on
|
//@formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
private val regex = Regex.fromLiteral("((.+)->)?([0-9]+):([0-9]+)")
|
|
||||||
|
|
||||||
private class ParcelParameterType(val worlds: Worlds) : ParameterType<Parcel, Unit>(Parcel::class.java) {
|
|
||||||
|
|
||||||
private fun invalidInput(parameter: IParameter<*>, message: String): Nothing {
|
private fun invalidInput(parameter: IParameter<*>, message: String): Nothing {
|
||||||
throw CommandException("invalid input for ${parameter.name}: $message")
|
throw CommandException("invalid input for ${parameter.name}: $message")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun Worlds.getTargetWorld(input: String?, sender: CommandSender, parameter: IParameter<*>): 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ParcelParameterType(val worlds: Worlds) : ParameterType<Parcel, Unit>(Parcel::class.java) {
|
||||||
|
val regex = Regex.fromLiteral("((.+)->)?([0-9]+):([0-9]+)")
|
||||||
|
|
||||||
override fun parse(parameter: IParameter<Parcel>, sender: CommandSender, buffer: ArgumentBuffer): Parcel {
|
override fun parse(parameter: IParameter<Parcel>, sender: CommandSender, buffer: ArgumentBuffer): Parcel {
|
||||||
val matchResult = regex.matchEntire(buffer.next())
|
val matchResult = regex.matchEntire(buffer.next())
|
||||||
?: invalidInput(parameter, "must match (w->)?a:b (/${regex.pattern}/)")
|
?: invalidInput(parameter, "must match (w->)?a:b (/${regex.pattern}/)")
|
||||||
|
|
||||||
val worldName = matchResult.groupValues[2]
|
val world = worlds.getTargetWorld(matchResult.groupValues[2], sender, parameter)
|
||||||
.takeUnless { it.isEmpty() }
|
|
||||||
?: (sender as? Player)?.world?.name
|
|
||||||
?: invalidInput(parameter, "console cannot omit the world name")
|
|
||||||
|
|
||||||
val world = worlds.getWorld(worldName)
|
|
||||||
?: invalidInput(parameter, "$worldName is not a parcel world")
|
|
||||||
|
|
||||||
val x = matchResult.groupValues[3].toIntOrNull()
|
val x = matchResult.groupValues[3].toIntOrNull()
|
||||||
?: invalidInput(parameter, "couldn't parse int")
|
?: invalidInput(parameter, "couldn't parse int")
|
||||||
@@ -66,3 +69,49 @@ private class ParcelParameterType(val worlds: Worlds) : ParameterType<Parcel, Un
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class NamedParcelTarget(val world: ParcelWorld, val player: OfflinePlayer, val index: Int)
|
||||||
|
|
||||||
|
private class ParcelHomeParameterType(val worlds: Worlds) : ParameterType<NamedParcelTarget, Unit>(NamedParcelTarget::class.java) {
|
||||||
|
val regex = Regex.fromLiteral("((.+)->)?(.+)|((.+):([0-9]+))")
|
||||||
|
|
||||||
|
private fun requirePlayer(sender: CommandSender, parameter: IParameter<*>): Player {
|
||||||
|
if (sender !is Player) invalidInput(parameter, "console cannot omit the player name")
|
||||||
|
return sender
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UsePropertyAccessSyntax")
|
||||||
|
private fun getOfflinePlayer(input: String, parameter: IParameter<*>) = Bukkit.getOfflinePlayer(input)
|
||||||
|
?.takeIf { it.isOnline() || it.hasPlayedBefore() }
|
||||||
|
?: invalidInput(parameter, "do not know who $input is")
|
||||||
|
|
||||||
|
override fun parse(parameter: IParameter<NamedParcelTarget>, 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: IParameter<NamedParcelTarget>, sender: CommandSender, buffer: ArgumentBuffer): NamedParcelTarget {
|
||||||
|
val world = worlds.getTargetWorld(null, sender, parameter)
|
||||||
|
val player = requirePlayer(sender, parameter)
|
||||||
|
return NamedParcelTarget(world, player, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,12 +4,16 @@ import io.dico.dicore.command.CommandException
|
|||||||
import io.dico.dicore.command.ExecutionContext
|
import io.dico.dicore.command.ExecutionContext
|
||||||
import io.dico.dicore.command.annotation.Cmd
|
import io.dico.dicore.command.annotation.Cmd
|
||||||
import io.dico.dicore.command.annotation.Desc
|
import io.dico.dicore.command.annotation.Desc
|
||||||
|
import io.dico.dicore.command.annotation.RequireParameters
|
||||||
import io.dico.parcels2.ParcelOwner
|
import io.dico.parcels2.ParcelOwner
|
||||||
import io.dico.parcels2.ParcelsPlugin
|
import io.dico.parcels2.ParcelsPlugin
|
||||||
|
import io.dico.parcels2.storage.getParcelBySerializedValue
|
||||||
|
import io.dico.parcels2.util.hasParcelHomeOthers
|
||||||
import io.dico.parcels2.util.parcelLimit
|
import io.dico.parcels2.util.parcelLimit
|
||||||
import io.dico.parcels2.util.uuid
|
import io.dico.parcels2.util.uuid
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
class ParcelCommands(override val plugin: ParcelsPlugin) : HasWorlds, HasPlugin {
|
class ParcelCommands(override val plugin: ParcelsPlugin) : HasWorlds, HasPlugin {
|
||||||
override val worlds = plugin.worlds
|
override val worlds = plugin.worlds
|
||||||
|
|
||||||
@@ -35,7 +39,7 @@ class ParcelCommands(override val plugin: ParcelsPlugin) : HasWorlds, HasPlugin
|
|||||||
val parcel = world.nextEmptyParcel()
|
val parcel = world.nextEmptyParcel()
|
||||||
?: error("This world is full, please ask an admin to upsize it")
|
?: error("This world is full, please ask an admin to upsize it")
|
||||||
parcel.owner = ParcelOwner(uuid = player.uuid)
|
parcel.owner = ParcelOwner(uuid = player.uuid)
|
||||||
player.teleport(world.generator.getHomeLocation(parcel))
|
player.teleport(parcel.homeLocation)
|
||||||
"Enjoy your new parcel!"
|
"Enjoy your new parcel!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,5 +51,32 @@ class ParcelCommands(override val plugin: ParcelsPlugin) : HasWorlds, HasPlugin
|
|||||||
shortVersion = "displays information about this parcel")
|
shortVersion = "displays information about this parcel")
|
||||||
fun cmdInfo(player: Player) = requireInParcel(player) { parcel.infoString }
|
fun cmdInfo(player: Player) = requireInParcel(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)
|
||||||
|
fun cmdHome(player: Player, context: ExecutionContext, target: NamedParcelTarget) {
|
||||||
|
if (player !== target.player && !player.hasParcelHomeOthers) {
|
||||||
|
error("You do not have permission to teleport to other people's parcels")
|
||||||
|
}
|
||||||
|
|
||||||
|
return delegateCommandAsync(context) {
|
||||||
|
val ownedParcelsResult = plugin.storage.getOwnedParcels(ParcelOwner(uuid = target.player.uuid)).await()
|
||||||
|
awaitSynchronousTask {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -176,6 +176,7 @@ class ExposedBacking(val dataSource: DataSource) : Backing {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO order by some new column
|
||||||
override suspend fun getOwnedParcels(user: ParcelOwner): List<SerializableParcel> = transaction {
|
override suspend fun getOwnedParcels(user: ParcelOwner): List<SerializableParcel> = transaction {
|
||||||
val where: SqlExpressionBuilder.() -> Op<Boolean>
|
val where: SqlExpressionBuilder.() -> Op<Boolean>
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package io.dico.parcels2.storage
|
|||||||
|
|
||||||
import io.dico.parcels2.Parcel
|
import io.dico.parcels2.Parcel
|
||||||
import io.dico.parcels2.ParcelWorld
|
import io.dico.parcels2.ParcelWorld
|
||||||
|
import io.dico.parcels2.Worlds
|
||||||
import io.dico.parcels2.math.Vec2i
|
import io.dico.parcels2.math.Vec2i
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
import org.bukkit.World
|
import org.bukkit.World
|
||||||
@@ -15,7 +16,7 @@ data class SerializableWorld(val name: String? = null,
|
|||||||
}
|
}
|
||||||
|
|
||||||
val world: World? by lazy { uid?.let { Bukkit.getWorld(it) } ?: name?.let { Bukkit.getWorld(it) } }
|
val world: World? by lazy { uid?.let { Bukkit.getWorld(it) } ?: name?.let { Bukkit.getWorld(it) } }
|
||||||
val parcelWorld: ParcelWorld? by lazy { TODO() }
|
//val parcelWorld: ParcelWorld? by lazy { TODO() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,5 +25,13 @@ data class SerializableWorld(val name: String? = null,
|
|||||||
data class SerializableParcel(val world: SerializableWorld,
|
data class SerializableParcel(val world: SerializableWorld,
|
||||||
val pos: Vec2i) {
|
val pos: Vec2i) {
|
||||||
|
|
||||||
val parcel: Parcel? by lazy { TODO() }
|
//val parcel: Parcel? by lazy { TODO() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Worlds.getWorldBySerializedValue(input: SerializableWorld): ParcelWorld? {
|
||||||
|
return input.world?.let { getWorld(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Worlds.getParcelBySerializedValue(input: SerializableParcel): Parcel? {
|
||||||
|
return getWorldBySerializedValue(input.world)?.parcelByID(input.pos)
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,11 @@ package io.dico.parcels2.util
|
|||||||
import io.dico.dicore.Formatting
|
import io.dico.dicore.Formatting
|
||||||
import io.dico.parcels2.ParcelsPlugin
|
import io.dico.parcels2.ParcelsPlugin
|
||||||
import io.dico.parcels2.logger
|
import io.dico.parcels2.logger
|
||||||
|
import org.bukkit.OfflinePlayer
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.plugin.java.JavaPlugin
|
import org.bukkit.plugin.java.JavaPlugin
|
||||||
|
|
||||||
inline val Player.uuid get() = uniqueId
|
inline val OfflinePlayer.uuid get() = uniqueId
|
||||||
inline val Player.hasBanBypass get() = hasPermission("parcels.admin.bypass.ban")
|
inline val Player.hasBanBypass get() = hasPermission("parcels.admin.bypass.ban")
|
||||||
inline val Player.hasBuildAnywhere get() = hasPermission("parcels.admin.bypass.build")
|
inline val Player.hasBuildAnywhere get() = hasPermission("parcels.admin.bypass.build")
|
||||||
inline val Player.hasGamemodeBypass get() = hasPermission("parcels.admin.bypass.gamemode")
|
inline val Player.hasGamemodeBypass get() = hasPermission("parcels.admin.bypass.gamemode")
|
||||||
|
|||||||
Reference in New Issue
Block a user