Java Code Examples for net.minecraft.util.math.BlockPos.MutableBlockPos#move()

The following examples show how to use net.minecraft.util.math.BlockPos.MutableBlockPos#move() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PipeNet.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HashMap<BlockPos, Node<NodeDataType>> findAllConnectedBlocks(BlockPos startPos) {
    HashMap<BlockPos, Node<NodeDataType>> observedSet = new HashMap<>();
    observedSet.put(startPos, getNodeAt(startPos));
    Node<NodeDataType> firstNode = getNodeAt(startPos);
    MutableBlockPos currentPos = new MutableBlockPos(startPos);
    Stack<EnumFacing> moveStack = new Stack<>();
    main:
    while (true) {
        for (EnumFacing facing : EnumFacing.VALUES) {
            currentPos.move(facing);
            Node<NodeDataType> secondNode = getNodeAt(currentPos);
            //if there is node, and it can connect with previous node, add it to list, and set previous node as current
            if (secondNode != null && canNodesConnect(firstNode, facing, secondNode, this) && !observedSet.containsKey(currentPos)) {
                observedSet.put(currentPos.toImmutable(), getNodeAt(currentPos));
                firstNode = secondNode;
                moveStack.push(facing.getOpposite());
                continue main;
            } else currentPos.move(facing.getOpposite());
        }
        if (!moveStack.isEmpty()) {
            currentPos.move(moveStack.pop());
            firstNode = getNodeAt(currentPos);
        } else break;
    }
    return observedSet;
}
 
Example 2
Source File: BlockFrame.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    ItemStack stackInHand = playerIn.getHeldItem(hand);
    if (stackInHand.isEmpty() || !(stackInHand.getItem() instanceof FrameItemBlock))
        return false;
    IBlockState blockState = ((FrameItemBlock) stackInHand.getItem()).getBlockState(stackInHand);
    if (blockState != worldIn.getBlockState(pos))
        return false;
    MutableBlockPos blockPos = new MutableBlockPos(pos);
    for (int i = 0; i < 32; i++) {
        IBlockState stateHere = worldIn.getBlockState(blockPos);
        if (stateHere == state) {
            blockPos.move(EnumFacing.UP);
            continue;
        }
        if (canPlaceBlockAt(worldIn, blockPos)) {
            worldIn.setBlockState(blockPos, blockState);
            if (!playerIn.capabilities.isCreativeMode)
                stackInHand.shrink(1);
            return true;
        } else {
            return false;
        }
    }
    return false;
}
 
Example 3
Source File: TreeHandler.java    From BetterChests with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean handleHarvest(IBetterChest chest, IBlockState state, World world, BlockPos pos) {
	MutableBlockPos start = new MutableBlockPos(pos);
	while (canBreak(world, start)) {
		start.move(EnumFacing.UP);
	}
	start.move(EnumFacing.DOWN);

	if (start.getY() >= pos.getY()) {
		BlockPos target = search(world, pos);
		IBlockState targetState = world.getBlockState(target);
		targetState.getBlock().breakBlock(world, pos, state);
		PlantHarvestHelper.breakBlockHandleDrop(world, target, targetState, chest);
		return true;
	}
	return false;
}
 
Example 4
Source File: MetaTileEntityTank.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Map<BlockPos, MetaTileEntityTank> findConnectedTanks(MetaTileEntityTank tank, Set<BlockPos> visitedSet) {
    BlockPos startPos = tank.getPos();
    HashMap<BlockPos, MetaTileEntityTank> observedSet = new HashMap<>();
    if (visitedSet.contains(startPos)) {
        return observedSet;
    }
    observedSet.put(tank.getPos(), tank);
    visitedSet.add(startPos);
    MetaTileEntityTank firstNode = observedSet.get(startPos);
    MutableBlockPos currentPos = new MutableBlockPos(startPos);
    Stack<EnumFacing> moveStack = new Stack<>();
    int currentAmount = 0;
    int scanSizeLimit = tank.maxSizeHorizontal * tank.maxSizeHorizontal * tank.maxSizeVertical * 4;
    main: while (true) {
        if (currentAmount >= scanSizeLimit) {
            break;
        }
        for (EnumFacing facing : EnumFacing.VALUES) {
            currentPos.move(facing);
            MetaTileEntityTank metaTileEntity;
            if (!visitedSet.contains(currentPos) &&
                !observedSet.containsKey(currentPos) &&
                (metaTileEntity = tank.getTankTile(currentPos)) != null &&
                canTanksConnect(firstNode, metaTileEntity, facing)) {
                observedSet.put(metaTileEntity.getPos(), metaTileEntity);
                visitedSet.add(metaTileEntity.getPos());
                firstNode = metaTileEntity;
                moveStack.push(facing.getOpposite());
                currentAmount++;
                continue main;
            } else currentPos.move(facing.getOpposite());
        }
        if (!moveStack.isEmpty()) {
            currentPos.move(moveStack.pop());
            firstNode = observedSet.get(currentPos);
        } else break;
    }
    return observedSet;
}
 
