Search in sources :

Example 31 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Denizen by DenizenScript.

the class EntityHelperImpl method mapTrace.

@Override
public MapTraceResult mapTrace(LivingEntity from, double range) {
    Location start = from.getEyeLocation();
    Vector startVec = start.toVector();
    double xzLen = Math.cos((start.getPitch() % 360) * (Math.PI / 180));
    double nx = xzLen * Math.sin(-start.getYaw() * (Math.PI / 180));
    double ny = Math.sin(start.getPitch() * (Math.PI / 180));
    double nz = xzLen * Math.cos(start.getYaw() * (Math.PI / 180));
    Vector endVec = startVec.clone().add(new Vector(nx, -ny, nz).multiply(range));
    HitResult l = rayTrace(start.getWorld(), startVec, endVec);
    if (!(l instanceof BlockHitResult) || l.getLocation() == null) {
        return null;
    }
    Vector finalVec = new Vector(l.getLocation().x, l.getLocation().y, l.getLocation().z);
    MapTraceResult mtr = new MapTraceResult();
    switch(((BlockHitResult) l).getDirection()) {
        case NORTH:
            mtr.angle = BlockFace.NORTH;
            break;
        case SOUTH:
            mtr.angle = BlockFace.SOUTH;
            break;
        case EAST:
            mtr.angle = BlockFace.EAST;
            break;
        case WEST:
            mtr.angle = BlockFace.WEST;
            break;
    }
    // wallPosition - ((end - start).normalize() * 0.072)
    Vector hit = finalVec.clone().subtract((endVec.clone().subtract(startVec)).normalize().multiply(0.072));
    mtr.hitLocation = new Location(start.getWorld(), hit.getX(), hit.getY(), hit.getZ());
    return mtr;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Vector(org.bukkit.util.Vector) ResourceLocation(net.minecraft.resources.ResourceLocation) Location(org.bukkit.Location)

Example 32 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project arnicalib-mcmod by auioc.

the class EntityUtils method getEntityHitResult.

@Nullable
static EntityHitResult getEntityHitResult(Entity entity, double rayLength, float pickRadiusAddition, boolean blockMode) {
    Vec3[] viewRay = getEntityViewRay(entity, rayLength);
    Vec3 to = viewRay[1];
    if (blockMode) {
        BlockHitResult rayHitBlock = getBlockHitResult(entity, rayLength, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE);
        if (rayHitBlock.getType() != HitResult.Type.MISS) {
            to = rayHitBlock.getLocation();
        }
    }
    return getEntityHitResult(entity, viewRay[0], to, pickRadiusAddition);
}
Also used : Vec3(net.minecraft.world.phys.Vec3) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Nullable(javax.annotation.Nullable)

Example 33 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project arnicalib-mcmod by auioc.

the class EntityUtils method rayHitEntityOrBlockOrMiss.

static int rayHitEntityOrBlockOrMiss(Entity entity, double rayLength, float pickRadiusAddition, ClipContext.Block blockMode, ClipContext.Fluid fluidMode, Function<EntityHitResult, Integer> e, Function<BlockHitResult, Integer> b, Function<BlockHitResult, Integer> m) {
    EntityHitResult rayHitEntity = getEntityHitResult(entity, rayLength, pickRadiusAddition, false);
    if (rayHitEntity != null) {
        return e.apply(rayHitEntity);
    }
    BlockHitResult rayHitBlock = getBlockHitResult(entity, rayLength, blockMode, fluidMode);
    if (rayHitBlock.getType() != HitResult.Type.MISS) {
        return b.apply(rayHitBlock);
    } else {
        return m.apply(rayHitBlock);
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) EntityHitResult(net.minecraft.world.phys.EntityHitResult)

Example 34 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightAdapter method simulateItemUse.

@Override
public boolean simulateItemUse(org.bukkit.World world, BlockVector3 position, BaseItem item, Direction face) {
    CraftWorld craftWorld = (CraftWorld) world;
    ServerLevel worldServer = craftWorld.getHandle();
    ItemStack stack = CraftItemStack.asNMSCopy(BukkitAdapter.adapt(item instanceof BaseItemStack ? ((BaseItemStack) item) : new BaseItemStack(item.getType(), item.getNbtData(), 1)));
    stack.setTag((net.minecraft.nbt.CompoundTag) fromNative(item.getNbtData()));
    PaperweightFakePlayer fakePlayer;
    try {
        fakePlayer = fakePlayers.get(worldServer);
    } catch (ExecutionException ignored) {
        return false;
    }
    fakePlayer.setItemInHand(InteractionHand.MAIN_HAND, stack);
    fakePlayer.absMoveTo(position.getBlockX(), position.getBlockY(), position.getBlockZ(), (float) face.toVector().toYaw(), (float) face.toVector().toPitch());
    final BlockPos blockPos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
    final Vec3 blockVec = Vec3.atLowerCornerOf(blockPos);
    final net.minecraft.core.Direction enumFacing = adapt(face);
    BlockHitResult rayTrace = new BlockHitResult(blockVec, enumFacing, blockPos, false);
    UseOnContext context = new UseOnContext(fakePlayer, InteractionHand.MAIN_HAND, rayTrace);
    InteractionResult result = stack.useOn(context, InteractionHand.MAIN_HAND);
    if (result != InteractionResult.SUCCESS) {
        if (worldServer.getBlockState(blockPos).use(worldServer, fakePlayer, InteractionHand.MAIN_HAND, rayTrace).consumesAction()) {
            result = InteractionResult.SUCCESS;
        } else {
            result = stack.getItem().use(worldServer, fakePlayer, InteractionHand.MAIN_HAND).getResult();
        }
    }
    return result == InteractionResult.SUCCESS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) UseOnContext(net.minecraft.world.item.context.UseOnContext) InteractionResult(net.minecraft.world.InteractionResult) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(net.minecraft.world.item.ItemStack) ExecutionException(java.util.concurrent.ExecutionException) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 35 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Applied-Energistics-2 by AppliedEnergistics.

the class MemoryCardItem method onItemUseFirst.

@Override
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
    var player = context.getPlayer();
    if (player != null && InteractionUtil.isInAlternateUseMode(player)) {
        var level = context.getLevel();
        if (!level.isClientSide()) {
            var state = context.getLevel().getBlockState(context.getClickedPos());
            var useResult = state.use(context.getLevel(), context.getPlayer(), context.getHand(), new BlockHitResult(context.getClickLocation(), context.getClickedFace(), context.getClickedPos(), context.isInside()));
            if (!useResult.consumesAction()) {
                clearCard(context.getPlayer(), context.getLevel(), context.getHand());
            }
        }
        return InteractionResult.sidedSuccess(level.isClientSide());
    }
    return InteractionResult.PASS;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult)

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