Renamed nbt package to general data package

This commit is contained in:
bluefireoly
2020-08-31 18:04:07 +02:00
parent 3e718b5fed
commit 7c1d4bae73
3 changed files with 3 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
package net.axay.kspigot.data
import net.axay.kspigot.annotations.NMS_General
import net.minecraft.server.v1_16_R2.MojangsonParser
import net.minecraft.server.v1_16_R2.NBTTagCompound
@NMS_General
class NBTData {
val nbtTagCompound: NBTTagCompound
constructor(nbtTagCompound: NBTTagCompound?) {
this.nbtTagCompound = nbtTagCompound ?: NBTTagCompound()
}
constructor() {
this.nbtTagCompound = NBTTagCompound()
}
constructor(nbtString: String) : this(MojangsonParser.parse(nbtString))
fun serialize() = nbtTagCompound.toString()
/**
* This method gets the value
* at the given [key]. The returned [dataType]
* must be specified.
* The returned value is null, if it
* was not possible to find any value at
* the specified location, or if the type
* is not the one which was specified.
*/
operator fun <T> get(key: String, dataType: NBTDataType<T>): T? {
val value = nbtTagCompound.get(key)
return if (value != null) {
dataType.decodeNMS(value)
} else null
}
/**
* This method sets some [value]
* at the position of the given [key].
* The [dataType] of the given [value]
* must be specified.
*/
operator fun <T> set(key: String, dataType: NBTDataType<T>, value: T) {
dataType.writeToCompound(key, value, nbtTagCompound)
}
companion object {
fun deserialize(nbtString: String) = NBTData(nbtString)
}
}

View File

@@ -0,0 +1,26 @@
package net.axay.kspigot.data
import net.axay.kspigot.annotations.NMS_General
import net.minecraft.server.v1_16_R2.NBTTagCompound
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack
import org.bukkit.entity.Entity
import org.bukkit.inventory.ItemStack
@NMS_General
var Entity.nbtData: NBTData
get() {
val nbtTagCompound = NBTTagCompound()
(this as CraftEntity).handle.load(nbtTagCompound)
return NBTData(nbtTagCompound)
}
set(value) {
(this as CraftEntity).handle.save(value.nbtTagCompound)
}
@NMS_General
val ItemStack.nbtData: NBTData get() {
CraftItemStack.asNMSCopy(this).let {
return if (it.hasTag()) NBTData(it.tag) else NBTData()
}
}

View File

@@ -0,0 +1,47 @@
package net.axay.kspigot.data
import net.axay.kspigot.annotations.NMS_General
import net.minecraft.server.v1_16_R2.*
@NMS_General
interface NBTDataType<T> {
fun decodeNMS(nbtBase: NBTBase): T?
fun writeToCompound(key: String, data: T, compound: NBTTagCompound)
companion object {
val COMPOUND = nbtDataType<NBTData, NBTTagCompound>({ NBTData(it) }, { key, data, compound -> compound.set(key, data.nbtTagCompound) })
val BYTE = nbtDataType<Byte, NBTTagByte>({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) })
val BYTE_ARRAY = nbtDataType<ByteArray, NBTTagByteArray>({ it.bytes }, { key, data, compound -> compound.setByteArray(key, data) })
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 INT_ARRAY = nbtDataType<IntArray, NBTTagIntArray>({ it.ints }, { key, data, compound -> compound.setIntArray(key, data) })
val LONG = nbtDataType<Long, NBTTagLong>({ it.asLong() }, { key, data, compound -> compound.setLong(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 STRING = nbtDataType<String, NBTTagString>({ it.asString() }, { key, data, compound -> compound.setString(key, data) })
}
}
/**
* @property T the JVM data type
* @property E the NBT data type
*/
private inline fun <T, reified E> nbtDataType(
crossinline decodeNMS: (E) -> T,
crossinline writeToCompound: (key: String, data: T, compound: NBTTagCompound) -> Unit
): NBTDataType<T> {
return object : NBTDataType<T> {
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)
}
}