mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-16 10:51:55 -04:00
properly synchronizing abstract bus availability of cases now for proper cable rendering; cleanup packet sender class a bit, getting rid of old, no longer used stuff
This commit is contained in:
parent
ad288ac5fd
commit
b2dedc24d4
@ -23,6 +23,7 @@ class PacketHandler extends CommonPacketHandler {
|
||||
|
||||
override def dispatch(p: PacketParser) =
|
||||
p.packetType match {
|
||||
case PacketType.AbstractBusState => onAbstractBusState(p)
|
||||
case PacketType.Analyze => onAnalyze(p)
|
||||
case PacketType.ChargerState => onChargerState(p)
|
||||
case PacketType.ComputerState => onComputerState(p)
|
||||
@ -47,6 +48,12 @@ class PacketHandler extends CommonPacketHandler {
|
||||
case _ => // Invalid packet.
|
||||
}
|
||||
|
||||
def onAbstractBusState(p: PacketParser) =
|
||||
p.readTileEntity[AbstractBusAware]() match {
|
||||
case Some(t) => t.isAbstractBusAvailable = p.readBoolean()
|
||||
case _ => // Invalid packet.
|
||||
}
|
||||
|
||||
def onAnalyze(p: PacketParser) {
|
||||
val player = p.player.asInstanceOf[EntityPlayer]
|
||||
val stats = p.readNBT().asInstanceOf[NBTTagCompound]
|
||||
|
@ -3,6 +3,7 @@ package li.cil.oc.common
|
||||
object PacketType extends Enumeration {
|
||||
val
|
||||
// Server -> Client
|
||||
AbstractBusState,
|
||||
Analyze,
|
||||
ChargerState,
|
||||
ComputerState,
|
||||
|
@ -1,61 +1,80 @@
|
||||
package li.cil.oc.common.tileentity
|
||||
|
||||
import cpw.mods.fml.common.{Loader, Optional}
|
||||
import li.cil.oc.Items
|
||||
import cpw.mods.fml.relauncher.{SideOnly, Side}
|
||||
import li.cil.oc.api.network.Node
|
||||
import li.cil.oc.server.component
|
||||
import li.cil.oc.server.{PacketSender => ServerPacketSender, component}
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraftforge.common.MinecraftForge
|
||||
import stargatetech2.api.StargateTechAPI
|
||||
import stargatetech2.api.bus.{BusEvent, IBusDevice}
|
||||
|
||||
@Optional.Interface(iface = "stargatetech2.api.bus.IBusDevice", modid = "StargateTech2")
|
||||
trait AbstractBusAware extends TileEntity with ComponentInventory with IBusDevice {
|
||||
def getInterfaces(side: Int) =
|
||||
if (hasAbstractBusCard) {
|
||||
components collect {
|
||||
case Some(abstractBus: component.AbstractBus) => abstractBus.busInterface
|
||||
if (isAbstractBusAvailable) {
|
||||
if (isServer) {
|
||||
components collect {
|
||||
case Some(abstractBus: component.AbstractBus) => abstractBus.busInterface
|
||||
}
|
||||
}
|
||||
else fakeInterface
|
||||
}
|
||||
else null
|
||||
|
||||
protected var _isAbstractBusAvailable = false
|
||||
|
||||
private lazy val fakeInterface = Array(StargateTechAPI.api.getFactory.getIBusInterface(this, null))
|
||||
|
||||
def getXCoord = x
|
||||
|
||||
def getYCoord = y
|
||||
|
||||
def getZCoord = z
|
||||
|
||||
protected def hasAbstractBusCard = components exists {
|
||||
case Some(_: component.AbstractBus) => true
|
||||
case _ => false
|
||||
def isAbstractBusAvailable = _isAbstractBusAvailable
|
||||
|
||||
def isAbstractBusAvailable_=(value: Boolean) = {
|
||||
if (value != isAbstractBusAvailable) {
|
||||
_isAbstractBusAvailable = value
|
||||
if (isAbstractBusAvailable) addAbstractBus()
|
||||
else removeAbstractBus()
|
||||
world.notifyBlocksOfNeighborChange(x, y, z, block.blockID)
|
||||
if (isServer) ServerPacketSender.sendAbstractBusState(this)
|
||||
else world.markBlockForRenderUpdate(x, y, z)
|
||||
}
|
||||
this
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
override def readFromNBTForClient(nbt: NBTTagCompound) {
|
||||
super.readFromNBTForClient(nbt)
|
||||
isAbstractBusAvailable = nbt.getBoolean("isAbstractBusAvailable")
|
||||
}
|
||||
|
||||
override def writeToNBTForClient(nbt: NBTTagCompound) {
|
||||
super.writeToNBTForClient(nbt)
|
||||
nbt.setBoolean("isAbstractBusAvailable", isAbstractBusAvailable)
|
||||
}
|
||||
|
||||
override protected def onItemAdded(slot: Int, stack: ItemStack) {
|
||||
super.onItemAdded(slot, stack)
|
||||
if (Items.abstractBus.parent.subItem(stack) == Items.abstractBus) {
|
||||
// Trigger network re-map after another interface was added.
|
||||
addAbstractBus()
|
||||
if (isServer) {
|
||||
isAbstractBusAvailable = hasAbstractBusCard
|
||||
}
|
||||
}
|
||||
|
||||
override protected def onItemRemoved(slot: Int, stack: ItemStack) {
|
||||
super.onItemRemoved(slot, stack)
|
||||
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()
|
||||
}
|
||||
if (isServer) {
|
||||
isAbstractBusAvailable = hasAbstractBusCard
|
||||
}
|
||||
}
|
||||
|
||||
abstract override def onConnect(node: Node) {
|
||||
super.onConnect(node)
|
||||
addAbstractBus()
|
||||
isAbstractBusAvailable = hasAbstractBusCard
|
||||
}
|
||||
|
||||
abstract override def onDisconnect(node: Node) {
|
||||
@ -65,15 +84,20 @@ trait AbstractBusAware extends TileEntity with ComponentInventory with IBusDevic
|
||||
|
||||
protected def addAbstractBus() {
|
||||
// Mod loaded check to avoid class not found errors.
|
||||
if (Loader.isModLoaded("StargateTech2") && hasAbstractBusCard) {
|
||||
if (isServer && Loader.isModLoaded("StargateTech2")) {
|
||||
MinecraftForge.EVENT_BUS.post(new BusEvent.AddToNetwork(world, x, y, z))
|
||||
}
|
||||
}
|
||||
|
||||
protected def removeAbstractBus(force: Boolean = false) {
|
||||
protected def removeAbstractBus() {
|
||||
// Mod loaded check to avoid class not found errors.
|
||||
if (Loader.isModLoaded("StargateTech2") && (hasAbstractBusCard || force)) {
|
||||
if (isServer && Loader.isModLoaded("StargateTech2")) {
|
||||
MinecraftForge.EVENT_BUS.post(new BusEvent.RemoveFromNetwork(world, x, y, z))
|
||||
}
|
||||
}
|
||||
|
||||
protected def hasAbstractBusCard = components exists {
|
||||
case Some(_: component.AbstractBus) => true
|
||||
case _ => false
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,17 @@ import li.cil.oc.util.PackedColor
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraftforge.common.ForgeDirection
|
||||
import scala.Some
|
||||
|
||||
object PacketSender {
|
||||
def sendAbstractBusState(t: AbstractBusAware) {
|
||||
val pb = new PacketBuilder(PacketType.AbstractBusState)
|
||||
|
||||
pb.writeTileEntity(t)
|
||||
pb.writeBoolean(t.isAbstractBusAvailable)
|
||||
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendAnalyze(stats: NBTTagCompound, address: String, player: Player) {
|
||||
val pb = new PacketBuilder(PacketType.Analyze)
|
||||
|
||||
@ -20,28 +28,22 @@ object PacketSender {
|
||||
pb.sendToPlayer(player)
|
||||
}
|
||||
|
||||
def sendChargerState(t: Charger, player: Option[Player] = None) {
|
||||
def sendChargerState(t: Charger) {
|
||||
val pb = new PacketBuilder(PacketType.ChargerState)
|
||||
|
||||
pb.writeTileEntity(t)
|
||||
pb.writeDouble(t.chargeSpeed)
|
||||
|
||||
player match {
|
||||
case Some(p) => pb.sendToPlayer(p)
|
||||
case _ => pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendComputerState(t: Computer, player: Option[Player] = None) {
|
||||
def sendComputerState(t: Computer) {
|
||||
val pb = new PacketBuilder(PacketType.ComputerState)
|
||||
|
||||
pb.writeTileEntity(t)
|
||||
pb.writeBoolean(t.isRunning)
|
||||
|
||||
player match {
|
||||
case Some(p) => pb.sendToPlayer(p)
|
||||
case _ => pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendComputerUserList(t: Computer, list: Array[String]) {
|
||||
@ -54,20 +56,17 @@ object PacketSender {
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendPowerState(t: PowerInformation, player: Option[Player] = None) {
|
||||
def sendPowerState(t: PowerInformation) {
|
||||
val pb = new PacketBuilder(PacketType.PowerState)
|
||||
|
||||
pb.writeTileEntity(t)
|
||||
pb.writeDouble(t.globalBuffer)
|
||||
pb.writeDouble(t.globalBufferSize)
|
||||
|
||||
player match {
|
||||
case Some(p) => pb.sendToPlayer(p)
|
||||
case _ => pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendRedstoneState(t: RedstoneAware, player: Option[Player] = None) {
|
||||
def sendRedstoneState(t: RedstoneAware) {
|
||||
val pb = new PacketBuilder(PacketType.RedstoneState)
|
||||
|
||||
pb.writeTileEntity(t)
|
||||
@ -76,10 +75,7 @@ object PacketSender {
|
||||
pb.writeByte(t.output(d))
|
||||
}
|
||||
|
||||
player match {
|
||||
case Some(p) => pb.sendToPlayer(p)
|
||||
case _ => pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendRobotMove(t: Robot, ox: Int, oy: Int, oz: Int, direction: ForgeDirection) {
|
||||
@ -150,17 +146,14 @@ object PacketSender {
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendRotatableState(t: Rotatable, player: Option[Player] = None) {
|
||||
def sendRotatableState(t: Rotatable) {
|
||||
val pb = new PacketBuilder(PacketType.RotatableState)
|
||||
|
||||
pb.writeTileEntity(t)
|
||||
pb.writeDirection(t.pitch)
|
||||
pb.writeDirection(t.yaw)
|
||||
|
||||
player match {
|
||||
case Some(p) => pb.sendToPlayer(p)
|
||||
case _ => pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
pb.sendToNearbyPlayers(t)
|
||||
}
|
||||
|
||||
def sendScreenColorChange(t: Buffer, foreground: Int, background: Int) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user