mirror of
https://github.com/GTNewHorizons/bdlib.git
synced 2026-11-13 00:37:59 +00:00
Refactored nbt helper
This commit is contained in:
1 parent
745170415e
commit
2a008aa093
14 files changed
+196
-131
No files matched your search
@@ -22,15 +22,15 @@ import net.minecraftforge.fluids.{Fluid, FluidStack}
|
||||
import net.minecraftforge.oredict.ShapedOreRecipe
|
||||
|
||||
object Misc {
|
||||
@deprecated("Use NBTImplicits instead", "bdlib 1.7.0")
|
||||
@deprecated("Use net.bdew.lib.nbt instead", "bdlib 1.7.0")
|
||||
def iterNbtCompoundList(parent: NBTTagCompound, name: String): Iterable[NBTTagCompound] = {
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
parent.getList[NBTTagCompound](name)
|
||||
}
|
||||
|
||||
@deprecated("Use NBTImplicits instead", "bdlib 1.7.0")
|
||||
@deprecated("Use net.bdew.lib.nbt instead", "bdlib 1.7.0")
|
||||
def iterNbtIntArray(parent: NBTTagCompound, name: String): Iterable[Array[Int]] = {
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
parent.getList[Array[Int]](name)
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ case class DataSlotInventory(name: String, parent: DataSlotContainer, size: Int)
|
||||
|
||||
override def load(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
inv = new Array[ItemStack](getSizeInventory)
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
for (nbtItem <- t.getList[NBTTagCompound](name)) {
|
||||
val slot = nbtItem.getByte("Slot")
|
||||
if (slot >= 0 && slot < inv.length) {
|
||||
|
||||
@@ -22,7 +22,7 @@ class InventoryItemAdapter(val player: EntityPlayer, val slot: Int, val size: In
|
||||
load()
|
||||
|
||||
def load() {
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
val tag = getStack.getTagCompound
|
||||
for (nbtItem <- tag.getList[NBTTagCompound](tagName)) {
|
||||
val slot = nbtItem.getByte("Slot")
|
||||
|
||||
@@ -26,7 +26,7 @@ case class DataSlotBlockFaceMap(name: String, parent: DataSlotContainer) extends
|
||||
def ent2arr(x: (BlockFace, Int)) = Array(x._2, x._1.x, x._1.y, x._1.z, x._1.face.ordinal())
|
||||
def arr2ent(x: Array[Int]) = BlockFace(x(1), x(2), x(3), ForgeDirection.values()(x(4))) -> x(0)
|
||||
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
|
||||
def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
t.setList(name, map.map(ent2arr))
|
||||
|
||||
@@ -21,7 +21,7 @@ case class DataSlotPosSet(name: String, parent: DataSlotContainer) extends DataS
|
||||
|
||||
setUpdate(UpdateKind.SAVE, UpdateKind.WORLD)
|
||||
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
|
||||
def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
t.setList(name, set.map(x => Misc.applyMutator(x.writeToNBT, new NBTTagCompound)))
|
||||
|
||||
@@ -21,7 +21,7 @@ class OutputConfigFluid extends OutputConfig with OutputConfigRSControllable {
|
||||
|
||||
final val ticks = 20
|
||||
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
|
||||
def avg = values.sum / values.size
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.nbt
|
||||
|
||||
import net.minecraft.nbt.NBTTagList
|
||||
|
||||
import scala.annotation.implicitNotFound
|
||||
|
||||
@implicitNotFound("Type ${R} can't currently be retrieved from NBT lists")
|
||||
case class ListElement[R](definition: Type[R], fromList: (NBTTagList, Int) => R)
|
||||
|
||||
object ListElement {
|
||||
def apply[R: ListElement] = implicitly[ListElement[R]]
|
||||
|
||||
import Type._
|
||||
|
||||
implicit val stringInList = ListElement(TString, _.getStringTagAt(_))
|
||||
implicit val compoundInList = ListElement(TCompound, _.getCompoundTagAt(_))
|
||||
implicit val intArrayInList = ListElement(TIntArray, _.func_150306_c(_))
|
||||
implicit val doubleInList = ListElement(TDouble, _.func_150309_d(_))
|
||||
implicit val floatInList = ListElement(TFloat, _.func_150308_e(_))
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.nbt
|
||||
|
||||
import net.minecraft.nbt.{NBTBase, NBTTagCompound, NBTTagList}
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
object NBTHelper {
|
||||
|
||||
import NBTTypes._
|
||||
|
||||
class RichNBTTagCompound(tag: NBTTagCompound) {
|
||||
def getList[T](name: String)(implicit ev: TypeInList[_, T]): Iterable[T] = {
|
||||
val list = tag.getTagList(name, ev.definition.id)
|
||||
for (i <- 0 until list.tagCount()) yield ev.fromList(list, i)
|
||||
}
|
||||
def setList[T](name: String, v: Iterable[T])(implicit ev: Type[_ <: NBTBase, T]): Unit = {
|
||||
val list = new NBTTagList
|
||||
for (x <- v) list.appendTag(ev.toNBT(x))
|
||||
tag.setTag(name, list)
|
||||
}
|
||||
}
|
||||
|
||||
implicit def compound2rich(p: NBTTagCompound): RichNBTTagCompound = new RichNBTTagCompound(p)
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.nbt
|
||||
|
||||
import net.minecraft.nbt._
|
||||
|
||||
import scala.annotation.implicitNotFound
|
||||
|
||||
object NBTTypes {
|
||||
var map = Map.empty[Int, Type[_, _]]
|
||||
|
||||
@implicitNotFound("Type ${R} is not supported by NBT")
|
||||
abstract class Type[T <: NBTBase, R](val id: Int, val cls: Class[T], val valueClass: Class[R]) {
|
||||
map += id -> this
|
||||
def toVal(p: T): R
|
||||
def toNBT(p: R): T
|
||||
}
|
||||
|
||||
class TypeSimple[T <: NBTBase](id: Int, cls: Class[T]) extends Type[T, T](id, cls, cls) {
|
||||
override def toVal(p: T): T = p
|
||||
override def toNBT(p: T): T = p
|
||||
}
|
||||
|
||||
@implicitNotFound("Type ${R} can't currently be retrieved from NBT lists")
|
||||
case class TypeInList[T <: NBTBase, R](definition: Type[T, R], fromList: (NBTTagList, Int) => R)
|
||||
|
||||
implicit object TByte extends Type(1, classOf[NBTTagByte], classOf[Byte]) {
|
||||
override def toVal(p: NBTTagByte): Byte = p.func_150290_f()
|
||||
override def toNBT(p: Byte): NBTTagByte = new NBTTagByte(p)
|
||||
}
|
||||
|
||||
implicit object TShort extends Type(2, classOf[NBTTagShort], classOf[Short]) {
|
||||
override def toVal(p: NBTTagShort): Short = p.func_150289_e()
|
||||
override def toNBT(p: Short): NBTTagShort = new NBTTagShort(p)
|
||||
}
|
||||
|
||||
implicit object TInt extends Type(3, classOf[NBTTagInt], classOf[Int]) {
|
||||
override def toVal(p: NBTTagInt): Int = p.func_150287_d()
|
||||
override def toNBT(p: Int): NBTTagInt = new NBTTagInt(p)
|
||||
}
|
||||
|
||||
implicit object TLong extends Type(4, classOf[NBTTagLong], classOf[Long]) {
|
||||
override def toVal(p: NBTTagLong): Long = p.func_150291_c()
|
||||
override def toNBT(p: Long): NBTTagLong = new NBTTagLong(p)
|
||||
}
|
||||
|
||||
implicit object TFloat extends Type(5, classOf[NBTTagFloat], classOf[Float]) {
|
||||
override def toVal(p: NBTTagFloat): Float = p.func_150288_h()
|
||||
override def toNBT(p: Float): NBTTagFloat = new NBTTagFloat(p)
|
||||
}
|
||||
|
||||
implicit object TDouble extends Type(6, classOf[NBTTagDouble], classOf[Double]) {
|
||||
override def toVal(p: NBTTagDouble): Double = p.func_150286_g()
|
||||
override def toNBT(p: Double): NBTTagDouble = new NBTTagDouble(p)
|
||||
}
|
||||
|
||||
implicit object TByteArray extends Type(7, classOf[NBTTagByteArray], classOf[Array[Byte]]) {
|
||||
override def toVal(p: NBTTagByteArray): Array[Byte] = p.func_150292_c()
|
||||
override def toNBT(p: Array[Byte]): NBTTagByteArray = new NBTTagByteArray(p)
|
||||
}
|
||||
|
||||
implicit object TString extends Type(8, classOf[NBTTagString], classOf[String]) {
|
||||
override def toVal(p: NBTTagString): String = p.func_150285_a_()
|
||||
override def toNBT(p: String): NBTTagString = new NBTTagString(p)
|
||||
}
|
||||
|
||||
implicit object TList extends TypeSimple(9, classOf[NBTTagList])
|
||||
|
||||
implicit object TCompound extends TypeSimple(10, classOf[NBTTagCompound])
|
||||
|
||||
implicit object TIntArray extends Type(11, classOf[NBTTagIntArray], classOf[Array[Int]]) {
|
||||
override def toVal(p: NBTTagIntArray): Array[Int] = p.func_150302_c()
|
||||
override def toNBT(p: Array[Int]): NBTTagIntArray = new NBTTagIntArray(p)
|
||||
}
|
||||
|
||||
implicit val stringInList = TypeInList(TString, _.getStringTagAt(_))
|
||||
implicit val compoundInList = TypeInList(TCompound, _.getCompoundTagAt(_))
|
||||
implicit val intArrayInList = TypeInList(TIntArray, _.func_150306_c(_))
|
||||
implicit val doubleInList = TypeInList(TDouble, _.func_150309_d(_))
|
||||
implicit val floatInList = TypeInList(TFloat, _.func_150308_e(_))
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.nbt
|
||||
|
||||
import net.minecraft.nbt.{NBTTagCompound, NBTTagList}
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
class RichNBTTagCompound(tag: NBTTagCompound) {
|
||||
|
||||
def getList[T: ListElement](name: String): Iterable[T] = {
|
||||
val tDef = ListElement[T]
|
||||
val list = tag.getTagList(name, tDef.definition.id)
|
||||
for (i <- 0 until list.tagCount()) yield tDef.fromList(list, i)
|
||||
}
|
||||
|
||||
def setList[T: Type](name: String, v: Iterable[T]): Unit = {
|
||||
val list = new NBTTagList
|
||||
for (x <- v) list.appendTag(Type[T].toNBT(x))
|
||||
tag.setTag(name, list)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.nbt
|
||||
|
||||
import net.minecraft.nbt._
|
||||
|
||||
import scala.annotation.implicitNotFound
|
||||
|
||||
@implicitNotFound("Type ${R} is not supported by NBT")
|
||||
trait Type[R] {
|
||||
def id: Int
|
||||
def toNBT(p: R): NBTBase
|
||||
def fromCompound(t: NBTTagCompound, n: String): Option[R]
|
||||
}
|
||||
|
||||
object Type {
|
||||
def apply[R: Type] = implicitly[Type[R]]
|
||||
|
||||
implicit object TByte extends TypeDef(1, classOf[NBTTagByte], classOf[Byte]) {
|
||||
override def toVal(p: NBTTagByte): Byte = p.func_150290_f()
|
||||
override def toNBT(p: Byte): NBTTagByte = new NBTTagByte(p)
|
||||
}
|
||||
|
||||
implicit object TShort extends TypeDef(2, classOf[NBTTagShort], classOf[Short]) {
|
||||
override def toVal(p: NBTTagShort): Short = p.func_150289_e()
|
||||
override def toNBT(p: Short): NBTTagShort = new NBTTagShort(p)
|
||||
}
|
||||
|
||||
implicit object TInt extends TypeDef(3, classOf[NBTTagInt], classOf[Int]) {
|
||||
override def toVal(p: NBTTagInt): Int = p.func_150287_d()
|
||||
override def toNBT(p: Int): NBTTagInt = new NBTTagInt(p)
|
||||
}
|
||||
|
||||
implicit object TLong extends TypeDef(4, classOf[NBTTagLong], classOf[Long]) {
|
||||
override def toVal(p: NBTTagLong): Long = p.func_150291_c()
|
||||
override def toNBT(p: Long): NBTTagLong = new NBTTagLong(p)
|
||||
}
|
||||
|
||||
implicit object TFloat extends TypeDef(5, classOf[NBTTagFloat], classOf[Float]) {
|
||||
override def toVal(p: NBTTagFloat): Float = p.func_150288_h()
|
||||
override def toNBT(p: Float): NBTTagFloat = new NBTTagFloat(p)
|
||||
}
|
||||
|
||||
implicit object TDouble extends TypeDef(6, classOf[NBTTagDouble], classOf[Double]) {
|
||||
override def toVal(p: NBTTagDouble): Double = p.func_150286_g()
|
||||
override def toNBT(p: Double): NBTTagDouble = new NBTTagDouble(p)
|
||||
}
|
||||
|
||||
implicit object TByteArray extends TypeDef(7, classOf[NBTTagByteArray], classOf[Array[Byte]]) {
|
||||
override def toVal(p: NBTTagByteArray): Array[Byte] = p.func_150292_c()
|
||||
override def toNBT(p: Array[Byte]): NBTTagByteArray = new NBTTagByteArray(p)
|
||||
}
|
||||
|
||||
implicit object TString extends TypeDef(8, classOf[NBTTagString], classOf[String]) {
|
||||
override def toVal(p: NBTTagString): String = p.func_150285_a_()
|
||||
override def toNBT(p: String): NBTTagString = new NBTTagString(p)
|
||||
}
|
||||
|
||||
implicit object TList extends TypeDefSimple(9, classOf[NBTTagList])
|
||||
|
||||
implicit object TCompound extends TypeDefSimple(10, classOf[NBTTagCompound])
|
||||
|
||||
implicit object TIntArray extends TypeDef(11, classOf[NBTTagIntArray], classOf[Array[Int]]) {
|
||||
override def toVal(p: NBTTagIntArray): Array[Int] = p.func_150302_c()
|
||||
override def toNBT(p: Array[Int]): NBTTagIntArray = new NBTTagIntArray(p)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.nbt
|
||||
|
||||
import net.minecraft.nbt.{NBTBase, NBTTagCompound}
|
||||
|
||||
import scala.annotation.implicitNotFound
|
||||
|
||||
@implicitNotFound("Type ${R} is not supported by NBT")
|
||||
abstract class TypeDef[T <: NBTBase, R](val id: Int, val cls: Class[T], val valueClass: Class[R]) extends Type[R] {
|
||||
TypeDef.map += id -> this
|
||||
def toVal(p: T): R
|
||||
def toNBT(p: R): T
|
||||
override def fromCompound(t: NBTTagCompound, n: String): Option[R] =
|
||||
if (t.hasKey(n, id)) Some(toVal(t.getTag(n).asInstanceOf[T])) else None
|
||||
}
|
||||
|
||||
object TypeDef {
|
||||
var map = Map.empty[Int, TypeDef[_, _]]
|
||||
}
|
||||
|
||||
class TypeDefSimple[T <: NBTBase](id: Int, cls: Class[T]) extends TypeDef[T, T](id, cls, cls) {
|
||||
override def toVal(p: T): T = p
|
||||
override def toNBT(p: T): T = p
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2015
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
package object nbt {
|
||||
implicit def compound2rich(p: NBTTagCompound): RichNBTTagCompound = new RichNBTTagCompound(p)
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
trait PersistentInventoryTile extends TileExtended with BaseInventory {
|
||||
|
||||
import net.bdew.lib.nbt.NBTHelper._
|
||||
import net.bdew.lib.nbt._
|
||||
|
||||
persistLoad.listen((tag: NBTTagCompound) => {
|
||||
inv = new Array[ItemStack](getSizeInventory)
|
||||
|
||||
Reference in new issue
Block a user