Search in sources :

Example 1 with BlockBlulectricCable

use of com.bluepowermod.block.power.BlockBlulectricCable in project BluePower by Qmunity.

the class TileBlulectricCable method getCapability.

@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    List<Direction> directions = new ArrayList<>(BlockBlulectricCable.FACING.getPossibleValues());
    if (level != null) {
        BlockState state = getBlockState();
        if (state.getBlock() instanceof BlockBlulectricCable) {
            // Remove upward connections
            directions.remove(state.getValue(BlockBlulectricCable.FACING));
            // Make sure the cable is on the same side of the block
            directions.removeIf(d -> level.getBlockState(worldPosition.relative(d)).getBlock() instanceof BlockBlulectricCable && level.getBlockState(worldPosition.relative(d)).getValue(BlockBlulectricCable.FACING) != state.getValue(BlockBlulectricCable.FACING));
        }
    }
    if (cap == CapabilityBlutricity.BLUTRICITY_CAPABILITY && (side == null || directions.contains(side))) {
        if (blutricityCap == null)
            blutricityCap = LazyOptional.of(() -> storage);
        return blutricityCap.cast();
    }
    return LazyOptional.empty();
}
Also used : BlockState(net.minecraft.block.BlockState) BlockBlulectricCable(com.bluepowermod.block.power.BlockBlulectricCable) ArrayList(java.util.ArrayList) Direction(net.minecraft.util.Direction) Nonnull(javax.annotation.Nonnull)

Example 2 with BlockBlulectricCable

use of com.bluepowermod.block.power.BlockBlulectricCable in project BluePower by Qmunity.

the class TileBlulectricCable method tick.

@Override
public void tick() {
    storage.resetCurrent();
    if (level != null && !level.isClientSide) {
        BlockState state = getBlockState();
        if (state.getBlock() instanceof BlockBlulectricCable) {
            List<Direction> directions = new ArrayList<>(BlockBlulectricCable.FACING.getPossibleValues());
            // Check the side has capability
            directions.removeIf(d -> !getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, d).isPresent());
            // Balance power of attached blulectric blocks.
            for (Direction facing : directions) {
                Block fBlock = level.getBlockState(worldPosition.relative(facing)).getBlock();
                if (fBlock != Blocks.AIR && fBlock != Blocks.WATER) {
                    TileEntity tile = level.getBlockEntity(worldPosition.relative(facing));
                    if (tile != null)
                        tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, facing.getOpposite()).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
                } else {
                    TileEntity tile = level.getBlockEntity(worldPosition.relative(facing).relative(state.getValue(BlockBlulectricCable.FACING).getOpposite()));
                    if (tile != null)
                        tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, state.getValue(BlockBlulectricCable.FACING)).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BlockBlulectricCable(com.bluepowermod.block.power.BlockBlulectricCable) IPowerBase(com.bluepowermod.api.power.IPowerBase) CompoundNBT(net.minecraft.nbt.CompoundNBT) CapabilityBlutricity(com.bluepowermod.api.power.CapabilityBlutricity) EnergyHelper(com.bluepowermod.helper.EnergyHelper) TileMachineBase(com.bluepowermod.tile.TileMachineBase) Direction(net.minecraft.util.Direction) NetworkManager(net.minecraft.network.NetworkManager) Blocks(net.minecraft.block.Blocks) Capability(net.minecraftforge.common.capabilities.Capability) LazyOptional(net.minecraftforge.common.util.LazyOptional) ArrayList(java.util.ArrayList) List(java.util.List) BlutricityStorage(com.bluepowermod.api.power.BlutricityStorage) Block(net.minecraft.block.Block) SUpdateTileEntityPacket(net.minecraft.network.play.server.SUpdateTileEntityPacket) BPTileEntityType(com.bluepowermod.tile.BPTileEntityType) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) INBT(net.minecraft.nbt.INBT) Nullable(javax.annotation.Nullable) BlockState(net.minecraft.block.BlockState) BlockBlulectricCable(com.bluepowermod.block.power.BlockBlulectricCable) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) Direction(net.minecraft.util.Direction)

Aggregations

BlockBlulectricCable (com.bluepowermod.block.power.BlockBlulectricCable)2 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 BlockState (net.minecraft.block.BlockState)2 Direction (net.minecraft.util.Direction)2 BlutricityStorage (com.bluepowermod.api.power.BlutricityStorage)1 CapabilityBlutricity (com.bluepowermod.api.power.CapabilityBlutricity)1 IPowerBase (com.bluepowermod.api.power.IPowerBase)1 EnergyHelper (com.bluepowermod.helper.EnergyHelper)1 BPTileEntityType (com.bluepowermod.tile.BPTileEntityType)1 TileMachineBase (com.bluepowermod.tile.TileMachineBase)1 List (java.util.List)1 Nullable (javax.annotation.Nullable)1 Block (net.minecraft.block.Block)1 Blocks (net.minecraft.block.Blocks)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 INBT (net.minecraft.nbt.INBT)1 NetworkManager (net.minecraft.network.NetworkManager)1 SUpdateTileEntityPacket (net.minecraft.network.play.server.SUpdateTileEntityPacket)1 TileEntity (net.minecraft.tileentity.TileEntity)1