Refactored some common code into bdlib, added ability for modules to decide if they can connect to a controller

This commit is contained in:
bdew committed 2015-03-25 17:34:13 +02:00
1 parent dc567bd1be
commit 93ccc42429
5 files changed
+241 -8

No files matched your search

@@ -41,13 +41,15 @@ trait TileModule extends TileDataSlots {
}
def onBreak() {
getCore map (_.moduleRemoved(this))
getCore foreach (_.moduleRemoved(this))
}
def canConnectToCore(br: BlockRef) = true
def tryConnect() {
if (getCore.isEmpty) {
for {
conn <- Tools.findConnections(getWorldObj, myPos, kind).headOption
conn <- Tools.findConnections(getWorldObj, myPos, kind).headOption if canConnectToCore(conn)
core <- conn.getTile[TileController](getWorldObj)
} {
connect(core)
@@ -0,0 +1,84 @@
/*
* Copyright (c) bdew, 2013 - 2015
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.sensors.multiblock
import net.bdew.lib.Misc
import net.bdew.lib.block.BlockRef
import net.bdew.lib.gui.GuiProvider
import net.bdew.lib.multiblock.block.BlockModule
import net.bdew.lib.rotate.{BaseRotatableBlock, IconType}
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.util.{ChatComponentTranslation, IIcon}
import net.minecraft.world.{IBlockAccess, World}
import net.minecraftforge.common.util.ForgeDirection
trait BlockRedstoneSensorModule[T <: TileRedstoneSensorModule] extends BlockModule[T] with BaseRotatableBlock with GuiProvider {
override def onBlockActivated(world: World, x: Int, y: Int, z: Int, player: EntityPlayer, meta: Int, xOffs: Float, yOffs: Float, zOffs: Float): Boolean = {
if (player.isSneaking) return false
if (world.isRemote) return true
val te = getTE(world, x, y, z)
if (te.getCore.isDefined) {
te.config.ensureValid(te.getCore.get)
doOpenGui(world, x, y, z, player)
} else {
player.addChatMessage(new ChatComponentTranslation("bdlib.multiblock.notconnected"))
}
true
}
def doOpenGui(world: World, x: Int, y: Int, z: Int, player: EntityPlayer)
def notifyTarget(w: World, x: Int, y: Int, z: Int): Unit = {
val bp = BlockRef(x, y, z).neighbour(getFacing(w, x, y, z))
w.notifyBlocksOfNeighborChange(bp.x, bp.y, bp.z, this)
}
override def getFacing(world: IBlockAccess, x: Int, y: Int, z: Int) =
Misc.forgeDirection(world.getBlockMetadata(x, y, z) & 7)
override def setFacing(world: World, x: Int, y: Int, z: Int, facing: ForgeDirection) = {
val m = world.getBlockMetadata(x, y, z) & 8
world.setBlockMetadataWithNotify(x, y, z, m | facing.ordinal(), 3)
notifyTarget(world, x, y, z)
}
def isSignalOn(world: IBlockAccess, x: Int, y: Int, z: Int) =
(world.getBlockMetadata(x, y, z) & 8) == 8
def setSignal(world: World, x: Int, y: Int, z: Int, signal: Boolean): Unit = {
val m = world.getBlockMetadata(x, y, z) & 7
world.setBlockMetadataWithNotify(x, y, z, m | (if (signal) 8 else 0), 3)
notifyTarget(world, x, y, z)
}
override def getIcon(meta: Int, kind: IconType.Value) = kind match {
case IconType.BACK => bottomIcon
case IconType.FRONT => if ((meta & 8) == 8) frontIconOn else frontIcon
case IconType.SIDE => if ((meta & 8) == 8) sideIconOn else sideIcon
}
override def canProvidePower = true
override def isSideSolid(world: IBlockAccess, x: Int, y: Int, z: Int, side: ForgeDirection) = true
override def isProvidingWeakPower(w: IBlockAccess, x: Int, y: Int, z: Int, side: Int) = {
val m = w.getBlockMetadata(x, y, z)
if (((m & 7) == Misc.forgeDirection(side).getOpposite.ordinal()) && ((m & 8) == 8))
15
else
0
}
var frontIcon: IIcon = null
var bottomIcon: IIcon = null
var frontIconOn: IIcon = null
var sideIconOn: IIcon = null
var sideIcon: IIcon = null
}
@@ -10,9 +10,10 @@
package net.bdew.lib.sensors.multiblock
import net.bdew.lib.multiblock.tile.TileController
import net.bdew.lib.sensors.GenericSensorType
import net.bdew.lib.sensors.{GenericSensorType, SensorSystem}
import net.minecraft.tileentity.TileEntity
trait CIRedstoneSensors extends TileController {
def sensorTypes: Seq[GenericSensorType[TileEntity, Boolean]]
def redstoneSensorsType: Seq[GenericSensorType[TileEntity, Boolean]]
def redstoneSensorSystem: SensorSystem[TileEntity, Boolean]
}
@@ -0,0 +1,111 @@
/*
* Copyright (c) bdew, 2013 - 2015
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.sensors.multiblock
import cpw.mods.fml.relauncher.{Side, SideOnly}
import net.bdew.lib.Misc
import net.bdew.lib.gui._
import net.bdew.lib.multiblock.interact.CIOutputFaces
import net.bdew.lib.render.connected.BlockAdditionalRender
import net.bdew.lib.sensors.{GenericSensorParameter, GenericSensorType, SensorSystem}
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.tileentity.TileEntity
trait SensorOutput extends GenericSensorType[TileEntity, Boolean] {
def system: SensorSystem[TileEntity, Boolean]
override def defaultParameter = system.DisabledParameter
case class SensorOutputFlowParameter(output: Int) extends GenericSensorParameter(system) {
override val uid: String = "output." + output
}
def getResultFromOutput(te: CIOutputFaces, output: Int): Boolean
override def loadParameter(tag: NBTTagCompound) = {
if (tag.hasKey("output"))
SensorOutputFlowParameter(tag.getInteger("output"))
else
system.DisabledParameter
}
override def saveParameter(p: GenericSensorParameter, tag: NBTTagCompound): Unit = p match {
case SensorOutputFlowParameter(n) => tag.setInteger("output", n)
case _ =>
}
override def isValidParameter(p: GenericSensorParameter, obj: TileEntity): Boolean = (p, obj) match {
case (SensorOutputFlowParameter(n), x: CIOutputFaces) => x.outputFaces.inverted.isDefinedAt(n)
case (param, _) if param == system.DisabledParameter => true
case _ => false
}
override def getResult(param: GenericSensorParameter, obj: TileEntity): Boolean = (param, obj) match {
case (SensorOutputFlowParameter(n), x: CIOutputFaces) => getResultFromOutput(x, n)
case _ => false
}
override def paramClicked(current: GenericSensorParameter, item: ItemStack, button: Int, mod: Int, obj: TileEntity) =
if (mod == 0 && (button == 0 || button == 1) && item == null)
(current, obj) match {
case (SensorOutputFlowParameter(n), x: CIOutputFaces) =>
val outputs = x.outputFaces.map.values.toList.sorted
if (outputs.size > 0)
if (button == 0)
SensorOutputFlowParameter(Misc.nextInSeq(outputs, n))
else
SensorOutputFlowParameter(Misc.prevInSeq(outputs, n))
else
system.DisabledParameter
case (_, x: CIOutputFaces) =>
if (button == 0)
x.outputFaces.map.values.toList.sorted.headOption map SensorOutputFlowParameter getOrElse system.DisabledParameter
else
x.outputFaces.map.values.toList.sorted.reverse.headOption map SensorOutputFlowParameter getOrElse system.DisabledParameter
case _ =>
system.DisabledParameter
}
else current
@SideOnly(Side.CLIENT)
override def drawParameter(rect: Rect, target: DrawTarget, obj: TileEntity, param: GenericSensorParameter): Unit = (param, obj) match {
case (SensorOutputFlowParameter(output), te: CIOutputFaces) =>
val faces = te.outputFaces.inverted
if (faces.isDefinedAt(output)) {
val bf = faces(output)
bf.origin.block(te.getWorldObj) foreach { block =>
target.drawTexture(rect, Texture(Texture.BLOCKS, block.getIcon(te.getWorldObj, bf.origin.x, bf.origin.y, bf.origin.z, bf.face.ordinal())))
if (block.isInstanceOf[BlockAdditionalRender]) {
for (over <- block.asInstanceOf[BlockAdditionalRender].getFaceOverlays(te.getWorldObj, bf.origin.x, bf.origin.y, bf.origin.z, bf.face))
target.drawTexture(rect, Texture(Texture.BLOCKS, over.icon), over.color)
}
}
}
case _ => target.drawTexture(rect, system.DisabledParameter.texture, system.DisabledParameter.textureColor)
}
override def getParamTooltip(obj: TileEntity, param: GenericSensorParameter) = (param, obj) match {
case (SensorOutputFlowParameter(output), te: CIOutputFaces) =>
val faces = te.outputFaces.inverted
var list = List.empty[String]
list :+= Misc.toLocal(te.resources.unlocalizedOutputName(output))
if (faces.isDefinedAt(output)) {
val bf = faces(output)
bf.origin.block(te.getWorldObj) foreach { block =>
list :+= block.getLocalizedName
list :+= "%d, %d, %d - %s".format(bf.x, bf.y, bf.z, Misc.toLocal("bdlib.multiblock.face." + bf.face.toString.toLowerCase))
}
}
list
case _ => super.getParamTooltip(obj, param)
}
}
@@ -9,11 +9,46 @@
package net.bdew.lib.sensors.multiblock
import net.bdew.lib.multiblock.tile.TileModule
import net.bdew.lib.sensors.DataSlotSensor
import net.bdew.lib.Misc
import net.bdew.lib.block.BlockRef
import net.bdew.lib.multiblock.tile.{TileController, TileModule}
import net.bdew.lib.sensors.{DataSlotSensor, RedstoneSensors, SensorPair}
import net.minecraft.block.Block
import net.minecraft.tileentity.TileEntity
import net.minecraft.world.World
abstract class TileRedstoneSensorModule extends TileModule {
def config: DataSlotSensor[TileEntity, Boolean]
abstract class TileRedstoneSensorModule(val system: RedstoneSensors[TileEntity], block: BlockRedstoneSensorModule[_]) extends TileModule {
override val kind = "Sensor"
override def getCore = getCoreAs[CIRedstoneSensors]
val config = DataSlotSensor(system, "sensor", this, system.DisabledSensor)
override def connect(target: TileController) = {
super.connect(target)
for {
core <- Misc.asInstanceOpt(target, classOf[CIRedstoneSensors])
sensor <- core.redstoneSensorsType.headOption
} {
config := SensorPair(sensor, sensor.defaultParameter)
}
}
override def coreRemoved() = {
config := system.DisabledSensorPair
super.coreRemoved()
}
override def canConnectToCore(br: BlockRef): Boolean =
br.getTile[CIRedstoneSensors](worldObj).exists(_.redstoneSensorSystem == system)
def isSignalOn = block.isSignalOn(worldObj, xCoord, yCoord, zCoord)
serverTick.listen(() => {
val act = getCore exists config.getResult
if (block.isSignalOn(worldObj, xCoord, yCoord, zCoord) != act) {
block.setSignal(worldObj, xCoord, yCoord, zCoord, act)
}
})
override def shouldRefresh(oldBlock: Block, newBlock: Block, oldMeta: Int, newMeta: Int, world: World, x: Int, y: Int, z: Int) =
newBlock != oldBlock
}