From 7e94dbee685b409d4130dd4b924f821e89cc4e99 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Sun, 16 Feb 2014 21:11:11 +0800 Subject: [PATCH] Solar panels now act as multiblock --- .../electrical/battery/BatteryNetwork.java | 61 --------- .../electrical/battery/BlockBattery.java | 30 ++-- .../battery/EnergyDistributionNetwork.java | 61 +++++++++ .../electrical/battery/TileBattery.java | 110 +-------------- .../battery/TileEnergyDistribution.java | 128 ++++++++++++++++++ .../generator/solar/BlockSolarPanel.java | 24 ++++ .../generator/solar/TileSolarPanel.java | 12 +- 7 files changed, 238 insertions(+), 188 deletions(-) delete mode 100644 electrical/src/main/java/resonantinduction/electrical/battery/BatteryNetwork.java create mode 100644 electrical/src/main/java/resonantinduction/electrical/battery/EnergyDistributionNetwork.java create mode 100644 electrical/src/main/java/resonantinduction/electrical/battery/TileEnergyDistribution.java diff --git a/electrical/src/main/java/resonantinduction/electrical/battery/BatteryNetwork.java b/electrical/src/main/java/resonantinduction/electrical/battery/BatteryNetwork.java deleted file mode 100644 index 60836748..00000000 --- a/electrical/src/main/java/resonantinduction/electrical/battery/BatteryNetwork.java +++ /dev/null @@ -1,61 +0,0 @@ -package resonantinduction.electrical.battery; - -import java.util.Arrays; - -import universalelectricity.core.net.Network; - -public class BatteryNetwork extends Network -{ - public void redistribute(TileBattery... exclusion) - { - long totalEnergy = 0; - long totalCapacity = 0; - - for (TileBattery battery : this.getConnectors()) - { - totalEnergy += battery.energy.getEnergy(); - totalCapacity += battery.energy.getEnergyCapacity(); - } - - /** - * Apply energy loss. - */ - double percentageLoss = Math.max(0, (1 - (getConnectors().size() * 6 / 100d))); - long energyLoss = (long) (percentageLoss * 100); - totalEnergy -= energyLoss; - - int amountOfNodes = this.getConnectors().size() - exclusion.length; - - if (totalEnergy > 0 && amountOfNodes > 0) - { - long remainingEnergy = totalEnergy; - - TileBattery firstNode = this.getFirstConnector(); - - for (TileBattery battery : this.getConnectors()) - { - if (battery != firstNode && !Arrays.asList(exclusion).contains(battery)) - { - double percentage = ((double) battery.energy.getEnergyCapacity() / (double) totalCapacity); - long energyForBattery = Math.round(totalEnergy * percentage); - battery.energy.setEnergy(energyForBattery); - remainingEnergy -= energyForBattery; - } - } - - firstNode.energy.setEnergy(remainingEnergy); - } - } - - @Override - protected void reconstructConnector(TileBattery node) - { - node.setNetwork(this); - } - - @Override - public BatteryNetwork newInstance() - { - return new BatteryNetwork(); - } -} diff --git a/electrical/src/main/java/resonantinduction/electrical/battery/BlockBattery.java b/electrical/src/main/java/resonantinduction/electrical/battery/BlockBattery.java index 4cde33c5..011d9e76 100644 --- a/electrical/src/main/java/resonantinduction/electrical/battery/BlockBattery.java +++ b/electrical/src/main/java/resonantinduction/electrical/battery/BlockBattery.java @@ -40,8 +40,21 @@ public class BlockBattery extends BlockSidedIO implements ITileEntityProvider { if (!world.isRemote) { - TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z); - battery.updateStructure(); + TileEnergyDistribution distribution = (TileEnergyDistribution) world.getBlockTileEntity(x, y, z); + distribution.updateStructure(); + } + } + + @Override + public void onNeighborBlockChange(World world, int x, int y, int z, int id) + { + if (!world.isRemote) + { + if (id == blockID) + { + TileEnergyDistribution distribution = (TileEnergyDistribution) world.getBlockTileEntity(x, y, z); + distribution.updateStructure(); + } } } @@ -59,19 +72,6 @@ public class BlockBattery extends BlockSidedIO implements ITileEntityProvider } } - @Override - public void onNeighborBlockChange(World world, int x, int y, int z, int id) - { - if (!world.isRemote) - { - if (id == blockID) - { - TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z); - battery.updateStructure(); - } - } - } - @Override public boolean onSneakUseWrench(World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ) { diff --git a/electrical/src/main/java/resonantinduction/electrical/battery/EnergyDistributionNetwork.java b/electrical/src/main/java/resonantinduction/electrical/battery/EnergyDistributionNetwork.java new file mode 100644 index 00000000..f3afdb0b --- /dev/null +++ b/electrical/src/main/java/resonantinduction/electrical/battery/EnergyDistributionNetwork.java @@ -0,0 +1,61 @@ +package resonantinduction.electrical.battery; + +import java.util.Arrays; + +import universalelectricity.core.net.Network; + +public class EnergyDistributionNetwork extends Network +{ + public void redistribute(TileEnergyDistribution... exclusion) + { + long totalEnergy = 0; + long totalCapacity = 0; + + for (TileEnergyDistribution energyContainer : this.getConnectors()) + { + totalEnergy += energyContainer.energy.getEnergy(); + totalCapacity += energyContainer.energy.getEnergyCapacity(); + } + + /** + * Apply energy loss. + */ + double percentageLoss = Math.max(0, (1 - (getConnectors().size() * 6 / 100d))); + long energyLoss = (long) (percentageLoss * 100); + totalEnergy -= energyLoss; + + int amountOfNodes = this.getConnectors().size() - exclusion.length; + + if (totalEnergy > 0 && amountOfNodes > 0) + { + long remainingEnergy = totalEnergy; + + TileEnergyDistribution firstNode = this.getFirstConnector(); + + for (TileEnergyDistribution node : this.getConnectors()) + { + if (node != firstNode && !Arrays.asList(exclusion).contains(node)) + { + double percentage = ((double) node.energy.getEnergyCapacity() / (double) totalCapacity); + long energyForBattery = Math.round(totalEnergy * percentage); + node.energy.setEnergy(energyForBattery); + remainingEnergy -= energyForBattery; + } + } + + firstNode.energy.setEnergy(remainingEnergy); + } + } + + @Override + protected void reconstructConnector(TileEnergyDistribution node) + { + node.setNetwork(this); + } + + @Override + public EnergyDistributionNetwork newInstance() + { + return new EnergyDistributionNetwork(); + } +} diff --git a/electrical/src/main/java/resonantinduction/electrical/battery/TileBattery.java b/electrical/src/main/java/resonantinduction/electrical/battery/TileBattery.java index 7185e64b..cbe925b2 100644 --- a/electrical/src/main/java/resonantinduction/electrical/battery/TileBattery.java +++ b/electrical/src/main/java/resonantinduction/electrical/battery/TileBattery.java @@ -26,7 +26,7 @@ import com.google.common.io.ByteArrayDataInput; * * @author Calclavia */ -public class TileBattery extends TileElectrical implements IConnector, IVoltageInput, IVoltageOutput, IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer +public class TileBattery extends TileEnergyDistribution implements IVoltageInput, IVoltageOutput, IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer { /** * Tiers: 0, 1, 2 @@ -39,11 +39,6 @@ public class TileBattery extends TileElectrical implements IConnector 0) && ticks % 5 == 0) - { - getNetwork().redistribute(); - markDistributionUpdate = false; - } - - if (markClientUpdate && ticks % 5 == 0) - { - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); - } + markDistributionUpdate |= produce() > 0; } - } - @Override - public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) - { - long returnValue = super.onReceiveEnergy(from, receive, doReceive); - markDistributionUpdate = true; - markClientUpdate = true; - return returnValue; - } - - @Override - public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract) - { - long returnValue = super.onExtractEnergy(from, extract, doExtract); - markDistributionUpdate = true; - markClientUpdate = true; - return returnValue; + super.updateEntity(); } @Override @@ -147,50 +95,6 @@ public class TileBattery extends TileElectrical implements IConnector getInstance(ForgeDirection from) - { - return this; - } - @Override public void setIO(ForgeDirection dir, int type) { diff --git a/electrical/src/main/java/resonantinduction/electrical/battery/TileEnergyDistribution.java b/electrical/src/main/java/resonantinduction/electrical/battery/TileEnergyDistribution.java new file mode 100644 index 00000000..fe3c7579 --- /dev/null +++ b/electrical/src/main/java/resonantinduction/electrical/battery/TileEnergyDistribution.java @@ -0,0 +1,128 @@ +package resonantinduction.electrical.battery; + +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.ForgeDirection; +import universalelectricity.api.net.IConnector; +import universalelectricity.api.vector.Vector3; +import calclavia.lib.prefab.tile.TileElectrical; + +public class TileEnergyDistribution extends TileElectrical implements IConnector +{ + private EnergyDistributionNetwork network; + + public boolean markClientUpdate = false; + public boolean markDistributionUpdate = false; + + @Override + public void initiate() + { + super.initiate(); + this.updateStructure(); + } + + @Override + public void updateEntity() + { + super.updateEntity(); + + if (!this.worldObj.isRemote) + { + if (markDistributionUpdate && ticks % 5 == 0) + { + getNetwork().redistribute(); + markDistributionUpdate = false; + } + + if (markClientUpdate && ticks % 5 == 0) + { + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } + } + } + + @Override + public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) + { + long returnValue = super.onReceiveEnergy(from, receive, doReceive); + markDistributionUpdate = true; + markClientUpdate = true; + return returnValue; + } + + @Override + public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract) + { + long returnValue = super.onExtractEnergy(from, extract, doExtract); + markDistributionUpdate = true; + markClientUpdate = true; + return returnValue; + } + + @Override + public EnergyDistributionNetwork getNetwork() + { + if (this.network == null) + { + this.network = new EnergyDistributionNetwork(); + this.network.addConnector(this); + } + + return this.network; + } + + @Override + public void setNetwork(EnergyDistributionNetwork structure) + { + this.network = structure; + } + + public void updateStructure() + { + if (!this.worldObj.isRemote) + { + for (Object obj : getConnections()) + { + if (obj != null) + { + this.getNetwork().merge(((TileEnergyDistribution) obj).getNetwork()); + } + } + + markDistributionUpdate = true; + markClientUpdate = true; + } + } + + @Override + public Object[] getConnections() + { + Object[] connections = new Object[6]; + + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = new Vector3(this).translate(dir).getTileEntity(this.worldObj); + + if (tile != null && tile.getClass() == this.getClass()) + { + connections[dir.ordinal()] = tile; + } + } + + return connections; + } + + @Override + public void invalidate() + { + this.getNetwork().redistribute(this); + this.getNetwork().split(this); + super.invalidate(); + } + + @Override + public IConnector getInstance(ForgeDirection from) + { + return this; + } + +} diff --git a/electrical/src/main/java/resonantinduction/electrical/generator/solar/BlockSolarPanel.java b/electrical/src/main/java/resonantinduction/electrical/generator/solar/BlockSolarPanel.java index 2a8775d5..922e3672 100644 --- a/electrical/src/main/java/resonantinduction/electrical/generator/solar/BlockSolarPanel.java +++ b/electrical/src/main/java/resonantinduction/electrical/generator/solar/BlockSolarPanel.java @@ -6,6 +6,7 @@ import net.minecraft.util.Icon; import net.minecraft.world.World; import resonantinduction.core.Reference; import resonantinduction.core.render.RIBlockRenderingHandler; +import resonantinduction.electrical.battery.TileEnergyDistribution; import universalelectricity.api.UniversalElectricity; import calclavia.lib.prefab.block.BlockTile; import cpw.mods.fml.relauncher.Side; @@ -23,6 +24,29 @@ public class BlockSolarPanel extends BlockTile setBlockBounds(0, 0, 0, 1, 0.3f, 1); } + @Override + public void onBlockAdded(World world, int x, int y, int z) + { + if (!world.isRemote) + { + TileEnergyDistribution distribution = (TileEnergyDistribution) world.getBlockTileEntity(x, y, z); + distribution.updateStructure(); + } + } + + @Override + public void onNeighborBlockChange(World world, int x, int y, int z, int id) + { + if (!world.isRemote) + { + if (id == blockID) + { + TileEnergyDistribution distribution = (TileEnergyDistribution) world.getBlockTileEntity(x, y, z); + distribution.updateStructure(); + } + } + } + @SideOnly(Side.CLIENT) @Override public void registerIcons(IconRegister iconReg) diff --git a/electrical/src/main/java/resonantinduction/electrical/generator/solar/TileSolarPanel.java b/electrical/src/main/java/resonantinduction/electrical/generator/solar/TileSolarPanel.java index 86641776..20f8d305 100644 --- a/electrical/src/main/java/resonantinduction/electrical/generator/solar/TileSolarPanel.java +++ b/electrical/src/main/java/resonantinduction/electrical/generator/solar/TileSolarPanel.java @@ -1,21 +1,19 @@ package resonantinduction.electrical.generator.solar; +import resonantinduction.electrical.battery.TileEnergyDistribution; import universalelectricity.api.energy.EnergyStorageHandler; -import calclavia.lib.prefab.tile.TileElectrical; -public class TileSolarPanel extends TileElectrical +public class TileSolarPanel extends TileEnergyDistribution { public TileSolarPanel() { - this.energy = new EnergyStorageHandler(500); + this.energy = new EnergyStorageHandler(800); this.ioMap = 728; } @Override public void updateEntity() { - super.updateEntity(); - if (!this.worldObj.isRemote) { if (this.worldObj.canBlockSeeTheSky(xCoord, yCoord + 1, zCoord) && !this.worldObj.provider.hasNoSky) @@ -25,11 +23,13 @@ public class TileSolarPanel extends TileElectrical if (!(this.worldObj.isThundering() || this.worldObj.isRaining())) { this.energy.receiveEnergy(25, true); - this.produce(); + markDistributionUpdate |= produce() > 0; } } } } + + super.updateEntity(); } }