From ab1c4abc92239951a48e10627f4e456b58b16004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 3 Jan 2014 19:03:18 +0100 Subject: [PATCH] abstract card localizations; triggering events to add/remove from abstract bus network; some cleanup --- assets/opencomputers/lang/de_DE.lang | 2 + assets/opencomputers/lang/en_US.lang | 2 + .../common/tileentity/AbstractBusAware.scala | 69 +++++++++++++++++-- li/cil/oc/common/tileentity/Environment.scala | 26 +------ li/cil/oc/common/tileentity/Router.scala | 2 +- li/cil/oc/common/tileentity/TileEntity.scala | 29 +++++--- li/cil/oc/server/component/AbstractBus.scala | 2 +- 7 files changed, 90 insertions(+), 42 deletions(-) diff --git a/assets/opencomputers/lang/de_DE.lang b/assets/opencomputers/lang/de_DE.lang index fb472408f..4facf3947 100644 --- a/assets/opencomputers/lang/de_DE.lang +++ b/assets/opencomputers/lang/de_DE.lang @@ -23,6 +23,7 @@ oc:block.Screen1.name=Hochwertiger Bildschirm oc:block.Screen2.name=Ausgezeichneter Bildschirm # Items +oc:item.AbstractBusCard.name=Abstrakter-Bus-Karte oc:item.Acid.name=Grog oc:item.ALU.name=Arithmetisch-logische Einheit (ALU) oc:item.Analyzer.name=Messgerät @@ -77,6 +78,7 @@ oc:container.Case=Computer oc:container.DiskDrive=Diskettenlaufwerk # Item / Block Tooltips +oc:tooltip.AbstractBusCard=Erlaubt es LIP-Pakete des Abstrakten Busses von §fStargateTech 2§7 zu senden und zu empfangen. oc:tooltip.Acid=Eine hochgiftige Möchtegernflüssigkeit, wird üblicherweise nur von gewissen Piraten konsumiert. Dank ihrer korrosiven Eigenschaften ideal zum Bedrucken von Leiterplatten geeignet. oc:tooltip.Adapter=Erlaubt es Blöcke anzusteuern, die keine Komponentenblöcke sind, wie etwa reguläre Minecraft-Blöcke oder Blöcke anderer Mods. oc:tooltip.ALU=Zählt Zahlen zum Zeitvertreib. Klingt komisch, is aber so. diff --git a/assets/opencomputers/lang/en_US.lang b/assets/opencomputers/lang/en_US.lang index 1e8933334..d9e40efee 100644 --- a/assets/opencomputers/lang/en_US.lang +++ b/assets/opencomputers/lang/en_US.lang @@ -23,6 +23,7 @@ oc:block.Screen1.name=Advanced Screen oc:block.Screen2.name=Superior Screen # Items +oc:item.AbstractBusCard.name=Abstract Bus Card oc:item.Acid.name=Grog oc:item.ALU.name=Arithmetic Logic Unit (ALU) oc:item.Analyzer.name=Analyzer @@ -77,6 +78,7 @@ oc:container.Case=Computer oc:container.DiskDrive=Disk Drive # Item / Block Tooltips +oc:tooltip.AbstractBusCard=Allows interacting with §fStargateTech 2§7's abstract bus by sending and receiving LIP packets. oc:tooltip.Acid=A highly toxic pseudo-liquid, usually only consumed by certain pirates. Thanks to its corrosive nature it is perfectly suited for etching circuit boards. oc:tooltip.Adapter=Used to control non-component blocks, such as vanilla blocks or blocks from other mods. oc:tooltip.ALU=Adds number so you don't have to. It might be better this way. diff --git a/li/cil/oc/common/tileentity/AbstractBusAware.scala b/li/cil/oc/common/tileentity/AbstractBusAware.scala index 2d14e8490..17026375f 100644 --- a/li/cil/oc/common/tileentity/AbstractBusAware.scala +++ b/li/cil/oc/common/tileentity/AbstractBusAware.scala @@ -1,12 +1,22 @@ package li.cil.oc.common.tileentity -import cpw.mods.fml.common.Optional +import cpw.mods.fml.common.{Loader, Optional} +import li.cil.oc.Items +import li.cil.oc.api.network.Node +import li.cil.oc.server.component import net.minecraft.item.ItemStack -import stargatetech2.api.bus.IBusDevice +import net.minecraftforge.common.MinecraftForge +import stargatetech2.api.bus.{BusEvent, IBusDevice} @Optional.Interface(iface = "stargatetech2.api.bus.IBusDevice", modid = "StargateTech2") -trait AbstractBusAware extends TileEntity with Inventory with IBusDevice { - def getInterfaces(side: Int) = if (hasAbstractBusCard) Array(null) else null +trait AbstractBusAware extends TileEntity with ComponentInventory with IBusDevice { + def getInterfaces(side: Int) = + if (hasAbstractBusCard) { + components collect { + case abstractBus: component.AbstractBus => abstractBus.busInterface + } + } + else null def getXCoord = x @@ -14,15 +24,60 @@ trait AbstractBusAware extends TileEntity with Inventory with IBusDevice { def getZCoord = z - protected def hasAbstractBusCard = false + protected def hasAbstractBusCard = components exists { + case abstractBus: component.AbstractBus => true + } override protected def onItemAdded(slot: Int, stack: ItemStack) { super.onItemAdded(slot, stack) - // TODO if card wasn't present, send device added event + if (Items.abstractBus.parent.subItem(stack) == Items.abstractBus) { + // Trigger network re-map after another interface was added. + addAbstractBus() + } } override protected def onItemRemoved(slot: Int, stack: ItemStack) { super.onItemRemoved(slot, stack) - // TODO if no card is present anymore, send device removed event + if (Items.abstractBus.parent.subItem(stack) == Items.abstractBus) { + if (!hasAbstractBusCard) { + // Last interface was removed, trigger removal. This is the case when + // the last abstract bus card was removed. + removeAbstractBus(force = true) + } + else { + // Trigger network re-map if some interface still remains. This is the + // case when one of multiple abstract bus cards was removed. + addAbstractBus() + } + } + } + + abstract override def onConnect(node: Node) { + super.onConnect(node) + addAbstractBus() + } + + override def onChunkUnload() { + super.onChunkUnload() + removeAbstractBus() + } + + override def invalidate() { + super.onChunkUnload() + removeAbstractBus() + } + + protected def addAbstractBus() { + // Mod loaded check to avoid class not found errors. + if (Loader.isModLoaded("StargateTech2") && hasAbstractBusCard) { + MinecraftForge.EVENT_BUS.post(new BusEvent.AddToNetwork(world, x, y, z)) + } + } + + protected def removeAbstractBus(force: Boolean = false) { + // Mod loaded check to avoid class not found errors. + if (Loader.isModLoaded("StargateTech2") && (hasAbstractBusCard || force)) { + MinecraftForge.EVENT_BUS.post(new BusEvent.RemoveFromNetwork(world, x, y, z)) + } } } diff --git a/li/cil/oc/common/tileentity/Environment.scala b/li/cil/oc/common/tileentity/Environment.scala index bd8ea125d..a86ba722a 100644 --- a/li/cil/oc/common/tileentity/Environment.scala +++ b/li/cil/oc/common/tileentity/Environment.scala @@ -6,22 +6,10 @@ import li.cil.oc.api.{Network, network} import li.cil.oc.util.ExtendedNBT._ import li.cil.oc.util.Persistable import net.minecraft.nbt.NBTTagCompound -import net.minecraft.network.INetworkManager -import net.minecraft.network.packet.Packet132TileEntityData import net.minecraftforge.common.ForgeDirection import scala.math.ScalaNumber -abstract class Environment extends net.minecraft.tileentity.TileEntity with TileEntity with network.Environment with Persistable { - def world = getWorldObj - - def x = xCoord - - def y = yCoord - - def z = zCoord - - def block = getBlockType - +abstract class Environment extends TileEntity with network.Environment with Persistable { protected var addedToNetwork = false // ----------------------------------------------------------------------- // @@ -76,18 +64,6 @@ abstract class Environment extends net.minecraft.tileentity.TileEntity with Tile // ----------------------------------------------------------------------- // - override def getDescriptionPacket = { - val nbt = new NBTTagCompound() - writeToNBTForClient(nbt) - if (nbt.hasNoTags) null else new Packet132TileEntityData(x, y, z, -1, nbt) - } - - override def onDataPacket(manager: INetworkManager, packet: Packet132TileEntityData) { - readFromNBTForClient(packet.data) - } - - // ----------------------------------------------------------------------- // - def onMessage(message: network.Message) {} def onConnect(node: network.Node) {} diff --git a/li/cil/oc/common/tileentity/Router.scala b/li/cil/oc/common/tileentity/Router.scala index aa308f407..49b1fb2ec 100644 --- a/li/cil/oc/common/tileentity/Router.scala +++ b/li/cil/oc/common/tileentity/Router.scala @@ -11,7 +11,7 @@ import net.minecraftforge.common.ForgeDirection import scala.collection.mutable @Optional.Interface(iface = "dan200.computer.api.IPeripheral", modid = "ComputerCraft") -class Router extends net.minecraft.tileentity.TileEntity with api.network.SidedEnvironment with IPeripheral { +class Router extends TileEntity with api.network.SidedEnvironment with IPeripheral { private val plugs = ForgeDirection.VALID_DIRECTIONS.map(side => new Plug(side)) // ----------------------------------------------------------------------- // diff --git a/li/cil/oc/common/tileentity/TileEntity.scala b/li/cil/oc/common/tileentity/TileEntity.scala index d894267d1..ba2538e6a 100644 --- a/li/cil/oc/common/tileentity/TileEntity.scala +++ b/li/cil/oc/common/tileentity/TileEntity.scala @@ -1,25 +1,38 @@ package li.cil.oc.common.tileentity import cpw.mods.fml.relauncher.{Side, SideOnly} -import net.minecraft.block.Block import net.minecraft.nbt.NBTTagCompound -import net.minecraft.world.World +import net.minecraft.network.INetworkManager +import net.minecraft.network.packet.Packet132TileEntityData +import net.minecraft.tileentity.{TileEntity => MCTileEntity} -trait TileEntity { - def world: World +trait TileEntity extends MCTileEntity { + def world = getWorldObj - def x: Int + def x = xCoord - def y: Int + def y = yCoord - def z: Int + def z = zCoord - def block: Block + def block = getBlockType lazy val isClient = world.isRemote lazy val isServer = !isClient + // ----------------------------------------------------------------------- // + + override def getDescriptionPacket = { + val nbt = new NBTTagCompound() + writeToNBTForClient(nbt) + if (nbt.hasNoTags) null else new Packet132TileEntityData(x, y, z, -1, nbt) + } + + override def onDataPacket(manager: INetworkManager, packet: Packet132TileEntityData) { + readFromNBTForClient(packet.data) + } + @SideOnly(Side.CLIENT) def readFromNBTForClient(nbt: NBTTagCompound) {} diff --git a/li/cil/oc/server/component/AbstractBus.scala b/li/cil/oc/server/component/AbstractBus.scala index a34602fbc..1ab8268f5 100644 --- a/li/cil/oc/server/component/AbstractBus.scala +++ b/li/cil/oc/server/component/AbstractBus.scala @@ -13,7 +13,7 @@ class AbstractBus(val owner: tileentity.Computer) extends ManagedComponent with withComponent("abstract_bus"). create() - protected val busInterface: IBusInterface = StargateTechAPI.api.getFactory.getIBusInterface(owner, this) + val busInterface: IBusInterface = StargateTechAPI.api.getFactory.getIBusInterface(owner, this) protected var isEnabled = true