diff --git a/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt b/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt index b83763fa..a76196a2 100644 --- a/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt +++ b/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt @@ -3,6 +3,9 @@ package net.axay.kspigot.nbt import net.axay.kspigot.annotations.NMS_General import net.minecraft.server.v1_16_R1.* +/** + * @property T the JVM data type + */ @NMS_General interface NBTDataType { @@ -11,60 +14,32 @@ interface NBTDataType { companion object { - val COMPOUND = object : NBTDataType { - override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagCompound) NBTData(nbtBase) else null - override fun writeToCompound(key: String, data: NBTData, compound: NBTTagCompound) { - compound.set(key, data.nbtTagCompound) - } - } - val BYTE = object : NBTDataType { - override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagByte) nbtBase.asByte() else null - override fun writeToCompound(key: String, data: Byte, compound: NBTTagCompound) = compound.setByte(key, data) - } - val BYTE_ARRAY = object : NBTDataType { - 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 { - 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) - } - val FLOAT = object : NBTDataType { - override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagFloat) nbtBase.asFloat() else null - override fun writeToCompound(key: String, data: Float, compound: NBTTagCompound) = compound.setFloat(key, data) - } - val INT = object : NBTDataType { - override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is NBTTagInt) nbtBase.asInt() else null - override fun writeToCompound(key: String, data: Int, compound: NBTTagCompound) = compound.setInt(key, data) - } - val INT_ARRAY = object : NBTDataType { - 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> { - 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 { - 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 { - 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 { - 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 { - 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) - } + val COMPOUND = nbtDataType({ NBTData(it) }, { key, data, compound -> compound.set(key, data.nbtTagCompound) }) + val BYTE = nbtDataType({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) }) + val BYTE_ARRAY = nbtDataType({ it.bytes }, { key, data, compound -> compound.setByteArray(key, data) }) + val DOUBLE = nbtDataType({ it.asDouble() }, { key, data, compound -> compound.setDouble(key, data) }) + val FLOAT = nbtDataType({ it.asFloat() }, { key, data, compound -> compound.setFloat(key, data) }) + val INT = nbtDataType({ it.asInt() }, { key, data, compound -> compound.setInt(key, data) }) + val INT_ARRAY = nbtDataType({ it.ints }, { key, data, compound -> compound.setIntArray(key, data) }) + val LONG = nbtDataType({ it.asLong() }, { key, data, compound -> compound.setLong(key, data) }) + val LONG_ARRAY = nbtDataType({ it.longs }, { key, data, compound -> compound.set(key, NBTTagLongArray(data)) }) + val SHORT = nbtDataType({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) }) + val STRING = nbtDataType({ it.asString() }, { key, data, compound -> compound.setString(key, data) }) + + } + +} + +private inline fun nbtDataType( + crossinline decodeNMS: (E) -> T, + crossinline writeToCompound: (key: String, data: T, compound: NBTTagCompound) -> Unit +): NBTDataType { + + return object : NBTDataType { + + override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is E) decodeNMS.invoke(nbtBase) else null + + override fun writeToCompound(key: String, data: T, compound: NBTTagCompound) = writeToCompound.invoke(key, data, compound) }