Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import li.cil.oc.api.network.Node
import li.cil.oc.common.EventHandler
import li.cil.oc.integration.appeng.AEUtil
import li.cil.oc.integration.appeng.NetworkControl.convert
import li.cil.oc.util.ExtendedNBT.mapToNbtRecursively
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.tileentity.TileEntity

import java.lang
import scala.collection.JavaConversions.iterableAsScalaIterable
import scala.collection.convert.WrapAsScala.mapAsScalaMap
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag

Expand Down Expand Up @@ -55,7 +57,7 @@ trait SubscriptionBase[T <: IAEStack[T]] extends IMEMonitorHandlerReceiver[T] wi
override def postChange(monitor: IBaseMonitor[T], change: lang.Iterable[T], actionSource: BaseActionSource): Unit = {
if (subscribe && tile != null) {
val flatArgs = ArrayBuffer[Object](event_name)
flatArgs ++= change.map(convert(_, tile))
flatArgs ++= change.map(stack => mapToNbtRecursively(mapAsScalaMap(convert(stack, tile))))
node.sendToReachable("computer.signal", flatArgs: _*)
}
}
Expand All @@ -71,4 +73,4 @@ trait SubscriptionBase[T <: IAEStack[T]] extends IMEMonitorHandlerReceiver[T] wi
override def save(nbt: NBTTagCompound): Unit = {
nbt.setBoolean(event_name, subscribe)
}
}
}
42 changes: 42 additions & 0 deletions src/main/scala/li/cil/oc/util/ExtendedNBT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,48 @@ object ExtendedNBT {
nbt
}

def mapToNbtRecursively(map: collection.Map[String, _]): NBTTagCompound = {
def valueToNbtRecursively(value: Any): NBTBase = value match {
case value: Boolean => value
case value: Byte => value
case value: Short => value
case value: Int => value
case value: Long => value
case value: Float => value
case value: Double => value
case value: Array[Byte] => value
case value: Array[Int] => value
case value: String => value
case value: ItemStack => value
case value: Map[String, _] => mapToNbtRecursively(value)
case value: Iterable[_] =>
val list = new NBTTagList
value.foreach { v =>
val nbt = valueToNbtRecursively(v)
if (nbt != null && (list.func_150303_d() == 0 || list.func_150303_d() == nbt.getId))
list.appendTag(nbt)
}
list
case value: Array[_] =>
val list = new NBTTagList
value.foreach { v =>
val nbt = valueToNbtRecursively(v)
if (nbt != null && (list.func_150303_d() == 0 || list.func_150303_d() == nbt.getId))
list.appendTag(nbt)
}
list
case _ => null
}

val nbt = new NBTTagCompound
map.foreach { case (key, value) =>
val tag = valueToNbtRecursively(value)
if (tag != null)
nbt.setTag(key, tag);
}
nbt
}

def typedMapToNbt(map: Map[_, _]): NBTBase = {
def mapToList(value: Array[(_, _)]) = value.collect {
// Ignore, can be stuff like the 'n' introduced by Lua's `pack`.
Expand Down