Fix batteries

This commit is contained in:
Aidan Brady 2013-11-29 18:19:18 -05:00
parent cd62f593fa
commit 69d1c5961e
4 changed files with 136 additions and 291 deletions

View file

@ -1,11 +1,11 @@
package mekanism.induction.common; package mekanism.induction.common;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import mekanism.api.Object3D;
import mekanism.common.util.ListUtils; import mekanism.common.util.ListUtils;
import mekanism.induction.common.tileentity.TileEntityBattery; import mekanism.induction.common.tileentity.TileEntityBattery;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
@ -13,7 +13,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
public class BatteryUpdateProtocol public class BatteryUpdateProtocol
{ {
@ -42,7 +41,7 @@ public class BatteryUpdateProtocol
boolean isCorner = true; boolean isCorner = true;
boolean rightBlocks = true; boolean rightBlocks = true;
Set<Vector3> locations = new HashSet<Vector3>(); Set<Object3D> locations = new HashSet<Object3D>();
int xmin = 0, xmax = 0, ymin = 0, ymax = 0, zmin = 0, zmax = 0; int xmin = 0, xmax = 0, ymin = 0, ymax = 0, zmin = 0, zmax = 0;
@ -59,7 +58,7 @@ public class BatteryUpdateProtocol
{ {
xmin = 0; xmin = 0;
while (isBattery(origX + x + 1, origY, origZ)) while(isBattery(origX + x + 1, origY, origZ))
{ {
x++; x++;
} }
@ -69,7 +68,7 @@ public class BatteryUpdateProtocol
else { else {
xmax = 0; xmax = 0;
while (isBattery(origX + x - 1, origY, origZ)) while(isBattery(origX + x - 1, origY, origZ))
{ {
x--; x--;
} }
@ -113,7 +112,7 @@ public class BatteryUpdateProtocol
else { else {
zmax = 0; zmax = 0;
while (isBattery(origX, origY, origZ + z - 1)) while(isBattery(origX, origY, origZ + z - 1))
{ {
z--; z--;
} }
@ -133,7 +132,7 @@ public class BatteryUpdateProtocol
break; break;
} }
else { else {
locations.add(new Vector3(tile).translate(new Vector3(x, y, z))); locations.add(Object3D.get(tile).translate(x, y, z));
} }
} }
@ -162,7 +161,7 @@ public class BatteryUpdateProtocol
structure.isMultiblock = true; structure.isMultiblock = true;
} }
if(structure.locations.contains(new Vector3(pointer))) if(structure.locations.contains(Object3D.get(pointer)))
{ {
structureFound = structure; structureFound = structure;
} }
@ -174,7 +173,7 @@ public class BatteryUpdateProtocol
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{ {
TileEntity tileEntity = new Vector3(tile).modifyPositionFromSide(side).getTileEntity(tile.worldObj); TileEntity tileEntity = Object3D.get(tile).getFromSide(side).getTileEntity(tile.worldObj);
if(tileEntity instanceof TileEntityBattery) if(tileEntity instanceof TileEntityBattery)
{ {
@ -198,70 +197,55 @@ public class BatteryUpdateProtocol
private void disperseCells() private void disperseCells()
{ {
Set<SynchronizedBatteryData> structures = new HashSet<SynchronizedBatteryData>(); SynchronizedBatteryData oldStructure = null;
List<ItemStack> mergedInv = new ArrayList<ItemStack>();
List<ItemStack[]> visibleInvs = new ArrayList<ItemStack[]>();
for(TileEntityBattery tile : iteratedNodes) for(TileEntityBattery tile : iteratedNodes)
{ {
structures.add(tile.structure); if(tile.structure.isMultiblock)
}
for(SynchronizedBatteryData data : structures)
{
mergedInv = ListUtils.merge(mergedInv, data.inventory);
if(data.hasVisibleInventory())
{ {
visibleInvs.add(data.visibleInventory); oldStructure = tile.structure;
break;
} }
} }
int maxCells = iteratedNodes.size() * BatteryManager.CELLS_PER_BATTERY; if(oldStructure != null)
List<ItemStack> rejected = ListUtils.capRemains(mergedInv, maxCells);
ejectItems(rejected, new Vector3(pointer));
ArrayList<List<ItemStack>> inventories = ListUtils.split(ListUtils.cap(mergedInv, maxCells), iteratedNodes.size());
List<TileEntityBattery> iterList = ListUtils.asList(iteratedNodes);
for(int i = 0; i < iterList.size(); i++)
{ {
TileEntityBattery tile = iterList.get(i); int maxCells = iteratedNodes.size() * BatteryManager.CELLS_PER_BATTERY;
tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i));
if(!visibleInvs.isEmpty()) List<ItemStack> rejected = ListUtils.capRemains(oldStructure.inventory, maxCells);
ejectItems(rejected, Object3D.get(pointer));
ArrayList<List<ItemStack>> inventories = ListUtils.split(ListUtils.cap(oldStructure.inventory, maxCells), iteratedNodes.size());
List<TileEntityBattery> iterList = ListUtils.asList(iteratedNodes);
boolean didVisibleInventory = false;
for(int i = 0; i < iterList.size(); i++)
{ {
tile.structure.visibleInventory = visibleInvs.get(0); TileEntityBattery tile = iterList.get(i);
visibleInvs.remove(0); tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i));
}
} if(!didVisibleInventory)
{
if(!visibleInvs.isEmpty()) tile.structure.visibleInventory = oldStructure.visibleInventory;
{ didVisibleInventory = true;
for(ItemStack[] inv : visibleInvs) }
{
ejectItems(Arrays.asList(inv), new Vector3(pointer));
} }
} }
} }
private void ejectItems(List<ItemStack> items, Vector3 vec) private void ejectItems(List<ItemStack> items, Object3D vec)
{ {
for(ItemStack itemStack : items) for(ItemStack itemStack : items)
{ {
if(itemStack != null) float motion = 0.7F;
{ double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
float motion = 0.7F; double motionY = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; double motionZ = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionY = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionZ = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; EntityItem entityItem = new EntityItem(pointer.worldObj, vec.xCoord + motionX, vec.yCoord + motionY, vec.zCoord + motionZ, itemStack);
EntityItem entityItem = new EntityItem(pointer.worldObj, vec.x + motionX, vec.y + motionY, vec.z + motionZ, itemStack); pointer.worldObj.spawnEntityInWorld(entityItem);
pointer.worldObj.spawnEntityInWorld(entityItem);
}
} }
} }
@ -273,39 +257,30 @@ public class BatteryUpdateProtocol
{ {
for(TileEntityBattery tileEntity : iteratedNodes) for(TileEntityBattery tileEntity : iteratedNodes)
{ {
if(!structureFound.locations.contains(new Vector3(tileEntity))) if(!structureFound.locations.contains(Object3D.get(tileEntity)))
{ {
disperseCells(); disperseCells();
return; return;
} }
} }
boolean foundVisibleInv = false;
for(Vector3 obj : structureFound.locations) for(Object3D obj : structureFound.locations)
{ {
TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj); TileEntityBattery tileEntity = (TileEntityBattery) obj.getTileEntity(pointer.worldObj);
structureFound.inventory = ListUtils.merge(structureFound.inventory, tileEntity.structure.inventory); structureFound.inventory = ListUtils.merge(structureFound.inventory, tileEntity.structure.inventory);
if(tileEntity.structure.hasVisibleInventory()) if(tileEntity.structure.hasVisibleInventory())
{ {
if(foundVisibleInv) structureFound.visibleInventory = tileEntity.structure.visibleInventory;
{
ejectItems(Arrays.asList(tileEntity.structure.visibleInventory), obj);
}
else {
structureFound.visibleInventory = tileEntity.structure.visibleInventory;
foundVisibleInv = true;
}
} }
tileEntity.structure = structureFound; tileEntity.structure = structureFound;
} }
List<ItemStack> rejected = ListUtils.capRemains(structureFound.inventory, structureFound.getMaxCells()); List<ItemStack> rejected = ListUtils.capRemains(structureFound.inventory, structureFound.getMaxCells());
ejectItems(rejected, new Vector3(pointer)); ejectItems(rejected, Object3D.get(pointer));
structureFound.inventory = ListUtils.cap(structureFound.inventory, structureFound.getMaxCells()); structureFound.inventory = ListUtils.cap(structureFound.inventory, structureFound.getMaxCells());
} }
@ -313,4 +288,4 @@ public class BatteryUpdateProtocol
disperseCells(); disperseCells();
} }
} }
} }

