Search in sources :

Example 46 with Direction

use of net.minecraft.util.Direction in project BluePower by Qmunity.

the class BlockBPCableBase method getStateForPos.

private BlockState getStateForPos(World world, BlockPos pos, BlockState state, Direction face) {
    List<Direction> directions = new ArrayList<>(FACING.getPossibleValues());
    List<Direction> internal = null;
    boolean connected_left = false;
    boolean connected_right = false;
    boolean connected_front = false;
    boolean connected_back = false;
    boolean join_left = false;
    boolean join_right = false;
    boolean join_front = false;
    boolean join_back = false;
    // Make sure the side we are trying to connect on isn't blocked.
    TileEntity ownTile = world.getBlockEntity(pos);
    if (ownTile instanceof TileBPMultipart) {
        directions.removeIf(d -> ((TileBPMultipart) ownTile).isSideBlocked(getCapability(), d));
        internal = ((TileBPMultipart) ownTile).getStates().stream().filter(s -> s.getBlock() == this).map(s -> s.getValue(FACING)).collect(Collectors.toList());
    }
    // Make sure the cable is on the same side of the block
    directions.removeIf(d -> {
        TileEntity t = world.getBlockEntity(pos.relative(d));
        return (world.getBlockState(pos.relative(d)).getBlock() == this && world.getBlockState(pos.relative(d)).getValue(FACING) != face) || (t instanceof TileBPMultipart && ((TileBPMultipart) t).getStates().stream().noneMatch(s -> s.getValue(FACING) == face));
    });
    // Populate all directions
    for (Direction d : directions) {
        TileEntity tileEntity = world.getBlockEntity(pos.relative(d));
        BlockState dirState = world.getBlockState(pos.relative(d));
        BlockPos dirPos = pos.relative(d);
        boolean join = false;
        // If Air look for a change in Direction
        if (world.getBlockState(pos.relative(d)).getBlock() == Blocks.AIR) {
            dirState = world.getBlockState(pos.relative(d).relative(face.getOpposite()));
            dirPos = pos.relative(d).relative(face.getOpposite());
            if (dirState.getBlock() == this && dirState.getValue(FACING) == d) {
                tileEntity = world.getBlockEntity(pos.relative(d).relative(face.getOpposite()));
                join = true;
            } else if (dirState.getBlock() instanceof BlockBPMultipart) {
                tileEntity = world.getBlockEntity(pos.relative(d).relative(face.getOpposite()));
                if (tileEntity instanceof TileBPMultipart && ((TileBPMultipart) tileEntity).getStates().stream().filter(s -> s.getBlock() == this).anyMatch(s -> s.getValue(FACING) == d)) {
                    join = true;
                } else {
                    tileEntity = null;
                }
            }
        }
        // Check Capability for Direction
        switch(state.getValue(FACING)) {
            case UP:
            case DOWN:
                switch(d) {
                    case EAST:
                        connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case WEST:
                        connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case NORTH:
                        connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case SOUTH:
                        connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                }
                break;
            case NORTH:
                switch(d) {
                    case WEST:
                        connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case EAST:
                        connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case UP:
                        connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case DOWN:
                        connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                }
                break;
            case SOUTH:
                switch(d) {
                    case EAST:
                        connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case WEST:
                        connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case UP:
                        connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case DOWN:
                        connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                }
                break;
            case EAST:
                switch(d) {
                    case NORTH:
                        connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case SOUTH:
                        connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case UP:
                        connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case DOWN:
                        connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                }
                break;
            case WEST:
                switch(d) {
                    case SOUTH:
                        connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case NORTH:
                        connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case UP:
                        connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                    case DOWN:
                        connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
                        break;
                }
        }
    }
    if (internal != null)
        for (Direction d : internal) {
            switch(state.getValue(FACING)) {
                case UP:
                case DOWN:
                    switch(d) {
                        case EAST:
                            connected_left = true;
                            break;
                        case WEST:
                            connected_right = true;
                            break;
                        case NORTH:
                            connected_back = true;
                            break;
                        case SOUTH:
                            connected_front = true;
                            break;
                    }
                    break;
                case NORTH:
                    switch(d) {
                        case WEST:
                            connected_left = true;
                            break;
                        case EAST:
                            connected_right = true;
                            break;
                        case UP:
                            connected_back = true;
                            break;
                        case DOWN:
                            connected_front = true;
                            break;
                    }
                    break;
                case SOUTH:
                    switch(d) {
                        case EAST:
                            connected_left = true;
                            break;
                        case WEST:
                            connected_right = true;
                            break;
                        case UP:
                            connected_back = true;
                            break;
                        case DOWN:
                            connected_front = true;
                            break;
                    }
                    break;
                case EAST:
                    switch(d) {
                        case NORTH:
                            connected_left = true;
                            break;
                        case SOUTH:
                            connected_right = true;
                            break;
                        case UP:
                            connected_back = true;
                            break;
                        case DOWN:
                            connected_front = true;
                            break;
                    }
                    break;
                case WEST:
                    switch(d) {
                        case SOUTH:
                            connected_left = true;
                            break;
                        case NORTH:
                            connected_right = true;
                            break;
                        case UP:
                            connected_back = true;
                            break;
                        case DOWN:
                            connected_front = true;
                            break;
                    }
            }
        }
    FluidState fluidstate = world.getFluidState(pos);
    return state.setValue(CONNECTED_LEFT, connected_left).setValue(CONNECTED_RIGHT, connected_right).setValue(CONNECTED_FRONT, connected_front).setValue(CONNECTED_BACK, connected_back).setValue(JOIN_LEFT, join_left).setValue(JOIN_RIGHT, join_right).setValue(JOIN_FRONT, join_front).setValue(JOIN_BACK, join_back).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidState(net.minecraft.fluid.FluidState) IWorld(net.minecraft.world.IWorld) Direction(net.minecraft.util.Direction) IWorldReader(net.minecraft.world.IWorldReader) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) StateContainer(net.minecraft.state.StateContainer) IBlockReader(net.minecraft.world.IBlockReader) net.minecraft.block(net.minecraft.block) ISelectionContext(net.minecraft.util.math.shapes.ISelectionContext) VoxelShape(net.minecraft.util.math.shapes.VoxelShape) Nullable(javax.annotation.Nullable) Fluids(net.minecraft.fluid.Fluids) BooleanProperty(net.minecraft.state.BooleanProperty) LivingEntity(net.minecraft.entity.LivingEntity) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) BlockItemUseContext(net.minecraft.item.BlockItemUseContext) Collectors(java.util.stream.Collectors) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Capability(net.minecraftforge.common.capabilities.Capability) AABBUtils(com.bluepowermod.util.AABBUtils) List(java.util.List) IBPPartBlock(com.bluepowermod.api.multipart.IBPPartBlock) Material(net.minecraft.block.material.Material) TileEntity(net.minecraft.tileentity.TileEntity) VoxelShapes(net.minecraft.util.math.shapes.VoxelShapes) DirectionProperty(net.minecraft.state.DirectionProperty) BlockStateProperties(net.minecraft.state.properties.BlockStateProperties) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Direction(net.minecraft.util.Direction) FluidState(net.minecraft.fluid.FluidState)

