Add /p global list, tweaks/fixes
This commit is contained in:
@@ -52,6 +52,7 @@ class DefaultParcelContainer(val world: ParcelWorld) : ParcelContainer {
|
||||
private fun walkInCircle(): Iterable<Parcel> = Iterable {
|
||||
iterator {
|
||||
val center = world.options.axisLimit
|
||||
yield(parcels[center][center])
|
||||
for (radius in 0..center) {
|
||||
var x = center - radius;
|
||||
var z = center - radius
|
||||
|
||||
88
src/main/kotlin/io/dico/parcels2/defaultimpl/InfoBuilder.kt
Normal file
88
src/main/kotlin/io/dico/parcels2/defaultimpl/InfoBuilder.kt
Normal file
@@ -0,0 +1,88 @@
|
||||
package io.dico.parcels2.defaultimpl
|
||||
|
||||
import io.dico.dicore.Formatting
|
||||
import io.dico.parcels2.Privilege
|
||||
import io.dico.parcels2.PrivilegeKey
|
||||
import io.dico.parcels2.RawPrivileges
|
||||
import io.dico.parcels2.filterProfilesWithPrivilegeTo
|
||||
|
||||
object InfoBuilder {
|
||||
val infoStringColor1 = Formatting.GREEN
|
||||
val infoStringColor2 = Formatting.AQUA
|
||||
|
||||
inline fun StringBuilder.appendField(field: StringBuilder.() -> Unit, value: StringBuilder.() -> Unit) {
|
||||
append(infoStringColor1)
|
||||
field()
|
||||
append(": ")
|
||||
append(infoStringColor2)
|
||||
value()
|
||||
append(' ')
|
||||
}
|
||||
|
||||
inline fun StringBuilder.appendField(name: String, value: StringBuilder.() -> Unit) {
|
||||
appendField({ append(name) }, value)
|
||||
}
|
||||
|
||||
inline fun StringBuilder.appendFieldWithCount(name: String, count: Int, value: StringBuilder.() -> Unit) {
|
||||
appendField({
|
||||
append(name)
|
||||
append('(')
|
||||
append(infoStringColor2)
|
||||
append(count)
|
||||
append(infoStringColor1)
|
||||
append(')')
|
||||
}, value)
|
||||
}
|
||||
|
||||
fun StringBuilder.appendProfilesWithPrivilege(fieldName: String, local: RawPrivileges, global: RawPrivileges?, privilege: Privilege) {
|
||||
val map = linkedMapOf<PrivilegeKey, Privilege>()
|
||||
local.filterProfilesWithPrivilegeTo(map, privilege)
|
||||
val localCount = map.size
|
||||
global?.filterProfilesWithPrivilegeTo(map, privilege)
|
||||
appendPrivilegeProfiles(fieldName, map, localCount)
|
||||
}
|
||||
|
||||
fun StringBuilder.appendPrivilegeProfiles(fieldName: String, map: LinkedHashMap<PrivilegeKey, Privilege>, localCount: Int) {
|
||||
if (map.isEmpty()) return
|
||||
|
||||
appendFieldWithCount(fieldName, map.size) {
|
||||
// first [localCount] entries are local
|
||||
val separator = "$infoStringColor1, $infoStringColor2"
|
||||
val iterator = map.iterator()
|
||||
|
||||
if (localCount != 0) {
|
||||
appendPrivilegeEntry(false, iterator.next().toPair())
|
||||
repeat(localCount - 1) {
|
||||
append(separator)
|
||||
appendPrivilegeEntry(false, iterator.next().toPair())
|
||||
}
|
||||
|
||||
} else if (iterator.hasNext()) {
|
||||
// ensure there is never a leading or trailing separator
|
||||
appendPrivilegeEntry(true, iterator.next().toPair())
|
||||
}
|
||||
|
||||
iterator.forEach { next ->
|
||||
append(separator)
|
||||
appendPrivilegeEntry(true, next.toPair())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun StringBuilder.appendPrivilegeEntry(global: Boolean, pair: Pair<PrivilegeKey, Privilege>) {
|
||||
val (key, priv) = pair
|
||||
|
||||
append(key.notNullName)
|
||||
|
||||
// suffix. Maybe T should be M for mod or something. T means they have CAN_MANAGE privilege.
|
||||
append(
|
||||
when {
|
||||
global && priv == Privilege.CAN_MANAGE -> " (G) (T)"
|
||||
global -> " (G)"
|
||||
priv == Privilege.CAN_MANAGE -> " (T)"
|
||||
else -> ""
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package io.dico.parcels2.defaultimpl
|
||||
|
||||
import io.dico.dicore.Formatting
|
||||
import io.dico.parcels2.*
|
||||
import io.dico.parcels2.Privilege.*
|
||||
import io.dico.parcels2.util.math.Vec2i
|
||||
import io.dico.parcels2.util.ext.alsoIfTrue
|
||||
import io.dico.parcels2.util.math.Vec2i
|
||||
import org.bukkit.Material
|
||||
import org.joda.time.DateTime
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
@@ -17,7 +16,6 @@ class ParcelImpl(
|
||||
override val id: ParcelId = this
|
||||
override val pos get() = Vec2i(x, z)
|
||||
override var data: ParcelDataHolder = ParcelDataHolder(); private set
|
||||
override val infoString get() = ParcelInfoStringComputer.getInfoString(this)
|
||||
override val hasBlockVisitors get() = blockVisitors.get() > 0
|
||||
override val worldId: ParcelWorldId get() = world.id
|
||||
|
||||
@@ -77,6 +75,7 @@ class ParcelImpl(
|
||||
?: globalPrivileges?.getStoredPrivilege(key)
|
||||
?: DEFAULT
|
||||
|
||||
override fun hasAnyDeclaredPrivileges() = data.hasAnyDeclaredPrivileges()
|
||||
|
||||
private var _interactableConfig: InteractableConfiguration? = null
|
||||
|
||||
@@ -120,108 +119,20 @@ class ParcelImpl(
|
||||
}
|
||||
|
||||
override fun toString() = toStringExt()
|
||||
|
||||
override val infoString: String
|
||||
get() = getInfoString()
|
||||
}
|
||||
|
||||
private object ParcelInfoStringComputer {
|
||||
val infoStringColor1 = Formatting.GREEN
|
||||
val infoStringColor2 = Formatting.AQUA
|
||||
|
||||
private inline fun StringBuilder.appendField(field: StringBuilder.() -> Unit, value: StringBuilder.() -> Unit) {
|
||||
append(infoStringColor1)
|
||||
field()
|
||||
append(": ")
|
||||
append(infoStringColor2)
|
||||
value()
|
||||
append(' ')
|
||||
}
|
||||
|
||||
private inline fun StringBuilder.appendField(name: String, value: StringBuilder.() -> Unit) {
|
||||
appendField({ append(name) }, value)
|
||||
}
|
||||
|
||||
private inline fun StringBuilder.appendFieldWithCount(name: String, count: Int, value: StringBuilder.() -> Unit) {
|
||||
appendField({
|
||||
append(name)
|
||||
append('(')
|
||||
append(infoStringColor2)
|
||||
append(count)
|
||||
append(infoStringColor1)
|
||||
append(')')
|
||||
}, value)
|
||||
}
|
||||
|
||||
private fun processPrivileges(local: RawPrivileges, global: RawPrivileges?,
|
||||
privilege: Privilege): Pair<LinkedHashMap<PrivilegeKey, Privilege>, Int> {
|
||||
val map = linkedMapOf<PrivilegeKey, Privilege>()
|
||||
local.privilegeOfStar.takeIf { it != DEFAULT }?.let { map[PlayerProfile.Star] = it }
|
||||
map.values.retainAll { it.isDistanceGrEq(privilege) }
|
||||
val localCount = map.size
|
||||
|
||||
if (global != null) {
|
||||
global.privilegeMap.forEach {
|
||||
if (it.value.isDistanceGrEq(privilege))
|
||||
map.putIfAbsent(it.key, it.value)
|
||||
}
|
||||
|
||||
global.privilegeOfStar.takeIf { it != DEFAULT && it.isDistanceGrEq(privilege) }
|
||||
?.let { map.putIfAbsent(PlayerProfile.Star, it) }
|
||||
}
|
||||
|
||||
return map to localCount
|
||||
}
|
||||
|
||||
private fun StringBuilder.appendAddedList(local: RawPrivileges, global: RawPrivileges?, privilege: Privilege, fieldName: String) {
|
||||
val (map, localCount) = processPrivileges(local, global, privilege)
|
||||
if (map.isEmpty()) return
|
||||
|
||||
appendFieldWithCount(fieldName, map.size) {
|
||||
// first [localCount] entries are local
|
||||
val separator = "$infoStringColor1, $infoStringColor2"
|
||||
val iterator = map.iterator()
|
||||
|
||||
if (localCount != 0) {
|
||||
appendPrivilegeEntry(false, iterator.next().toPair())
|
||||
repeat(localCount - 1) {
|
||||
append(separator)
|
||||
appendPrivilegeEntry(false, iterator.next().toPair())
|
||||
}
|
||||
|
||||
} else if (iterator.hasNext()) {
|
||||
// ensure there is never a leading or trailing separator
|
||||
appendPrivilegeEntry(true, iterator.next().toPair())
|
||||
}
|
||||
|
||||
iterator.forEach { next ->
|
||||
append(separator)
|
||||
appendPrivilegeEntry(true, next.toPair())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun StringBuilder.appendPrivilegeEntry(global: Boolean, pair: Pair<PrivilegeKey, Privilege>) {
|
||||
val (key, priv) = pair
|
||||
|
||||
append(key.notNullName)
|
||||
|
||||
// suffix. Maybe T should be M for mod or something. T means they have CAN_MANAGE privilege.
|
||||
append(
|
||||
when {
|
||||
global && priv == CAN_MANAGE -> " (G) (T)"
|
||||
global -> " (G)"
|
||||
priv == CAN_MANAGE -> " (T)"
|
||||
else -> ""
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun getInfoString(parcel: Parcel): String = buildString {
|
||||
private fun Parcel.getInfoString() = StringBuilder().apply {
|
||||
with(InfoBuilder) {
|
||||
appendField("ID") {
|
||||
append(parcel.x)
|
||||
append(x)
|
||||
append(',')
|
||||
append(parcel.z)
|
||||
append(z)
|
||||
}
|
||||
|
||||
val owner = parcel.owner
|
||||
val owner = owner
|
||||
appendField("Owner") {
|
||||
if (owner == null) {
|
||||
append(infoStringColor1)
|
||||
@@ -235,18 +146,17 @@ private object ParcelInfoStringComputer {
|
||||
|
||||
append('\n')
|
||||
|
||||
val local: RawPrivileges = parcel.data
|
||||
val global = parcel.globalPrivileges
|
||||
appendAddedList(local, global, CAN_BUILD, "Allowed") // includes CAN_MANAGE privilege
|
||||
val local: RawPrivileges = data
|
||||
val global = globalPrivileges
|
||||
appendProfilesWithPrivilege("Allowed", local, global, CAN_BUILD) // includes CAN_MANAGE privilege
|
||||
append('\n')
|
||||
appendAddedList(local, global, BANNED, "Banned")
|
||||
appendProfilesWithPrivilege("Banned", local, global, BANNED)
|
||||
|
||||
if (!parcel.interactableConfig.isDefault()) {
|
||||
val interactables = parcel.interactableConfig.interactableClasses
|
||||
if (!interactableConfig.isDefault()) {
|
||||
val interactables = interactableConfig.interactableClasses
|
||||
appendFieldWithCount("Interactables", interactables.size) {
|
||||
interactables.asSequence().map { it.name }.joinTo(this)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}.toString()
|
||||
|
||||
@@ -106,8 +106,11 @@ class ParcelProviderImpl(val plugin: ParcelsPlugin) : ParcelProvider {
|
||||
val channel2 = plugin.storage.transmitAllGlobalPrivileges()
|
||||
while (true) {
|
||||
val (profile, data) = channel2.receiveOrNull() ?: break
|
||||
val key = profile as? PrivilegeKey ?: continue
|
||||
(plugin.globalPrivileges[key] as PrivilegesHolder).copyPrivilegesFrom(data)
|
||||
if (profile !is PrivilegeKey) {
|
||||
logger.error("Received profile that is not a privilege key: ${profile.javaClass}, $profile")
|
||||
continue
|
||||
}
|
||||
(plugin.globalPrivileges[profile] as PrivilegesHolder).copyPrivilegesFrom(data)
|
||||
}
|
||||
|
||||
logger.info("Loading data completed")
|
||||
|
||||
Reference in New Issue
Block a user