View file

@ -131,19 +131,19 @@ public class MekanismInduction implements IModule
public void postInit(FMLPostInitializationEvent evt) public void postInit(FMLPostInitializationEvent evt)
{ {
/** Linker **/ /** Linker **/
GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Linker), new Object[] {" E ", "GCG", " E ", 'E', Item.eyeOfEnder, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'G', UniversalRecipes.SECONDARY_METAL})); GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Linker), new Object[] {" E ", "GCG", " E ", 'E', Item.eyeOfEnder, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'G', "ingotOsmium"}));
/** Tesla - by Jyzarc */ /** Tesla - by Jyzarc */
GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Tesla), new Object[] {"WEW", " C ", " I ", 'W', Mekanism.EnrichedAlloy, 'E', Item.eyeOfEnder, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'I', new ItemStack(Mekanism.BasicBlock, 1, 8)})); GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Tesla), new Object[] {"WEW", " C ", " I ", 'W', Mekanism.EnrichedAlloy, 'E', Item.eyeOfEnder, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'I', new ItemStack(Mekanism.BasicBlock, 1, 8)}));
/** Multimeter */ /** Multimeter */
GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Multimeter), new Object[] {"WWW", "ICI", 'W', Mekanism.EnrichedAlloy, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'I', UniversalRecipes.PRIMARY_METAL})); GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Multimeter), new Object[] {"WWW", "ICI", 'W', Mekanism.EnrichedAlloy, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'I', "ingotSteel"}));
/** Battery */ /** Battery */
GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Battery), new Object[] {"III", "IRI", "III", 'R', Block.blockRedstone, 'I', MekanismUtils.getEnergyCube(EnergyCubeTier.BASIC)})); GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(Battery, 4), new Object[] {"RRR", "CIC", "RRR", 'R', Item.redstone, 'I', MekanismUtils.getEnergyCube(EnergyCubeTier.BASIC), 'C', "circuitBasic"}));
/** EM Contractor */ /** EM Contractor */
GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(ElectromagneticContractor), new Object[] {" I ", "GCG", "WWW", 'W', UniversalRecipes.PRIMARY_METAL, 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'G', UniversalRecipes.SECONDARY_METAL, 'I', UniversalRecipes.PRIMARY_METAL})); GameRegistry.addRecipe(new MekanismRecipe(new ItemStack(ElectromagneticContractor), new Object[] {" I ", "GCG", "WWW", 'W', "ingotSteel", 'C', Mekanism.EnergyTablet.getUnchargedItem(), 'G', "ingotOsmium", 'I', "ingotSteel"}));
} }
@Override @Override

