Hbm-s-Nuclear-Tech-GIT/com/hbm/blocks/TileEntityConverterRfHe.java
2016-06-28 23:20:42 +02:00

225 lines
6.4 KiB
Java

package com.hbm.blocks;
import java.util.ArrayList;
import java.util.List;
import com.hbm.calc.UnionOfTileEntitiesAndBooleans;
import com.hbm.interfaces.IConductor;
import com.hbm.interfaces.IConsumer;
import com.hbm.interfaces.ISource;
import com.hbm.lib.Library;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
public class TileEntityConverterRfHe extends TileEntity implements ISource, IEnergyReceiver {
public int power;
public final int maxPower = 1000000;
public List<IConsumer> list = new ArrayList();
public int age = 0;
public EnergyStorage storage = new EnergyStorage(4000000, 2500000, 2500000);
@Override
public void updateEntity() {
if (!worldObj.isRemote) {
if(storage.getEnergyStored() >= 400000 && power + 100000 <= maxPower)
{
storage.setEnergyStored(storage.getEnergyStored() - 400000);
power += 100000;
}
if(storage.getEnergyStored() >= 40000 && power + 10000 <= maxPower)
{
storage.setEnergyStored(storage.getEnergyStored() - 40000);
power += 10000;
}
if(storage.getEnergyStored() >= 4000 && power + 1000 <= maxPower)
{
storage.setEnergyStored(storage.getEnergyStored() - 4000);
power += 1000;
}
if(storage.getEnergyStored() >= 400 && power + 100 <= maxPower)
{
storage.setEnergyStored(storage.getEnergyStored() - 400);
power += 100;
}
if(storage.getEnergyStored() >= 40 && power + 10 <= maxPower)
{
storage.setEnergyStored(storage.getEnergyStored() - 40);
power += 10;
}
if(storage.getEnergyStored() >= 4 && power + 1 <= maxPower)
{
storage.setEnergyStored(storage.getEnergyStored() - 4);
power += 1;
}
}
age++;
if(age >= 20)
{
age = 0;
}
if(age == 9 || age == 19)
ffgeuaInit();
}
@Override
public boolean canConnectEnergy(ForgeDirection from) {
return true;
}
@Override
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
return storage.receiveEnergy(maxReceive, simulate);
}
@Override
public int getEnergyStored(ForgeDirection from) {
return storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(ForgeDirection from) {
return storage.getMaxEnergyStored();
}
@Override
public void ffgeua(int x, int y, int z, boolean newTact) {
Block block = this.worldObj.getBlock(x, y, z);
TileEntity tileentity = this.worldObj.getTileEntity(x, y, z);
if(block == ModBlocks.factory_titanium_conductor && this.worldObj.getBlock(x, y + 1, z) == ModBlocks.factory_titanium_core)
{
tileentity = this.worldObj.getTileEntity(x, y + 1, z);
}
if(block == ModBlocks.factory_titanium_conductor && this.worldObj.getBlock(x, y - 1, z) == ModBlocks.factory_titanium_core)
{
tileentity = this.worldObj.getTileEntity(x, y - 1, z);
}
if(block == ModBlocks.factory_advanced_conductor && this.worldObj.getBlock(x, y + 1, z) == ModBlocks.factory_advanced_core)
{
tileentity = this.worldObj.getTileEntity(x, y + 1, z);
}
if(block == ModBlocks.factory_advanced_conductor && this.worldObj.getBlock(x, y - 1, z) == ModBlocks.factory_advanced_core)
{
tileentity = this.worldObj.getTileEntity(x, y - 1, z);
}
if(tileentity instanceof IConductor)
{
if(tileentity instanceof TileEntityCable)
{
if(Library.checkUnionList(((TileEntityCable)tileentity).uoteab, this))
{
for(int i = 0; i < ((TileEntityCable)tileentity).uoteab.size(); i++)
{
if(((TileEntityCable)tileentity).uoteab.get(i).source == this)
{
if(((TileEntityCable)tileentity).uoteab.get(i).ticked != newTact)
{
((TileEntityCable)tileentity).uoteab.get(i).ticked = newTact;
ffgeua(x, y + 1, z, getTact());
ffgeua(x, y - 1, z, getTact());
ffgeua(x - 1, y, z, getTact());
ffgeua(x + 1, y, z, getTact());
ffgeua(x, y, z - 1, getTact());
ffgeua(x, y, z + 1, getTact());
}
}
}
} else {
((TileEntityCable)tileentity).uoteab.add(new UnionOfTileEntitiesAndBooleans(this, newTact));
}
}
if(tileentity instanceof TileEntityWireCoated)
{
if(Library.checkUnionList(((TileEntityWireCoated)tileentity).uoteab, this))
{
for(int i = 0; i < ((TileEntityWireCoated)tileentity).uoteab.size(); i++)
{
if(((TileEntityWireCoated)tileentity).uoteab.get(i).source == this)
{
if(((TileEntityWireCoated)tileentity).uoteab.get(i).ticked != newTact)
{
((TileEntityWireCoated)tileentity).uoteab.get(i).ticked = newTact;
ffgeua(x, y + 1, z, getTact());
ffgeua(x, y - 1, z, getTact());
ffgeua(x - 1, y, z, getTact());
ffgeua(x + 1, y, z, getTact());
ffgeua(x, y, z - 1, getTact());
ffgeua(x, y, z + 1, getTact());
}
}
}
} else {
((TileEntityWireCoated)tileentity).uoteab.add(new UnionOfTileEntitiesAndBooleans(this, newTact));
}
}
}
if(tileentity instanceof IConsumer && newTact && !(tileentity instanceof TileEntityMachineBattery && ((TileEntityMachineBattery)tileentity).conducts))
{
list.add((IConsumer)tileentity);
}
if(!newTact)
{
int size = list.size();
if(size > 0)
{
int part = this.power / size;
for(IConsumer consume : list)
{
if(consume.getPower() < consume.getMaxPower())
{
if(consume.getMaxPower() - consume.getPower() >= part)
{
this.power -= part;
consume.setPower(consume.getPower() + part);
} else {
this.power -= consume.getMaxPower() - consume.getPower();
consume.setPower(consume.getMaxPower());
}
}
}
}
list.clear();
}
}
@Override
public void ffgeuaInit() {
ffgeua(this.xCoord, this.yCoord + 1, this.zCoord, getTact());
ffgeua(this.xCoord, this.yCoord - 1, this.zCoord, getTact());
ffgeua(this.xCoord - 1, this.yCoord, this.zCoord, getTact());
ffgeua(this.xCoord + 1, this.yCoord, this.zCoord, getTact());
ffgeua(this.xCoord, this.yCoord, this.zCoord - 1, getTact());
ffgeua(this.xCoord, this.yCoord, this.zCoord + 1, getTact());
}
public boolean getTact() {
if(age >= 0 && age < 10)
{
return true;
}
return false;
}
public int getPowerScaled(int i) {
return (power * i) / maxPower;
}
public int getFluxScaled(int i) {
return (storage.getEnergyStored() * i) / storage.getMaxEnergyStored();
}
}