Archived
0

Update parcels2 project

This commit is contained in:
Dico200
2018-07-25 01:45:59 +01:00
parent 80def8901a
commit 60503351a3
11 changed files with 171 additions and 107 deletions

13
.gitignore vendored
View File

@@ -1,6 +1,9 @@
/.idea/ .idea/
/*.iml *.iml
/.gradle/ .gradle/
/out/ out/
build/
/debug/ /debug/
/target/ target/
/gradle-output.txt
/dicore3/

View File

@@ -1,2 +1,7 @@
rootProject.name = "parcels2" rootProject.name = "parcels2"
include("dicore3:core")
findProject(":dicore3:core")?.name = "dicore3-core"
include("dicore3:command")
findProject(":dicore3:command")?.name = "dicore3-command"

View File

@@ -24,10 +24,6 @@ interface ParcelData {
return owner?.uuid == uuid return owner?.uuid == uuid
} }
val infoString: String
get() {
TODO()
}
} }
/** /**
@@ -42,6 +38,11 @@ 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) val homeLocation get() = world.generator.getHomeLocation(this)
val infoString: String
get() {
return "$id; owned by ${owner?.let { it.name ?: Bukkit.getOfflinePlayer(it.uuid).name }}"
}
var data: ParcelData = ParcelDataHolder(); private set var data: ParcelData = ParcelDataHolder(); private set
fun copyDataIgnoringDatabase(data: ParcelData) { fun copyDataIgnoringDatabase(data: ParcelData) {

View File

@@ -182,10 +182,11 @@ class DefaultParcelContainer(private val world: ParcelWorld,
} }
override fun parcelByID(x: Int, z: Int): Parcel? { override fun parcelByID(x: Int, z: Int): Parcel? {
return parcels[x][z] return parcels.getOrNull(x)?.getOrNull(z)
} }
override fun nextEmptyParcel(): Parcel? { override fun nextEmptyParcel(): Parcel? {
return parcels[0][0]
TODO() TODO()
} }

View File

@@ -46,6 +46,7 @@ class ParcelsPlugin : JavaPlugin() {
try { try {
storage = options.storage.newStorageInstance() storage = options.storage.newStorageInstance()
storage.init()
} catch (ex: Exception) { } catch (ex: Exception) {
plogger.error("Failed to connect to database", ex) plogger.error("Failed to connect to database", ex)
return false return false

View File

@@ -23,7 +23,7 @@ class CommandAsyncScope {
suspend fun <T> HasPlugin.awaitSynchronousTask(delay: Int = 0, task: () -> T): T { suspend fun <T> HasPlugin.awaitSynchronousTask(delay: Int = 0, task: () -> T): T {
return suspendCoroutine { cont: Continuation<T> -> return suspendCoroutine { cont: Continuation<T> ->
plugin.server.scheduler.runTaskLater(plugin, l@ { plugin.server.scheduler.runTaskLater(plugin, l@{
val result = try { val result = try {
task() task()
} catch (ex: CommandException) { } catch (ex: CommandException) {

View File

@@ -0,0 +1,84 @@
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 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.isOnline() || it.hasPlayedBefore() }
?: 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)
}
}

View File

@@ -1,23 +1,16 @@
package io.dico.parcels2.command package io.dico.parcels2.command
import io.dico.dicore.command.CommandBuilder import io.dico.dicore.command.CommandBuilder
import io.dico.dicore.command.CommandException
import io.dico.dicore.command.ICommandDispatcher import io.dico.dicore.command.ICommandDispatcher
import io.dico.dicore.command.parameter.ArgumentBuffer import io.dico.parcels2.ParcelsPlugin
import io.dico.dicore.command.parameter.IParameter import io.dico.parcels2.debugging
import io.dico.dicore.command.parameter.type.ParameterType
import io.dico.parcels2.*
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
@Suppress("UsePropertyAccessSyntax") @Suppress("UsePropertyAccessSyntax")
fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher { 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)) .addParameterType(true, ParcelHomeParameterType(plugin.worlds))
.group("parcel", "plot", "plots", "p") .group("parcel", "plot", "plots", "p")
.registerCommands(ParcelCommands(plugin)) .registerCommands(ParcelCommands(plugin))
.putDebugCommands(plugin) .putDebugCommands(plugin)
@@ -34,84 +27,3 @@ private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuild
.parent() .parent()
//@formatter:on //@formatter:on
} }
private fun invalidInput(parameter: IParameter<*>, message: String): Nothing {
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 {
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")
}
}
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)
}
}

View File

@@ -75,8 +75,19 @@ class ParcelCommands(override val plugin: ParcelsPlugin) : HasWorlds, HasPlugin
?: error("The specified parcel could not be matched") ?: error("The specified parcel could not be matched")
player.teleport(targetMatch.homeLocation) player.teleport(targetMatch.homeLocation)
""
} }
} }
} }
@Cmd("claim")
@Desc("If this parcel is unowned, makes you the owner",
shortVersion = "claims this parcel")
fun cmdClaim(player: Player) {
}
} }

View File

@@ -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")
}
}

View File

@@ -14,14 +14,14 @@ import java.util.*
import javax.sql.DataSource import javax.sql.DataSource
object WorldsT : Table("worlds") { object WorldsT : Table("worlds") {
val id = integer("id").autoIncrement().primaryKey() val id = integer("world_id").autoIncrement().primaryKey()
val name = varchar("name", 50) val name = varchar("name", 50)
val uid = binary("uid", 16) val uid = binary("uid", 16)
.also { uniqueIndex("index_uid", it) } .also { uniqueIndex("index_uid", it) }
} }
object ParcelsT : Table("parcels") { object ParcelsT : Table("parcels") {
val id = integer("id").autoIncrement().primaryKey() val id = integer("parcel_id").autoIncrement().primaryKey()
val px = integer("px") val px = integer("px")
val pz = integer("pz") val pz = integer("pz")
val world_id = integer("id") val world_id = integer("id")
@@ -98,7 +98,7 @@ class ExposedBacking(val dataSource: DataSource) : Backing {
private inline fun Transaction.getOrInitWorldId(worldUid: UUID, worldName: String): Int { private inline fun Transaction.getOrInitWorldId(worldUid: UUID, worldName: String): Int {
val binaryUid = worldUid.toByteArray()!! val binaryUid = worldUid.toByteArray()!!
return getWorldId(binaryUid) return getWorldId(binaryUid)
?: WorldsT.insertIgnore { it[uid] = binaryUid; it[name] = worldName }.get(WorldsT.id) ?: WorldsT.insert /*Ignore*/ { it[uid] = binaryUid; it[name] = worldName }.get(WorldsT.id)
?: throw ExposedDatabaseException("This should not happen - failed to insert world named $worldName and get its id") ?: throw ExposedDatabaseException("This should not happen - failed to insert world named $worldName and get its id")
} }
@@ -114,7 +114,7 @@ class ExposedBacking(val dataSource: DataSource) : Backing {
private inline fun Transaction.getOrInitParcelId(worldUid: UUID, worldName: String, parcelX: Int, parcelZ: Int): Int { private inline fun Transaction.getOrInitParcelId(worldUid: UUID, worldName: String, parcelX: Int, parcelZ: Int): Int {
val worldId = getOrInitWorldId(worldUid, worldName) val worldId = getOrInitWorldId(worldUid, worldName)
return getParcelId(worldId, parcelX, parcelZ) return getParcelId(worldId, parcelX, parcelZ)
?: ParcelsT.insertIgnore { it[world_id] = worldId; it[px] = parcelX; it[pz] = parcelZ }.get(ParcelsT.id) ?: ParcelsT.insert /*Ignore*/ { it[world_id] = worldId; it[px] = parcelX; it[pz] = parcelZ }.get(ParcelsT.id)
?: throw ExposedDatabaseException("This should not happen - failed to insert parcel at $worldName($parcelX, $parcelZ)") ?: throw ExposedDatabaseException("This should not happen - failed to insert parcel at $worldName($parcelX, $parcelZ)")
} }