Example 47 with Direction

use of net.minecraft.util.Direction in project Overloaded by CJ-MC-Mods.

the class TileEnergyExtractor method tick.

@Override
public void tick() {
    if (getLevel().isClientSide) {
        return;
    }
    BlockPos me = this.getBlockPos();
    TileEntity frontTE = getLevel().getBlockEntity(me.offset(getFacing().getNormal()));
    if (frontTE == null) {
        return;
    }
    LazyOptional<IHyperHandlerEnergy> optionalStorage = frontTE.getCapability(HYPER_ENERGY_HANDLER, getFacing().getOpposite());
    if (!optionalStorage.isPresent()) {
        return;
    }
    IHyperHandlerEnergy storage = optionalStorage.orElse(null);
    for (Direction facing : Direction.values()) {
        if (facing == getFacing())
            continue;
        TileEntity te = level.getBlockEntity(me.offset(facing.getNormal()));
        if (te == null)
            continue;
        LazyOptional<IEnergyStorage> optionalReceiver = te.getCapability(ENERGY, facing.getOpposite());
        LongEnergyStack energy = storage.take(new LongEnergyStack(Long.MAX_VALUE), false);
        if (energy.getAmount() == 0L)
            return;
        optionalReceiver.ifPresent(receiver -> {
            if (!receiver.canReceive())
                return;
            int acceptedAmount = receiver.receiveEnergy((int) Math.min(energy.getAmount(), Integer.MAX_VALUE), true);
            if (acceptedAmount != 0) {
                LongEnergyStack actualTaken = storage.take(new LongEnergyStack(acceptedAmount), true);
                receiver.receiveEnergy((int) Math.min(actualTaken.getAmount(), Integer.MAX_VALUE), false);
            }
        });
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ITickableTileEntity(net.minecraft.tileentity.ITickableTileEntity) LongEnergyStack(com.cjm721.overloaded.storage.stacks.intint.LongEnergyStack) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.util.math.BlockPos) IHyperHandlerEnergy(com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy) Direction(net.minecraft.util.Direction)

Example 48 with Direction

use of net.minecraft.util.Direction in project NetherEx by LogicTechCorp.

the class EnokiCapBlock method isValidPosition.

@Override
public boolean isValidPosition(BlockState state, IWorldReader world, BlockPos pos) {
    BlockState upState = world.getBlockState(pos.up());
    Block upBlock = upState.getBlock();
    if (upBlock != NetherExBlocks.ENOKI_MUSHROOM_STEM.get() && upBlock != NetherExBlocks.LIVELY_NETHERRACK.get()) {
        if (!upState.isAir(world, pos.up())) {
            return false;
        } else {
            boolean foundStem = false;
            for (Direction direction : Direction.Plane.HORIZONTAL) {
                BlockState offsetState = world.getBlockState(pos.offset(direction));
                if (offsetState.getBlock() == NetherExBlocks.ENOKI_MUSHROOM_STEM.get()) {
                    if (foundStem) {
                        return false;
                    }
                    foundStem = true;
                } else if (!offsetState.isAir(world, pos.offset(direction))) {
                    return false;
                }
            }
            return foundStem;
        }
    } else {
        return true;
    }
}
Also used : BlockState(net.minecraft.block.BlockState) Block(net.minecraft.block.Block) Direction(net.minecraft.util.Direction)

Example 49 with Direction

use of net.minecraft.util.Direction in project NetherEx by LogicTechCorp.

the class EnokiCapBlock method tick.

@Override
public void tick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
    if (!state.isValidPosition(world, pos)) {
        world.destroyBlock(pos, true);
    } else {
        BlockPos downPos = pos.down();
        if (world.isAirBlock(downPos) && downPos.getY() < world.getDimension().getHeight()) {
            int age = state.get(AGE);
            if (age < 5 && ForgeHooks.onCropsGrowPre(world, downPos, state, true)) {
                boolean validPosition = false;
                boolean foundNetherrack = false;
                BlockState upState = world.getBlockState(pos.up());
                Block upBlock = upState.getBlock();
                if (upBlock == NetherExBlocks.LIVELY_NETHERRACK.get()) {
                    validPosition = true;
                } else if (upBlock == NetherExBlocks.ENOKI_MUSHROOM_STEM.get()) {
                    int height = 1;
                    for (int k = 0; k < 4; k++) {
                        Block block1 = world.getBlockState(pos.up(height + 1)).getBlock();
                        if (block1 != NetherExBlocks.ENOKI_MUSHROOM_STEM.get()) {
                            if (block1 == NetherExBlocks.LIVELY_NETHERRACK.get()) {
                                foundNetherrack = true;
                            }
                            break;
                        }
                        height++;
                    }
                    if (height < 2 || height <= random.nextInt(foundNetherrack ? 5 : 4)) {
                        validPosition = true;
                    }
                } else if (upState.isAir(world, pos.down())) {
                    validPosition = true;
                }
                if (validPosition && areAllNeighborsEmpty(world, downPos, null) && world.isAirBlock(pos.up(2))) {
                    world.setBlockState(pos, ((EnokiStemBlock) NetherExBlocks.ENOKI_MUSHROOM_STEM.get()).makeConnections(world, pos), 2);
                    this.placeCap(world, downPos, age);
                } else if (age < 4) {
                    int randomHeight = random.nextInt(4);
                    if (foundNetherrack) {
                        randomHeight++;
                    }
                    boolean placedCap = false;
                    for (int i1 = 0; i1 < randomHeight; ++i1) {
                        Direction direction = Direction.Plane.HORIZONTAL.random(random);
                        BlockPos offsetPos = pos.offset(direction);
                        if (world.isAirBlock(offsetPos) && world.isAirBlock(offsetPos.down()) && areAllNeighborsEmpty(world, offsetPos, direction.getOpposite())) {
                            this.placeCap(world, offsetPos, age + 1);
                            placedCap = true;
                        }
                    }
                    if (placedCap) {
                        world.setBlockState(pos, ((EnokiStemBlock) NetherExBlocks.ENOKI_MUSHROOM_STEM.get()).makeConnections(world, pos), 2);
                    } else {
                        this.placeDeadCap(world, pos);
                    }
                } else {
                    this.placeDeadCap(world, pos);
                }
                ForgeHooks.onCropsGrowPost(world, pos, state);
            }
        }
    }
}
Also used : BlockState(net.minecraft.block.BlockState) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction)

