Replace AddedData API with Privileges API, adding CAN_MANAGE and required changes
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
@file:Suppress("UNCHECKED_CAST")
|
||||
|
||||
package io.dico.parcels2.defaultimpl
|
||||
|
||||
import io.dico.parcels2.*
|
||||
import io.dico.parcels2.util.ext.alsoIfTrue
|
||||
import java.util.Collections
|
||||
|
||||
class GlobalAddedDataManagerImpl(val plugin: ParcelsPlugin) : GlobalAddedDataManager {
|
||||
private val map = mutableMapOf<PlayerProfile, GlobalAddedData>()
|
||||
|
||||
override fun get(owner: PlayerProfile): GlobalAddedData {
|
||||
return map[owner] ?: GlobalAddedDataImpl(owner).also { map[owner] = it }
|
||||
}
|
||||
|
||||
private inner class GlobalAddedDataImpl(override val owner: PlayerProfile,
|
||||
data: MutableAddedDataMap = emptyData)
|
||||
: AddedDataHolder(data), GlobalAddedData {
|
||||
|
||||
private inline var data get() = addedMap; set(value) = run { addedMap = value }
|
||||
private inline val isEmpty get() = data === emptyData
|
||||
|
||||
override fun setStatus(key: StatusKey, status: AddedStatus): Boolean {
|
||||
if (isEmpty) {
|
||||
if (status == AddedStatus.DEFAULT) return false
|
||||
data = mutableMapOf()
|
||||
}
|
||||
return super.setStatus(key, status).alsoIfTrue {
|
||||
plugin.storage.setGlobalAddedStatus(owner, key, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val emptyData = Collections.emptyMap<Any, Any>() as MutableAddedDataMap
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
@file:Suppress("UNCHECKED_CAST")
|
||||
|
||||
package io.dico.parcels2.defaultimpl
|
||||
|
||||
import io.dico.parcels2.*
|
||||
import io.dico.parcels2.util.ext.alsoIfTrue
|
||||
import java.util.Collections
|
||||
|
||||
class GlobalPrivilegesManagerImpl(val plugin: ParcelsPlugin) : GlobalPrivilegesManager {
|
||||
private val map = mutableMapOf<PlayerProfile, GlobalPrivileges>()
|
||||
|
||||
override fun get(owner: PlayerProfile): GlobalPrivileges {
|
||||
return map[owner] ?: GlobalPrivilegesImpl(owner).also { map[owner] = it }
|
||||
}
|
||||
|
||||
private inner class GlobalPrivilegesImpl(override val owner: PlayerProfile,
|
||||
data: MutablePrivilegeMap = emptyData)
|
||||
: PrivilegesHolder(data), GlobalPrivileges {
|
||||
|
||||
private inline var data get() = map; set(value) = run { map = value }
|
||||
private inline val isEmpty get() = data === emptyData
|
||||
|
||||
override fun setPrivilege(key: PrivilegeKey, privilege: Privilege): Boolean {
|
||||
if (isEmpty) {
|
||||
if (privilege == Privilege.DEFAULT) return false
|
||||
data = mutableMapOf()
|
||||
}
|
||||
return super.set(key, privilege).alsoIfTrue {
|
||||
plugin.storage.setGlobalPrivilege(owner, key, privilege)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val emptyData = Collections.emptyMap<Any, Any>() as MutablePrivilegeMap
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,20 +35,20 @@ class ParcelImpl(
|
||||
world.storage.setParcelData(this, null)
|
||||
}
|
||||
|
||||
override val addedMap: AddedDataMap get() = data.addedMap
|
||||
override fun getStatus(key: StatusKey) = data.getStatus(key)
|
||||
override fun isBanned(key: StatusKey) = data.isBanned(key)
|
||||
override fun isAllowed(key: StatusKey) = data.isAllowed(key)
|
||||
override val map: PrivilegeMap get() = data.map
|
||||
override fun privilege(key: PrivilegeKey) = data.privilege(key)
|
||||
override fun isBanned(key: PrivilegeKey) = data.isBanned(key)
|
||||
override fun hasPrivilegeToBuild(key: PrivilegeKey) = data.hasPrivilegeToBuild(key)
|
||||
override fun canBuild(player: OfflinePlayer, checkAdmin: Boolean, checkGlobal: Boolean): Boolean {
|
||||
return (data.canBuild(player, checkAdmin, false))
|
||||
|| checkGlobal && world.globalAddedData[owner ?: return false].isAllowed(player)
|
||||
|| checkGlobal && world.globalPrivileges[owner ?: return false].hasPrivilegeToBuild(player)
|
||||
}
|
||||
|
||||
override var statusOfStar: AddedStatus
|
||||
get() = data.statusOfStar
|
||||
set(value) = run { setStatus(PlayerProfile.Star, value) }
|
||||
override var privilegeOfStar: Privilege
|
||||
get() = data.privilegeOfStar
|
||||
set(value) = run { setPrivilege(PlayerProfile.Star, value) }
|
||||
|
||||
val globalAddedMap: AddedDataMap? get() = owner?.let { world.globalAddedData[it].addedMap }
|
||||
val globalAddedMap: PrivilegeMap? get() = owner?.let { world.globalPrivileges[it].map }
|
||||
|
||||
override val lastClaimTime: DateTime? get() = data.lastClaimTime
|
||||
|
||||
@@ -71,12 +71,16 @@ class ParcelImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override fun setStatus(key: StatusKey, status: AddedStatus): Boolean {
|
||||
return data.setStatus(key, status).alsoIfTrue {
|
||||
world.storage.setParcelPlayerStatus(this, key, status)
|
||||
override fun setPrivilege(key: PrivilegeKey, privilege: Privilege): Boolean {
|
||||
return data.setPrivilege(key, privilege).alsoIfTrue {
|
||||
world.storage.setLocalPrivilege(this, key, privilege)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateInteractableConfigStorage() {
|
||||
world.storage.setParcelOptionsInteractConfig(this, data.interactableConfig)
|
||||
}
|
||||
|
||||
private var _interactableConfig: InteractableConfiguration? = null
|
||||
override var interactableConfig: InteractableConfiguration
|
||||
get() {
|
||||
@@ -86,21 +90,18 @@ class ParcelImpl(
|
||||
override fun isInteractable(clazz: Interactables): Boolean = data.interactableConfig.isInteractable(clazz)
|
||||
|
||||
override fun setInteractable(clazz: Interactables, interactable: Boolean): Boolean =
|
||||
data.interactableConfig.setInteractable(clazz, interactable).alsoIfTrue {
|
||||
// TODO update storage
|
||||
}
|
||||
data.interactableConfig.setInteractable(clazz, interactable).alsoIfTrue { updateInteractableConfigStorage() }
|
||||
|
||||
override fun clear(): Boolean =
|
||||
data.interactableConfig.clear().alsoIfTrue {
|
||||
// TODO update storage
|
||||
}
|
||||
data.interactableConfig.clear().alsoIfTrue { updateInteractableConfigStorage() }
|
||||
}
|
||||
}
|
||||
return _interactableConfig!!
|
||||
}
|
||||
set(value) {
|
||||
data.interactableConfig.copyFrom(value)
|
||||
// TODO update storage
|
||||
if (data.interactableConfig.copyFrom(value)) {
|
||||
updateInteractableConfigStorage()
|
||||
}
|
||||
}
|
||||
|
||||
private var blockVisitors = AtomicInteger(0)
|
||||
@@ -139,10 +140,10 @@ private object ParcelInfoStringComputer {
|
||||
append(' ')
|
||||
}
|
||||
|
||||
private fun StringBuilder.appendAddedList(local: AddedDataMap, global: AddedDataMap, status: AddedStatus, fieldName: String) {
|
||||
private fun StringBuilder.appendAddedList(local: PrivilegeMap, global: PrivilegeMap, status: Privilege, fieldName: String) {
|
||||
val globalSet = global.filterValues { it == status }.keys
|
||||
val localList = local.filterValues { it == status }.keys.filter { it !in globalSet }
|
||||
val stringList = globalSet.map(StatusKey::notNullName).map { "(G)$it" } + localList.map(StatusKey::notNullName)
|
||||
val stringList = globalSet.map(PrivilegeKey::notNullName).map { "(G)$it" } + localList.map(PrivilegeKey::notNullName)
|
||||
if (stringList.isEmpty()) return
|
||||
|
||||
appendField({
|
||||
@@ -182,11 +183,11 @@ private object ParcelInfoStringComputer {
|
||||
|
||||
append('\n')
|
||||
|
||||
val global = owner?.let { parcel.world.globalAddedData[owner].addedMap } ?: emptyMap()
|
||||
val local = parcel.addedMap
|
||||
appendAddedList(local, global, AddedStatus.ALLOWED, "Allowed")
|
||||
val global = owner?.let { parcel.world.globalPrivileges[owner].map } ?: emptyMap()
|
||||
val local = parcel.map
|
||||
appendAddedList(local, global, Privilege.CAN_BUILD, "Allowed")
|
||||
append('\n')
|
||||
appendAddedList(local, global, AddedStatus.BANNED, "Banned")
|
||||
appendAddedList(local, global, Privilege.BANNED, "Banned")
|
||||
|
||||
/* TODO options
|
||||
if (!parcel.allowInteractInputs || !parcel.allowInteractInventory) {
|
||||
|
||||
@@ -2,8 +2,6 @@ package io.dico.parcels2.defaultimpl
|
||||
|
||||
import io.dico.parcels2.*
|
||||
import io.dico.parcels2.util.schedule
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.CoroutineStart.*
|
||||
import kotlinx.coroutines.Unconfined
|
||||
import kotlinx.coroutines.launch
|
||||
import org.bukkit.Bukkit
|
||||
@@ -61,7 +59,7 @@ class ParcelProviderImpl(val plugin: ParcelsPlugin) : ParcelProvider {
|
||||
else WorldCreator(worldName).generator(generator).createWorld().also { logger.info("Creating world $worldName") }
|
||||
|
||||
parcelWorld = ParcelWorldImpl(bukkitWorld, generator, worldOptions.runtime, plugin.storage,
|
||||
plugin.globalAddedData, ::DefaultParcelContainer, plugin, plugin.worktimeLimiter)
|
||||
plugin.globalPrivileges, ::DefaultParcelContainer, plugin, plugin.worktimeLimiter)
|
||||
|
||||
if (!worldExists) {
|
||||
val time = DateTime.now()
|
||||
@@ -119,7 +117,7 @@ class ParcelProviderImpl(val plugin: ParcelsPlugin) : ParcelProvider {
|
||||
worldOptions,
|
||||
worldOptions.generator.newGenerator(this, worldName),
|
||||
plugin.storage,
|
||||
plugin.globalAddedData,
|
||||
plugin.globalPrivileges,
|
||||
::DefaultParcelContainer)
|
||||
|
||||
} catch (ex: Exception) {
|
||||
|
||||
@@ -15,7 +15,7 @@ class ParcelWorldImpl(override val world: World,
|
||||
override val generator: ParcelGenerator,
|
||||
override var options: RuntimeWorldOptions,
|
||||
override val storage: Storage,
|
||||
override val globalAddedData: GlobalAddedDataManager,
|
||||
val globalPrivileges: GlobalPrivilegesManager,
|
||||
containerFactory: ParcelContainerFactory,
|
||||
coroutineScope: CoroutineScope,
|
||||
worktimeLimiter: WorktimeLimiter)
|
||||
|
||||
Reference in New Issue
Block a user