From 56f9b1dbff7fa4d4a00f38415e446795132d8ee0 Mon Sep 17 00:00:00 2001 From: Dico Date: Mon, 24 Sep 2018 04:38:17 +0100 Subject: Implement parcel infoString for new interactables --- src/main/kotlin/io/dico/parcels2/Interactable.kt | 13 +++++-- .../io/dico/parcels2/defaultimpl/ParcelImpl.kt | 42 ++++++++++------------ src/main/kotlin/io/dico/parcels2/util/ext/Math.kt | 7 +++- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/io/dico/parcels2/Interactable.kt b/src/main/kotlin/io/dico/parcels2/Interactable.kt index cb83d6e..100e433 100644 --- a/src/main/kotlin/io/dico/parcels2/Interactable.kt +++ b/src/main/kotlin/io/dico/parcels2/Interactable.kt @@ -1,5 +1,6 @@ package io.dico.parcels2 +import io.dico.parcels2.util.ext.ceilDiv import io.dico.parcels2.util.ext.getMaterialsWithWoodTypePrefix import org.bukkit.Material import java.util.EnumMap @@ -115,9 +116,10 @@ interface InteractableConfiguration { fun isInteractable(material: Material): Boolean fun isInteractable(clazz: Interactables): Boolean + fun isDefault(): Boolean + fun setInteractable(clazz: Interactables, interactable: Boolean): Boolean fun clear(): Boolean - fun copyFrom(other: InteractableConfiguration) = Interactables.classesById.fold(false) { cur, elem -> setInteractable(elem, other.isInteractable(elem) || cur) } @@ -128,7 +130,7 @@ interface InteractableConfiguration { fun InteractableConfiguration.isInteractable(clazz: Interactables?) = clazz != null && isInteractable(clazz) class BitmaskInteractableConfiguration : InteractableConfiguration { - val bitmaskArray = IntArray((Interactables.classesById.size + 31) / 32) + val bitmaskArray = IntArray(Interactables.classesById.size ceilDiv Int.SIZE_BITS) private fun isBitSet(classId: Int): Boolean { val idx = classId.ushr(5) @@ -144,6 +146,13 @@ class BitmaskInteractableConfiguration : InteractableConfiguration { return isBitSet(clazz.id) != clazz.interactableByDefault } + override fun isDefault(): Boolean { + for (x in bitmaskArray) { + if (x != 0) return false + } + return true + } + override fun setInteractable(clazz: Interactables, interactable: Boolean): Boolean { val idx = clazz.id.ushr(5) if (idx >= bitmaskArray.size) return false diff --git a/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt b/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt index 041e529..f592724 100644 --- a/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt +++ b/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt @@ -88,6 +88,7 @@ class ParcelImpl( _interactableConfig = object : InteractableConfiguration { override fun isInteractable(material: Material): Boolean = data.interactableConfig.isInteractable(material) override fun isInteractable(clazz: Interactables): Boolean = data.interactableConfig.isInteractable(clazz) + override fun isDefault(): Boolean = data.interactableConfig.isDefault() override fun setInteractable(clazz: Interactables, interactable: Boolean): Boolean = data.interactableConfig.setInteractable(clazz, interactable).alsoIfTrue { updateInteractableConfigStorage() } @@ -135,12 +136,18 @@ private object ParcelInfoStringComputer { } private inline fun StringBuilder.appendField(name: String, value: StringBuilder.() -> Unit) { - append(infoStringColor1) - append(name) - append(": ") - append(infoStringColor2) - value() - append(' ') + 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 StringBuilder.appendAddedList(local: PrivilegeMap, global: PrivilegeMap, privilege: Privilege, fieldName: String) { @@ -151,14 +158,7 @@ private object ParcelInfoStringComputer { val all = localFiltered + global.filterValues { it.isDistanceGrEq(privilege) } if (all.isEmpty()) return - appendField({ - append(fieldName) - append('(') - append(infoStringColor2) - append(all.size) - append(infoStringColor1) - append(')') - }) { + appendFieldWithCount(fieldName, all.size) { val separator = "$infoStringColor1, $infoStringColor2" // first [localCount] entries are local @@ -225,16 +225,12 @@ private object ParcelInfoStringComputer { append('\n') appendAddedList(local, global, Privilege.BANNED, "Banned") - /* TODO options - if (!parcel.allowInteractInputs || !parcel.allowInteractInventory) { - appendField("Options") { - append("(") - appendField("inputs") { append(parcel.allowInteractInputs) } - append(", ") - appendField("inventory") { append(parcel.allowInteractInventory) } - append(")") + if (!parcel.interactableConfig.isDefault()) { + val interactables = parcel.interactableConfig.interactableClasses + appendFieldWithCount("Interactables", interactables.size) { + interactables.asSequence().map { it.name }.joinTo(this) } - }*/ + } } } \ No newline at end of file diff --git a/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt b/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt index 62ee220..f3b6d3b 100644 --- a/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt +++ b/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt @@ -32,4 +32,9 @@ fun IntRange.clamp(min: Int, max: Int): IntRange { } // the name coerceAtMost is bad -fun Int.clampMax(max: Int) = coerceAtMost(max) \ No newline at end of file +fun Int.clampMax(max: Int) = coerceAtMost(max) + +// Why does this not exist? +infix fun Int.ceilDiv(divisor: Int): Int { + return -Math.floorDiv(-this, divisor) +} \ No newline at end of file -- cgit v1.2.3