Added battery wrench pick up and fixed distribution

This commit is contained in:
Calclavia 2014-01-23 21:22:07 +08:00
parent 0198bd3738
commit 6fa2530dd6
3 changed files with 65 additions and 30 deletions

View file

@ -9,35 +9,34 @@ public class BatteryNetwork extends Network<BatteryNetwork, TileBattery>
public void redistribute(TileBattery... exclusion)
{
long totalEnergy = 0;
long totalCapacity = 0;
for (TileBattery battery : this.getConnectors())
{
totalEnergy += battery.energy.getEnergy();
totalCapacity += battery.energy.getEnergyCapacity();
}
int amountOfNodes = this.getConnectors().size() - exclusion.length;
if (totalEnergy > 0 && amountOfNodes > 0)
{
long totalPerBattery = totalEnergy / amountOfNodes;
long totalPerBatteryRemainder = totalPerBattery + totalEnergy % amountOfNodes;
long remainingEnergy = totalEnergy;
TileBattery firstNode = this.getFirstConnector();
for (TileBattery battery : this.getConnectors())
{
if (!Arrays.asList(exclusion).contains(battery))
if (battery != firstNode && !Arrays.asList(exclusion).contains(battery))
{
if (battery == firstNode)
{
battery.energy.setEnergy(totalPerBatteryRemainder);
}
else
{
battery.energy.setEnergy(totalPerBattery);
}
double percentage = ((double) battery.energy.getEnergyCapacity() / (double) totalCapacity);
long energyForBattery = (long) Math.round(totalEnergy * percentage);
battery.energy.setEnergy(energyForBattery);
remainingEnergy -= energyForBattery;
}
}
firstNode.energy.setEnergy(remainingEnergy);
}
}

View file

@ -3,8 +3,11 @@
*/
package resonantinduction.electrical.battery;
import java.util.ArrayList;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
@ -14,7 +17,6 @@ import resonantinduction.core.Reference;
import resonantinduction.core.Settings;
import resonantinduction.core.prefab.block.BlockIOBase;
import resonantinduction.core.render.RIBlockRenderingHandler;
import resonantinduction.electrical.Electrical;
import universalelectricity.api.CompatibilityModule;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@ -44,15 +46,16 @@ public class BlockBattery extends BlockIOBase implements ITileEntityProvider
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityliving, ItemStack itemstack)
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityliving, ItemStack itemStack)
{
if (!world.isRemote && itemstack.getItem() instanceof ItemBlockBattery)
if (!world.isRemote && itemStack.getItem() instanceof ItemBlockBattery)
{
ItemBlockBattery itemBlock = (ItemBlockBattery) itemstack.getItem();
ItemBlockBattery itemBlock = (ItemBlockBattery) itemStack.getItem();
TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z);
battery.energy.setCapacity(TileBattery.getEnergyForTier(itemBlock.getTier(itemstack)));
battery.energy.setEnergy(itemBlock.getEnergy(itemstack));
battery.energy.setCapacity(TileBattery.getEnergyForTier(itemBlock.getTier(itemStack)));
battery.energy.setEnergy(itemBlock.getEnergy(itemStack));
battery.updateStructure();
world.setBlockMetadataWithNotify(x, y, z, itemBlock.getTier(itemStack), 3);
}
}
@ -69,6 +72,39 @@ public class BlockBattery extends BlockIOBase implements ITileEntityProvider
}
}
@Override
public boolean onSneakUseWrench(World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ)
{
if (!world.isRemote)
{
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
world.setBlock(x, y, z, 0);
}
return true;
}
@Override
public ArrayList<ItemStack> getBlockDropped(World world, int x, int y, int z, int metadata, int fortune)
{
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
ItemStack itemStack = new ItemStack(this, 1);
if (world.getBlockTileEntity(x, y, z) instanceof TileBattery)
{
TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z);
ItemBlockBattery itemBlock = (ItemBlockBattery) itemStack.getItem();
itemBlock.setTier(itemStack, (byte) metadata);
itemBlock.setEnergy(itemStack, battery.energy.getEnergy());
}
ret.add(itemStack);
return ret;
}
@Override
public boolean renderAsNormalBlock()
{

View file

@ -36,7 +36,8 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
private BatteryNetwork network;
public boolean markUpdate = false;
public boolean markClientUpdate = false;
public boolean markDistributionUpdate = false;
public TileBattery()
{
@ -50,7 +51,7 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
*/
public static long getEnergyForTier(int tier)
{
return (long) Math.pow(1000000, tier + 1);
return (long) Math.pow(100000, tier + 1) + 900000;
}
@Override
@ -74,9 +75,9 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
}
}
this.energy.setMaxTransfer(DEFAULT_WATTAGE * this.getNetwork().getConnectors().size());
this.getNetwork().redistribute();
markUpdate = true;
energy.setMaxTransfer(DEFAULT_WATTAGE * this.getNetwork().getConnectors().size());
markDistributionUpdate = true;
markClientUpdate = true;
}
}
@ -87,12 +88,13 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
if (!this.worldObj.isRemote)
{
if (this.produce() > 0)
if ((markDistributionUpdate || this.produce() > 0) && ticks % 5 == 0)
{
this.getNetwork().redistribute();
markDistributionUpdate = false;
}
if (markUpdate && ticks % 5 == 0)
if (markClientUpdate && ticks % 5 == 0)
{
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@ -103,9 +105,8 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{
long returnValue = super.onReceiveEnergy(from, receive, doReceive);
if (ticks % 5 == 0)
this.getNetwork().redistribute();
markUpdate = true;
markDistributionUpdate = true;
markClientUpdate = true;
return returnValue;
}
@ -113,9 +114,8 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract)
{
long returnValue = super.onExtractEnergy(from, extract, doExtract);
if (ticks % 5 == 0)
this.getNetwork().redistribute();
markUpdate = true;
markDistributionUpdate = true;
markClientUpdate = true;
return returnValue;
}