Update NBTDataType.kt

This commit is contained in:
bluefireoly
2020-08-27 15:03:36 +02:00
parent 530b310c54
commit 67aab70a66

View File

@@ -3,6 +3,9 @@ package net.axay.kspigot.nbt
import net.axay.kspigot.annotations.NMS_General import net.axay.kspigot.annotations.NMS_General
import net.minecraft.server.v1_16_R1.* import net.minecraft.server.v1_16_R1.*
/**
* @property T the JVM data type
*/
@NMS_General @NMS_General
interface NBTDataType<T> { interface NBTDataType<T> {
@@ -11,60 +14,32 @@ interface NBTDataType<T> {
companion object { companion object {
val COMPOUND = object : NBTDataType<NBTData> { val COMPOUND = nbtDataType<NBTData, NBTTagCompound>({ NBTData(it) }, { key, data, compound -> compound.set(key, data.nbtTagCompound) })
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagCompound) NBTData(nbtBase) else null val BYTE = nbtDataType<Byte, NBTTagByte>({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) })
override fun writeToCompound(key: String, data: NBTData, compound: NBTTagCompound) { val BYTE_ARRAY = nbtDataType<ByteArray, NBTTagByteArray>({ it.bytes }, { key, data, compound -> compound.setByteArray(key, data) })
compound.set(key, data.nbtTagCompound) val DOUBLE = nbtDataType<Double, NBTTagDouble>({ it.asDouble() }, { key, data, compound -> compound.setDouble(key, data) })
} val FLOAT = nbtDataType<Float, NBTTagFloat>({ it.asFloat() }, { key, data, compound -> compound.setFloat(key, data) })
} val INT = nbtDataType<Int, NBTTagInt>({ it.asInt() }, { key, data, compound -> compound.setInt(key, data) })
val BYTE = object : NBTDataType<Byte> { val INT_ARRAY = nbtDataType<IntArray, NBTTagIntArray>({ it.ints }, { key, data, compound -> compound.setIntArray(key, data) })
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagByte) nbtBase.asByte() else null val LONG = nbtDataType<Long, NBTTagLong>({ it.asLong() }, { key, data, compound -> compound.setLong(key, data) })
override fun writeToCompound(key: String, data: Byte, compound: NBTTagCompound) = compound.setByte(key, data) val LONG_ARRAY = nbtDataType<LongArray, NBTTagLongArray>({ it.longs }, { key, data, compound -> compound.set(key, NBTTagLongArray(data)) })
} val SHORT = nbtDataType<Short, NBTTagShort>({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) })
val BYTE_ARRAY = object : NBTDataType<ByteArray> { val STRING = nbtDataType<String, NBTTagString>({ it.asString() }, { key, data, compound -> compound.setString(key, data) })
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagByteArray) nbtBase.bytes else null
override fun writeToCompound(key: String, data: ByteArray, compound: NBTTagCompound) = compound.setByteArray(key, data) }
}
val DOUBLE = object : NBTDataType<Double> { }
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagDouble) nbtBase.asDouble() else null
override fun writeToCompound(key: String, data: Double, compound: NBTTagCompound) = compound.setDouble(key, data) private inline fun <T, reified E> nbtDataType(
} crossinline decodeNMS: (E) -> T,
val FLOAT = object : NBTDataType<Float> { crossinline writeToCompound: (key: String, data: T, compound: NBTTagCompound) -> Unit
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagFloat) nbtBase.asFloat() else null ): NBTDataType<T> {
override fun writeToCompound(key: String, data: Float, compound: NBTTagCompound) = compound.setFloat(key, data)
} return object : NBTDataType<T> {
val INT = object : NBTDataType<Int> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagInt) nbtBase.asInt() else null override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is E) decodeNMS.invoke(nbtBase) else null
override fun writeToCompound(key: String, data: Int, compound: NBTTagCompound) = compound.setInt(key, data)
} override fun writeToCompound(key: String, data: T, compound: NBTTagCompound) = writeToCompound.invoke(key, data, compound)
val INT_ARRAY = object : NBTDataType<IntArray> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagIntArray) nbtBase.ints else null
override fun writeToCompound(key: String, data: IntArray, compound: NBTTagCompound) = compound.setIntArray(key, data)
}
val LIST = object : NBTDataType<List<*>> {
override fun decodeNMS(nbtBase: NBTBase): List<*>? = if (nbtBase is List<*>) nbtBase else null
override fun writeToCompound(key: String, data: List<*>, compound: NBTTagCompound) {
(data as? NBTList<*>)?.let { compound.set(key, it) }
}
}
val LONG = object : NBTDataType<Long> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagLong) nbtBase.asLong() else null
override fun writeToCompound(key: String, data: Long, compound: NBTTagCompound) = compound.setLong(key, data)
}
val LONG_ARRAY = object : NBTDataType<LongArray> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagLongArray) nbtBase.longs else null
override fun writeToCompound(key: String, data: LongArray, compound: NBTTagCompound) {
compound.set(key, NBTTagLongArray(data))
}
}
val SHORT = object : NBTDataType<Short> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagShort) nbtBase.asShort() else null
override fun writeToCompound(key: String, data: Short, compound: NBTTagCompound) = compound.setShort(key, data)
}
val STRING = object : NBTDataType<String> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagString) nbtBase.asString() else null
override fun writeToCompound(key: String, data: String, compound: NBTTagCompound) = compound.setString(key, data)
}
} }