Archived
0

Initial commit

This commit is contained in:
Dico200
2018-07-21 20:48:03 +01:00
commit 519f3f6b5c
10 changed files with 234 additions and 0 deletions

View 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)
}
}

View File

@@ -0,0 +1,4 @@
package io.dico.parcels2
class ParcelWorld {
}

View File

@@ -0,0 +1,12 @@
package io.dico.parcels2
import org.bukkit.plugin.java.JavaPlugin
class ParcelsPlugin : JavaPlugin() {
}

View File

@@ -0,0 +1,7 @@
package io.dico.parcels2.math
data class Vec2i(
val x: Int,
val z: Int
)

View 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) }
}
*/

View 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 {
}

View 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) }
}