Example 5
Source File: BlockFrame.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean canFrameSupportVertical(World worldIn, BlockPos framePos) {
    MutableBlockPos blockPos = new MutableBlockPos(framePos);
    do {
        blockPos.move(EnumFacing.DOWN);
        IBlockState blockState = worldIn.getBlockState(blockPos);
        if (!(blockState.getBlock() instanceof BlockFrame)) {
            return blockState.getBlockFaceShape(worldIn, blockPos, EnumFacing.UP) == BlockFaceShape.SOLID;
        }
    } while (true);
}
 
Example 6
Source File: EnergyNet.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List<RoutePath> computePatches(BlockPos startPos) {
    ArrayList<RoutePath> readyPaths = new ArrayList<>();
    RoutePath currentPath = new RoutePath();
    Node<WireProperties> firstNode = getNodeAt(startPos);
    currentPath.path.put(startPos, firstNode.data);
    readyPaths.add(currentPath.cloneAndCompute(startPos));
    HashSet<BlockPos> observedSet = new HashSet<>();
    observedSet.add(startPos);
    MutableBlockPos currentPos = new MutableBlockPos(startPos);
    Stack<EnumFacing> moveStack = new Stack<>();
    main:
    while (true) {
        for (EnumFacing facing : EnumFacing.VALUES) {
            currentPos.move(facing);
            Node<WireProperties> secondNode = getNodeAt(currentPos);
            if (secondNode != null && canNodesConnect(firstNode, facing, secondNode, this) && !observedSet.contains(currentPos)) {
                BlockPos immutablePos = currentPos.toImmutable();
                observedSet.add(immutablePos);
                firstNode = secondNode;
                moveStack.push(facing.getOpposite());
                currentPath.path.put(immutablePos, getNodeAt(immutablePos).data);
                if (secondNode.isActive) {
                    //if we are on active node, this is end of our path
                    RoutePath finalizedPath = currentPath.cloneAndCompute(immutablePos);
                    readyPaths.add(finalizedPath);
                }
                continue main;
            } else {
                currentPos.move(facing.getOpposite());
            }
        }
        if (!moveStack.isEmpty()) {
            currentPos.move(moveStack.pop());
            //also remove already visited block from path
            currentPath.path.remove(currentPos);
        } else break;
    }
    return readyPaths;
}
 
Example 7
Source File: FoamSprayerBehavior.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static List<BlockPos> gatherReplacableBlocks(World worldIn, BlockPos centerPos, int maxRadiusSq) {
    HashSet<BlockPos> observedSet = new HashSet<>();
    ArrayList<BlockPos> resultAirBlocks = new ArrayList<>();
    observedSet.add(centerPos);
    resultAirBlocks.add(centerPos);
    Stack<EnumFacing> moveStack = new Stack<>();
    MutableBlockPos currentPos = new MutableBlockPos(centerPos);
    main:
    while (true) {
        for (EnumFacing facing : EnumFacing.VALUES) {
            currentPos.move(facing);
            IBlockState blockStateHere = worldIn.getBlockState(currentPos);
            //if there is node, and it can connect with previous node, add it to list, and set previous node as current
            if (blockStateHere.getBlock().isReplaceable(worldIn, currentPos) &&
                currentPos.distanceSq(centerPos) <= maxRadiusSq && !observedSet.contains(currentPos)) {
                BlockPos immutablePos = currentPos.toImmutable();
                observedSet.add(immutablePos);
                resultAirBlocks.add(immutablePos);
                moveStack.push(facing.getOpposite());
                continue main;
            } else currentPos.move(facing.getOpposite());
        }
        if (!moveStack.isEmpty()) {
            currentPos.move(moveStack.pop());
        } else break;
    }
    resultAirBlocks.sort(Comparator.comparing(it -> it.distanceSq(centerPos)));
    return resultAirBlocks;
}
 
