summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt')
-rw-r--r--src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt124
1 files changed, 17 insertions, 107 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt b/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt
index 65378bf..16136e9 100644
--- a/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt
+++ b/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt
@@ -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()
-}
-
-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 -> ""
- }
- )
- }
+ override val infoString: String
+ get() = getInfoString()
+}
- 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)
}
}
-
}
-} \ No newline at end of file
+}.toString()