mirror of
https://github.com/GTNewHorizons/bdlib.git
synced 2026-11-21 12:42:18 +00:00
Aded multiblock system (moved from DCM)
This commit is contained in:
1 parent
12f19fc410
commit
82328d10ec
31 files changed
+1163
-1
No files matched your search
@@ -12,6 +12,7 @@ package net.bdew.lib
|
||||
import cpw.mods.fml.common.Mod
|
||||
import cpw.mods.fml.common.Mod.EventHandler
|
||||
import cpw.mods.fml.common.event.{FMLPreInitializationEvent, FMLServerStartingEvent}
|
||||
import net.bdew.lib.multiblock.network.NetHandler
|
||||
import net.minecraft.command.CommandHandler
|
||||
import org.apache.logging.log4j.Logger
|
||||
|
||||
@@ -29,6 +30,7 @@ object BdLib {
|
||||
def preInit(ev: FMLPreInitializationEvent) {
|
||||
log = ev.getModLog
|
||||
log.info("bdlib BDLIB_VER loaded")
|
||||
NetHandler.init()
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
||||
@@ -11,7 +11,7 @@ package net.bdew.lib.config
|
||||
|
||||
import cpw.mods.fml.common.registry.GameRegistry
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.block.{NamedBlock, HasTE, SimpleBlock}
|
||||
import net.bdew.lib.block.{HasTE, NamedBlock}
|
||||
import net.minecraft.block.Block
|
||||
import net.minecraft.creativetab.CreativeTabs
|
||||
import net.minecraft.item.ItemStack
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.gui.widgets
|
||||
|
||||
import net.bdew.lib.gui.{Point, Rect, Texture}
|
||||
import net.minecraft.client.Minecraft
|
||||
import net.minecraft.client.audio.PositionedSoundRecord
|
||||
import net.minecraft.util.ResourceLocation
|
||||
|
||||
import scala.collection.mutable
|
||||
|
||||
class WidgetButtonIcon(p: Point, clicked: WidgetButtonIcon => Unit, baseTex: Texture, hoverTex: Texture) extends Widget {
|
||||
val rect = new Rect(p, 16, 16)
|
||||
val iconRect = new Rect(p +(1, 1), 14, 14)
|
||||
|
||||
var icon: Texture = null
|
||||
var hover: String = null
|
||||
|
||||
override def draw(mouse: Point) {
|
||||
if (rect.contains(mouse))
|
||||
parent.drawTexture(rect, hoverTex)
|
||||
else
|
||||
parent.drawTexture(rect, baseTex)
|
||||
|
||||
if (icon != null)
|
||||
parent.drawTexture(iconRect, icon)
|
||||
}
|
||||
|
||||
override def handleTooltip(p: Point, tip: mutable.MutableList[String]) {
|
||||
if (hover != null && hover > "")
|
||||
tip += hover
|
||||
}
|
||||
|
||||
override def mouseClicked(p: Point, button: Int) {
|
||||
Minecraft.getMinecraft.getSoundHandler.playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F))
|
||||
clicked(this)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock
|
||||
|
||||
import net.bdew.lib.recipes.gencfg.{ConfigSection, EntryDouble}
|
||||
|
||||
trait MachineCore {
|
||||
var tuning: ConfigSection
|
||||
|
||||
def cfgSectionToMap(name: String) = tuning.getSection(name).map({
|
||||
case (n, EntryDouble(v)) => n -> v.toInt
|
||||
case (n, x) => sys.error("Invalid config value in section %s%s.%s = %s".format(tuning.pfx, name, n, x))
|
||||
}).toMap
|
||||
|
||||
lazy val modules = cfgSectionToMap("Modules")
|
||||
lazy val required = cfgSectionToMap("Required")
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock
|
||||
|
||||
import net.bdew.lib.gui.{Color, Texture}
|
||||
import net.minecraft.util.IIcon
|
||||
|
||||
trait ResourceProvider {
|
||||
// Icons for rendering, *must* be on block spritesheet
|
||||
def edge: IIcon
|
||||
def disabled: IIcon
|
||||
def output: IIcon
|
||||
def arrowTop: IIcon
|
||||
def arrowRight: IIcon
|
||||
def arrowBottom: IIcon
|
||||
def arrowLeft: IIcon
|
||||
|
||||
// Textures for GUI stuff
|
||||
def btBase: Texture
|
||||
def btHover: Texture
|
||||
def btRsOn: Texture
|
||||
def btRsOff: Texture
|
||||
def btEnabled: Texture
|
||||
def btDisabled: Texture
|
||||
|
||||
val outputColors: Map[Int, Color]
|
||||
val unlocalizedOutputName: Map[Int, String]
|
||||
def getModuleName(s: String): String
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock
|
||||
|
||||
import net.bdew.lib.block.BlockRef
|
||||
import net.bdew.lib.multiblock.tile.{TileController, TileModule}
|
||||
import net.minecraft.world.World
|
||||
|
||||
import scala.collection.mutable
|
||||
|
||||
object Tools {
|
||||
def canConnect(world: World, core: BlockRef, kind: String): Boolean = {
|
||||
val t = core.getTile[TileController](world).getOrElse(return false)
|
||||
t.getNumOfMoudules(kind) < t.cfg.modules.getOrElse(kind, return false)
|
||||
}
|
||||
|
||||
def findConnections(world: World, start: BlockRef, kind: String) =
|
||||
(start.neighbours.values flatMap { case pos =>
|
||||
(pos.tile(world) flatMap {
|
||||
case t: TileModule => t.connected.cval
|
||||
case t: TileController => Some(pos)
|
||||
case _ => None
|
||||
}) filter (x => canConnect(world, x, kind))
|
||||
}).toList.distinct
|
||||
|
||||
def getAdjancedConnected(w: World, core: BlockRef, pos: BlockRef, seen: mutable.Set[BlockRef]) =
|
||||
pos.neighbours.values
|
||||
.filterNot(seen.contains)
|
||||
.flatMap(_.getTile[TileModule](w))
|
||||
.filter(x => x.connected.contains(core))
|
||||
.map(_.mypos)
|
||||
|
||||
def findReachableModules(world: World, core: BlockRef): Set[BlockRef] = {
|
||||
val seen = mutable.Set.empty[BlockRef]
|
||||
val queue = mutable.Queue.empty[BlockRef]
|
||||
queue ++= getAdjancedConnected(world, core, core, seen)
|
||||
while (queue.size > 0) {
|
||||
val current = queue.dequeue()
|
||||
seen.add(current)
|
||||
queue ++= getAdjancedConnected(world, core, current, seen)
|
||||
}
|
||||
return seen.toSet
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.block
|
||||
|
||||
import net.bdew.lib.block.{BlockRef, HasTE}
|
||||
import net.bdew.lib.multiblock.ResourceProvider
|
||||
import net.bdew.lib.multiblock.tile.TileController
|
||||
import net.bdew.lib.render.connected.ConnectedTextureBlock
|
||||
import net.minecraft.block.Block
|
||||
import net.minecraft.block.material.Material
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.world.{IBlockAccess, World}
|
||||
|
||||
abstract class BlockController[T <: TileController](val name: String, material: Material, val TEClass: Class[T])
|
||||
extends Block(material) with HasTE[T] with ConnectedTextureBlock {
|
||||
|
||||
def resources: ResourceProvider
|
||||
|
||||
override def edgeIcon = resources.edge
|
||||
|
||||
override def breakBlock(world: World, x: Int, y: Int, z: Int, block: Block, meta: Int) {
|
||||
getTE(world, x, y, z).onBreak()
|
||||
super.breakBlock(world, x, y, z, block, meta)
|
||||
}
|
||||
|
||||
override def canConnect(world: IBlockAccess, origin: BlockRef, target: BlockRef) =
|
||||
getTE(world, origin).modules.contains(target)
|
||||
|
||||
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
|
||||
getTE(world, x, y, z).onClick(player)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.block
|
||||
|
||||
import net.bdew.lib.block.{BlockRef, HasTE}
|
||||
import net.bdew.lib.multiblock.tile.TileModule
|
||||
import net.bdew.lib.multiblock.{ResourceProvider, Tools}
|
||||
import net.bdew.lib.render.connected.ConnectedTextureBlock
|
||||
import net.minecraft.block.Block
|
||||
import net.minecraft.block.material.Material
|
||||
import net.minecraft.entity.EntityLivingBase
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.util.ChatComponentTranslation
|
||||
import net.minecraft.world.{IBlockAccess, World}
|
||||
|
||||
abstract class BlockModule[T <: TileModule](val name: String, val kind: String, material: Material, val TEClass: Class[T])
|
||||
extends Block(material) with HasTE[T] with ConnectedTextureBlock {
|
||||
|
||||
def resources: ResourceProvider
|
||||
|
||||
override def edgeIcon = resources.edge
|
||||
|
||||
override def canPlaceBlockAt(world: World, x: Int, y: Int, z: Int): Boolean =
|
||||
Tools.findConnections(world, BlockRef(x, y, z), kind).size <= 1
|
||||
|
||||
override def onBlockPlacedBy(world: World, x: Int, y: Int, z: Int, player: EntityLivingBase, stack: ItemStack) {
|
||||
getTE(world, x, y, z).tryConnect()
|
||||
}
|
||||
|
||||
override def onNeighborBlockChange(world: World, x: Int, y: Int, z: Int, block: Block) {
|
||||
getTE(world, x, y, z).tryConnect()
|
||||
}
|
||||
|
||||
override def breakBlock(world: World, x: Int, y: Int, z: Int, block: Block, meta: Int) {
|
||||
getTE(world, x, y, z).onBreak()
|
||||
super.breakBlock(world, x, y, z, block, meta)
|
||||
}
|
||||
|
||||
override def canConnect(world: IBlockAccess, origin: BlockRef, target: BlockRef) = {
|
||||
val me = getTE(world, origin)
|
||||
me.connected.contains(target) ||
|
||||
(target.getTile[TileModule](world) exists { other =>
|
||||
me.getCore.isDefined && other.getCore == me.getCore
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
(for {
|
||||
p <- te.connected.cval
|
||||
bl <- p.block(world)
|
||||
} yield {
|
||||
bl.onBlockActivated(world, p.x, p.y, p.z, player, meta, 0, 0, 0)
|
||||
}) getOrElse {
|
||||
player.addChatMessage(new ChatComponentTranslation("bdlib.multiblock.notconnected"))
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.block
|
||||
|
||||
import net.bdew.lib.block.{BlockFace, BlockRef}
|
||||
import net.bdew.lib.multiblock.tile.TileOutput
|
||||
import net.bdew.lib.render.connected.{BlockAdditionalRender, ConnectedHelper, FaceOverlay}
|
||||
import net.minecraft.world.IBlockAccess
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
|
||||
trait BlockOutput[T <: TileOutput] extends BlockModule[T] with BlockAdditionalRender {
|
||||
def getAdjancedFaces(face: ForgeDirection, bp: BlockRef) = {
|
||||
val sides = ConnectedHelper.faceAdjanced(face)
|
||||
Seq(
|
||||
(BlockFace(bp, sides.top), resources.arrowTop),
|
||||
(BlockFace(bp, sides.right), resources.arrowRight),
|
||||
(BlockFace(bp, sides.bottom), resources.arrowBottom),
|
||||
(BlockFace(bp, sides.left), resources.arrowLeft)
|
||||
)
|
||||
}
|
||||
|
||||
def getFaceOverlays(world: IBlockAccess, x: Int, y: Int, z: Int, face: ForgeDirection): List[FaceOverlay] = {
|
||||
var result = List.empty[FaceOverlay]
|
||||
Option(getTE(world, x, y, z)) flatMap (_.getCore) map { core =>
|
||||
val bf = BlockFace(x, y, z, face)
|
||||
if (core.outputFaces.contains(bf))
|
||||
result :+= FaceOverlay(resources.output, resources.outputColors(core.outputFaces(bf)))
|
||||
for ((bf, icon) <- getAdjancedFaces(face, bf.origin)) {
|
||||
if (core.outputFaces.contains(bf))
|
||||
result :+= FaceOverlay(icon, resources.outputColors(core.outputFaces(bf)))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.block.BlockFace
|
||||
import net.bdew.lib.data.base.{DataSlot, TileDataSlots, UpdateKind}
|
||||
import net.minecraft.nbt.{NBTTagCompound, NBTTagIntArray, NBTTagList}
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
|
||||
case class DataSlotBlockFaceMap(name: String, parent: TileDataSlots) extends DataSlot {
|
||||
val map = collection.mutable.Map.empty[BlockFace, Int]
|
||||
|
||||
setUpdate(UpdateKind.SAVE, UpdateKind.WORLD)
|
||||
|
||||
def inverted = map.map(_.swap)
|
||||
|
||||
def ent2arr(x: (BlockFace, Int)) = Array(x._2, x._1.x, x._1.y, x._1.z, x._1.face.ordinal())
|
||||
def arr2ent(x: Array[Int]) = BlockFace(x(1), x(2), x(3), ForgeDirection.values()(x(4))) -> x(0)
|
||||
|
||||
def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
val lst = new NBTTagList()
|
||||
for (x <- map) lst.appendTag(new NBTTagIntArray(ent2arr(x)))
|
||||
t.setTag(name, lst)
|
||||
}
|
||||
|
||||
def load(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
map.clear()
|
||||
map ++= Misc.iterNbtIntArray(t, name) map arr2ent
|
||||
}
|
||||
|
||||
def updated() = parent.dataSlotChanged(this)
|
||||
}
|
||||
|
||||
object DataSlotBlockFaceMap {
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
implicit def dsbfm2map(v: DataSlotBlockFaceMap) = v.map
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
import net.bdew.lib.data.base.{DataSlot, TileDataSlots, UpdateKind}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
case class DataSlotOutputConfig(name: String, parent: TileDataSlots, slots: Int) extends DataSlot {
|
||||
val map = collection.mutable.Map.empty[Int, OutputConfig]
|
||||
|
||||
setUpdate(UpdateKind.SAVE, UpdateKind.GUI)
|
||||
|
||||
def inverted = map.map(_.swap)
|
||||
|
||||
def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
for ((n, x) <- map) {
|
||||
val tmp = new NBTTagCompound()
|
||||
x.write(tmp)
|
||||
val kind = x match {
|
||||
case _: OutputConfigPower => "power"
|
||||
case _ => sys.error("Unknown output config kind: " + x)
|
||||
}
|
||||
tmp.setString("kind", kind)
|
||||
t.setTag(name + "_" + n, tmp)
|
||||
}
|
||||
}
|
||||
|
||||
def load(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
map.clear()
|
||||
for (i <- 0 until slots) {
|
||||
if (t.hasKey(name + "_" + i)) {
|
||||
val cfg = t.getCompoundTag(name + "_" + i)
|
||||
val cfgObj = cfg.getString("kind") match {
|
||||
case "power" => new OutputConfigPower
|
||||
case x => new OutputConfigInvalid
|
||||
}
|
||||
cfgObj.read(cfg)
|
||||
map += i -> cfgObj
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def updated() = parent.dataSlotChanged(this)
|
||||
}
|
||||
|
||||
object DataSlotOutputConfig {
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
implicit def dsbfm2map(v: DataSlotOutputConfig) = v.map
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.block.BlockRef
|
||||
import net.bdew.lib.data.DataSlotOption
|
||||
import net.bdew.lib.data.base.{TileDataSlots, UpdateKind}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
case class DataSlotPos(name: String, parent: TileDataSlots) extends DataSlotOption[BlockRef] {
|
||||
setUpdate(UpdateKind.SAVE, UpdateKind.WORLD)
|
||||
|
||||
def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
cval map (x => t.setTag(name, Misc.applyMutator(x.writeToNBT, new NBTTagCompound)))
|
||||
}
|
||||
|
||||
def load(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
cval = if (t.hasKey(name))
|
||||
Some(BlockRef.fromNBT(t.getCompoundTag(name)))
|
||||
else
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.block.BlockRef
|
||||
import net.bdew.lib.data.base.{DataSlot, TileDataSlots, UpdateKind}
|
||||
import net.minecraft.nbt.{NBTTagCompound, NBTTagList}
|
||||
|
||||
case class DataSlotPosSet(name: String, parent: TileDataSlots) extends DataSlot {
|
||||
val set = collection.mutable.Set.empty[BlockRef]
|
||||
|
||||
setUpdate(UpdateKind.SAVE, UpdateKind.WORLD)
|
||||
|
||||
def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
val lst = new NBTTagList()
|
||||
for (x <- set) lst.appendTag(Misc.applyMutator(x.writeToNBT, new NBTTagCompound))
|
||||
t.setTag(name, lst)
|
||||
}
|
||||
|
||||
def load(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
set.clear()
|
||||
set ++= Misc.iterNbtCompoundList(t, name) map BlockRef.fromNBT
|
||||
}
|
||||
|
||||
def updated() = parent.dataSlotChanged(this)
|
||||
|
||||
}
|
||||
|
||||
object DataSlotPosSet {
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
implicit def dsps2set(v: DataSlotPosSet) = v.set
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
import net.bdew.lib.multiblock.network.MsgOutputCfg
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException
|
||||
|
||||
abstract class OutputConfig {
|
||||
def read(t: NBTTagCompound)
|
||||
def write(t: NBTTagCompound)
|
||||
def handleConfigPacket(m: MsgOutputCfg)
|
||||
}
|
||||
|
||||
class OutputConfigInvalid extends OutputConfig {
|
||||
def read(t: NBTTagCompound) {}
|
||||
def write(t: NBTTagCompound) = throw new NotImplementedException
|
||||
def handleConfigPacket(m: MsgOutputCfg) = throw new NotImplementedException
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
import net.bdew.lib.multiblock.network.MsgOutputCfg
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
case class MsgOutputCfgPower(output: Int, rsMode: RSMode.Value) extends MsgOutputCfg
|
||||
|
||||
class OutputConfigPower extends OutputConfig {
|
||||
var avg = 0.0
|
||||
var rsMode = RSMode.ALWAYS
|
||||
var unit = "MJ"
|
||||
|
||||
final val decay = 0.5
|
||||
|
||||
def updateAvg(v: Double) {
|
||||
avg = avg * decay + (1 - decay) * v
|
||||
}
|
||||
|
||||
def read(t: NBTTagCompound) {
|
||||
avg = t.getDouble("avg")
|
||||
rsMode = RSMode(t.getInteger("rsMode"))
|
||||
unit = t.getString("unit")
|
||||
}
|
||||
|
||||
def write(t: NBTTagCompound) {
|
||||
t.setDouble("avg", avg)
|
||||
t.setInteger("rsMode", rsMode.id)
|
||||
t.setString("unit", unit)
|
||||
}
|
||||
|
||||
def handleConfigPacket(m: MsgOutputCfg) = m match {
|
||||
case MsgOutputCfgPower(_, r) => rsMode = r
|
||||
case _ => sys.error("Invalid output config packet %s to config %s".format(m, this))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.data
|
||||
|
||||
object RSMode extends Enumeration {
|
||||
val ALWAYS = Value(0, "ALWAYS")
|
||||
val RS_ON = Value(1, "RS_ON")
|
||||
val RS_OFF = Value(2, "RS_OFF")
|
||||
val NEVER = Value(3, "NEVER")
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.gui
|
||||
|
||||
import net.bdew.lib.gui.widgets.Widget
|
||||
import net.bdew.lib.gui.{Color, Point, Rect, Texture}
|
||||
|
||||
import scala.collection.mutable
|
||||
|
||||
class WidgetInfo(val rect: Rect, icon: Texture, text: => String, tooltip: => String) extends Widget {
|
||||
final val iconRect = Rect(1, 1, 8, 8)
|
||||
|
||||
override def handleTooltip(p: Point, tip: mutable.MutableList[String]) {
|
||||
if (iconRect.contains(p) && tooltip != null) tip += tooltip
|
||||
}
|
||||
|
||||
override def draw(mouse: Point) {
|
||||
parent.drawTexture(iconRect + rect.origin, icon)
|
||||
parent.drawText(text, rect.origin +(12, 1), Color.darkgray, false)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.gui
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.gui.widgets.Widget
|
||||
import net.bdew.lib.gui.{Point, Rect, Texture}
|
||||
import net.bdew.lib.multiblock.interact.CIOutputFaces
|
||||
import net.bdew.lib.render.connected.BlockAdditionalRender
|
||||
|
||||
import scala.collection.mutable
|
||||
|
||||
class WidgetOutputIcon(p: Point, te: CIOutputFaces, output: Int) extends Widget {
|
||||
val rect = new Rect(p, 16, 16)
|
||||
|
||||
override def draw(mouse: Point) {
|
||||
val faces = te.outputFaces.inverted
|
||||
if (faces.isDefinedAt(output)) {
|
||||
val bf = faces(output)
|
||||
bf.origin.block(te.getWorldObj) map { block =>
|
||||
parent.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))
|
||||
parent.drawTexture(rect, Texture(Texture.BLOCKS, over.icon), over.color)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
parent.drawTexture(rect, Texture(Texture.BLOCKS, te.resources.disabled), te.resources.outputColors(output))
|
||||
}
|
||||
}
|
||||
|
||||
override def handleTooltip(p: Point, tip: mutable.MutableList[String]) {
|
||||
val faces = te.outputFaces.inverted
|
||||
tip += Misc.toLocal(te.resources.unlocalizedOutputName(output))
|
||||
if (faces.isDefinedAt(output)) {
|
||||
val bf = faces(output)
|
||||
bf.origin.block(te.getWorldObj) map { block =>
|
||||
tip += block.getLocalizedName
|
||||
tip += "%d, %d, %d - %s".format(bf.x, bf.y, bf.z, Misc.toLocal("bdlib.multiblock.face." + bf.face.toString.toLowerCase))
|
||||
}
|
||||
} else {
|
||||
tip += Misc.toLocal("bdlib.multiblock.disabled")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.gui
|
||||
|
||||
import net.bdew.lib.gui.widgets.{WidgetMultipane, WidgetSubcontainer}
|
||||
import net.bdew.lib.gui.{Rect, _}
|
||||
import net.bdew.lib.multiblock.data.OutputConfigPower
|
||||
import net.bdew.lib.multiblock.interact.CIOutputFaces
|
||||
|
||||
class WidgetOutputs(p: Point, te: CIOutputFaces, rows: Int) extends WidgetSubcontainer(new Rect(p, 92, 19 * rows)) {
|
||||
for (i <- 0 until rows)
|
||||
add(new WidgetOutputRow(Point(0, 19 * i), te, i))
|
||||
}
|
||||
|
||||
class WidgetOutputDisplay extends WidgetSubcontainer(Rect(20, 0, 72, 18))
|
||||
|
||||
class WidgetOutputRow(p: Point, te: CIOutputFaces, output: Int) extends WidgetMultipane(new Rect(p, 92, 18)) {
|
||||
add(new WidgetOutputIcon(Point(1, 1), te, output))
|
||||
val emptyPane = addPane(new WidgetSubcontainer(rect))
|
||||
val powerPane = addPane(new WidgetPowerOutput(te, output))
|
||||
|
||||
def getActivePane =
|
||||
te.outputConfig.get(output) match {
|
||||
case Some(x: OutputConfigPower) => powerPane
|
||||
case _ => emptyPane
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.gui
|
||||
|
||||
import java.text.DecimalFormat
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.gui.widgets.{WidgetButtonIcon, WidgetDynLabel}
|
||||
import net.bdew.lib.gui.{Color, Point}
|
||||
import net.bdew.lib.multiblock.data.{MsgOutputCfgPower, OutputConfigPower, RSMode}
|
||||
import net.bdew.lib.multiblock.interact.CIOutputFaces
|
||||
import net.bdew.lib.multiblock.network.NetHandler
|
||||
|
||||
class WidgetPowerOutput(te: CIOutputFaces, output: Int) extends WidgetOutputDisplay {
|
||||
def cfg = te.outputConfig(output).asInstanceOf[OutputConfigPower]
|
||||
val dec = new DecimalFormat("#,##0")
|
||||
val bt = add(new WidgetButtonIcon(Point(56, 1), clicked, te.resources.btBase, te.resources.btHover))
|
||||
add(new WidgetDynLabel("%s %s/t".format(dec.format(cfg.avg), cfg.unit), 1, 5, Color.darkgray))
|
||||
|
||||
val icons = Map(
|
||||
RSMode.ALWAYS -> te.resources.btEnabled,
|
||||
RSMode.NEVER -> te.resources.btDisabled,
|
||||
RSMode.RS_ON -> te.resources.btRsOn,
|
||||
RSMode.RS_OFF -> te.resources.btRsOff
|
||||
)
|
||||
|
||||
val next = Map(
|
||||
RSMode.ALWAYS -> RSMode.RS_ON,
|
||||
RSMode.RS_ON -> RSMode.RS_OFF,
|
||||
RSMode.RS_OFF -> RSMode.NEVER,
|
||||
RSMode.NEVER -> RSMode.ALWAYS
|
||||
)
|
||||
|
||||
override def draw(mouse: Point) {
|
||||
bt.icon = icons(cfg.rsMode)
|
||||
bt.hover = Misc.toLocal("bdlib.rsmode." + cfg.rsMode.toString.toLowerCase)
|
||||
super.draw(mouse)
|
||||
}
|
||||
|
||||
def clicked(b: WidgetButtonIcon) {
|
||||
NetHandler.sendToServer(MsgOutputCfgPower(output, next(cfg.rsMode)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.interact
|
||||
|
||||
import net.bdew.lib.multiblock.tile.TileController
|
||||
import net.minecraftforge.fluids.{Fluid, FluidStack, FluidTankInfo}
|
||||
|
||||
trait CIFluidInput extends TileController {
|
||||
def inputFluid(resource: FluidStack, doFill: Boolean): Int
|
||||
def canInputFluid(fluid: Fluid): Boolean
|
||||
def getTankInfo: Array[FluidTankInfo]
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.interact
|
||||
|
||||
import net.bdew.lib.block.{BlockFace, BlockRef}
|
||||
import net.bdew.lib.multiblock.data._
|
||||
import net.bdew.lib.multiblock.tile.{TileController, TileModule}
|
||||
import net.minecraft.util.ChatComponentTranslation
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
|
||||
trait CIOutputFaces extends TileController {
|
||||
val maxOutputs: Int
|
||||
|
||||
val outputFaces = new DataSlotBlockFaceMap("outputs", this)
|
||||
val outputConfig = new DataSlotOutputConfig("cfg", this, maxOutputs)
|
||||
|
||||
def newOutput(bp: BlockRef, face: ForgeDirection, cfg: OutputConfig): Int = {
|
||||
val bf = BlockFace(bp, face)
|
||||
if (outputFaces.contains(bf)) {
|
||||
println("Adding already registered output??? " + bf.toString)
|
||||
return outputFaces(bf)
|
||||
}
|
||||
val rv = outputFaces.inverted
|
||||
for (i <- 0 until maxOutputs if !rv.contains(i)) {
|
||||
outputFaces += (bf -> i)
|
||||
outputConfig += i -> cfg
|
||||
outputFaces.updated()
|
||||
return i
|
||||
}
|
||||
val pl = getWorldObj.getClosestPlayer(bf.x, bf.y, bf.z, 10)
|
||||
if (pl != null) pl.addChatMessage(new ChatComponentTranslation("bdlib.multiblock.toomanyoutputs"))
|
||||
return -1
|
||||
}
|
||||
|
||||
serverTick.listen(doOutputs)
|
||||
|
||||
override def moduleRemoved(module: TileModule) {
|
||||
for ((bf, n) <- outputFaces if bf.origin == module.mypos) {
|
||||
outputFaces -= bf
|
||||
outputConfig -= n
|
||||
}
|
||||
outputFaces.updated()
|
||||
outputConfig.updated()
|
||||
super.moduleRemoved(module)
|
||||
}
|
||||
|
||||
def doOutputs() {
|
||||
for ((x, n) <- outputFaces) {
|
||||
val t = x.origin.getTile[MIOutput](getWorldObj)
|
||||
if (t.isDefined) {
|
||||
if (!outputConfig.isDefinedAt(n) || outputConfig(n).isInstanceOf[OutputConfigInvalid])
|
||||
outputConfig(n) = t.get.makeCfgObject(x.face)
|
||||
t.get.doOutput(x.face, outputConfig(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def removeOutput(bp: BlockRef, face: ForgeDirection) {
|
||||
val bf = BlockFace(bp, face)
|
||||
outputConfig -= outputFaces(bf)
|
||||
outputFaces -= bf
|
||||
outputFaces.updated()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.interact
|
||||
|
||||
trait CIPowerProducer extends CIOutputFaces {
|
||||
def extract(v: Float, simulate: Boolean): Float
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.interact
|
||||
|
||||
import net.minecraft.inventory.Container
|
||||
|
||||
trait ContainerOutputFaces extends Container {
|
||||
def te: CIOutputFaces
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.interact
|
||||
|
||||
import net.bdew.lib.multiblock.data.OutputConfig
|
||||
import net.bdew.lib.multiblock.tile.TileModule
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
|
||||
trait MIOutput extends TileModule {
|
||||
def doOutput(face: ForgeDirection, cfg: OutputConfig)
|
||||
def makeCfgObject(face: ForgeDirection): OutputConfig
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.network
|
||||
|
||||
import net.bdew.lib.network.Message
|
||||
|
||||
abstract class MsgOutputCfg extends Message {
|
||||
val output: Int
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.network
|
||||
|
||||
import net.bdew.lib.multiblock.interact.ContainerOutputFaces
|
||||
import net.bdew.lib.network.NetChannel
|
||||
import net.minecraft.entity.player.EntityPlayerMP
|
||||
|
||||
object NetHandler extends NetChannel("bdew.multiblock") {
|
||||
regServerHandler {
|
||||
case (msg: MsgOutputCfg, p: EntityPlayerMP) =>
|
||||
val te = p.openContainer.asInstanceOf[ContainerOutputFaces].te
|
||||
te.outputConfig(msg.output).handleConfigPacket(msg)
|
||||
te.outputConfig.updated()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.tile
|
||||
|
||||
import net.bdew.lib.BdLib
|
||||
import net.bdew.lib.block.BlockRef
|
||||
import net.bdew.lib.data.base.{TileDataSlots, UpdateKind}
|
||||
import net.bdew.lib.multiblock.block.BlockModule
|
||||
import net.bdew.lib.multiblock.data.DataSlotPosSet
|
||||
import net.bdew.lib.multiblock.{MachineCore, ResourceProvider, Tools}
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
|
||||
trait TileController extends TileDataSlots {
|
||||
val modules = new DataSlotPosSet("modules", this).setUpdate(UpdateKind.WORLD, UpdateKind.SAVE, UpdateKind.RENDER)
|
||||
|
||||
def resources: ResourceProvider
|
||||
def cfg: MachineCore
|
||||
|
||||
var acceptNewModules = true
|
||||
var revalidateOnNextTick = true
|
||||
var modulesChanged = true
|
||||
|
||||
lazy val mypos = BlockRef(xCoord, yCoord, zCoord)
|
||||
|
||||
def getNumOfMoudules(kind: String) = modules.map(_.getTile[TileModule](getWorldObj)).flatten.count(_.kind == kind)
|
||||
|
||||
def onModulesChanged()
|
||||
def onClick(player: EntityPlayer)
|
||||
|
||||
def moduleConnected(module: TileModule): Boolean = {
|
||||
if (acceptNewModules) {
|
||||
modules.add(BlockRef(module.xCoord, module.yCoord, module.zCoord))
|
||||
getWorldObj.markBlockForUpdate(xCoord, yCoord, zCoord)
|
||||
modulesChanged = true
|
||||
return true
|
||||
} else false
|
||||
}
|
||||
|
||||
def moduleRemoved(module: TileModule) {
|
||||
modules.remove(BlockRef(module.xCoord, module.yCoord, module.zCoord))
|
||||
getWorldObj.markBlockForUpdate(xCoord, yCoord, zCoord)
|
||||
revalidateOnNextTick = true
|
||||
modulesChanged = true
|
||||
}
|
||||
|
||||
def onBreak() {
|
||||
acceptNewModules = false
|
||||
for (x <- modules.flatMap(_.getTile[TileModule](getWorldObj)))
|
||||
x.coreRemoved()
|
||||
modules.clear()
|
||||
}
|
||||
|
||||
def validateModules() {
|
||||
modules.filter(x => {
|
||||
!x.getTile[TileModule](getWorldObj).isDefined || !x.getBlock[BlockModule[TileModule]](getWorldObj).isDefined
|
||||
}).foreach(x => {
|
||||
BdLib.logWarn("Block at %s is not a valid module, removing from machine %s at %d,%d,%d", x, this.getClass.getSimpleName, xCoord, yCoord, zCoord)
|
||||
modules.remove(x)
|
||||
})
|
||||
val reachable = Tools.findReachableModules(getWorldObj, mypos)
|
||||
val toremove = modules.filterNot(reachable.contains).flatMap(_.getTile[TileModule](getWorldObj))
|
||||
acceptNewModules = false
|
||||
toremove.foreach(moduleRemoved)
|
||||
toremove.foreach(_.coreRemoved())
|
||||
acceptNewModules = true
|
||||
modulesChanged = true
|
||||
modules.map(x =>
|
||||
(x.getBlock[BlockModule[TileModule]](getWorldObj).orNull, x.getTile[TileModule](getWorldObj).orNull)
|
||||
).filter({ case (a, b) => a.kind != b.kind }).foreach({ case (a, b) => sys.error("Type mismatch between Block/Tile Block=%s(%s) Tile=%s(%s)".format(a.kind, a, b.kind, b)) })
|
||||
}
|
||||
|
||||
serverTick.listen(() => {
|
||||
if (revalidateOnNextTick) {
|
||||
revalidateOnNextTick = false
|
||||
validateModules()
|
||||
}
|
||||
if (modulesChanged) {
|
||||
modulesChanged = false
|
||||
onModulesChanged()
|
||||
lastChange = getWorldObj.getTotalWorldTime
|
||||
getWorldObj.markBlockForUpdate(xCoord, yCoord, zCoord)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.tile
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.util.{ChatComponentTranslation, ChatStyle, EnumChatFormatting}
|
||||
|
||||
abstract class TileControllerGui extends TileController {
|
||||
def openGui(player: EntityPlayer)
|
||||
|
||||
def onClick(player: EntityPlayer) = {
|
||||
val missing = cfg.required.filter({ case (mod, cnt) => getNumOfMoudules(mod) < cnt })
|
||||
if (missing.size > 0) {
|
||||
player.addChatMessage(new ChatComponentTranslation("bdlib.multiblock.incomplete")
|
||||
.setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)))
|
||||
for ((mod, cnt) <- missing)
|
||||
player.addChatMessage(
|
||||
new ChatComponentTranslation("- %s %s", Integer.valueOf(cnt),
|
||||
new ChatComponentTranslation(resources.getModuleName(mod)))
|
||||
.setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)))
|
||||
} else openGui(player)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.tile
|
||||
|
||||
import net.bdew.lib.block.BlockRef
|
||||
import net.bdew.lib.data.base.{TileDataSlots, UpdateKind}
|
||||
import net.bdew.lib.multiblock.Tools
|
||||
import net.bdew.lib.multiblock.data.DataSlotPos
|
||||
|
||||
import scala.reflect.ClassTag
|
||||
|
||||
trait TileModule extends TileDataSlots {
|
||||
val kind: String
|
||||
val connected = new DataSlotPos("connected", this).setUpdate(UpdateKind.WORLD, UpdateKind.SAVE, UpdateKind.RENDER)
|
||||
|
||||
def getCoreAs[T: ClassTag] = connected flatMap (_.getTile[TileController with T](getWorldObj))
|
||||
def getCore = connected flatMap (_.getTile[TileController](getWorldObj))
|
||||
|
||||
lazy val mypos = BlockRef(xCoord, yCoord, zCoord)
|
||||
|
||||
def connect(target: TileController) {
|
||||
if (target.moduleConnected(this)) {
|
||||
connected.set(target.mypos)
|
||||
getWorldObj.markBlockForUpdate(xCoord, yCoord, zCoord)
|
||||
getWorldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType)
|
||||
}
|
||||
}
|
||||
|
||||
def coreRemoved() {
|
||||
connected.unset()
|
||||
getWorldObj.markBlockForUpdate(xCoord, yCoord, zCoord)
|
||||
getWorldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType)
|
||||
}
|
||||
|
||||
def onBreak() {
|
||||
getCore map (_.moduleRemoved(this))
|
||||
}
|
||||
|
||||
def tryConnect() {
|
||||
if (getCore.isEmpty) {
|
||||
for {
|
||||
conn <- Tools.findConnections(getWorldObj, mypos, kind).headOption
|
||||
core <- conn.getTile[TileController](getWorldObj)
|
||||
} {
|
||||
connect(core)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.multiblock.tile
|
||||
|
||||
import net.bdew.lib.multiblock.data.{OutputConfigPower, RSMode}
|
||||
import net.bdew.lib.multiblock.interact.{CIOutputFaces, MIOutput}
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
|
||||
abstract class TileOutput extends TileModule with MIOutput {
|
||||
val unit: String
|
||||
|
||||
override def getCore = getCoreAs[CIOutputFaces]
|
||||
|
||||
def checkCanOutput(cfg: OutputConfigPower): Boolean = {
|
||||
if (cfg.rsMode == RSMode.ALWAYS) return true
|
||||
if (cfg.rsMode == RSMode.NEVER) return false
|
||||
return worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord) ^ (cfg.rsMode == RSMode.RS_OFF)
|
||||
}
|
||||
|
||||
def makeCfgObject(face: ForgeDirection) = {
|
||||
val cfg = new OutputConfigPower
|
||||
cfg.unit = unit
|
||||
cfg
|
||||
}
|
||||
|
||||
var rescanFaces = false
|
||||
|
||||
serverTick.listen(() => {
|
||||
if (rescanFaces) {
|
||||
rescanFaces = false
|
||||
doRescanFaces()
|
||||
}
|
||||
})
|
||||
|
||||
override def tryConnect() {
|
||||
super.tryConnect()
|
||||
if (connected.isDefined) rescanFaces = true
|
||||
}
|
||||
|
||||
def canConnectoToFace(d: ForgeDirection): Boolean
|
||||
|
||||
def onConnectionsChanged(added: Set[ForgeDirection], removed: Set[ForgeDirection]) {}
|
||||
|
||||
def doRescanFaces() {
|
||||
getCore map { core =>
|
||||
val connections = ForgeDirection.VALID_DIRECTIONS.filter(canConnectoToFace).toSet
|
||||
val known = core.outputFaces.filter(_._1.origin == mypos).map(_._1.face).toSet
|
||||
val toAdd = connections -- known
|
||||
val toRemove = known -- connections
|
||||
toRemove.foreach(x => core.removeOutput(mypos, x))
|
||||
toAdd.foreach(x => core.newOutput(mypos, x, makeCfgObject(x)))
|
||||
if (toAdd.size > 0 || toRemove.size > 0)
|
||||
onConnectionsChanged(toAdd, toRemove)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user