Example 8
Source File: FoamSprayerBehavior.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static List<BlockPos> gatherFrameBlocks(World worldIn, BlockPos centerPos, int maxRadiusSq) {
    HashSet<BlockPos> observedSet = new HashSet<>();
    ArrayList<BlockPos> resultFrameBlocks = new ArrayList<>();
    observedSet.add(centerPos);
    resultFrameBlocks.add(centerPos);
    IBlockState frameState = null;
    Stack<EnumFacing> moveStack = new Stack<>();
    MutableBlockPos currentPos = new MutableBlockPos(centerPos);
    main:
    while (true) {
        for (EnumFacing facing : EnumFacing.VALUES) {
            currentPos.move(facing);
            IBlockState blockStateHere = worldIn.getBlockState(currentPos);
            //if there is node, and it can connect with previous node, add it to list, and set previous node as current
            if (blockStateHere.getBlock() instanceof BlockFrame &&
                currentPos.distanceSq(centerPos) <= maxRadiusSq &&
                (frameState == null || frameState == blockStateHere) && !observedSet.contains(currentPos)) {
                BlockPos immutablePos = currentPos.toImmutable();
                observedSet.add(immutablePos);
                resultFrameBlocks.add(immutablePos);
                moveStack.push(facing.getOpposite());
                frameState = blockStateHere;
                continue main;
            } else currentPos.move(facing.getOpposite());
        }
        if (!moveStack.isEmpty()) {
            currentPos.move(moveStack.pop());
        } else break;
    }
    resultFrameBlocks.sort(Comparator.comparing(it -> it.distanceSq(centerPos)));
    return resultFrameBlocks;
}
 
Example 9
Source File: ReedHandler.java    From BetterChests with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean handleHarvest(IBetterChest chest, IBlockState state, World world, BlockPos pos) {
	MutableBlockPos mut = new MutableBlockPos(pos);
	while (world.getBlockState(mut).getBlock() == state.getBlock()) {
		mut.move(EnumFacing.UP);
	}
	mut.move(EnumFacing.DOWN);
	if (mut.getY() != pos.getY()) {
		PlantHarvestHelper.breakBlockHandleDrop(world, mut, state, chest);
		return true;
	}
	return false;
}
 
Example 10
Source File: BlockFrame.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected boolean canBlockStay(World worldIn, BlockPos pos) {
    MutableBlockPos currentPos = new MutableBlockPos(pos);
    currentPos.move(EnumFacing.DOWN);
    IBlockState downState = worldIn.getBlockState(currentPos);
    if (downState.getBlock() instanceof BlockFrame) {
        if (canFrameSupportVertical(worldIn, currentPos)) {
            return true;
        }
    } else if (downState.getBlockFaceShape(worldIn, currentPos, EnumFacing.UP) == BlockFaceShape.SOLID) {
        return true;
    }
    currentPos.move(EnumFacing.UP);
    HashSet<BlockPos> observedSet = new HashSet<>();
    Stack<EnumFacing> moveStack = new Stack<>();
    main:
    while (true) {
        for (EnumFacing facing : EnumFacing.HORIZONTALS) {
            currentPos.move(facing);
            IBlockState blockStateHere = worldIn.getBlockState(currentPos);
            //if there is node, and it can connect with previous node, add it to list, and set previous node as current
            if (blockStateHere.getBlock() instanceof BlockFrame && currentPos.distanceSq(pos) <= SCAFFOLD_PILLAR_RADIUS_SQ && !observedSet.contains(currentPos)) {
                observedSet.add(currentPos.toImmutable());
                currentPos.move(EnumFacing.DOWN);
                downState = worldIn.getBlockState(currentPos);
                if (downState.getBlock() instanceof BlockFrame) {
                    if (canFrameSupportVertical(worldIn, currentPos)) {
                        return true;
                    }
                } else if (downState.getBlockFaceShape(worldIn, currentPos, EnumFacing.UP) == BlockFaceShape.SOLID) {
                    return true;
                }
                currentPos.move(EnumFacing.UP);
                moveStack.push(facing.getOpposite());
                continue main;
            } else currentPos.move(facing.getOpposite());
        }
        if (!moveStack.isEmpty()) {
            currentPos.move(moveStack.pop());
        } else break;
    }
    return false;
}