diff --git a/src/main/java/resonantinduction/battery/BatteryStructure.java b/src/main/java/resonantinduction/battery/BatteryStructure.java index 09bce65c..2c78158b 100644 --- a/src/main/java/resonantinduction/battery/BatteryStructure.java +++ b/src/main/java/resonantinduction/battery/BatteryStructure.java @@ -1,96 +1,48 @@ package resonantinduction.battery; import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.Set; -import net.minecraft.item.ItemStack; -import universalelectricity.api.energy.EnergyStorageHandler; +import universalelectricity.api.net.IConnector; +import calclavia.lib.multiblock.structure.Structure; -public class BatteryStructure extends EnergyStorageHandler +public class BatteryStructure extends Structure { - public BatteryStructure(TileBattery battery) + public void redistribute() { - super(TileBattery.STORAGE); - this.battery.add(battery); + long totalEnergy = 0; + + for (TileBattery battery : this.get()) + { + totalEnergy += battery.getEnergy(null); + } + + long totalPerBattery = totalEnergy / this.get().size(); + long totalPerBatteryRemainder = totalPerBattery + totalEnergy % this.get().size(); + + TileBattery firstNode = this.getFirstNode(); + + for (TileBattery battery : this.get()) + { + if (battery == firstNode) + { + battery.setEnergy(null, totalPerBatteryRemainder); + } + else + { + battery.setEnergy(null, totalPerBattery); + } + } } - public Set battery = new LinkedHashSet(); - - public int length; - - public int width; - - public int height; - - public ItemStack tempStack; - - public boolean isMultiblock; - - public boolean didTick; - - public boolean wroteNBT; - - public int getVolume() - { - return this.battery.size(); - } - - /* - * @Override - * public int hashCode() - * { - * int code = 1; - * code = 31 * locations.hashCode(); - * code = 31 * length; - * code = 31 * width; - * code = 31 * height; - * return code; - * } - */ @Override - public boolean equals(Object obj) + public Structure getNew() { - if (!(obj instanceof BatteryStructure)) - { - return false; - } - - BatteryStructure data = (BatteryStructure) obj; - - if (!data.battery.equals(battery)) - { - return false; - } - - if (data.length != length || data.width != width || data.height != height) - { - return false; - } - - return true; + return new BatteryStructure(); } - public void merge(TileBattery tile) + @Override + protected void refreshNode(TileBattery node) { - // Merge structure. - long energyToMerge = tile.structure.getEnergy(); - long capacityToMerge = tile.structure.getEnergyCapacity(); - this.battery.addAll(tile.structure.battery); - tile.structure.battery.clear(); - this.resetReferences(); - this.setCapacity(capacityToMerge); - this.receiveEnergy(energyToMerge, true); - } - - public void resetReferences() - { - Iterator it = this.battery.iterator(); - - while (it.hasNext()) - { - TileBattery tile = it.next(); - tile.structure = this; - } + node.setNetwork(this); } } diff --git a/src/main/java/resonantinduction/battery/BlockBattery.java b/src/main/java/resonantinduction/battery/BlockBattery.java index f93dbcba..1a04d2df 100644 --- a/src/main/java/resonantinduction/battery/BlockBattery.java +++ b/src/main/java/resonantinduction/battery/BlockBattery.java @@ -34,7 +34,7 @@ public class BlockBattery extends BlockBase implements ITileEntityProvider { if (!world.isRemote) { - System.out.println(((TileBattery) world.getBlockTileEntity(x, y, z)).structure.hashCode()); + System.out.println(((TileBattery) world.getBlockTileEntity(x, y, z)).getNetwork().hashCode()); } return true; } diff --git a/src/main/java/resonantinduction/battery/RenderBattery.java b/src/main/java/resonantinduction/battery/RenderBattery.java index 4480e417..5d789f3f 100644 --- a/src/main/java/resonantinduction/battery/RenderBattery.java +++ b/src/main/java/resonantinduction/battery/RenderBattery.java @@ -93,14 +93,7 @@ public class RenderBattery extends TileEntitySpecialRenderer GL11.glTranslated(x + 0.5, y + 1.5, z + 0.5); GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F); - if (((TileBattery) t).structure.isMultiblock) - { - this.bindTexture(TEXTURE_MULTI); - } - else - { - this.bindTexture(TEXTURE); - } + this.bindTexture(TEXTURE); MODEL.render(0.0625f); GL11.glPopMatrix(); diff --git a/src/main/java/resonantinduction/battery/TileBattery.java b/src/main/java/resonantinduction/battery/TileBattery.java index f7bdff35..dfe96df4 100644 --- a/src/main/java/resonantinduction/battery/TileBattery.java +++ b/src/main/java/resonantinduction/battery/TileBattery.java @@ -1,6 +1,3 @@ -/** - * - */ package resonantinduction.battery; import java.util.ArrayList; @@ -8,13 +5,15 @@ import java.util.HashSet; import java.util.Set; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import resonantinduction.ResonantInduction; import universalelectricity.api.UniversalClass; import universalelectricity.api.energy.EnergyStorageHandler; import universalelectricity.api.energy.IEnergyContainer; import universalelectricity.api.energy.IEnergyInterface; +import universalelectricity.api.net.IConnector; +import universalelectricity.api.vector.Vector3; import calclavia.lib.network.IPacketReceiver; import calclavia.lib.network.IPacketSender; import calclavia.lib.prefab.tile.TileElectrical; @@ -29,13 +28,13 @@ import cpw.mods.fml.common.network.PacketDispatcher; * @author Calclavia */ @UniversalClass -public class TileBattery extends TileElectrical implements IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer +public class TileBattery extends TileElectrical implements IConnector, IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer { public static final long STORAGE = 100000000; - public Set playersUsing = new HashSet(); + private BatteryStructure structure; - public BatteryStructure structure = new BatteryStructure(this); + public Set playersUsing = new HashSet(); public float clientEnergy; public int clientCells; @@ -56,17 +55,16 @@ public class TileBattery extends TileElectrical implements IPacketSender, IPacke { if (!this.worldObj.isRemote) { - /* - * for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - * { - * TileEntity tile = new - * Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj); - * if (tile instanceof TileBattery) - * { - * this.structure.merge((TileBattery) tile); - * } - * } - */ + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj); + + if (tile instanceof TileBattery) + { + this.getNetwork().merge(((TileBattery) tile).getNetwork()); + } + } + } } @@ -77,59 +75,29 @@ public class TileBattery extends TileElectrical implements IPacketSender, IPacke if (!this.worldObj.isRemote) { + if (this.getNetwork().getFirstNode() == this) + { + this.getNetwork().redistribute(); + } + this.produce(); } } - public float getTransferThreshhold() - { - return this.structure.getVolume() * 50; - } - public void updateClient() { PacketDispatcher.sendPacketToAllPlayers(ResonantInduction.PACKET_TILE.getPacket(this, getPacketData(0).toArray())); } - @Override - public void readFromNBT(NBTTagCompound nbt) - { - super.readFromNBT(nbt); - this.structure.readFromNBT(nbt); - } - - @Override - public void writeToNBT(NBTTagCompound nbt) - { - super.writeToNBT(nbt); - - if (!structure.wroteNBT) - { - this.structure.writeToNBT(nbt); - structure.wroteNBT = true; - } - } - @Override public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra) { - structure.isMultiblock = data.readBoolean(); - - structure.height = data.readInt(); - structure.length = data.readInt(); - structure.width = data.readInt(); } @Override public ArrayList getPacketData(int type) { ArrayList data = new ArrayList(); - data.add(structure.isMultiblock); - - data.add(structure.height); - data.add(structure.length); - data.add(structure.width); - return data; } @@ -138,4 +106,47 @@ public class TileBattery extends TileElectrical implements IPacketSender, IPacke { return true; } + + @Override + public BatteryStructure getNetwork() + { + if (this.structure == null) + { + this.structure = new BatteryStructure(); + this.structure.add(this); + } + + return this.structure; + } + + @Override + public void setNetwork(BatteryStructure structure) + { + this.structure = structure; + } + + @Override + public Object[] getConnections() + { + Object[] connections = new Object[6]; + + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj); + + if (tile instanceof TileBattery) + { + connections[dir.ordinal()] = tile; + } + } + + return connections; + } + + @Override + public void invalidate() + { + this.getNetwork().split(this); + super.invalidate(); + } }