Skip to content

v3 graveyard

ocpu edited this page Aug 10, 2020 · 1 revision

This is the graveyard / code that is under consideration or completly rejected.

import kotlin.reflect.KProperty

//////////////////////////////////////////////////////////////////////
//                          NBT UTILS                               //
//////////////////////////////////////////////////////////////////////

operator fun CompoundNBT.getValue(thisRef: Any?, property: KProperty<*>): INBT? = get(property.name)
operator fun CompoundNBT.setValue(thisRef: Any?, property: KProperty<*>, value: INBT?) {
  if (value == null) {
    remove(property.name)
  } else {
    put(property.name, value)
  }
}

inline fun <reified T : Serializable> CompoundNBT.delegated(): ReadWriteProperty<Any?, T?> = DelegatedNBTProperty(WeakReference(this), T::class.java)

class DelegatedNBTProperty<T : Serializable?>(
  private val compound: WeakReference<CompoundNBT>,
  clazz: Class<T>
) : ReadWriteProperty<Any?, T?> {

  init {
    check(!Collection::class.java.isAssignableFrom(clazz)) { "Collections are not supported" }
    check(clazz in classNameToId) { "The class provided is not a supported value type" }
  }

  private val id = classNameToId[clazz]!!.toByte()
  @Suppress("UNCHECKED_CAST")
  private val resolver = classNameToResolver[clazz]!! as (INBT) -> T
  @Suppress("UNCHECKED_CAST")
  private val converter = classNameToConverter[clazz]!! as (T) -> INBT

  override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
    val compoundInstance = compound.get()
    checkNotNull(compoundInstance) { "The NBT tag compound has been garbage collected" }
    val value = compoundInstance.get(property.name) ?: return null
    check(value.id == id) { "Illegal NBT value has been set on key ${property.name}" }
    return resolver(value)
  }

  override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
    val compoundInstance = compound.get()
    checkNotNull(compoundInstance) { "The NBT tag compound has been garbage collected" }

    if (value == null) {
      compoundInstance.remove(property.name)
    } else {
      compoundInstance.put(property.name, converter(value))
    }
  }

  companion object {
    val classNameToId = mapOf(
      Byte::class.java to Constants.NBT.TAG_BYTE,
      Short::class.java to Constants.NBT.TAG_SHORT,
      Int::class.java to Constants.NBT.TAG_INT,
      Long::class.java to Constants.NBT.TAG_LONG,
      Float::class.java to Constants.NBT.TAG_FLOAT,
      Double::class.java to Constants.NBT.TAG_DOUBLE,
      ByteArray::class.java to Constants.NBT.TAG_BYTE_ARRAY,
      IntArray::class.java to Constants.NBT.TAG_INT_ARRAY,
      LongArray::class.java to Constants.NBT.TAG_LONG_ARRAY,
      String::class.java to Constants.NBT.TAG_STRING
    )

    val classNameToResolver = mapOf(
      Byte::class.java to { i: INBT -> (i as ByteNBT).byte },
      Short::class.java to { i: INBT -> (i as ShortNBT).short },
      Int::class.java to { i: INBT -> (i as IntNBT).int },
      Long::class.java to { i: INBT -> (i as LongNBT).long },
      Float::class.java to { i: INBT -> (i as FloatNBT).float },
      Double::class.java to { i: INBT -> (i as DoubleNBT).double },
      ByteArray::class.java to { i: INBT -> (i as ByteArrayNBT).byteArray },
      IntArray::class.java to { i: INBT -> (i as IntArrayNBT).intArray },
      LongArray::class.java to { i: INBT -> (i as LongArrayNBT).asLongArray },
      String::class.java to { i: INBT -> (i as StringNBT).string }
    )

    val classNameToConverter = mapOf(
      Byte::class.java to { i: Any -> ByteNBT.func_229671_a_(i as Byte) },
      Short::class.java to { i: Any -> ShortNBT.func_229701_a_(i as Short) },
      Int::class.java to { i: Any -> IntNBT.func_229692_a_(i as Int) },
      Long::class.java to { i: Any -> LongNBT.func_229698_a_(i as Long) },
      Float::class.java to { i: Any -> FloatNBT.func_229689_a_(i as Float) },
      Double::class.java to { i: Any -> DoubleNBT.func_229684_a_(i as Double) },
      ByteArray::class.java to { i: Any -> ByteArrayNBT(i as ByteArray) },
      IntArray::class.java to { i: Any -> IntArrayNBT(i as IntArray) },
      LongArray::class.java to { i: Any -> LongArrayNBT(i as LongArray) },
      String::class.java to { i: Any -> StringNBT.func_229705_a_(i as String) }
    )
  }
}

private inline fun <reified T : INBT, reified R> INBT.getContainedValue(id: Int, getter: T.() -> R): R {
  require(this.id == id.toByte())
  requireNotNull(this as? T)
  return getter(this as T)
}

fun INBT.byte() = getContainedValue<ByteNBT, Byte>(Constants.NBT.TAG_BYTE) { byte }
fun INBT.short() = getContainedValue<ShortNBT, Short>(Constants.NBT.TAG_SHORT) { short }
fun INBT.int() = getContainedValue<IntNBT, Int>(Constants.NBT.TAG_INT) { int }
fun INBT.long() = getContainedValue<LongNBT, Long>(Constants.NBT.TAG_LONG) { long }
fun INBT.float() = getContainedValue<FloatNBT, Float>(Constants.NBT.TAG_FLOAT) { float }
fun INBT.double() = getContainedValue<DoubleNBT, Double>(Constants.NBT.TAG_DOUBLE) { double }
fun INBT.byteArray() = getContainedValue<ByteArrayNBT, ByteArray>(Constants.NBT.TAG_BYTE_ARRAY) { byteArray }
fun INBT.intArray() = getContainedValue<IntArrayNBT, IntArray>(Constants.NBT.TAG_INT_ARRAY) { intArray }
fun INBT.longArray() = getContainedValue<LongArrayNBT, LongArray>(Constants.NBT.TAG_LONG_ARRAY) { asLongArray }
fun INBT.string() = getContainedValue<StringNBT, String>(Constants.NBT.TAG_STRING) { string }
fun INBT.list() = getContainedValue<ListNBT, List<INBT>>(Constants.NBT.TAG_LIST) { this }

//////////////////////////////////////////////////////////////////////
//                        NBT UTILS END                             //
//////////////////////////////////////////////////////////////////////

Clone this wiki locally