diff --git a/src/net/bdew/lib/Misc.scala b/src/net/bdew/lib/Misc.scala index 84def2e..077836c 100644 --- a/src/net/bdew/lib/Misc.scala +++ b/src/net/bdew/lib/Misc.scala @@ -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) } diff --git a/src/net/bdew/lib/data/DataSlotInventory.scala b/src/net/bdew/lib/data/DataSlotInventory.scala index 337efaf..df6e952 100644 --- a/src/net/bdew/lib/data/DataSlotInventory.scala +++ b/src/net/bdew/lib/data/DataSlotInventory.scala @@ -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) { diff --git a/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala b/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala index 3dac250..0519118 100644 --- a/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala +++ b/src/net/bdew/lib/items/inventory/InventoryItemAdapter.scala @@ -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") diff --git a/src/net/bdew/lib/multiblock/data/DataSlotBlockFaceMap.scala b/src/net/bdew/lib/multiblock/data/DataSlotBlockFaceMap.scala index 2395864..36b735d 100644 --- a/src/net/bdew/lib/multiblock/data/DataSlotBlockFaceMap.scala +++ b/src/net/bdew/lib/multiblock/data/DataSlotBlockFaceMap.scala @@ -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)) diff --git a/src/net/bdew/lib/multiblock/data/DataSlotPosSet.scala b/src/net/bdew/lib/multiblock/data/DataSlotPosSet.scala index 7e9c468..c16a083 100644 --- a/src/net/bdew/lib/multiblock/data/DataSlotPosSet.scala +++ b/src/net/bdew/lib/multiblock/data/DataSlotPosSet.scala @@ -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))) diff --git a/src/net/bdew/lib/multiblock/data/OutputConfigFluid.scala b/src/net/bdew/lib/multiblock/data/OutputConfigFluid.scala index 196c28d..15642b0 100644 --- a/src/net/bdew/lib/multiblock/data/OutputConfigFluid.scala +++ b/src/net/bdew/lib/multiblock/data/OutputConfigFluid.scala @@ -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 diff --git a/src/net/bdew/lib/nbt/ListElement.scala b/src/net/bdew/lib/nbt/ListElement.scala new file mode 100644 index 0000000..7da4ccd --- /dev/null +++ b/src/net/bdew/lib/nbt/ListElement.scala @@ -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(_)) +} \ No newline at end of file diff --git a/src/net/bdew/lib/nbt/NBTHelper.scala b/src/net/bdew/lib/nbt/NBTHelper.scala deleted file mode 100644 index 75d3e96..0000000 --- a/src/net/bdew/lib/nbt/NBTHelper.scala +++ /dev/null @@ -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) -} diff --git a/src/net/bdew/lib/nbt/NBTTypes.scala b/src/net/bdew/lib/nbt/NBTTypes.scala deleted file mode 100644 index ce3fbed..0000000 --- a/src/net/bdew/lib/nbt/NBTTypes.scala +++ /dev/null @@ -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(_)) -} diff --git a/src/net/bdew/lib/nbt/RichNBTTagCompound.scala b/src/net/bdew/lib/nbt/RichNBTTagCompound.scala new file mode 100644 index 0000000..ad951ca --- /dev/null +++ b/src/net/bdew/lib/nbt/RichNBTTagCompound.scala @@ -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) + } + +} \ No newline at end of file diff --git a/src/net/bdew/lib/nbt/Type.scala b/src/net/bdew/lib/nbt/Type.scala new file mode 100644 index 0000000..bf21e90 --- /dev/null +++ b/src/net/bdew/lib/nbt/Type.scala @@ -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) + } + +} + + diff --git a/src/net/bdew/lib/nbt/TypeDef.scala b/src/net/bdew/lib/nbt/TypeDef.scala new file mode 100644 index 0000000..b1909c3 --- /dev/null +++ b/src/net/bdew/lib/nbt/TypeDef.scala @@ -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 +} diff --git a/src/net/bdew/lib/nbt/package.scala b/src/net/bdew/lib/nbt/package.scala new file mode 100644 index 0000000..9685a03 --- /dev/null +++ b/src/net/bdew/lib/nbt/package.scala @@ -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) +} diff --git a/src/net/bdew/lib/tile/inventory/PersistentInventoryTile.scala b/src/net/bdew/lib/tile/inventory/PersistentInventoryTile.scala index ddccf64..3a22fe4 100644 --- a/src/net/bdew/lib/tile/inventory/PersistentInventoryTile.scala +++ b/src/net/bdew/lib/tile/inventory/PersistentInventoryTile.scala @@ -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)