Search in sources :

Example 86 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MC-Prefab by Brian-Wuest.

the class GuiStructure method rayTrace.

@Nullable
public static BlockHitResult rayTrace(Vec3 from, Vec3 to, BlockGetter world) {
    ClipContext rayTraceContext = new ClipContext(from, to, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, null);
    BlockHitResult result = world.clip(rayTraceContext);
    return result;
}
Also used : ClipContext(net.minecraft.world.level.ClipContext) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Nullable(javax.annotation.Nullable)

Example 87 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MagicPlugin by elBukkit.

the class CompatibilityUtils method setAutoBlockState.

@Override
public boolean setAutoBlockState(Block block, Location target, BlockFace facing, boolean physics, Player originator) {
    if (block == null || facing == null || target == null)
        return false;
    net.minecraft.world.level.block.state.BlockState blockState = ((CraftBlock) block).getNMS();
    if (blockState == null)
        return false;
    net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
    ItemStack blockItem = new ItemStack(block.getType());
    ServerPlayer originatorHandle = ((CraftPlayer) originator).getHandle();
    ServerLevel world = ((CraftWorld) block.getWorld()).getHandle();
    Object item = platform.getItemUtils().getHandle(platform.getItemUtils().makeReal(blockItem));
    if (originatorHandle == null || world == null || item == null) {
        return false;
    }
    BlockPos blockPosition = new BlockPos(block.getX(), block.getY(), block.getZ());
    Vec3 vec3D = new Vec3(target.getX(), target.getY(), target.getZ());
    Direction direction;
    try {
        direction = Direction.valueOf(facing.name());
    } catch (Exception ex) {
        platform.getLogger().log(Level.SEVERE, "Could not translate to NMS direction: " + facing);
        return false;
    }
    BlockHitResult hitResult = new BlockHitResult(vec3D, direction, blockPosition, false);
    BlockPlaceContext actionContext = new BlockPlaceContext(originatorHandle, InteractionHand.MAIN_HAND, (net.minecraft.world.item.ItemStack) item, hitResult);
    net.minecraft.world.level.block.state.BlockState state = nmsBlock.getStateForPlacement(actionContext);
    if (state == null)
        return false;
    ((CraftBlock) block).setTypeAndData(state, physics);
    return true;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) CraftBlock(org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock) Direction(net.minecraft.core.Direction) ServerPlayer(net.minecraft.server.level.ServerPlayer) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) ItemStack(org.bukkit.inventory.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld)

Example 88 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MagicPlugin by elBukkit.

the class CompatibilityUtils method setAutoBlockState.

