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;
}
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;
}
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;
}
}
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();
}
}
}
}
Aggregations