| package li.cil.oc.common.tileentity |
| |
| import cpw.mods.fml.relauncher.{Side, SideOnly} |
| import li.cil.oc.api.network.{Node, Message, Visibility, SidedEnvironment} |
| import li.cil.oc.util.ExtendedNBT._ |
| import li.cil.oc.{api, Settings} |
| import net.minecraft.nbt.NBTTagCompound |
| import net.minecraftforge.common.ForgeDirection |
| |
| trait Hub extends Environment with SidedEnvironment { |
| protected val plugs = ForgeDirection.VALID_DIRECTIONS.map(side => new Plug(side)) |
| |
| // ----------------------------------------------------------------------- // |
| |
| def node = null |
| |
| @SideOnly(Side.CLIENT) |
| def canConnect(side: ForgeDirection) = true |
| |
| def sidedNode(side: ForgeDirection) = plugs(side.ordinal()).node |
| |
| // ----------------------------------------------------------------------- // |
| |
| override def readFromNBT(nbt: NBTTagCompound) { |
| super.readFromNBT(nbt) |
| nbt.getTagList(Settings.namespace + "plugs").iterator[NBTTagCompound].zip(plugs).foreach { |
| case (plugNbt, plug) => plug.node.load(plugNbt) |
| } |
| } |
| |
| override def writeToNBT(nbt: NBTTagCompound) { |
| super.writeToNBT(nbt) |
| nbt.setNewTagList(Settings.namespace + "plugs", plugs.map(plug => { |
| val plugNbt = new NBTTagCompound() |
| plug.node.save(plugNbt) |
| plugNbt |
| })) |
| } |
| |
| // ----------------------------------------------------------------------- // |
| |
| protected class Plug(val side: ForgeDirection) extends api.network.Environment { |
| val node = createNode(this) |
| |
| def onMessage(message: Message) { |
| if (isPrimary) { |
| onPlugMessage(this, message) |
| } |
| } |
| |
| def onConnect(node: Node) = onPlugConnect(this, node) |
| |
| def onDisconnect(node: Node) = onPlugDisconnect(this, node) |
| |
| def isPrimary = plugs(plugs.indexWhere(_.node.network == node.network)) == this |
| |
| def plugsInOtherNetworks = plugs.filter(_.node.network != node.network) |
| } |
| |
| protected def onPlugConnect(plug: Plug, node: Node) {} |
| |
| protected def onPlugDisconnect(plug: Plug, node: Node) {} |
| |
| protected def onPlugMessage(plug: Plug, message: Message) { |
| if (message.name == "network.message") { |
| plug.plugsInOtherNetworks.foreach(_.node.sendToReachable(message.name, message.data: _*)) |
| } |
| } |
| |
| protected def createNode(plug: Plug): Node = api.Network.newNode(plug, Visibility.Network).create() |
| } |