added energy net integration to powersupply

This commit is contained in:
Johannes Lohrer 2013-09-30 22:18:48 +02:00
parent b5d1bef7d3
commit b2093a9066
4 changed files with 121 additions and 3 deletions

View File

@ -8,6 +8,7 @@ object Blocks {
var computer: Computer = null
var screen: Screen = null
var keyboard: Keyboard = null
var powersupply: PowerSupply = null
def init() {
// IMPORTANT: the multi block must come first, since the sub blocks will
@ -19,5 +20,6 @@ object Blocks {
computer = new Computer(blockSimple)
screen = new Screen(blockSimple)
keyboard = new Keyboard(blockSpecial)
powersupply = new PowerSupply(blockSimple)
}
}

View File

@ -1,5 +1,9 @@
package li.cil.oc.common.block
import cpw.mods.fml.common.registry.GameRegistry
import li.cil.oc.common.tileentity
import net.minecraft.world.World
/**
* Created with IntelliJ IDEA.
* User: lordjoda
@ -7,6 +11,19 @@ package li.cil.oc.common.block
* Time: 20:36
* To change this template use File | Settings | File Templates.
*/
class PowerSupply {
class PowerSupply(val parent: Delegator) extends Delegate {
GameRegistry.registerTileEntity(classOf[tileentity.PowerSupply], "oc.powersupply" )
val unlocalizedName = "PowerSupply"
override def breakBlock(world: World, x: Int, y: Int, z: Int, blockId: Int, metadata: Int) = {
world.getBlockTileEntity(x, y, z).asInstanceOf[tileentity.PowerSupply].onUnload()
super.breakBlock(world, x, y, z, blockId, metadata)
}
// ----------------------------------------------------------------------- //
// Tile entity
// ----------------------------------------------------------------------- //
override def hasTileEntity = true
override def createTileEntity(world: World, metadata: Int) = Some(new tileentity.PowerSupply)
}

View File

@ -92,7 +92,7 @@ class Computer(isClient: Boolean) extends Rotatable with ComputerEnvironment wit
override def updateEntity() = if (!worldObj.isRemote) {
computer.update()
if (hasChanged.get) {
worldObj.updateTileEntityChunkAndDoNothing(xCoord, yCoord, zCoord, this)
worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this)
}
if (isRunning != computer.isRunning) {
isRunning = computer.isRunning

View File

@ -1,5 +1,12 @@
package li.cil.oc.common.tileentity
import net.minecraft.tileentity.TileEntity
import li.cil.oc.api.network.{Visibility, Node}
import net.minecraftforge.common.{ForgeDirection, MinecraftForge}
import ic2.api.energy.event.{EnergyTileLoadEvent, EnergyTileUnloadEvent}
import cpw.mods.fml.common.FMLCommonHandler
import ic2.api.energy.tile.IEnergySink
/**
* Created with IntelliJ IDEA.
* User: lordjoda
@ -7,6 +14,98 @@ package li.cil.oc.common.tileentity
* Time: 20:37
* To change this template use File | Settings | File Templates.
*/
class PowerSupply {
class PowerSupply extends Rotatable with Node with IEnergySink{
var addedToEnet = false
override def name = "powersupply"
override def visibility = Visibility.Network
override def onChunkUnload(){
super.onChunkUnload()
onUnload()
}
def onUnload(){
if(addedToEnet){
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this))
addedToEnet = false
}
}
override def updateEntity(){
super.updateEntity()
if(!addedToEnet) {
onLoaded()
}
}
/**
* Notification that the TileEntity finished loaded, for advanced uses.
* Either onUpdateEntity or onLoaded have to be used.
*/
def onLoaded() {
if (!addedToEnet && !FMLCommonHandler.instance.getEffectiveSide.isClient) {
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this))
addedToEnet = true
}
}
var storedEnergy = 0.0;
var lastInjectedEnergy =0.0;
var MAXENERGY = 1000;
//IC2 stuff
/**
* Determine how much energy the sink accepts.
*
* This value is unrelated to getMaxSafeInput().
*
* Make sure that injectEnergy() does accepts energy if demandsEnergy() returns anything > 0.
*
* @return max accepted input in eu
*/
override def demandedEnergyUnits: Double={
val needed = MAXENERGY-storedEnergy
if(needed>lastInjectedEnergy||needed>MAXENERGY/2)
return needed
0
}
/**
* Transfer energy to the sink.
*
* It's highly recommended to accept all energy by letting the internal buffer overflow to
* increase the performance and accuracy of the distribution simulation.
*
* @param directionFrom direction from which the energy comes from
* @param amount energy to be transferred
* @return Energy not consumed (leftover)
*/
override def injectEnergyUnits(directionFrom: ForgeDirection, amount: Double): Double ={
lastInjectedEnergy = amount;
storedEnergy+=amount;
0
}
/**
* Determine the amount of eu which can be safely injected into the specific energy sink without exploding.
*
* Typical values are 32 for LV, 128 for MV, 512 for HV and 2048 for EV. A value of Integer.MAX_VALUE indicates no
* limit.
*
* This value is unrelated to demandsEnergy().
*
* @return max safe input in eu
*/
override def getMaxSafeInput: Int =Integer.MAX_VALUE
/**
* Determine if this acceptor can accept current from an adjacent emitter in a direction.
*
* The TileEntity in the emitter parameter is what was originally added to the energy net,
* which may be normal in-world TileEntity, a delegate or an IMetaDelegate.
*
* @param emitter energy emitter
* @param direction direction the energy is being received from
*/
override def acceptsEnergyFrom(emitter: TileEntity, direction: ForgeDirection): Boolean = true
}