Improved NBTData support

This commit is contained in:
bluefireoly
2020-07-30 19:02:41 +02:00
parent ab5ca22d3a
commit 622e088591
2 changed files with 77 additions and 35 deletions

View File

@@ -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 <T> get(key: String): T? {
operator fun <T> get(key: String, dataType: NBTDataType<T>): 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 <T> 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 <T> set(key: String, dataType: NBTDataType<T>, value: T) {
dataType.writeToCompound(key, value, nbtTagCompound)
}
companion object {

View File

@@ -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<T> {
fun decodeNMS(nbtBase: NBTBase): T?
fun writeToCompound(key: String, data: T, compound: NBTTagCompound)
companion object {
val COMPOUND = object : NBTDataType<NBTData> {
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<Byte> {
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<ByteArray> {
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)
}
val FLOAT = object : NBTDataType<Float> {
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<Int> {
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<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)
}
}
}