Search in sources :

Example 1 with RayTraceContext

use of net.minecraft.util.math.RayTraceContext in project Bookshelf by Darkhax-Minecraft.

the class EntityUtils method rayTrace.

/**
 * Performs a ray trace for the look vector of an entity.
 *
 * @param entity The entity to perform a ray trace on.
 * @param length The distance to cast the rays.
 * @param blockMode The mode used when detecting blocks.
 * @param fluidMode The mode used when detecting fluids.
 * @return An object containing the results of the ray trace.
 */
public static RayTraceResult rayTrace(LivingEntity entity, double length, BlockMode blockMode, FluidMode fluidMode) {
    final Vector3d startingPosition = new Vector3d(entity.getX(), entity.getY() + entity.getEyeHeight(), entity.getZ());
    final Vector3d lookVector = entity.getLookAngle();
    final Vector3d endingPosition = startingPosition.add(lookVector.x * length, lookVector.y * length, lookVector.z * length);
    return entity.level.clip(new RayTraceContext(startingPosition, endingPosition, blockMode, fluidMode, entity));
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) RayTraceContext(net.minecraft.util.math.RayTraceContext)

Example 2 with RayTraceContext

use of net.minecraft.util.math.RayTraceContext in project Overloaded by CJ-MC-Mods.

the class ItemRayGun method handleMessage.

public void handleMessage(ServerPlayerEntity player, RayGunMessage message) {
    ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
    if (itemStack.getItem() != this) {
        return;
    }
    LazyOptional<IEnergyStorage> opEnergy = itemStack.getCapability(ENERGY);
    if (!opEnergy.isPresent()) {
        Overloaded.logger.warn("Railgun has no Energy Capability? NBT: " + itemStack.getTag());
        return;
    }
    IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    if (energy.getEnergyStored() < OverloadedConfig.INSTANCE.rayGun.energyPerShot) {
        player.displayClientMessage(new StringTextComponent("Not enough power to fire."), true);
        return;
    }
    Vector3d eyePos = player.getEyePosition(1);
    if (eyePos.distanceTo(message.vector) > OverloadedConfig.INSTANCE.rayGun.maxRange) {
        player.displayClientMessage(new StringTextComponent("Target out of range."), true);
        return;
    }
    BlockRayTraceResult sanityCheckVec = player.level.clip(new RayTraceContext(eyePos, message.vector, RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.NONE, player));
    if (sanityCheckVec.getType() != RayTraceResult.Type.MISS) {
        player.displayClientMessage(new StringTextComponent("Target no longer in sight."), true);
        return;
    }
    energy.extractEnergy(OverloadedConfig.INSTANCE.rayGun.energyPerShot, false);
    LightningBoltEntity entity = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, player.level);
    entity.moveTo(message.vector.x, message.vector.y, message.vector.z, 0, 0);
    player.level.addFreshEntity(entity);
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) RayTraceContext(net.minecraft.util.math.RayTraceContext) LightningBoltEntity(net.minecraft.entity.effect.LightningBoltEntity) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) StringTextComponent(net.minecraft.util.text.StringTextComponent) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack)

Aggregations

RayTraceContext (net.minecraft.util.math.RayTraceContext)2 Vector3d (net.minecraft.util.math.vector.Vector3d)2 LightningBoltEntity (net.minecraft.entity.effect.LightningBoltEntity)1 ItemStack (net.minecraft.item.ItemStack)1 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)1 StringTextComponent (net.minecraft.util.text.StringTextComponent)1 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)1