work on global added data
This commit is contained in:
@@ -1,19 +1,63 @@
|
|||||||
package io.dico.parcels2
|
package io.dico.parcels2
|
||||||
|
|
||||||
|
import io.dico.parcels2.util.uuid
|
||||||
|
import kotlinx.coroutines.experimental.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.experimental.Deferred
|
||||||
import org.bukkit.OfflinePlayer
|
import org.bukkit.OfflinePlayer
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
interface GlobalAddedData : AddedData {
|
||||||
|
val uuid: UUID
|
||||||
|
}
|
||||||
|
|
||||||
class GlobalAddedDataManager(val plugin: ParcelsPlugin) {
|
class GlobalAddedDataManager(val plugin: ParcelsPlugin) {
|
||||||
|
private val map = mutableMapOf<UUID, GlobalAddedData?>()
|
||||||
|
|
||||||
|
operator fun get(player: OfflinePlayer) = get(player.uuid)
|
||||||
|
|
||||||
operator fun get(player: OfflinePlayer): AddedData {
|
operator fun get(uuid: UUID): GlobalAddedData? {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
operator fun get(uuid: UUID): AddedData {
|
fun getDeferred(uuid: UUID): Deferred<AddedData> {
|
||||||
|
get(uuid)?.let { return CompletableDeferred(it) }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun getAsync(uuid: UUID): GlobalAddedData {
|
||||||
|
val data = plugin.storage.readGlobalAddedData(ParcelOwner(uuid = uuid)).await()
|
||||||
|
?: return GlobalAddedDataImpl(uuid)
|
||||||
|
val result = GlobalAddedDataImpl(uuid, data)
|
||||||
|
map[uuid] = result
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private inner class GlobalAddedDataImpl(override val uuid: UUID,
|
||||||
|
data: MutableMap<UUID, AddedStatus> = emptyData)
|
||||||
|
: AddedDataHolder(data), GlobalAddedData {
|
||||||
|
|
||||||
|
private inline var data get() = added; set(value) = run { added = value }
|
||||||
|
private inline val isEmpty get() = data === emptyData
|
||||||
|
|
||||||
|
override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean {
|
||||||
|
if (isEmpty) {
|
||||||
|
if (status == AddedStatus.DEFAULT) return false
|
||||||
|
data = mutableMapOf()
|
||||||
|
}
|
||||||
|
return super.setAddedStatus(uuid, status).also {
|
||||||
|
if (it) plugin.storage.setGlobalAddedStatus(ParcelOwner(uuid = this.uuid), uuid, status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
val emptyData = mapOf<UUID, AddedStatus>() as MutableMap<UUID, AddedStatus>
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -117,8 +117,8 @@ class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData {
|
|||||||
var hasBlockVisitors: Boolean = false; private set
|
var hasBlockVisitors: Boolean = false; private set
|
||||||
}
|
}
|
||||||
|
|
||||||
open class AddedDataHolder : AddedData {
|
open class AddedDataHolder(override var added: MutableMap<UUID, AddedStatus>
|
||||||
override var added = mutableMapOf<UUID, AddedStatus>()
|
= mutableMapOf<UUID, AddedStatus>()) : AddedData {
|
||||||
override fun getAddedStatus(uuid: UUID): AddedStatus = added.getOrDefault(uuid, AddedStatus.DEFAULT)
|
override fun getAddedStatus(uuid: UUID): AddedStatus = added.getOrDefault(uuid, AddedStatus.DEFAULT)
|
||||||
override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean = status.takeIf { it != AddedStatus.DEFAULT }
|
override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean = status.takeIf { it != AddedStatus.DEFAULT }
|
||||||
?.let { added.put(uuid, it) != it }
|
?.let { added.put(uuid, it) != it }
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import io.dico.parcels2.listener.ParcelListeners
|
|||||||
import io.dico.parcels2.storage.Storage
|
import io.dico.parcels2.storage.Storage
|
||||||
import io.dico.parcels2.storage.yamlObjectMapper
|
import io.dico.parcels2.storage.yamlObjectMapper
|
||||||
import io.dico.parcels2.util.tryCreate
|
import io.dico.parcels2.util.tryCreate
|
||||||
import kotlinx.coroutines.experimental.asCoroutineDispatcher
|
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
import org.bukkit.plugin.java.JavaPlugin
|
import org.bukkit.plugin.java.JavaPlugin
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
@@ -33,6 +32,14 @@ class ParcelsPlugin : JavaPlugin() {
|
|||||||
private var cmdDispatcher: ICommandDispatcher? = null
|
private var cmdDispatcher: ICommandDispatcher? = null
|
||||||
val worktimeLimiter: WorktimeLimiter by lazy { TickWorktimeLimiter(this, options.tickWorktime) }
|
val worktimeLimiter: WorktimeLimiter by lazy { TickWorktimeLimiter(this, options.tickWorktime) }
|
||||||
|
|
||||||
|
val mainThreadDispatcher = object : Executor {
|
||||||
|
private val mainThread = Thread.currentThread()
|
||||||
|
override fun execute(command: Runnable) {
|
||||||
|
if (Thread.currentThread() === mainThread) command.run()
|
||||||
|
else server.scheduler.runTask(this@ParcelsPlugin, command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
plogger.info("Debug enabled: ${plogger.isDebugEnabled}")
|
plogger.info("Debug enabled: ${plogger.isDebugEnabled}")
|
||||||
if (!init()) {
|
if (!init()) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package io.dico.parcels2.storage
|
package io.dico.parcels2.storage
|
||||||
|
|
||||||
import io.dico.parcels2.AddedData
|
import io.dico.parcels2.AddedStatus
|
||||||
import io.dico.parcels2.Parcel
|
import io.dico.parcels2.Parcel
|
||||||
import io.dico.parcels2.ParcelData
|
import io.dico.parcels2.ParcelData
|
||||||
import io.dico.parcels2.ParcelOwner
|
import io.dico.parcels2.ParcelOwner
|
||||||
@@ -44,8 +44,8 @@ interface Backing {
|
|||||||
suspend fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean)
|
suspend fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean)
|
||||||
|
|
||||||
|
|
||||||
suspend fun readGlobalPlayerStateData(owner: ParcelOwner): AddedData?
|
suspend fun readGlobalAddedData(owner: ParcelOwner): MutableMap<UUID, AddedStatus>
|
||||||
|
|
||||||
suspend fun setGlobalPlayerState(owner: ParcelOwner, player: UUID, state: Boolean?)
|
suspend fun setGlobalAddedStatus(owner: ParcelOwner, player: UUID, status: AddedStatus)
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -275,11 +275,11 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource) : Backing
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun readGlobalPlayerStateData(owner: ParcelOwner): AddedData? {
|
override suspend fun readGlobalAddedData(owner: ParcelOwner): AddedData? {
|
||||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setGlobalPlayerState(owner: ParcelOwner, player: UUID, state: Boolean?) {
|
override suspend fun setGlobalAddedStatus(owner: ParcelOwner, player: UUID, status: Boolean?) {
|
||||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package io.dico.parcels2.storage
|
package io.dico.parcels2.storage
|
||||||
|
|
||||||
import io.dico.parcels2.AddedData
|
import io.dico.parcels2.AddedStatus
|
||||||
import io.dico.parcels2.Parcel
|
import io.dico.parcels2.Parcel
|
||||||
import io.dico.parcels2.ParcelData
|
import io.dico.parcels2.ParcelData
|
||||||
import io.dico.parcels2.ParcelOwner
|
import io.dico.parcels2.ParcelOwner
|
||||||
@@ -44,9 +44,9 @@ interface Storage {
|
|||||||
fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean): Job
|
fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean): Job
|
||||||
|
|
||||||
|
|
||||||
fun readGlobalPlayerStateData(owner: ParcelOwner): Deferred<AddedData?>
|
fun readGlobalAddedData(owner: ParcelOwner): Deferred<MutableMap<UUID, AddedStatus>?>
|
||||||
|
|
||||||
fun setGlobalPlayerState(owner: ParcelOwner, player: UUID, state: Boolean?): Job
|
fun setGlobalAddedStatus(owner: ParcelOwner, player: UUID, status: AddedStatus): Job
|
||||||
}
|
}
|
||||||
|
|
||||||
class StorageWithCoroutineBacking internal constructor(val backing: Backing) : Storage {
|
class StorageWithCoroutineBacking internal constructor(val backing: Backing) : Storage {
|
||||||
@@ -94,7 +94,7 @@ class StorageWithCoroutineBacking internal constructor(val backing: Backing) : S
|
|||||||
override fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean) = job { backing.setParcelAllowsInteractInputs(parcel, value) }
|
override fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean) = job { backing.setParcelAllowsInteractInputs(parcel, value) }
|
||||||
|
|
||||||
|
|
||||||
override fun readGlobalPlayerStateData(owner: ParcelOwner): Deferred<AddedData?> = defer { backing.readGlobalPlayerStateData(owner) }
|
override fun readGlobalAddedData(owner: ParcelOwner): Deferred<MutableMap<UUID, AddedStatus>?> = defer { backing.readGlobalAddedData(owner) }
|
||||||
|
|
||||||
override fun setGlobalPlayerState(owner: ParcelOwner, player: UUID, state: Boolean?) = job { backing.setGlobalPlayerState(owner, player, state) }
|
override fun setGlobalAddedStatus(owner: ParcelOwner, player: UUID, status: AddedStatus) = job { backing.setGlobalAddedStatus(owner, player, status) }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user