use of gregtech.common.blocks.tileentity.TileEntityCableEmitter in project GregTech by GregTechCE.
the class BlockCable method notifyNetworkAboutRefresh.
private void notifyNetworkAboutRefresh(World world, BlockPos initialPos) {
PooledMutableBlockPos currentPos = PooledMutableBlockPos.retain(initialPos.getX(), initialPos.getY(), initialPos.getZ());
List<BlockPos> visited = new ArrayList<>();
Stack<EnumFacing> moveStack = new Stack<>();
while (true) {
for (EnumFacing facing : EnumFacing.VALUES) {
currentPos.move(facing);
EnumFacing opposite = facing.getOpposite();
if (!visited.contains(currentPos)) {
visited.add(currentPos.toImmutable());
} else {
currentPos.move(opposite);
continue;
}
if (world.getBlockState(currentPos).getBlock() instanceof BlockCable) {
world.setBlockState(currentPos.up(), Blocks.LOG.getDefaultState());
// if we are cable, move forward, and update emitter, if we has one
TileEntityCableEmitter emitter = (TileEntityCableEmitter) world.getTileEntity(currentPos);
if (emitter != null)
emitter.refreshConnections();
moveStack.push(opposite);
continue;
}
// move back if we aren't cable and din't continue
currentPos.move(opposite);
}
// if we didn't found any cable, go back, or return
if (!moveStack.isEmpty()) {
currentPos.move(moveStack.pop());
} else
break;
}
// DebugRenderer.blockPosSet = ImmutableSet.copyOf(visited);
currentPos.release();
}
use of gregtech.common.blocks.tileentity.TileEntityCableEmitter 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);
}
}
Aggregations