summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDico200 <dico.karssiens@gmail.com>2018-07-25 01:45:59 +0100
committerDico200 <dico.karssiens@gmail.com>2018-07-25 01:45:59 +0100
commit60503351a30a91985b95ee8aa64e163ef084b34b (patch)
tree493bc97dec1e75801b2a30b964b4c8235c14b836
parent80def8901a168ba7be3f41d10147c083750771c6 (diff)
Update parcels2 project
-rw-r--r--.gitignore13
-rw-r--r--settings.gradle.kts5
-rw-r--r--src/main/kotlin/io/dico/parcels2/Parcel.kt9
-rw-r--r--src/main/kotlin/io/dico/parcels2/ParcelWorld.kt3
-rw-r--r--src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt1
-rw-r--r--src/main/kotlin/io/dico/parcels2/command/CommandAsync.kt4
-rw-r--r--src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt84
-rw-r--r--src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt94
-rw-r--r--src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt11
-rw-r--r--src/main/kotlin/io/dico/parcels2/command/ParcelParameterTypes.kt46
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/ExposedBacking.kt8
11 files changed, 171 insertions, 107 deletions
diff --git a/.gitignore b/.gitignore
index 93978c7..d24d2c2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,9 @@
-/.idea/
-/*.iml
-/.gradle/
-/out/
+.idea/
+*.iml
+.gradle/
+out/
+build/
/debug/
-/target/ \ No newline at end of file
+target/
+/gradle-output.txt
+/dicore3/ \ No newline at end of file
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 25ca636..020ed4e 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -1,2 +1,7 @@
rootProject.name = "parcels2"
+include("dicore3:core")
+findProject(":dicore3:core")?.name = "dicore3-core"
+
+include("dicore3:command")
+findProject(":dicore3:command")?.name = "dicore3-command"
diff --git a/src/main/kotlin/io/dico/parcels2/Parcel.kt b/src/main/kotlin/io/dico/parcels2/Parcel.kt
index b3eb7c9..cd05fb3 100644
--- a/src/main/kotlin/io/dico/parcels2/Parcel.kt
+++ b/src/main/kotlin/io/dico/parcels2/Parcel.kt
@@ -24,10 +24,6 @@ interface ParcelData {
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 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
fun copyDataIgnoringDatabase(data: ParcelData) {
diff --git a/src/main/kotlin/io/dico/parcels2/ParcelWorld.kt b/src/main/kotlin/io/dico/parcels2/ParcelWorld.kt
index 1003075..c346a04 100644
--- a/src/main/kotlin/io/dico/parcels2/ParcelWorld.kt
+++ b/src/main/kotlin/io/dico/parcels2/ParcelWorld.kt
@@ -182,10 +182,11 @@ class DefaultParcelContainer(private val world: ParcelWorld,
}
override fun parcelByID(x: Int, z: Int): Parcel? {
- return parcels[x][z]
+ return parcels.getOrNull(x)?.getOrNull(z)
}
override fun nextEmptyParcel(): Parcel? {
+ return parcels[0][0]
TODO()
}
diff --git a/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt b/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt
index da8577c..4330d1c 100644
--- a/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt
+++ b/src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt
@@ -46,6 +46,7 @@ class ParcelsPlugin : JavaPlugin() {
try {
storage = options.storage.newStorageInstance()
+ storage.init()
} catch (ex: Exception) {
plogger.error("Failed to connect to database", ex)
return false
diff --git a/src/main/kotlin/io/dico/parcels2/command/CommandAsync.kt b/src/main/kotlin/io/dico/parcels2/command/CommandAsync.kt
index bf38e04..2131715 100644
--- a/src/main/kotlin/io/dico/parcels2/command/CommandAsync.kt
+++ b/src/main/kotlin/io/dico/parcels2/command/CommandAsync.kt
@@ -23,7 +23,7 @@ class CommandAsyncScope {
suspend fun <T> HasPlugin.awaitSynchronousTask(delay: Int = 0, task: () -> T): T {
return suspendCoroutine { cont: Continuation<T> ->
- plugin.server.scheduler.runTaskLater(plugin, l@ {
+ plugin.server.scheduler.runTaskLater(plugin, l@{
val result = try {
task()
} catch (ex: CommandException) {
@@ -45,7 +45,7 @@ class CommandAsyncScope {
}
fun <T : Any?> HasPlugin.delegateCommandAsync(context: ExecutionContext,
- block: suspend CommandAsyncScope.() -> T) {
+ block: suspend CommandAsyncScope.() -> T) {
val job: Deferred<Any?> = async(/*context = plugin.storage.asyncDispatcher, */start = CoroutineStart.ATOMIC) {
CommandAsyncScope().block()
diff --git a/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt b/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt
new file mode 100644
index 0000000..ceb226f
--- /dev/null
+++ b/src/main/kotlin/io/dico/parcels2/command/NamedParcelTargetType.kt
@@ -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)
+ }
+
+}
+
diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt
index 9b09192..e06357f 100644
--- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt
+++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt
@@ -1,23 +1,16 @@
package io.dico.parcels2.command
import io.dico.dicore.command.CommandBuilder
-import io.dico.dicore.command.CommandException
import io.dico.dicore.command.ICommandDispatcher
-import io.dico.dicore.command.parameter.ArgumentBuffer
-import io.dico.dicore.command.parameter.IParameter
-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
+import io.dico.parcels2.ParcelsPlugin
+import io.dico.parcels2.debugging
@Suppress("UsePropertyAccessSyntax")
fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher {
//@formatter:off
return CommandBuilder()
.addParameterType(false, ParcelParameterType(plugin.worlds))
- .addParameterType(false, ParcelHomeParameterType(plugin.worlds))
+ .addParameterType(true, ParcelHomeParameterType(plugin.worlds))
.group("parcel", "plot", "plots", "p")
.registerCommands(ParcelCommands(plugin))
.putDebugCommands(plugin)
@@ -34,84 +27,3 @@ private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuild
.parent()
//@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)
- }
-
-}
diff --git a/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt b/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt
index 6cc5e02..b10c23e 100644
--- a/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt
+++ b/src/main/kotlin/io/dico/parcels2/command/ParcelCommands.kt
@@ -75,8 +75,19 @@ class ParcelCommands(override val plugin: ParcelsPlugin) : HasWorlds, HasPlugin
?: error("The specified parcel could not be matched")
player.teleport(targetMatch.homeLocation)
+ ""
}
}
}
+ @Cmd("claim")
+ @Desc("If this parcel is unowned, makes you the owner",
+ shortVersion = "claims this parcel")
+ fun cmdClaim(player: Player) {
+
+ }
+
+
+
+
} \ No newline at end of file
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")
+ }
+
+}
diff --git a/src/main/kotlin/io/dico/parcels2/storage/ExposedBacking.kt b/src/main/kotlin/io/dico/parcels2/storage/ExposedBacking.kt
index fdbb9e2..f25c2b7 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/ExposedBacking.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/ExposedBacking.kt
@@ -14,14 +14,14 @@ import java.util.*
import javax.sql.DataSource
object WorldsT : Table("worlds") {
- val id = integer("id").autoIncrement().primaryKey()
+ val id = integer("world_id").autoIncrement().primaryKey()
val name = varchar("name", 50)
val uid = binary("uid", 16)
.also { uniqueIndex("index_uid", it) }
}
object ParcelsT : Table("parcels") {
- val id = integer("id").autoIncrement().primaryKey()
+ val id = integer("parcel_id").autoIncrement().primaryKey()
val px = integer("px")
val pz = integer("pz")
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 {
val binaryUid = worldUid.toByteArray()!!
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")
}
@@ -114,7 +114,7 @@ class ExposedBacking(val dataSource: DataSource) : Backing {
private inline fun Transaction.getOrInitParcelId(worldUid: UUID, worldName: String, parcelX: Int, parcelZ: Int): Int {
val worldId = getOrInitWorldId(worldUid, worldName)
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)")
}