Implement parcel infoString for new interactables
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -32,4 +32,9 @@ fun IntRange.clamp(min: Int, max: Int): IntRange {
|
||||
}
|
||||
|
||||
// the name coerceAtMost is bad
|
||||
fun Int.clampMax(max: Int) = coerceAtMost(max)
|
||||
fun Int.clampMax(max: Int) = coerceAtMost(max)
|
||||
|
||||
// Why does this not exist?
|
||||
infix fun Int.ceilDiv(divisor: Int): Int {
|
||||
return -Math.floorDiv(-this, divisor)
|
||||
}
|
||||
Reference in New Issue
Block a user