Example 50 with Direction

use of net.minecraft.util.Direction in project NetherEx by LogicTechCorp.

the class EnokiCapBlock method growTreeRecursive.

private static void growTreeRecursive(IWorld world, BlockPos pos, Random random, BlockPos otherPos, int maximumHeight, int minimumHeight) {
    int randomHeight = random.nextInt(4) + 1;
    if (minimumHeight == 0) {
        randomHeight++;
    }
    for (int j = 0; j < randomHeight; j++) {
        BlockPos downPos = pos.down(j + 1);
        if (!areAllNeighborsEmpty(world, downPos, null)) {
            return;
        }
        world.setBlockState(downPos, ((EnokiStemBlock) NetherExBlocks.ENOKI_MUSHROOM_STEM.get()).makeConnections(world, downPos), 2);
        world.setBlockState(downPos.up(), ((EnokiStemBlock) NetherExBlocks.ENOKI_MUSHROOM_STEM.get()).makeConnections(world, downPos.up()), 2);
    }
    boolean continueGrowing = false;
    if (minimumHeight < 4) {
        int additionalHeight = random.nextInt(4);
        if (minimumHeight == 0) {
            additionalHeight++;
        }
        for (int additional = 0; additional < additionalHeight; additional++) {
            Direction direction = Direction.Plane.HORIZONTAL.random(random);
            BlockPos offsetPos = pos.down(randomHeight).offset(direction);
            if (Math.abs(offsetPos.getX() - otherPos.getX()) < maximumHeight && Math.abs(offsetPos.getZ() - otherPos.getZ()) < maximumHeight && world.isAirBlock(offsetPos) && world.isAirBlock(offsetPos.up()) && areAllNeighborsEmpty(world, offsetPos, direction.getOpposite())) {
                continueGrowing = true;
                world.setBlockState(offsetPos, ((EnokiStemBlock) NetherExBlocks.ENOKI_MUSHROOM_STEM.get()).makeConnections(world, offsetPos), 2);
                world.setBlockState(offsetPos.offset(direction.getOpposite()), ((EnokiStemBlock) NetherExBlocks.ENOKI_MUSHROOM_STEM.get()).makeConnections(world, offsetPos.offset(direction.getOpposite())), 2);
                growTreeRecursive(world, offsetPos, random, otherPos, maximumHeight, minimumHeight + 1);
            }
        }
    }
    if (!continueGrowing) {
        world.setBlockState(pos.down(randomHeight), NetherExBlocks.ENOKI_MUSHROOM_CAP.get().getDefaultState().with(AGE, 5), 2);
    }
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction)

