Initial commit
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
/.idea/
|
||||||
|
/*.iml
|
||||||
|
/.gradle/
|
||||||
|
/out/
|
||||||
|
/debug/
|
||||||
|
/target/
|
||||||
32
build.gradle.kts
Normal file
32
build.gradle.kts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.dsl.Coroutines
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm") version "1.2.51"
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "io.dico"
|
||||||
|
version = "0.1"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven("https://dl.bintray.com/kotlin/exposed")
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin.experimental.coroutines = Coroutines.ENABLE
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
compile(files("../res/spigot-1.13-pre7.jar"))
|
||||||
|
compile(kotlin("stdlib-jdk8"))
|
||||||
|
compile("org.jetbrains.exposed:exposed:0.10.3")
|
||||||
|
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.4")
|
||||||
|
testCompile("junit:junit:4.12")
|
||||||
|
}
|
||||||
|
|
||||||
|
val jar by tasks.getting(Jar::class)
|
||||||
|
val fatJar by tasks.creating(Jar::class) {
|
||||||
|
baseName = "parcels2-all"
|
||||||
|
manifest.attributes["Main-Class"] = ""
|
||||||
|
destinationDir = file("debug/plugins")
|
||||||
|
from(*configurations.compile.map(::zipTree).toTypedArray())
|
||||||
|
with(jar)
|
||||||
|
}
|
||||||
2
settings.gradle.kts
Normal file
2
settings.gradle.kts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
rootProject.name = "parcels2"
|
||||||
|
|
||||||
38
src/main/kotlin/io/dico/parcels2/Parcel.kt
Normal file
38
src/main/kotlin/io/dico/parcels2/Parcel.kt
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package io.dico.parcels2
|
||||||
|
|
||||||
|
import io.dico.parcels2.math.Vec2i
|
||||||
|
import io.dico.parcels2.util.getPlayerName
|
||||||
|
import org.bukkit.Bukkit
|
||||||
|
import org.bukkit.entity.Player
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class Parcel(val world: ParcelWorld,
|
||||||
|
val pos: Vec2i,
|
||||||
|
val data: ParcelData = ParcelData()) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ParcelData {
|
||||||
|
val owner: ParcelOwner? = null
|
||||||
|
val added = mutableMapOf<UUID, Boolean>()
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ParcelOwner(val uuid: UUID? = null,
|
||||||
|
val name: String? = null) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
uuid ?: name ?: throw IllegalArgumentException("uuid and/or name must be present")
|
||||||
|
}
|
||||||
|
|
||||||
|
val playerName get() = getPlayerName(uuid, name)
|
||||||
|
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
val offlinePlayer get() = (uuid?.let { Bukkit.getOfflinePlayer(it) } ?: Bukkit.getOfflinePlayer(name))
|
||||||
|
?.takeIf { it.isOnline() || it.hasPlayedBefore() }
|
||||||
|
|
||||||
|
fun matches(player: Player, allowNameMatch: Boolean = false): Boolean {
|
||||||
|
return player.uniqueId == uuid || (allowNameMatch && player.name == name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
4
src/main/kotlin/io/dico/parcels2/ParcelWorld.kt
Normal file
4
src/main/kotlin/io/dico/parcels2/ParcelWorld.kt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package io.dico.parcels2
|
||||||
|
|
||||||
|
class ParcelWorld {
|
||||||
|
}
|
||||||
12
src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt
Normal file
12
src/main/kotlin/io/dico/parcels2/ParcelsPlugin.kt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package io.dico.parcels2
|
||||||
|
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin
|
||||||
|
|
||||||
|
class ParcelsPlugin : JavaPlugin() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
7
src/main/kotlin/io/dico/parcels2/math/Vec2i.kt
Normal file
7
src/main/kotlin/io/dico/parcels2/math/Vec2i.kt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package io.dico.parcels2.math
|
||||||
|
|
||||||
|
data class Vec2i(
|
||||||
|
val x: Int,
|
||||||
|
val z: Int
|
||||||
|
)
|
||||||
|
|
||||||
69
src/main/kotlin/io/dico/parcels2/storage/Storage.kt
Normal file
69
src/main/kotlin/io/dico/parcels2/storage/Storage.kt
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package io.dico.parcels2.storage
|
||||||
|
|
||||||
|
import kotlinx.coroutines.experimental.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.experimental.CoroutineScope
|
||||||
|
import kotlinx.coroutines.experimental.CoroutineStart
|
||||||
|
import kotlinx.coroutines.experimental.asCoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.experimental.channels.ProducerJob
|
||||||
|
import kotlinx.coroutines.experimental.channels.produce
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.CompletableFuture
|
||||||
|
import java.util.concurrent.Executor
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
|
/*
|
||||||
|
interface Storage {
|
||||||
|
|
||||||
|
val name: String
|
||||||
|
|
||||||
|
val syncDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
val asyncDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
fun init(): CompletableFuture<Unit>
|
||||||
|
|
||||||
|
fun shutdown(): CompletableFuture<Unit>
|
||||||
|
|
||||||
|
fun readPlotData(plotFor: Plot): CompletableFuture<PlotData?>
|
||||||
|
|
||||||
|
fun readPlotData(plotsFor: Sequence<Plot>, channelCapacity: Int): ProducerJob<Pair<Plot, PlotData?>>
|
||||||
|
|
||||||
|
fun getOwnedPlots(user: PlotOwner): CompletableFuture<List<SerializablePlot>>
|
||||||
|
|
||||||
|
fun setPlotOwner(plotFor: Plot, owner: PlotOwner?): CompletableFuture<Unit>
|
||||||
|
|
||||||
|
fun setPlotPlayerState(plotFor: Plot, player: UUID, state: Boolean?): CompletableFuture<Unit>
|
||||||
|
|
||||||
|
fun setPlotAllowsInteractInventory(plot: Plot, value: Boolean): CompletableFuture<Unit>
|
||||||
|
|
||||||
|
fun setPlotAllowsInteractInputs(plot: Plot, value: Boolean): CompletableFuture<Unit>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class StorageWithBacking internal constructor(val backing: Backing) : Storage {
|
||||||
|
override val name get() = backing.name
|
||||||
|
override val syncDispatcher = Executor { it.run() }.asCoroutineDispatcher()
|
||||||
|
override val asyncDispatcher = Executors.newFixedThreadPool(4) { Thread(it, "AbstractStorageThread") }.asCoroutineDispatcher()
|
||||||
|
|
||||||
|
private fun <T> future(block: suspend CoroutineScope.() -> T) = kotlinx.coroutines.experimental.future.future(asyncDispatcher, CoroutineStart.ATOMIC, block)
|
||||||
|
|
||||||
|
override fun init(): CompletableFuture<Unit> = future { backing.init() }
|
||||||
|
|
||||||
|
override fun shutdown(): CompletableFuture<Unit> = future { backing.shutdown() }
|
||||||
|
|
||||||
|
override fun readPlotData(plotFor: Plot) = future { backing.readPlotData(plotFor) }
|
||||||
|
|
||||||
|
override fun readPlotData(plotsFor: Sequence<Plot>, channelCapacity: Int) =
|
||||||
|
produce<Pair<Plot, PlotData?>>(asyncDispatcher, capacity = channelCapacity) { backing.producePlotData(this, plotsFor) }
|
||||||
|
|
||||||
|
override fun getOwnedPlots(user: PlotOwner) = future { backing.getOwnedPlots(user) }
|
||||||
|
|
||||||
|
override fun setPlotOwner(plotFor: Plot, owner: PlotOwner?) = future { backing.setPlotOwner(plotFor, owner) }
|
||||||
|
|
||||||
|
override fun setPlotPlayerState(plotFor: Plot, player: UUID, state: Boolean?) = future { backing.setPlotPlayerState(plotFor, player, state) }
|
||||||
|
|
||||||
|
override fun setPlotAllowsInteractInventory(plot: Plot, value: Boolean) = future { backing.setPlotAllowsInteractInventory(plot, value) }
|
||||||
|
|
||||||
|
override fun setPlotAllowsInteractInputs(plot: Plot, value: Boolean) = future { backing.setPlotAllowsInteractInputs(plot, value) }
|
||||||
|
}
|
||||||
|
*/
|
||||||
41
src/main/kotlin/io/dico/parcels2/storage/backing/Exposed.kt
Normal file
41
src/main/kotlin/io/dico/parcels2/storage/backing/Exposed.kt
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package io.dico.parcels2.storage.backing
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.ReferenceOption
|
||||||
|
import org.jetbrains.exposed.sql.Table
|
||||||
|
|
||||||
|
object ParcelsTable : Table() {
|
||||||
|
val id = integer("id").autoIncrement().primaryKey()
|
||||||
|
val px = integer("px")
|
||||||
|
val pz = integer("pz")
|
||||||
|
val world_uuid = binary("world_uuid", 16).also { uniqueIndex("location", it, px, pz) }
|
||||||
|
val world = varchar("world", 32).nullable()
|
||||||
|
val owner_uuid = binary("owner_uuid", 16).nullable()
|
||||||
|
val owner = varchar("owner", 16).nullable()
|
||||||
|
}
|
||||||
|
|
||||||
|
object ParcelsAddedTable : Table() {
|
||||||
|
val id = integer("id").references(ParcelsTable.id, ReferenceOption.CASCADE)
|
||||||
|
val player_uuid = binary("player_uuid", 16).also { uniqueIndex("pair", id, it) }
|
||||||
|
val allowed_flag = bool("allowed_flag")
|
||||||
|
}
|
||||||
|
|
||||||
|
object PlayerAddedTable : Table() {
|
||||||
|
val owner_uuid = binary("owner_uuid", 16)
|
||||||
|
val player_uuid = binary("player_uuid", 16).also { uniqueIndex("pair", owner_uuid, it) }
|
||||||
|
val allowed_flag = bool("allowed_flag")
|
||||||
|
}
|
||||||
|
|
||||||
|
class AbstractParcelsDatabase {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
23
src/main/kotlin/io/dico/parcels2/util/UUIDUtil.kt
Normal file
23
src/main/kotlin/io/dico/parcels2/util/UUIDUtil.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package io.dico.parcels2.util
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
@Suppress("UsePropertyAccessSyntax")
|
||||||
|
fun getPlayerName(uuid: UUID?, ifUnknown: String? = null): String {
|
||||||
|
return uuid?.let { Bukkit.getOfflinePlayer(uuid)?.takeIf { it.isOnline() || it.hasPlayedBefore() }?.name }
|
||||||
|
?: ifUnknown
|
||||||
|
?: ":unknown_name:"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UUID?.toByteArray(): ByteArray? = this?.let {
|
||||||
|
ByteBuffer.allocate(16).apply {
|
||||||
|
putLong(mostSignificantBits)
|
||||||
|
putLong(leastSignificantBits)
|
||||||
|
}.array()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray?.toUUID(): UUID? = this?.let {
|
||||||
|
ByteBuffer.wrap(it).run { UUID(long, long) }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user