Search in sources :

Example 1 with TileEntityCableEmitter

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();
}
Also used : PooledMutableBlockPos(net.minecraft.util.math.BlockPos.PooledMutableBlockPos) EnumFacing(net.minecraft.util.EnumFacing) TileEntityCableEmitter(gregtech.common.blocks.tileentity.TileEntityCableEmitter) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) PooledMutableBlockPos(net.minecraft.util.math.BlockPos.PooledMutableBlockPos) Stack(java.util.Stack)

Example 2 with TileEntityCableEmitter

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

Aggregations

TileEntityCableEmitter (gregtech.common.blocks.tileentity.TileEntityCableEmitter)2 EnumFacing (net.minecraft.util.EnumFacing)2 PooledMutableBlockPos (net.minecraft.util.math.BlockPos.PooledMutableBlockPos)2 IEnergyContainer (gregtech.api.capability.IEnergyContainer)1 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 IBlockState (net.minecraft.block.state.IBlockState)1 TileEntity (net.minecraft.tileentity.TileEntity)1 BlockPos (net.minecraft.util.math.BlockPos)1