Aggregations

Direction (net.minecraft.util.Direction)51 BlockState (net.minecraft.block.BlockState)18 TileEntity (net.minecraft.tileentity.TileEntity)17 BlockPos (net.minecraft.util.math.BlockPos)16 ItemStack (net.minecraft.item.ItemStack)13 Block (net.minecraft.block.Block)10 Nonnull (javax.annotation.Nonnull)8 Nullable (javax.annotation.Nullable)8 PlayerEntity (net.minecraft.entity.player.PlayerEntity)6 CompoundNBT (net.minecraft.nbt.CompoundNBT)6 World (net.minecraft.world.World)6 IPowerBase (com.bluepowermod.api.power.IPowerBase)5 Capability (net.minecraftforge.common.capabilities.Capability)5 BlutricityStorage (com.bluepowermod.api.power.BlutricityStorage)4 CapabilityBlutricity (com.bluepowermod.api.power.CapabilityBlutricity)4 EnergyHelper (com.bluepowermod.helper.EnergyHelper)4 BPTileEntityType (com.bluepowermod.tile.BPTileEntityType)4 TileMachineBase (com.bluepowermod.tile.TileMachineBase)4 INBT (net.minecraft.nbt.INBT)4 LazyOptional (net.minecraftforge.common.util.LazyOptional)4