use of net.minecraft.world.item.context.DirectionalPlaceContext in project SolarCraftRepository by FINDERFEED.
the class MyFallingBlockEntity method tick.
public void tick() {
if (this.blockState.isAir()) {
this.discard();
} else {
Block block = this.blockState.getBlock();
++this.time;
if (!this.isNoGravity()) {
this.setDeltaMovement(this.getDeltaMovement().add(0.0D, -0.04D, 0.0D));
}
this.move(MoverType.SELF, this.getDeltaMovement());
if (!this.level.isClientSide) {
BlockPos blockpos = this.blockPosition();
boolean flag = this.blockState.getBlock() instanceof ConcretePowderBlock;
boolean flag1 = flag && this.level.getFluidState(blockpos).is(FluidTags.WATER);
double d0 = this.getDeltaMovement().lengthSqr();
if (flag && d0 > 1.0D) {
BlockHitResult blockhitresult = this.level.clip(new ClipContext(new Vec3(this.xo, this.yo, this.zo), this.position(), ClipContext.Block.COLLIDER, ClipContext.Fluid.SOURCE_ONLY, this));
if (blockhitresult.getType() != HitResult.Type.MISS && this.level.getFluidState(blockhitresult.getBlockPos()).is(FluidTags.WATER)) {
blockpos = blockhitresult.getBlockPos();
flag1 = true;
}
}
if (!this.onGround && !flag1) {
if (!this.level.isClientSide && (this.time > 100 && (blockpos.getY() <= this.level.getMinBuildHeight() || blockpos.getY() > this.level.getMaxBuildHeight()) || this.time > 600)) {
if (this.dropItem && this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
this.spawnAtLocation(block);
}
this.discard();
}
} else {
BlockState blockstate = this.level.getBlockState(blockpos);
this.setDeltaMovement(this.getDeltaMovement().multiply(0.7D, -0.5D, 0.7D));
if (!blockstate.is(Blocks.MOVING_PISTON)) {
if (!this.cancelDrop) {
boolean flag2 = blockstate.canBeReplaced(new DirectionalPlaceContext(this.level, blockpos, Direction.DOWN, ItemStack.EMPTY, Direction.UP));
boolean flag3 = FallingBlock.isFree(this.level.getBlockState(blockpos.below())) && (!flag || !flag1);
boolean flag4 = this.blockState.canSurvive(this.level, blockpos) && !flag3;
if (flag2 && flag4) {
if (this.blockState.hasProperty(BlockStateProperties.WATERLOGGED) && this.level.getFluidState(blockpos).getType() == Fluids.WATER) {
this.blockState = this.blockState.setValue(BlockStateProperties.WATERLOGGED, true);
}
if (this.level.setBlock(blockpos, this.blockState, 3)) {
((ServerLevel) this.level).getChunkSource().chunkMap.broadcast(this, new ClientboundBlockUpdatePacket(blockpos, this.level.getBlockState(blockpos)));
this.discard();
if (this.blockData != null && this.blockState.hasBlockEntity()) {
BlockEntity blockentity = this.level.getBlockEntity(blockpos);
if (blockentity != null) {
CompoundTag compoundtag = blockentity.saveWithoutMetadata();
for (String s : this.blockData.getAllKeys()) {
compoundtag.put(s, this.blockData.get(s).copy());
}
try {
blockentity.load(compoundtag);
} catch (Exception exception) {
SolarForge.LOGGER.error("Failed to load block entity from falling block", (Throwable) exception);
}
blockentity.setChanged();
}
}
} else if (this.dropItem && this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
this.discard();
this.callOnBrokenAfterFall(block, blockpos);
this.spawnAtLocation(block);
}
} else {
this.discard();
if (this.dropItem && this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
this.callOnBrokenAfterFall(block, blockpos);
this.spawnAtLocation(block);
}
}
} else {
this.discard();
this.callOnBrokenAfterFall(block, blockpos);
}
}
}
}
this.setDeltaMovement(this.getDeltaMovement().scale(0.98D));
}
}
use of net.minecraft.world.item.context.DirectionalPlaceContext in project Botania by VazkiiMods.
the class BehaviourCocoaBeans method execute.
@Nonnull
@Override
public ItemStack execute(BlockSource source, ItemStack stack) {
Block block = Blocks.COCOA;
Direction facing = source.getBlockState().getValue(DispenserBlock.FACING);
BlockPos pos = source.getPos().relative(facing);
Level world = source.getLevel();
BlockPlaceContext ctx = new DirectionalPlaceContext(source.getLevel(), source.getPos().relative(facing), facing, new ItemStack(block), facing.getOpposite());
BlockState cocoa = block.getStateForPlacement(ctx);
if (cocoa != null && world.isEmptyBlock(pos)) {
world.setBlockAndUpdate(pos, cocoa);
stack.shrink(1);
return stack;
}
return stack;
}
use of net.minecraft.world.item.context.DirectionalPlaceContext in project TerraFirmaCraft by TerraFirmaCraft.
the class TFCFallingBlockEntity method tick.
@Override
public void tick() {
final BlockState fallingBlockState = getBlockState();
if (fallingBlockState.isAir()) {
remove(RemovalReason.DISCARDED);
} else {
Block block = fallingBlockState.getBlock();
if (time++ == 0) {
// First tick, replace the existing block
BlockPos blockpos = blockPosition();
if (block == level.getBlockState(blockpos).getBlock()) {
level.removeBlock(blockpos, false);
} else if (!level.isClientSide) {
remove(RemovalReason.DISCARDED);
return;
}
}
if (!isNoGravity()) {
setDeltaMovement(getDeltaMovement().add(0.0D, -0.04D, 0.0D));
}
move(MoverType.SELF, getDeltaMovement());
if (!level.isClientSide) {
BlockPos posAt = blockPosition();
if (!onGround) {
failedBreakCheck = false;
if ((time > 100 && (posAt.getY() < 1 || posAt.getY() > 256)) || time > 600) {
if (dropItem && level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
spawnAtLocation(block);
}
remove(RemovalReason.DISCARDED);
}
} else {
// On ground
if (!failedBreakCheck) {
if (!level.isEmptyBlock(posAt) && canFallThrough(level, posAt, level.getBlockState(posAt))) {
level.destroyBlock(posAt, true);
failedBreakCheck = true;
return;
} else if (!level.isEmptyBlock(posAt.below()) && canFallThrough(level, posAt.below(), level.getBlockState(posAt.below()))) {
level.destroyBlock(posAt.below(), true);
failedBreakCheck = true;
return;
}
}
BlockState hitBlockState = level.getBlockState(posAt);
setDeltaMovement(getDeltaMovement().multiply(0.7D, -0.5D, 0.7D));
if (hitBlockState.getBlock() != Blocks.MOVING_PISTON) {
remove(RemovalReason.DISCARDED);
if (!dontSetBlock) {
if (hitBlockState.canBeReplaced(new DirectionalPlaceContext(this.level, posAt, Direction.DOWN, ItemStack.EMPTY, Direction.UP)) && fallingBlockState.canSurvive(this.level, posAt) && !FallingBlock.isFree(this.level.getBlockState(posAt.below()))) {
if (fallingBlockState.hasProperty(BlockStateProperties.WATERLOGGED) && this.level.getFluidState(posAt).getType() == Fluids.WATER) {
// todo: mixin
// ((FallingBlockEntityAccessor) this).accessor$setBlockState(fallingBlockState.setValue(BlockStateProperties.WATERLOGGED, Boolean.TRUE));
}
if (level.setBlockAndUpdate(posAt, fallingBlockState)) {
if (block instanceof FallingBlock) {
((FallingBlock) block).onLand(this.level, posAt, fallingBlockState, hitBlockState, this);
}
if (TFCTags.Blocks.CAN_LANDSLIDE.contains(fallingBlockState.getBlock())) {
level.getCapability(WorldTrackerCapability.CAPABILITY).ifPresent(cap -> cap.addLandslidePos(posAt));
}
// Sets the tile entity if it exists
if (blockData != null && fallingBlockState.hasBlockEntity()) {
BlockEntity tileEntity = level.getBlockEntity(posAt);
if (tileEntity != null) {
CompoundTag tileEntityData = tileEntity.saveWithoutMetadata();
for (String dataKey : tileEntityData.getAllKeys()) {
Tag dataElement = tileEntityData.get(dataKey);
if (!"x".equals(dataKey) && !"y".equals(dataKey) && !"z".equals(dataKey) && dataElement != null) {
tileEntityData.put(dataKey, dataElement.copy());
}
}
tileEntity.load(tileEntityData);
tileEntity.setChanged();
}
}
} else if (dropItem && level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
spawnAtLocation(block);
}
} else if (dropItem && this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
spawnAtLocation(block);
}
}
if (block instanceof IFallableBlock) {
((IFallableBlock) block).onceFinishedFalling(this.level, posAt, this);
}
}
}
}
setDeltaMovement(getDeltaMovement().scale(0.98D));
}
}
use of net.minecraft.world.item.context.DirectionalPlaceContext in project Applied-Energistics-2 by AppliedEnergistics.
the class CubeGeneratorBlockEntity method spawn.
private void spawn() {
this.level.removeBlock(this.worldPosition, false);
final Item i = this.is.getItem();
final Direction side = Direction.UP;
final int half = (int) Math.floor(this.size / 2);
for (int y = 0; y < this.size; y++) {
for (int x = -half; x < half; x++) {
for (int z = -half; z < half; z++) {
final BlockPos p = this.worldPosition.offset(x, y - 1, z);
UseOnContext useContext = new DirectionalPlaceContext(this.level, p, side, this.is, side.getOpposite());
i.useOn(useContext);
}
}
}
}
use of net.minecraft.world.item.context.DirectionalPlaceContext in project Cyclic by Lothrazar.
the class UtilPlaceBlocks method placeTorchSafely.
public static boolean placeTorchSafely(Level world, BlockPos blockPos, Direction solidBlockDirection) {
BlockItem torch = (BlockItem) Items.TORCH;
BlockPlaceContext context = new DirectionalPlaceContext(world, blockPos, solidBlockDirection, Items.TORCH.getDefaultInstance(), solidBlockDirection);
return torch.place(context).consumesAction();
}
Aggregations