View file

@ -5,15 +5,15 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import mekanism.api.Object3D;
import mekanism.common.util.ListUtils; import mekanism.common.util.ListUtils;
import mekanism.induction.common.tileentity.TileEntityBattery; import mekanism.induction.common.tileentity.TileEntityBattery;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import universalelectricity.core.item.IItemElectric; import universalelectricity.core.item.IItemElectric;
import universalelectricity.core.vector.Vector3;
public class SynchronizedBatteryData public class SynchronizedBatteryData
{ {
public Set<Vector3> locations = new HashSet<Vector3>(); public Set<Object3D> locations = new HashSet<Object3D>();
public List<ItemStack> inventory = new ArrayList<ItemStack>(); public List<ItemStack> inventory = new ArrayList<ItemStack>();
@ -42,10 +42,10 @@ public class SynchronizedBatteryData
public boolean addCell(ItemStack cell) public boolean addCell(ItemStack cell)
{ {
if (this.inventory.size() < this.getMaxCells()) if(inventory.size() < getMaxCells())
{ {
this.inventory.add(cell); inventory.add(cell);
this.sortInventory(); sortInventory();
return true; return true;
} }
@ -58,7 +58,7 @@ public class SynchronizedBatteryData
ItemStack[] toSort = new ItemStack[array.length]; ItemStack[] toSort = new ItemStack[array.length];
for (int i = 0; i < array.length; i++) for(int i = 0; i < array.length; i++)
{ {
toSort[i] = (ItemStack) array[i]; toSort[i] = (ItemStack) array[i];
} }
@ -66,13 +66,13 @@ public class SynchronizedBatteryData
boolean cont = true; boolean cont = true;
ItemStack temp; ItemStack temp;
while (cont) while(cont)
{ {
cont = false; cont = false;
for (int i = 0; i < toSort.length - 1; i++) for(int i = 0; i < toSort.length - 1; i++)
{ {
if (((IItemElectric) toSort[i].getItem()).getElectricityStored(toSort[i]) < ((IItemElectric) toSort[i + 1].getItem()).getElectricityStored(toSort[i + 1])) if(((IItemElectric) toSort[i].getItem()).getElectricityStored(toSort[i]) < ((IItemElectric) toSort[i + 1].getItem()).getElectricityStored(toSort[i + 1]))
{ {
temp = toSort[i]; temp = toSort[i];
toSort[i] = toSort[i + 1]; toSort[i] = toSort[i + 1];
@ -84,7 +84,7 @@ public class SynchronizedBatteryData
inventory = new ArrayList<ItemStack>(); inventory = new ArrayList<ItemStack>();
for (ItemStack itemStack : toSort) for(ItemStack itemStack : toSort)
{ {
inventory.add(itemStack); inventory.add(itemStack);
} }
@ -92,9 +92,9 @@ public class SynchronizedBatteryData
public boolean hasVisibleInventory() public boolean hasVisibleInventory()
{ {
for (ItemStack itemStack : visibleInventory) for(ItemStack itemStack : visibleInventory)
{ {
if (itemStack != null) if(itemStack != null)
{ {
return true; return true;
} }
@ -114,7 +114,7 @@ public class SynchronizedBatteryData
public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity) public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity)
{ {
SynchronizedBatteryData structure = new SynchronizedBatteryData(); SynchronizedBatteryData structure = new SynchronizedBatteryData();
structure.locations.add(new Vector3(tileEntity)); structure.locations.add(Object3D.get(tileEntity));
return structure; return structure;
} }
@ -130,14 +130,14 @@ public class SynchronizedBatteryData
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
if (!(obj instanceof SynchronizedBatteryData)) if(!(obj instanceof SynchronizedBatteryData))
{ {
return false; return false;
} }
SynchronizedBatteryData data = (SynchronizedBatteryData) obj; SynchronizedBatteryData data = (SynchronizedBatteryData) obj;
if (!data.locations.equals(locations)) if(!data.locations.equals(locations))
{ {
return false; return false;
} }

View file

@ -11,40 +11,33 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.common.ITileNetwork; import mekanism.api.energy.IEnergizedItem;
import mekanism.common.Mekanism; import mekanism.common.Mekanism;
import mekanism.common.PacketHandler; import mekanism.common.PacketHandler;
import mekanism.common.PacketHandler.Transmission; import mekanism.common.PacketHandler.Transmission;
import mekanism.common.network.PacketDataRequest;
import mekanism.common.network.PacketTileEntity; import mekanism.common.network.PacketTileEntity;
import mekanism.common.tileentity.TileEntityElectricBlock;
import mekanism.common.util.CableUtils;
import mekanism.common.util.ListUtils; import mekanism.common.util.ListUtils;
import mekanism.induction.common.BatteryUpdateProtocol; import mekanism.induction.common.BatteryUpdateProtocol;
import mekanism.induction.common.SynchronizedBatteryData; import mekanism.induction.common.SynchronizedBatteryData;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.compatibility.TileEntityUniversalElectrical;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.item.IItemElectric; import universalelectricity.core.item.IItemElectric;
import universalelectricity.core.vector.Vector3; import universalelectricity.core.vector.Vector3;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
/** /**
* A modular battery with no GUI. * A modular battery with no GUI.
* *
* @author AidanBrady * @author AidanBrady
*/ */
public class TileEntityBattery extends TileEntityUniversalElectrical implements ITileNetwork, IInventory public class TileEntityBattery extends TileEntityElectricBlock
{ {
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>(); public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
@ -52,12 +45,17 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
public SynchronizedBatteryData prevStructure; public SynchronizedBatteryData prevStructure;
public float clientEnergy; public double clientEnergy;
public int clientCells; public int clientCells;
public float clientMaxEnergy; public double clientMaxEnergy;
public int clientVolume; public int clientVolume;
private EnumSet inputSides = EnumSet.allOf(ForgeDirection.class); private EnumSet inputSides = EnumSet.allOf(ForgeDirection.class);
public TileEntityBattery()
{
super("Battery", 0);
}
@Override @Override
public void updateEntity() public void updateEntity()
@ -66,7 +64,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
if(!worldObj.isRemote) if(!worldObj.isRemote)
{ {
if(ticks == 5 && !structure.isMultiblock) if(ticker == 5 && !structure.isMultiblock)
{ {
update(); update();
} }
@ -84,88 +82,33 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
} }
} }
/** if(structure.visibleInventory[1] != null)
* Attempt to charge entities above it.
*/
ItemStack chargeItem = null;
if(worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{ {
List<Entity> entities = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord + 1, zCoord, xCoord + 1, yCoord + 2, zCoord + 1)); ItemStack itemStack = structure.visibleInventory[1];
IEnergizedItem battery = (IEnergizedItem)itemStack.getItem();
electricItemLoop: double energyStored = getMaxEnergy();
for (Entity entity : entities) double batteryNeeded = battery.getMaxEnergy(itemStack) - battery.getEnergy(itemStack);
{ double toGive = Math.min(energyStored, Math.min(battery.getMaxTransfer(itemStack), batteryNeeded));
if(entity instanceof EntityPlayer)
{
IInventory inventory = ((EntityPlayer) entity).inventory;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack checkStack = inventory.getStackInSlot(i);
if(checkStack != null) battery.setEnergy(itemStack, battery.getEnergy(itemStack) + remove(toGive, true));
{
if(checkStack.getItem() instanceof IItemElectric)
{
if(((IItemElectric) checkStack.getItem()).recharge(checkStack, provideElectricity(getTransferThreshhold(), false).getWatts(), false) > 0)
{
chargeItem = checkStack;
break electricItemLoop;
}
}
}
}
}
else if(entity instanceof EntityItem)
{
ItemStack checkStack = ((EntityItem) entity).getEntityItem();
if(checkStack != null)
{
if(checkStack.getItem() instanceof IItemElectric)
{
if(((IItemElectric) checkStack.getItem()).recharge(checkStack, provideElectricity(getTransferThreshhold(), false).getWatts(), false) > 0)
{
chargeItem = checkStack;
break electricItemLoop;
}
}
}
}
}
}
if(chargeItem == null)
{
chargeItem = structure.visibleInventory[1];
}
if(chargeItem != null)
{
ItemStack itemStack = chargeItem;
IItemElectric battery = (IItemElectric) itemStack.getItem();
float energyStored = getMaxEnergyStored();
float batteryNeeded = battery.recharge(itemStack, provideElectricity(getTransferThreshhold(), false).getWatts(), false);
float toGive = Math.min(energyStored, Math.min(battery.getTransfer(itemStack), batteryNeeded));
battery.recharge(itemStack, provideElectricity(toGive, true).getWatts(), true);
} }
if(structure.visibleInventory[2] != null) if(structure.visibleInventory[2] != null)
{ {
ItemStack itemStack = structure.visibleInventory[2]; ItemStack itemStack = structure.visibleInventory[2];
IItemElectric battery = (IItemElectric) itemStack.getItem(); IEnergizedItem battery = (IEnergizedItem)itemStack.getItem();
float energyNeeded = getMaxEnergyStored() - getEnergyStored(); double energyNeeded = getMaxEnergy() - getEnergy();
float batteryStored = battery.getElectricityStored(itemStack); double batteryStored = battery.getEnergy(itemStack);
float toReceive = Math.min(energyNeeded, Math.min(getTransferThreshhold(), Math.min(battery.getTransfer(itemStack), batteryStored))); double toReceive = Math.min(energyNeeded, Math.min(battery.getMaxTransfer(itemStack), batteryStored));
battery.discharge(itemStack, receiveElectricity(toReceive, true), true); battery.setEnergy(itemStack, battery.getEnergy(itemStack) - add(toReceive, true));
} }
if(prevStructure != structure) if(prevStructure != structure)
{ {
for (EntityPlayer player : playersUsing) for(EntityPlayer player : playersUsing)
{ {
player.closeScreen(); player.closeScreen();
} }
@ -183,20 +126,15 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
updateClient(); updateClient();
} }
for (EntityPlayer player : playersUsing) for(EntityPlayer player : playersUsing)
{ {
PacketHandler.sendPacket(Transmission.SINGLE_CLIENT, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), player); PacketHandler.sendPacket(Transmission.SINGLE_CLIENT, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), player);
} }
produce(); CableUtils.emit(this);
} }
} }
public float getTransferThreshhold()
{
return structure.getVolume() * 50;
}
public void updateClient() public void updateClient()
{ {
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList()))); PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())));
@ -204,24 +142,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
public void updateAllClients() public void updateAllClients()
{ {
for (Vector3 vec : structure.locations) for(Object3D vec : structure.locations)
{ {
TileEntityBattery battery = (TileEntityBattery) vec.getTileEntity(worldObj); TileEntityBattery battery = (TileEntityBattery)vec.getTileEntity(worldObj);
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(battery), battery.getNetworkedData(new ArrayList()))); PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(battery), battery.getNetworkedData(new ArrayList())));
} }
} }
@Override
public void validate()
{
super.validate();
if(worldObj.isRemote)
{
PacketHandler.sendPacket(Transmission.SERVER, new PacketDataRequest().setParams(Object3D.get(this)));
}
}
@Override @Override
public void readFromNBT(NBTTagCompound nbtTags) public void readFromNBT(NBTTagCompound nbtTags)
{ {
@ -233,7 +160,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
NBTTagList tagList = nbtTags.getTagList("Items"); NBTTagList tagList = nbtTags.getTagList("Items");
structure.inventory = new ArrayList<ItemStack>(); structure.inventory = new ArrayList<ItemStack>();
for (int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
{ {
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount); NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
int slotID = tagCompound.getInteger("Slot"); int slotID = tagCompound.getInteger("Slot");
@ -247,7 +174,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
NBTTagList tagList = nbtTags.getTagList("VisibleItems"); NBTTagList tagList = nbtTags.getTagList("VisibleItems");
structure.visibleInventory = new ItemStack[3]; structure.visibleInventory = new ItemStack[3];
for (int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
{ {
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount); NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
byte slotID = tagCompound.getByte("Slot"); byte slotID = tagCompound.getByte("Slot");
@ -258,8 +185,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{ {
setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound)); setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound));
} }
else else {
{
setInventorySlotContents(slotID + 1, ItemStack.loadItemStackFromNBT(tagCompound)); setInventorySlotContents(slotID + 1, ItemStack.loadItemStackFromNBT(tagCompound));
} }
} }
@ -270,7 +196,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
NBTTagList tagList = nbtTags.getTagList("inputSides"); NBTTagList tagList = nbtTags.getTagList("inputSides");
for (int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
{ {
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount); NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
byte side = tagCompound.getByte("side"); byte side = tagCompound.getByte("side");
@ -287,7 +213,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
if(!structure.wroteInventory) if(!structure.wroteInventory)
{ {
// Inventory //Inventory
if(structure.inventory != null) if(structure.inventory != null)
{ {
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
@ -306,7 +232,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
nbt.setTag("Items", tagList); nbt.setTag("Items", tagList);
} }
// Visible inventory //Visible inventory
if(structure.visibleInventory != null) if(structure.visibleInventory != null)
{ {
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
@ -338,7 +264,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
Iterator<ForgeDirection> it = inputSides.iterator(); Iterator<ForgeDirection> it = inputSides.iterator();
while (it.hasNext()) while(it.hasNext())
{ {
ForgeDirection dir = it.next(); ForgeDirection dir = it.next();
@ -367,24 +293,22 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
} }
@Override public double add(double amount, boolean doAdd)
public float receiveElectricity(ElectricityPack receive, boolean doAdd)
{ {
float amount = receive.getWatts(); double added = 0;
float added = 0;
for (ItemStack itemStack : structure.inventory) for(ItemStack itemStack : structure.inventory)
{ {
if(itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IEnergizedItem)
{ {
IItemElectric battery = (IItemElectric) itemStack.getItem(); IEnergizedItem battery = (IEnergizedItem)itemStack.getItem();
float needed = amount - added; double needed = amount - added;
float itemAdd = Math.min(battery.getMaxElectricityStored(itemStack) - battery.getElectricityStored(itemStack), needed); double itemAdd = Math.min(battery.getMaxEnergy(itemStack) - battery.getEnergy(itemStack), needed);
if(doAdd) if(doAdd)
{ {
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) + itemAdd); battery.setEnergy(itemStack, battery.getEnergy(itemStack) + itemAdd);
} }
added += itemAdd; added += itemAdd;
@ -399,26 +323,24 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return added; return added;
} }
@Override public double remove(double amount, boolean doRemove)
public ElectricityPack provideElectricity(ElectricityPack pack, boolean doRemove)
{ {
float amount = pack.getWatts();
List<ItemStack> inverse = ListUtils.inverse(structure.inventory); List<ItemStack> inverse = ListUtils.inverse(structure.inventory);
float removed = 0; float removed = 0;
for (ItemStack itemStack : inverse)
for(ItemStack itemStack : inverse)
{ {
if(itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IEnergizedItem)
{ {
IItemElectric battery = (IItemElectric) itemStack.getItem(); IEnergizedItem battery = (IEnergizedItem)itemStack.getItem();
float needed = amount - removed; double needed = amount - removed;
float itemRemove = Math.min(battery.getElectricityStored(itemStack), needed); double itemRemove = Math.min(battery.getEnergy(itemStack), needed);
if(doRemove) if(doRemove)
{ {
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) - itemRemove); battery.setEnergy(itemStack, battery.getEnergy(itemStack) - itemRemove);
} }
removed += itemRemove; removed += itemRemove;
@ -430,23 +352,23 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
} }
return ElectricityPack.getFromWatts(removed, getVoltage()); return removed;
} }
@Override @Override
public float getMaxEnergyStored() public double getMaxEnergy()
{ {
if(!worldObj.isRemote) if(!worldObj.isRemote)
{ {
float max = 0; float max = 0;
for (ItemStack itemStack : structure.inventory) for(ItemStack itemStack : structure.inventory)
{ {
if(itemStack != null) if(itemStack != null)
{ {
if(itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IEnergizedItem)
{ {
max += ((IItemElectric) itemStack.getItem()).getMaxElectricityStored(itemStack); max += ((IEnergizedItem)itemStack.getItem()).getMaxEnergy(itemStack);
} }
} }
} }
@ -459,19 +381,19 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
@Override @Override
public float getEnergyStored() public double getEnergy()
{ {
if(!worldObj.isRemote) if(!worldObj.isRemote)
{ {
float energy = 0; double energy = 0;
for(ItemStack itemStack : structure.inventory) for(ItemStack itemStack : structure.inventory)
{ {
if(itemStack != null) if(itemStack != null)
{ {
if(itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IEnergizedItem)
{ {
energy += ((IItemElectric) itemStack.getItem()).getElectricityStored(itemStack); energy += ((IEnergizedItem)itemStack.getItem()).getEnergy(itemStack);
} }
} }
} }
@ -488,9 +410,9 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{ {
structure.isMultiblock = input.readBoolean(); structure.isMultiblock = input.readBoolean();
clientEnergy = input.readFloat(); clientEnergy = input.readDouble();
clientCells = input.readInt(); clientCells = input.readInt();
clientMaxEnergy = input.readFloat(); clientMaxEnergy = input.readDouble();
clientVolume = input.readInt(); clientVolume = input.readInt();
} }
@ -499,9 +421,9 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{ {
data.add(structure.isMultiblock); data.add(structure.isMultiblock);
data.add(getEnergyStored()); data.add(getEnergy());
data.add(structure.inventory.size()); data.add(structure.inventory.size());
data.add(getMaxEnergyStored()); data.add(getMaxEnergy());
data.add(structure.getVolume()); data.add(structure.getVolume());
return data; return data;
@ -602,71 +524,19 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
@Override @Override
public String getInvName() public boolean isItemValidForSlot(int i, ItemStack itemStack)
{ {
return "Battery"; return itemStack.getItem() instanceof IItemElectric;
} }
@Override @Override
public boolean isInvNameLocalized() public EnumSet<ForgeDirection> getConsumingSides()
{
return false;
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return true;
}
@Override
public void openChest() {}
@Override
public void closeChest() {}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemsSack)
{
return itemsSack.getItem() instanceof IItemElectric;
}
@Override
public float getRequest(ForgeDirection direction)
{
if(getInputDirections().contains(direction))
{
return Math.min(getMaxEnergyStored() - getEnergyStored(), getTransferThreshhold());
}
return 0;
}
@Override
public float getProvide(ForgeDirection direction)
{
if(getOutputDirections().contains(direction))
{
return Math.min(getEnergyStored(), getTransferThreshhold());
}
return 0;
}
@Override
public EnumSet<ForgeDirection> getInputDirections()
{ {
return inputSides; return inputSides;
} }
@Override @Override
public EnumSet<ForgeDirection> getOutputDirections() public EnumSet<ForgeDirection> getOutputtingSides()
{ {
return EnumSet.complementOf(inputSides); return EnumSet.complementOf(inputSides);
} }