Search in sources :

Example 1 with IEnergyContainer

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);
        }
    }
}
Also used : MetaTileEntity(gregtech.api.metatileentity.MetaTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IEnergyContainer(gregtech.api.capability.IEnergyContainer) EnumFacing(net.minecraft.util.EnumFacing)

Example 2 with IEnergyContainer

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);
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) PooledMutableBlockPos(net.minecraft.util.math.BlockPos.PooledMutableBlockPos) IBlockState(net.minecraft.block.state.IBlockState) IEnergyContainer(gregtech.api.capability.IEnergyContainer) EnumFacing(net.minecraft.util.EnumFacing) TileEntityCableEmitter(gregtech.common.blocks.tileentity.TileEntityCableEmitter)

Example 3 with IEnergyContainer

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)));
        }
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IEnergyContainer(gregtech.api.capability.IEnergyContainer) Style(net.minecraft.util.text.Style)

Example 4 with IEnergyContainer

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);
        }
    }
}
Also used : IEnergyContainer(gregtech.api.capability.IEnergyContainer) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 5 with IEnergyContainer

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);
    }
}
Also used : MetaTileEntity(gregtech.api.metatileentity.MetaTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) IElectricItem(gregtech.api.capability.IElectricItem) IEnergyContainer(gregtech.api.capability.IEnergyContainer) EnumFacing(net.minecraft.util.EnumFacing) TIntList(gnu.trove.list.TIntList) ItemStack(net.minecraft.item.ItemStack) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Aggregations

IEnergyContainer (gregtech.api.capability.IEnergyContainer)7 TileEntity (net.minecraft.tileentity.TileEntity)5 EnumFacing (net.minecraft.util.EnumFacing)5 MetaTileEntity (gregtech.api.metatileentity.MetaTileEntity)2 BlockPos (net.minecraft.util.math.BlockPos)2 PooledMutableBlockPos (net.minecraft.util.math.BlockPos.PooledMutableBlockPos)2 World (net.minecraft.world.World)2 TIntList (gnu.trove.list.TIntList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 IElectricItem (gregtech.api.capability.IElectricItem)1 TileEntityCableEmitter (gregtech.common.blocks.tileentity.TileEntityCableEmitter)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 Style (net.minecraft.util.text.Style)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)1