From 622e088591d424941eac87d930c51f55754f10a4 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Thu, 30 Jul 2020 19:02:41 +0200 Subject: [PATCH] Improved NBTData support --- .../kotlin/net/axay/kspigot/nbt/NBTData.kt | 41 ++--------- .../net/axay/kspigot/nbt/NBTDataType.kt | 71 +++++++++++++++++++ 2 files changed, 77 insertions(+), 35 deletions(-) create mode 100644 src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt diff --git a/src/main/kotlin/net/axay/kspigot/nbt/NBTData.kt b/src/main/kotlin/net/axay/kspigot/nbt/NBTData.kt index 0c4aff60..f5a0aa4c 100644 --- a/src/main/kotlin/net/axay/kspigot/nbt/NBTData.kt +++ b/src/main/kotlin/net/axay/kspigot/nbt/NBTData.kt @@ -1,7 +1,8 @@ package net.axay.kspigot.nbt import net.axay.kspigot.annotations.NMS_General -import net.minecraft.server.v1_16_R1.* +import net.minecraft.server.v1_16_R1.MojangsonParser +import net.minecraft.server.v1_16_R1.NBTTagCompound @NMS_General class NBTData { @@ -25,25 +26,10 @@ class NBTData { * the specified location, or if the type * is not the one which was specified. */ - operator fun get(key: String): T? { + operator fun get(key: String, dataType: NBTDataType): T? { val value = nbtTagCompound.get(key) return if (value != null) { - val loadedValue = when (value) { - is NBTTagCompound -> NBTData(value) - is NBTTagByte -> value.asByte() - is NBTTagByteArray -> value.bytes - is NBTTagDouble -> value.asDouble() - is NBTTagFloat -> value.asFloat() - is NBTTagInt -> value.asInt() - is NBTTagIntArray -> value.ints - is NBTTagList -> value - is NBTTagLong -> value.asLong() - is NBTTagLongArray -> value.longs - is NBTTagShort -> value.asShort() - is NBTTagString -> value.asString() - else -> null - } - return loadedValue as? T + dataType.decodeNMS(value) } else null } @@ -51,23 +37,8 @@ class NBTData { * This method sets some [value] * at the position of the given [key]. */ - operator fun set(key: String, value: T): Boolean { - when (value) { - is NBTData -> nbtTagCompound.set(key, value.nbtTagCompound) - is Byte -> nbtTagCompound.setByte(key, value) - is ByteArray -> nbtTagCompound.setByteArray(key, value) - is Double -> nbtTagCompound.setDouble(key, value) - is Float -> nbtTagCompound.setFloat(key, value) - is Int -> nbtTagCompound.setInt(key, value) - is IntArray -> nbtTagCompound.setIntArray(key, value) - is List<*> -> nbtTagCompound.set(key, value as NBTList<*>) - is Long -> nbtTagCompound.setLong(key, value) - is LongArray -> nbtTagCompound.set(key, NBTTagLongArray(value)) - is Short -> nbtTagCompound.setShort(key, value) - is String -> nbtTagCompound.setString(key, value) - else -> return false - } - return true + operator fun set(key: String, dataType: NBTDataType, value: T) { + dataType.writeToCompound(key, value, nbtTagCompound) } companion object { diff --git a/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt b/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt new file mode 100644 index 00000000..b83763fa --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/nbt/NBTDataType.kt @@ -0,0 +1,71 @@ +package net.axay.kspigot.nbt + +import net.axay.kspigot.annotations.NMS_General +import net.minecraft.server.v1_16_R1.* + +@NMS_General +interface NBTDataType { + + fun decodeNMS(nbtBase: NBTBase): T? + fun writeToCompound(key: String, data: T, compound: NBTTagCompound) + + 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) + } + + } + +} \ No newline at end of file