Improved NBTData api

This commit is contained in:
bluefireoly
2020-08-09 17:59:20 +02:00
parent 8f1b8c0215
commit 530b310c54
2 changed files with 18 additions and 6 deletions

View File

@@ -13,6 +13,10 @@ class NBTData {
this.nbtTagCompound = nbtTagCompound ?: NBTTagCompound() this.nbtTagCompound = nbtTagCompound ?: NBTTagCompound()
} }
constructor() {
this.nbtTagCompound = NBTTagCompound()
}
constructor(nbtString: String) : this(MojangsonParser.parse(nbtString)) constructor(nbtString: String) : this(MojangsonParser.parse(nbtString))
fun serialize() = nbtTagCompound.toString() fun serialize() = nbtTagCompound.toString()

View File

@@ -8,11 +8,19 @@ import org.bukkit.entity.Entity
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
@NMS_General @NMS_General
val Entity.nbtData: NBTData get() { var Entity.nbtData: NBTData
get() {
val nbtTagCompound = NBTTagCompound() val nbtTagCompound = NBTTagCompound()
(this as CraftEntity).handle.load(nbtTagCompound) (this as CraftEntity).handle.load(nbtTagCompound)
return NBTData(nbtTagCompound) return NBTData(nbtTagCompound)
} }
set(value) {
(this as CraftEntity).handle.save(value.nbtTagCompound)
}
@NMS_General @NMS_General
val ItemStack.nbtData: NBTData get() = NBTData(CraftItemStack.asNMSCopy(this).tag) val ItemStack.nbtData: NBTData get() {
CraftItemStack.asNMSCopy(this).let {
return if (it.hasTag()) NBTData(it.tag) else NBTData()
}
}