use of net.minecraft.world.phys.BlockHitResult in project Electrodynamics by aurilisdev.
the class MathUtils method getRaytracedBlock.
public static Location getRaytracedBlock(Level world, Vec3 direction, Vec3 from, double rayLength) {
// Just normalize for safety. Allows the direction
// vector to be from some block to another with no
// consideration for more math.
Vec3 rayPath = direction.normalize().scale(rayLength);
Vec3 to = from.add(rayPath);
ClipContext rayContext = new ClipContext(from, to, ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, null);
BlockHitResult rayHit = world.clip(rayContext);
return rayHit.getType() != Type.BLOCK ? null : new Location(rayHit.getBlockPos());
}
use of net.minecraft.world.phys.BlockHitResult in project JustEnoughResources by way2muchnoise.
the class ChunkProfiler method profileChunk.
private void profileChunk(ChunkAccess chunk) {
final ResourceKey<Level> worldRegistryKey = level.dimension();
this.timer.startChunk(worldRegistryKey);
Map<String, Integer[]> temp = new HashMap<>();
BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
HitResult rayTraceResult = new BlockHitResult(new Vec3(0, 0, 0), Direction.DOWN, blockPos, true);
Player player = Minecraft.getInstance().player;
final int maxY = chunk.getHighestSectionPosition();
for (int y = 0; y < maxY; y++) for (int x = 0; x < CHUNK_SIZE; x++) for (int z = 0; z < CHUNK_SIZE; z++) {
blockPos.set(x + chunk.getPos().x * CHUNK_SIZE, y, z + chunk.getPos().z * CHUNK_SIZE);
BlockState blockState = chunk.getBlockState(new BlockPos(x, y, z));
if (blacklist.contains(blockState))
continue;
final String key = MapKeys.getKey(blockState, rayTraceResult, level, blockPos, player);
if (!dimensionData.dropsMap.containsKey(key)) {
dimensionData.dropsMap.put(key, getDrops(level, blockPos, blockState));
}
if (!dimensionData.silkTouchMap.containsKey(key)) {
Block block = blockState.getBlock();
boolean canSilkTouch = block.canHarvestBlock(blockState, level, blockPos, player);
dimensionData.silkTouchMap.put(key, canSilkTouch);
}
Integer[] array = temp.get(key);
if (array == null) {
array = new Integer[CHUNK_HEIGHT];
Arrays.fill(array, 0);
}
array[y]++;
temp.put(key, array);
}
for (Map.Entry<String, Integer[]> entry : temp.entrySet()) {
Integer[] array = dimensionData.distributionMap.get(entry.getKey());
if (array == null) {
array = new Integer[CHUNK_HEIGHT];
Arrays.fill(array, 0);
}
for (int i = 0; i < CHUNK_HEIGHT; i++) array[i] += entry.getValue()[i];
dimensionData.distributionMap.put(entry.getKey(), array);
}
this.timer.endChunk(dimensionKey);
}
use of net.minecraft.world.phys.BlockHitResult in project AlexsMobs by Alex-the-666.
the class EntityDropBear method hasLineOfSightBlock.
private boolean hasLineOfSightBlock(BlockPos destinationBlock) {
Vec3 Vector3d = new Vec3(this.getX(), this.getEyeY(), this.getZ());
Vec3 blockVec = net.minecraft.world.phys.Vec3.atCenterOf(destinationBlock);
BlockHitResult result = this.level.clip(new ClipContext(Vector3d, blockVec, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this));
return result.getBlockPos().equals(destinationBlock);
}
use of net.minecraft.world.phys.BlockHitResult in project pollen by MoonflowerTeam.
the class BucketItemBase method use.
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
Fluid content = this.getFluid();
ItemStack itemStack = player.getItemInHand(hand);
BlockHitResult hitResult = getPlayerPOVHitResult(level, player, content == Fluids.EMPTY ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE);
if (hitResult.getType() == HitResult.Type.MISS)
return InteractionResultHolder.pass(itemStack);
if (hitResult.getType() != HitResult.Type.BLOCK)
return InteractionResultHolder.pass(itemStack);
BlockPos pos = hitResult.getBlockPos();
Direction direction = hitResult.getDirection();
BlockPos offsetPos = pos.relative(direction);
if (level.mayInteract(player, pos) && player.mayUseItemAt(offsetPos, direction, itemStack)) {
BlockState blockState = level.getBlockState(pos);
if (content == Fluids.EMPTY) {
if (blockState.getBlock() instanceof BucketPickup) {
Fluid fluid = ((BucketPickup) blockState.getBlock()).takeLiquid(level, pos, blockState);
if (fluid != Fluids.EMPTY) {
player.awardStat(Stats.ITEM_USED.get(this));
this.playFillSound(player, level, pos);
ItemStack itemStack2 = ItemUtils.createFilledResult(itemStack, player, new ItemStack(fluid.getBucket()));
if (player instanceof ServerPlayer)
CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer) player, new ItemStack(fluid.getBucket()));
return InteractionResultHolder.sidedSuccess(itemStack2, level.isClientSide());
}
}
} else {
BlockPos emptyPos = blockState.getBlock() instanceof LiquidBlockContainer && content == Fluids.WATER ? pos : offsetPos;
if (this.emptyBucket(player, level, emptyPos, hitResult)) {
this.checkExtraContent(level, itemStack, emptyPos);
if (player instanceof ServerPlayer)
CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer) player, emptyPos, itemStack);
player.awardStat(Stats.ITEM_USED.get(this));
return InteractionResultHolder.sidedSuccess(this.getEmptySuccessItem(itemStack, player), level.isClientSide());
}
}
}
return InteractionResultHolder.fail(itemStack);
}
use of net.minecraft.world.phys.BlockHitResult in project TerraFirmaCraft by TerraFirmaCraft.
the class WoodenBucketItem method use.
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
final ItemStack stack = player.getItemInHand(hand);
final IFluidHandler handler = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).resolve().orElse(null);
if (handler == null) {
return InteractionResultHolder.pass(stack);
}
if (handler.getFluidInTank(0).isEmpty()) {
final BlockHitResult hit = Helpers.rayTracePlayer(level, player, ClipContext.Fluid.SOURCE_ONLY);
if (hit.getType() == HitResult.Type.BLOCK) {
final BlockPos pos = hit.getBlockPos();
final BlockState state = level.getBlockState(pos);
if (FluidHelpers.pickupFluidInto(level, pos, state, player, handler)) {
return InteractionResultHolder.success(stack);
}
final IFluidHandler blockHandler = FluidHelpers.getBlockEntityFluidHandler(level, pos, state);
if (blockHandler != null && FluidHelpers.transferExact(blockHandler, handler, getCapacity())) {
return InteractionResultHolder.success(stack);
}
}
return afterFillFailed(handler, level, player, stack, hand);
} else {
final BlockHitResult hit = Helpers.rayTracePlayer(level, player, ClipContext.Fluid.SOURCE_ONLY);
if (hit.getType() == HitResult.Type.BLOCK) {
final BlockPos pos = hit.getBlockPos();
final BlockState state = level.getBlockState(pos);
final IFluidHandler blockHandler = FluidHelpers.getBlockEntityFluidHandler(level, pos, state);
if (blockHandler != null && FluidHelpers.transferExact(handler, blockHandler, getCapacity())) {
return InteractionResultHolder.success(stack);
}
if (emptyContents(handler, level, pos, state, hit)) {
return InteractionResultHolder.sidedSuccess(player.isCreative() ? stack : new ItemStack(this), level.isClientSide);
}
}
return afterEmptyFailed(handler, level, player, stack, hand);
}
}
Aggregations