use of gregtech.api.capability.IEnergyContainer in project GregTech by GregTechCE.
the class EnergyContainerHandler method update.
@Override
public void update() {
if (getMetaTileEntity().getWorld().isRemote)
return;
if (getEnergyStored() >= getOutputVoltage() && getOutputVoltage() > 0 && getOutputAmperage() > 0) {
long outputVoltage = getOutputVoltage();
long outputAmperes = Math.min(getEnergyStored() / outputVoltage, getOutputAmperage());
if (outputAmperes == 0)
return;
long amperesUsed = 0;
for (EnumFacing side : EnumFacing.VALUES) {
if (!outputsEnergy(side))
continue;
TileEntity tileEntity = metaTileEntity.getWorld().getTileEntity(metaTileEntity.getPos().offset(side));
EnumFacing oppositeSide = side.getOpposite();
if (tileEntity != null && tileEntity.hasCapability(GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER, oppositeSide)) {
IEnergyContainer energyContainer = tileEntity.getCapability(GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER, oppositeSide);
if (energyContainer == null || !energyContainer.inputsEnergy(oppositeSide))
continue;
amperesUsed += energyContainer.acceptEnergyFromNetwork(oppositeSide, outputVoltage, outputAmperes - amperesUsed);
if (amperesUsed == outputAmperes)
break;
}
}
if (amperesUsed > 0) {
setEnergyStored(getEnergyStored() - amperesUsed * outputVoltage);
}
}
}
use of gregtech.api.capability.IEnergyContainer in project GregTech by GregTechCE.
the class BlockCable method refreshSelfState.
private void refreshSelfState(World world, BlockPos selfPos) {
boolean shouldPlaceEmitter = false;
PooledMutableBlockPos mutableBlockPos = PooledMutableBlockPos.retain(selfPos);
for (EnumFacing facing : EnumFacing.VALUES) {
mutableBlockPos.move(facing);
EnumFacing opposite = facing.getOpposite();
IBlockState blockState = world.getBlockState(mutableBlockPos);
if (blockState.getBlock().hasTileEntity(blockState)) {
TileEntity tileEntity = world.getTileEntity(mutableBlockPos);
IEnergyContainer container = tileEntity == null ? null : tileEntity.getCapability(IEnergyContainer.CAPABILITY_ENERGY_CONTAINER, opposite);
if (container != null && container.outputsEnergy(opposite)) {
shouldPlaceEmitter = true;
break;
}
}
mutableBlockPos.move(opposite);
}
mutableBlockPos.release();
// remove emitter if we don't have inputs, or add if we have inputs and it doesn't exist
TileEntity currentTileEntity = world.getTileEntity(selfPos);
if (shouldPlaceEmitter) {
if (!(currentTileEntity instanceof TileEntityCableEmitter)) {
TileEntityCableEmitter tileEntityCableEmitter = new TileEntityCableEmitter();
world.setTileEntity(selfPos, tileEntityCableEmitter);
tileEntityCableEmitter.refreshConnections();
}
} else if (currentTileEntity instanceof TileEntityCableEmitter) {
world.removeTileEntity(selfPos);
}
}
use of gregtech.api.capability.IEnergyContainer in project GregTech by GregTechCE.
the class RecipeMapMultiblockController method addDisplayText.
@Override
protected void addDisplayText(List<ITextComponent> textList) {
super.addDisplayText(textList);
if (isStructureFormed()) {
IEnergyContainer energyContainer = recipeMapWorkable.getEnergyContainer();
if (energyContainer != null && energyContainer.getEnergyCapacity() > 0) {
long maxVoltage = energyContainer.getInputVoltage();
String voltageName = GTValues.VN[GTUtility.getTierByVoltage(maxVoltage)];
textList.add(new TextComponentTranslation("gregtech.multiblock.max_energy_per_tick", maxVoltage, voltageName));
}
if (!recipeMapWorkable.isWorkingEnabled()) {
textList.add(new TextComponentTranslation("gregtech.multiblock.work_paused"));
} else if (recipeMapWorkable.isActive()) {
textList.add(new TextComponentTranslation("gregtech.multiblock.running"));
int currentProgress = (int) (recipeMapWorkable.getProgressPercent() * 100);
textList.add(new TextComponentTranslation("gregtech.multiblock.progress", currentProgress));
} else {
textList.add(new TextComponentTranslation("gregtech.multiblock.idling"));
}
if (recipeMapWorkable.isHasNotEnoughEnergy()) {
textList.add(new TextComponentTranslation("gregtech.multiblock.not_enough_energy").setStyle(new Style().setColor(TextFormatting.RED)));
}
}
}
use of gregtech.api.capability.IEnergyContainer in project GregTech by GregTechCE.
the class CoverSolarPanel method update.
@Override
public void update() {
World world = coverHolder.getWorld();
BlockPos blockPos = coverHolder.getPos().up();
if (canSeeSunClearly(world, blockPos)) {
IEnergyContainer energyContainer = coverHolder.getCapability(GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER, null);
if (energyContainer != null) {
energyContainer.addEnergy(EUt);
}
}
}
use of gregtech.api.capability.IEnergyContainer in project GregTech by GregTechCE.
the class EnergyContainerBatteryBuffer method update.
@Override
public void update() {
if (!metaTileEntity.getWorld().isRemote) {
this.batterySlotsUsedThisTick.clear();
EnumFacing outFacing = metaTileEntity.getFrontFacing();
TileEntity tileEntity = metaTileEntity.getWorld().getTileEntity(metaTileEntity.getPos().offset(outFacing));
if (tileEntity == null) {
return;
}
IEnergyContainer energyContainer = tileEntity.getCapability(GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER, outFacing.getOpposite());
if (energyContainer == null) {
return;
}
IItemHandlerModifiable inventory = getInventory();
long voltage = getOutputVoltage();
long maxAmperage = 0L;
TIntList slotsList = new TIntArrayList();
for (int i = 0; i < inventory.getSlots(); i++) {
ItemStack batteryStack = inventory.getStackInSlot(i);
IElectricItem electricItem = getBatteryContainer(batteryStack);
if (electricItem == null)
continue;
if (electricItem.discharge(voltage, getTier(), true, true, true) == voltage) {
slotsList.add(i);
maxAmperage++;
}
}
if (maxAmperage == 0)
return;
long amperageUsed = energyContainer.acceptEnergyFromNetwork(outFacing.getOpposite(), voltage, maxAmperage);
if (amperageUsed == 0)
return;
for (int i : slotsList.toArray()) {
ItemStack batteryStack = inventory.getStackInSlot(i);
IElectricItem electricItem = getBatteryContainer(batteryStack);
if (electricItem == null)
continue;
electricItem.discharge(voltage, getTier(), true, true, false);
inventory.setStackInSlot(i, batteryStack);
if (--amperageUsed == 0)
break;
}
notifyEnergyListener(false);
}
}
Aggregations