Search in sources :

Example 1 with HitResult

use of net.minecraft.world.phys.HitResult in project Denizen-For-Bukkit 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 2 with HitResult

use of net.minecraft.world.phys.HitResult in project Denizen-For-Bukkit 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 3 with HitResult

use of net.minecraft.world.phys.HitResult in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method handleCollideImpactEvent.

public static boolean handleCollideImpactEvent(final net.minecraft.world.entity.Entity projectile, @Nullable final ProjectileSource projectileSource, final HitResult movingObjectPosition) {
    final HitResult.Type movingObjectType = movingObjectPosition.getType();
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(projectile);
        frame.addContext(EventContextKeys.PROJECTILE_SOURCE, projectileSource == null ? UnknownProjectileSource.UNKNOWN : projectileSource);
        final Optional<UUID> creator = PhaseTracker.getInstance().getPhaseContext().getCreator();
        creator.ifPresent(user -> frame.addContext(EventContextKeys.CREATOR, user));
        final ServerLocation impactPoint = ServerLocation.of((ServerWorld) projectile.level, VecHelper.toVector3d(movingObjectPosition.getLocation()));
        boolean cancelled = false;
        if (movingObjectType == HitResult.Type.BLOCK) {
            final BlockHitResult blockMovingObjectPosition = (BlockHitResult) movingObjectPosition;
            final BlockPos blockPos = blockMovingObjectPosition.getBlockPos();
            if (blockPos.getY() <= 0) {
                return false;
            }
            final BlockSnapshot targetBlock = ((ServerWorld) projectile.level).createSnapshot(blockPos.getX(), blockPos.getY(), blockPos.getZ());
            final Direction side = DirectionFacingProvider.INSTANCE.getKey(blockMovingObjectPosition.getDirection()).get();
            final CollideBlockEvent.Impact event = SpongeEventFactory.createCollideBlockEventImpact(frame.currentCause(), impactPoint, targetBlock.state(), targetBlock.location().get(), side);
            cancelled = SpongeCommon.post(event);
            // Track impact block if event is not cancelled
            if (!cancelled && creator.isPresent()) {
                final BlockPos targetPos = VecHelper.toBlockPos(impactPoint.blockPosition());
                final LevelChunkBridge spongeChunk = (LevelChunkBridge) projectile.level.getChunkAt(targetPos);
                spongeChunk.bridge$addTrackedBlockPosition((Block) targetBlock.state().type(), targetPos, creator.get(), PlayerTracker.Type.NOTIFIER);
            }
        } else if (movingObjectType == HitResult.Type.ENTITY) {
            // entity
            final EntityHitResult entityMovingObjectPosition = (EntityHitResult) movingObjectPosition;
            final ArrayList<Entity> entityList = new ArrayList<>();
            entityList.add((Entity) entityMovingObjectPosition.getEntity());
            final CollideEntityEvent.Impact event = SpongeEventFactory.createCollideEntityEventImpact(frame.currentCause(), entityList, impactPoint);
            cancelled = SpongeCommon.post(event);
        }
        return cancelled;
    }
}
Also used : JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) Entity(org.spongepowered.api.entity.Entity) LivingEntity(net.minecraft.world.entity.LivingEntity) ServerLocation(org.spongepowered.api.world.server.ServerLocation) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ArrayList(java.util.ArrayList) Direction(org.spongepowered.api.util.Direction) CollideBlockEvent(org.spongepowered.api.event.block.CollideBlockEvent) EntityHitResult(net.minecraft.world.phys.EntityHitResult) BlockHitResult(net.minecraft.world.phys.BlockHitResult) EntityHitResult(net.minecraft.world.phys.EntityHitResult) HitResult(net.minecraft.world.phys.HitResult) ServerWorld(org.spongepowered.api.world.server.ServerWorld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) UUID(java.util.UUID) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 4 with HitResult

use of net.minecraft.world.phys.HitResult in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$throwAnimationAndInteractEvents.

@SuppressWarnings("ConstantConditions")
@Inject(method = "handleAnimate", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"), cancellable = true)
private void impl$throwAnimationAndInteractEvents(final ServerboundSwingPacket packetIn, final CallbackInfo ci) {
    if (PhaseTracker.getInstance().getPhaseContext().isEmpty()) {
        return;
    }
    final InteractionHand hand = packetIn.getHand();
    if (!((ServerPlayerGameModeAccessor) this.player.gameMode).accessor$isDestroyingBlock()) {
        if (this.impl$ignorePackets > 0) {
            this.impl$ignorePackets--;
        } else {
            if (ShouldFire.INTERACT_ITEM_EVENT_PRIMARY) {
                final Vec3 startPos = this.player.getEyePosition(1);
                // TODO hook for blockReachDistance?
                final Vec3 endPos = startPos.add(this.player.getLookAngle().scale(5d));
                HitResult result = this.player.getLevel().clip(new ClipContext(startPos, endPos, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, this.player));
                if (result.getType() == HitResult.Type.MISS) {
                    final ItemStack heldItem = this.player.getItemInHand(hand);
                    SpongeCommonEventFactory.callInteractItemEventPrimary(this.player, heldItem, hand);
                }
            }
        }
    }
    if (ShouldFire.ANIMATE_HAND_EVENT) {
        final HandType handType = (HandType) (Object) hand;
        final ItemStack heldItem = this.player.getItemInHand(hand);
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
            frame.addContext(EventContextKeys.USED_HAND, handType);
            final AnimateHandEvent event = SpongeEventFactory.createAnimateHandEvent(frame.currentCause(), handType, (Humanoid) this.player);
            if (SpongeCommon.post(event)) {
                ci.cancel();
            }
        }
    }
}
Also used : HitResult(net.minecraft.world.phys.HitResult) ClipContext(net.minecraft.world.level.ClipContext) HandType(org.spongepowered.api.data.type.HandType) CauseStackManager(org.spongepowered.api.event.CauseStackManager) InteractionHand(net.minecraft.world.InteractionHand) Vec3(net.minecraft.world.phys.Vec3) AnimateHandEvent(org.spongepowered.api.event.entity.living.AnimateHandEvent) ItemStack(net.minecraft.world.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

HitResult (net.minecraft.world.phys.HitResult)4 BlockHitResult (net.minecraft.world.phys.BlockHitResult)3 ResourceLocation (net.minecraft.resources.ResourceLocation)2 Location (org.bukkit.Location)2 Vector (org.bukkit.util.Vector)2 CauseStackManager (org.spongepowered.api.event.CauseStackManager)2 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 BlockPos (net.minecraft.core.BlockPos)1 InteractionHand (net.minecraft.world.InteractionHand)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 ItemStack (net.minecraft.world.item.ItemStack)1 ClipContext (net.minecraft.world.level.ClipContext)1 JukeboxBlockEntity (net.minecraft.world.level.block.entity.JukeboxBlockEntity)1 EntityHitResult (net.minecraft.world.phys.EntityHitResult)1 Vec3 (net.minecraft.world.phys.Vec3)1 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)1 HandType (org.spongepowered.api.data.type.HandType)1 Entity (org.spongepowered.api.entity.Entity)1 CollideBlockEvent (org.spongepowered.api.event.block.CollideBlockEvent)1