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();
}
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));
}
}
}
}
}
Aggregations