electrodynamics/src/main/scala/edx/core/prefab/part/connector/TPartNodeProvider.scala

85 lines
2 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.core.prefab.part.connector
2014-09-14 09:02:11 +02:00
import java.util
2015-01-17 05:58:45 +01:00
import java.util.{List => JList}
2014-11-09 06:28:58 +01:00
import codechicken.multipart.TMultiPart
2014-09-14 09:02:11 +02:00
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.common.util.ForgeDirection
import resonant.api.ISave
2014-12-09 23:46:07 +01:00
import resonant.api.tile.INodeProvider
import resonant.api.tile.node.INode
2015-01-17 05:58:45 +01:00
import resonant.lib.debug.IDebugInfo
2015-01-17 05:30:25 +01:00
import resonant.lib.grid.core.Node
import scala.collection.convert.wrapAll._
2014-09-14 09:02:11 +02:00
/**
2014-09-14 09:48:55 +02:00
* A node trait that can be mixed into any multipart nodes. Mixing this trait will cause nodes to reconstruct/deconstruct when needed.
2014-09-14 09:02:11 +02:00
* @author Calclavia
*/
2015-01-17 05:58:45 +01:00
trait TPartNodeProvider extends PartAbstract with INodeProvider with IDebugInfo
2014-09-14 09:02:11 +02:00
{
protected val nodes = new util.HashSet[Node]
2014-09-14 09:02:11 +02:00
override def start()
{
super.start()
if (!world.isRemote)
nodes.foreach(_.reconstruct())
}
2014-09-14 09:02:11 +02:00
override def onWorldJoin()
{
if (!world.isRemote)
nodes.foreach(_.reconstruct())
2014-09-14 09:02:11 +02:00
}
override def onNeighborChanged()
{
if (!world.isRemote)
nodes.foreach(_.reconstruct())
2014-09-14 09:02:11 +02:00
}
2014-11-09 06:28:58 +01:00
override def onPartChanged(part: TMultiPart)
{
if (!world.isRemote)
nodes.foreach(_.reconstruct())
}
2014-09-14 09:02:11 +02:00
override def onWorldSeparate()
{
2014-11-10 14:20:42 +01:00
if (!world.isRemote)
nodes.foreach(_.deconstruct())
2014-09-14 09:02:11 +02:00
}
override def save(nbt: NBTTagCompound)
{
super.save(nbt)
nodes.filter(_.isInstanceOf[ISave]).foreach(_.asInstanceOf[ISave].save(nbt))
2014-09-14 09:02:11 +02:00
}
override def load(nbt: NBTTagCompound)
{
super.load(nbt)
nodes.filter(_.isInstanceOf[ISave]).foreach(_.asInstanceOf[ISave].load(nbt))
2014-09-14 09:02:11 +02:00
}
2014-11-02 13:51:56 +01:00
override def getNode[N <: INode](nodeType: Class[_ <: N], from: ForgeDirection): N =
2014-09-14 09:02:11 +02:00
{
return nodes.filter(node => nodeType.isAssignableFrom(node.getClass)).headOption.getOrElse(null).asInstanceOf[N]
2014-09-14 09:02:11 +02:00
}
2015-01-08 14:24:04 +01:00
2015-01-17 05:58:45 +01:00
override def getDebugInfo: JList[String] =
2015-01-08 14:24:04 +01:00
{
2015-01-17 05:58:45 +01:00
val debugs = nodes.toList.filter(_.isInstanceOf[IDebugInfo])
2015-01-08 14:24:04 +01:00
if (debugs.size > 0)
{
2015-01-17 05:58:45 +01:00
return debugs.map(_.asInstanceOf[IDebugInfo].getDebugInfo.toList).reduceLeft(_ ::: _)
2015-01-08 14:24:04 +01:00
}
return List[String]()
}
2014-09-14 09:02:11 +02:00
}