@Override
public boolean setAutoBlockState(Block block, Location target, BlockFace facing, boolean physics, Player originator) {
    if (block == null || facing == null || target == null)
        return false;
    net.minecraft.world.level.block.state.BlockState blockState = ((CraftBlock) block).getNMS();
    if (blockState == null)
        return false;
    net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
    ItemStack blockItem = new ItemStack(block.getType());
    ServerPlayer originatorHandle = ((CraftPlayer) originator).getHandle();
    ServerLevel world = ((CraftWorld) block.getWorld()).getHandle();
    Object item = platform.getItemUtils().getHandle(platform.getItemUtils().makeReal(blockItem));
    if (originatorHandle == null || world == null || item == null) {
        return false;
    }
    BlockPos blockPosition = new BlockPos(block.getX(), block.getY(), block.getZ());
    Vec3 vec3D = new Vec3(target.getX(), target.getY(), target.getZ());
    Direction direction;
    try {
        direction = Direction.valueOf(facing.name());
    } catch (Exception ex) {
        platform.getLogger().log(Level.SEVERE, "Could not translate to NMS direction: " + facing);
        return false;
    }
    BlockHitResult hitResult = new BlockHitResult(vec3D, direction, blockPosition, false);
    BlockPlaceContext actionContext = new BlockPlaceContext(originatorHandle, InteractionHand.MAIN_HAND, (net.minecraft.world.item.ItemStack) item, hitResult);
    net.minecraft.world.level.block.state.BlockState state = nmsBlock.getStateForPlacement(actionContext);
    if (state == null)
        return false;
    CraftBlock cBlock = (CraftBlock) block;
    CraftBlock.setTypeAndData(cBlock.getHandle(), cBlock.getPosition(), cBlock.getNMS(), state, physics);
    return true;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) CraftPlayer(org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer) CraftBlock(org.bukkit.craftbukkit.v1_18_R1.block.CraftBlock) Direction(net.minecraft.core.Direction) ServerPlayer(net.minecraft.server.level.ServerPlayer) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) ItemStack(org.bukkit.inventory.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 89 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project AlexsMobs by Alex-the-666.

the class AnimalAILootChests method hasLineOfSightChest.

public boolean hasLineOfSightChest() {
    HitResult raytraceresult = entity.level.clip(new ClipContext(entity.getEyePosition(1.0F), new Vec3(blockPos.getX() + 0.5, blockPos.getY() + 0.5, blockPos.getZ() + 0.5), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity));
    if (raytraceresult instanceof BlockHitResult) {
        BlockHitResult blockRayTraceResult = (BlockHitResult) raytraceresult;
        BlockPos pos = blockRayTraceResult.getBlockPos();
        return pos.equals(blockPos) || entity.level.isEmptyBlock(pos) || this.entity.level.getBlockEntity(pos) == this.entity.level.getBlockEntity(blockPos);
    }
    return true;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 90 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project AlexsMobs by Alex-the-666.

the class EntityVoidWorm method createPortal.

public void createPortal(Vec3 from, Vec3 to, @Nullable Direction outDir) {
    if (!level.isClientSide && portalTarget == null) {
        Vec3 Vector3d = new Vec3(this.getX(), this.getEyeY(), this.getZ());
        HitResult result = this.level.clip(new ClipContext(Vector3d, from, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this));
        Vec3 vec = result.getLocation() != null ? result.getLocation() : this.position();
        if (result instanceof BlockHitResult) {
            BlockHitResult result1 = (BlockHitResult) result;
            vec = vec.add(net.minecraft.world.phys.Vec3.atLowerCornerOf(result1.getDirection().getNormal()));
        }
        EntityVoidPortal portal = AMEntityRegistry.VOID_PORTAL.create(level);
        portal.setPos(vec.x, vec.y, vec.z);
        Vec3 dirVec = vec.subtract(this.position());
        Direction dir = Direction.getNearest(dirVec.x, dirVec.y, dirVec.z);
        portal.setAttachmentFacing(dir);
        portal.setLifespan(10000);
        if (!level.isClientSide) {
            level.addFreshEntity(portal);
        }
        portalTarget = portal;
        portal.setDestination(new BlockPos(to.x, to.y, to.z), outDir);
        makePortalCooldown = 300;
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)181 BlockPos (net.minecraft.core.BlockPos)117 Vec3 (net.minecraft.world.phys.Vec3)79 BlockState (net.minecraft.world.level.block.state.BlockState)66 ItemStack (net.minecraft.world.item.ItemStack)63 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)41 ClipContext (net.minecraft.world.level.ClipContext)33 Player (net.minecraft.world.entity.player.Player)32 ServerLevel (net.minecraft.server.level.ServerLevel)23 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)20 Entity (net.minecraft.world.entity.Entity)19 InteractionHand (net.minecraft.world.InteractionHand)18 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)18 ServerPlayer (net.minecraft.server.level.ServerPlayer)17 LivingEntity (net.minecraft.world.entity.LivingEntity)17 AABB (net.minecraft.world.phys.AABB)17 EntityHitResult (net.minecraft.world.phys.EntityHitResult)17 Minecraft (net.minecraft.client.Minecraft)16