Added battery wrench pick up and fixed distribution
This commit is contained in:
parent
0198bd3738
commit
6fa2530dd6
3 changed files with 65 additions and 30 deletions
|
@ -9,35 +9,34 @@ public class BatteryNetwork extends Network<BatteryNetwork, TileBattery>
|
||||||
public void redistribute(TileBattery... exclusion)
|
public void redistribute(TileBattery... exclusion)
|
||||||
{
|
{
|
||||||
long totalEnergy = 0;
|
long totalEnergy = 0;
|
||||||
|
long totalCapacity = 0;
|
||||||
|
|
||||||
for (TileBattery battery : this.getConnectors())
|
for (TileBattery battery : this.getConnectors())
|
||||||
{
|
{
|
||||||
totalEnergy += battery.energy.getEnergy();
|
totalEnergy += battery.energy.getEnergy();
|
||||||
|
totalCapacity += battery.energy.getEnergyCapacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
int amountOfNodes = this.getConnectors().size() - exclusion.length;
|
int amountOfNodes = this.getConnectors().size() - exclusion.length;
|
||||||
|
|
||||||
if (totalEnergy > 0 && amountOfNodes > 0)
|
if (totalEnergy > 0 && amountOfNodes > 0)
|
||||||
{
|
{
|
||||||
long totalPerBattery = totalEnergy / amountOfNodes;
|
long remainingEnergy = totalEnergy;
|
||||||
long totalPerBatteryRemainder = totalPerBattery + totalEnergy % amountOfNodes;
|
|
||||||
|
|
||||||
TileBattery firstNode = this.getFirstConnector();
|
TileBattery firstNode = this.getFirstConnector();
|
||||||
|
|
||||||
for (TileBattery battery : this.getConnectors())
|
for (TileBattery battery : this.getConnectors())
|
||||||
{
|
{
|
||||||
if (!Arrays.asList(exclusion).contains(battery))
|
if (battery != firstNode && !Arrays.asList(exclusion).contains(battery))
|
||||||
{
|
{
|
||||||
if (battery == firstNode)
|
double percentage = ((double) battery.energy.getEnergyCapacity() / (double) totalCapacity);
|
||||||
{
|
long energyForBattery = (long) Math.round(totalEnergy * percentage);
|
||||||
battery.energy.setEnergy(totalPerBatteryRemainder);
|
battery.energy.setEnergy(energyForBattery);
|
||||||
}
|
remainingEnergy -= energyForBattery;
|
||||||
else
|
|
||||||
{
|
|
||||||
battery.energy.setEnergy(totalPerBattery);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
firstNode.energy.setEnergy(remainingEnergy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,11 @@
|
||||||
*/
|
*/
|
||||||
package resonantinduction.electrical.battery;
|
package resonantinduction.electrical.battery;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import net.minecraft.block.ITileEntityProvider;
|
import net.minecraft.block.ITileEntityProvider;
|
||||||
import net.minecraft.entity.EntityLivingBase;
|
import net.minecraft.entity.EntityLivingBase;
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
@ -14,7 +17,6 @@ import resonantinduction.core.Reference;
|
||||||
import resonantinduction.core.Settings;
|
import resonantinduction.core.Settings;
|
||||||
import resonantinduction.core.prefab.block.BlockIOBase;
|
import resonantinduction.core.prefab.block.BlockIOBase;
|
||||||
import resonantinduction.core.render.RIBlockRenderingHandler;
|
import resonantinduction.core.render.RIBlockRenderingHandler;
|
||||||
import resonantinduction.electrical.Electrical;
|
|
||||||
import universalelectricity.api.CompatibilityModule;
|
import universalelectricity.api.CompatibilityModule;
|
||||||
import cpw.mods.fml.relauncher.Side;
|
import cpw.mods.fml.relauncher.Side;
|
||||||
import cpw.mods.fml.relauncher.SideOnly;
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
@ -44,15 +46,16 @@ public class BlockBattery extends BlockIOBase implements ITileEntityProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z);
|
||||||
battery.energy.setCapacity(TileBattery.getEnergyForTier(itemBlock.getTier(itemstack)));
|
battery.energy.setCapacity(TileBattery.getEnergyForTier(itemBlock.getTier(itemStack)));
|
||||||
battery.energy.setEnergy(itemBlock.getEnergy(itemstack));
|
battery.energy.setEnergy(itemBlock.getEnergy(itemStack));
|
||||||
battery.updateStructure();
|
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
|
@Override
|
||||||
public boolean renderAsNormalBlock()
|
public boolean renderAsNormalBlock()
|
||||||
{
|
{
|
||||||
|
|
|
@ -36,7 +36,8 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
|
||||||
|
|
||||||
private BatteryNetwork network;
|
private BatteryNetwork network;
|
||||||
|
|
||||||
public boolean markUpdate = false;
|
public boolean markClientUpdate = false;
|
||||||
|
public boolean markDistributionUpdate = false;
|
||||||
|
|
||||||
public TileBattery()
|
public TileBattery()
|
||||||
{
|
{
|
||||||
|
@ -50,7 +51,7 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
|
||||||
*/
|
*/
|
||||||
public static long getEnergyForTier(int tier)
|
public static long getEnergyForTier(int tier)
|
||||||
{
|
{
|
||||||
return (long) Math.pow(1000000, tier + 1);
|
return (long) Math.pow(100000, tier + 1) + 900000;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,9 +75,9 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.energy.setMaxTransfer(DEFAULT_WATTAGE * this.getNetwork().getConnectors().size());
|
energy.setMaxTransfer(DEFAULT_WATTAGE * this.getNetwork().getConnectors().size());
|
||||||
this.getNetwork().redistribute();
|
markDistributionUpdate = true;
|
||||||
markUpdate = true;
|
markClientUpdate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,12 +88,13 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
|
||||||
|
|
||||||
if (!this.worldObj.isRemote)
|
if (!this.worldObj.isRemote)
|
||||||
{
|
{
|
||||||
if (this.produce() > 0)
|
if ((markDistributionUpdate || this.produce() > 0) && ticks % 5 == 0)
|
||||||
{
|
{
|
||||||
this.getNetwork().redistribute();
|
this.getNetwork().redistribute();
|
||||||
|
markDistributionUpdate = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (markUpdate && ticks % 5 == 0)
|
if (markClientUpdate && ticks % 5 == 0)
|
||||||
{
|
{
|
||||||
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
|
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)
|
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
|
||||||
{
|
{
|
||||||
long returnValue = super.onReceiveEnergy(from, receive, doReceive);
|
long returnValue = super.onReceiveEnergy(from, receive, doReceive);
|
||||||
if (ticks % 5 == 0)
|
markDistributionUpdate = true;
|
||||||
this.getNetwork().redistribute();
|
markClientUpdate = true;
|
||||||
markUpdate = true;
|
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,9 +114,8 @@ public class TileBattery extends TileElectrical implements IConnector<BatteryNet
|
||||||
public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract)
|
public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract)
|
||||||
{
|
{
|
||||||
long returnValue = super.onExtractEnergy(from, extract, doExtract);
|
long returnValue = super.onExtractEnergy(from, extract, doExtract);
|
||||||
if (ticks % 5 == 0)
|
markDistributionUpdate = true;
|
||||||
this.getNetwork().redistribute();
|
markClientUpdate = true;
|
||||||
markUpdate = true;
|
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue