From 26574bf9b64ce720a2db733f51a832d93035aa0d Mon Sep 17 00:00:00 2001 From: Johannes Lohrer Date: Sat, 5 Oct 2013 14:21:46 +0200 Subject: [PATCH] basic computer network just registering for now --- javax/annotation/Nullable.java | 4 +- li/cil/oc/Blocks.scala | 3 +- li/cil/oc/api/network/PoweredNode.scala | 30 ++++++++ li/cil/oc/common/block/PowerDistributer.scala | 30 ++++++++ .../common/component/ScreenEnvironment.scala | 4 +- .../tileentity/ComponentInventory.scala | 4 +- .../common/tileentity/PowerDistributer.scala | 72 +++++++++++++++++++ li/cil/oc/common/tileentity/PowerSupply.scala | 11 ++- 8 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 li/cil/oc/api/network/PoweredNode.scala create mode 100644 li/cil/oc/common/block/PowerDistributer.scala create mode 100644 li/cil/oc/common/tileentity/PowerDistributer.scala diff --git a/javax/annotation/Nullable.java b/javax/annotation/Nullable.java index d31993dbd..fdcb56e86 100644 --- a/javax/annotation/Nullable.java +++ b/javax/annotation/Nullable.java @@ -4,11 +4,11 @@ import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import javax.annotation.meta.TypeQualifierNickname; +//import javax.annotation.meta.TypeQualifierNickname; import javax.annotation.meta.When; @Documented -@TypeQualifierNickname +//@TypeQualifierNickname @Nonnull(when = When.UNKNOWN) @Retention(RetentionPolicy.RUNTIME) public @interface Nullable { diff --git a/li/cil/oc/Blocks.scala b/li/cil/oc/Blocks.scala index 663e75305..fd38d59a3 100644 --- a/li/cil/oc/Blocks.scala +++ b/li/cil/oc/Blocks.scala @@ -9,7 +9,7 @@ object Blocks { var screen: Screen = null var keyboard: Keyboard = null var powersupply: PowerSupply = null - + var powerdistributer: PowerDistributer = null def init() { // IMPORTANT: the multi block must come first, since the sub blocks will // try to register with it. Also, the order the sub blocks are created in @@ -21,5 +21,6 @@ object Blocks { screen = new Screen(blockSimple) keyboard = new Keyboard(blockSpecial) powersupply = new PowerSupply(blockSimple) + powerdistributer = new PowerDistributer(blockSimple) } } \ No newline at end of file diff --git a/li/cil/oc/api/network/PoweredNode.scala b/li/cil/oc/api/network/PoweredNode.scala new file mode 100644 index 000000000..8946b4851 --- /dev/null +++ b/li/cil/oc/api/network/PoweredNode.scala @@ -0,0 +1,30 @@ +package li.cil.oc.api.network + +/** + * Created with IntelliJ IDEA. + * User: lordjoda + * Date: 04.10.13 + * Time: 17:29 + * To change this template use File | Settings | File Templates. + */ +trait PoweredNode extends Node{ + var main:Node = null + var demand = 2; + override def receive(message: Message): Option[Array[Any]] = { + val ret = super.receive(message) + message.name match { + case "power.connect" => { + println("connect") + if(main != message.source){ + println("setting main") + main = message.source + network.foreach(_.sendToAddress(this,message.source.address.get,"power.request",demand)) + } + + } + case "network.disconnect"=> {if(message.source == main)main = null} + case _ => // Ignore. + } + return ret + } +} diff --git a/li/cil/oc/common/block/PowerDistributer.scala b/li/cil/oc/common/block/PowerDistributer.scala new file mode 100644 index 000000000..d72d5ab6b --- /dev/null +++ b/li/cil/oc/common/block/PowerDistributer.scala @@ -0,0 +1,30 @@ +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 + * Date: 03.10.13 + * Time: 19:48 + * To change this template use File | Settings | File Templates. + */ +class PowerDistributer (val parent: Delegator) extends Delegate { + GameRegistry.registerTileEntity(classOf[tileentity.PowerDistributer], "oc.powerdistributer" ) + val unlocalizedName = "PowerDistributer" + + override def breakBlock(world: World, x: Int, y: Int, z: Int, blockId: Int, metadata: Int) = { + //world.getBlockTileEntity(x, y, z).asInstanceOf[tileentity.PowerDistributer] + super.breakBlock(world, x, y, z, blockId, metadata) + } + // ----------------------------------------------------------------------- // + // Tile entity + // ----------------------------------------------------------------------- // + + override def hasTileEntity = true + + override def createTileEntity(world: World, metadata: Int) = Some(new tileentity.PowerDistributer) +} + diff --git a/li/cil/oc/common/component/ScreenEnvironment.scala b/li/cil/oc/common/component/ScreenEnvironment.scala index 02fa05b25..3b7cc6b7e 100644 --- a/li/cil/oc/common/component/ScreenEnvironment.scala +++ b/li/cil/oc/common/component/ScreenEnvironment.scala @@ -1,6 +1,6 @@ package li.cil.oc.common.component -import li.cil.oc.api.network.{Visibility, Node, Message} +import li.cil.oc.api.network.{PoweredNode, Visibility, Node, Message} import net.minecraft.nbt.NBTTagCompound /** @@ -10,7 +10,7 @@ import net.minecraft.nbt.NBTTagCompound * between server and client. These callbacks are only called on the server * side to trigger changes being sent to clients and saving the current state. */ -trait ScreenEnvironment extends Node { +trait ScreenEnvironment extends PoweredNode { val screen = new Screen(this) override def name = "screen" diff --git a/li/cil/oc/common/tileentity/ComponentInventory.scala b/li/cil/oc/common/tileentity/ComponentInventory.scala index 20bd11c5b..9c484fc6e 100644 --- a/li/cil/oc/common/tileentity/ComponentInventory.scala +++ b/li/cil/oc/common/tileentity/ComponentInventory.scala @@ -2,7 +2,7 @@ package li.cil.oc.common.tileentity import li.cil.oc.Items import li.cil.oc.api.driver.Slot -import li.cil.oc.api.network.Node +import li.cil.oc.api.network.{PoweredNode, Node} import li.cil.oc.common.item import li.cil.oc.server.component import li.cil.oc.server.driver.Registry @@ -12,7 +12,7 @@ import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagList import net.minecraft.world.World -trait ComponentInventory extends IInventory with Node { +trait ComponentInventory extends IInventory with PoweredNode { protected val inventory = new Array[ItemStack](inventorySize) protected val itemComponents = Array.fill[Option[Node]](inventorySize)(None) diff --git a/li/cil/oc/common/tileentity/PowerDistributer.scala b/li/cil/oc/common/tileentity/PowerDistributer.scala new file mode 100644 index 000000000..4413ff7e3 --- /dev/null +++ b/li/cil/oc/common/tileentity/PowerDistributer.scala @@ -0,0 +1,72 @@ +package li.cil.oc.common.tileentity + +import li.cil.oc.api.network.{PoweredNode, Message, Visibility, Node} + +/** + * Created with IntelliJ IDEA. + * User: lordjoda + * Date: 03.10.13 + * Time: 19:51 + * To change this template use File | Settings | File Templates. + */ +class PowerDistributer extends Rotatable with PoweredNode { + + var powerDemand:Int = 0 + override def name = "powerdistributer" + + override def visibility = Visibility.Network + + + override def receive(message: Message): Option[Array[Any]] = { + + message.name match { + case "network.disconnect"=> { + println("recieved disc") + if(message.source == main){ + main = this + network.foreach(_.sendToAddress(this,address.get,"power.request",demand)) + network.foreach(_.sendToVisible(this, "power.connect")) + } + } + case _ => // Ignore. + } + val ret = super.receive(message) + message.name match { + case "network.connect"=>{ + if(main==this){ + network.foreach(_.sendToAddress(this,message.source.address.get,"power.connect")) + } + } + case "power.find"=>{ + if(main==this){ + network.foreach(_.sendToAddress(this,message.source.address.get,"power.connect")) + message.cancel() + } + } + case "power.request"=>{ + println("recieved power request") + if(main == this){ + println("this is main") + message.data match { + case Array(value:Int)=> { + powerDemand+=value + println("now demanding "+powerDemand) + } + case _ => // Ignore. + } + } + } + case _ => // Ignore. + } + return ret + } + + override protected def onConnect() { + network.foreach(_.sendToVisible(this, "power.find")) + if(main==null) + { main = this + network.foreach(_.sendToAddress(this,address.get,"power.request",demand)) + } + super.onConnect() + } +} diff --git a/li/cil/oc/common/tileentity/PowerSupply.scala b/li/cil/oc/common/tileentity/PowerSupply.scala index fac34a31e..e30554a40 100644 --- a/li/cil/oc/common/tileentity/PowerSupply.scala +++ b/li/cil/oc/common/tileentity/PowerSupply.scala @@ -1,7 +1,7 @@ package li.cil.oc.common.tileentity import net.minecraft.tileentity.TileEntity -import li.cil.oc.api.network.{Visibility, Node} +import li.cil.oc.api.network.{PoweredNode, Visibility, Node} import net.minecraftforge.common.{ForgeDirection, MinecraftForge} import ic2.api.energy.event.{EnergyTileLoadEvent, EnergyTileUnloadEvent} import cpw.mods.fml.common.FMLCommonHandler @@ -9,7 +9,6 @@ import ic2.api.energy.tile.IEnergySink import buildcraft.api.power.{PowerHandler, IPowerReceptor} import net.minecraft.world.World import net.minecraft.nbt.NBTTagCompound -import buildcraft.api.power.PowerHandler.PerditionCalculator import universalelectricity.core.block.IElectrical import universalelectricity.core.electricity.ElectricityPack @@ -20,7 +19,7 @@ import universalelectricity.core.electricity.ElectricityPack * Time: 20:37 * To change this template use File | Settings | File Templates. */ -class PowerSupply extends Rotatable with Node with IEnergySink with IPowerReceptor with IElectrical{ +class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowerReceptor with IElectrical{ var addedToEnet = false var powerHandler:PowerHandler = null override def name = "powersupply" @@ -149,7 +148,7 @@ class PowerSupply extends Rotatable with Node with IEnergySink with IPowerRecept */ def getPowerReceiver(side: ForgeDirection): PowerHandler#PowerReceiver={ - return getPowerProvider() .getPowerReceiver + return getPowerProvider().getPowerReceiver } def getPowerProvider():PowerHandler= { @@ -171,9 +170,7 @@ class PowerSupply extends Rotatable with Node with IEnergySink with IPowerRecept * @param workProvider */ def doWork(workProvider: PowerHandler){ - println("do work") - storedEnergy+=getPowerProvider().useEnergy(1,MAXENERGY-storedEnergy.toFloat,true) - println("stored: "+storedEnergy) + } def getWorld: World=worldObj