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