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