Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reghzy.api.utils;
- import net.minecraft.entity.Entity;
- import net.minecraft.util.EnumMovingObjectType;
- import net.minecraft.util.MovingObjectPosition;
- import net.minecraft.util.Vec3;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
- import reghzy.api.utils.types.Vector3;
- import javax.annotation.Nonnull;
- public final class Raycast {
- public final Vector3 from;
- public final Vector3 to;
- public final float yaw;
- public final float pitch;
- private final double distance;
- public Raycast(@Nonnull Vector3 from, @Nonnull Vector3 to) {
- this.from = from;
- this.to = to;
- this.yaw = 0;
- this.pitch = 0;
- this.distance = 0;
- }
- public Raycast(@Nonnull Location location, double distance) {
- this.from = new Vector3(location.getX(), location.getY(), location.getZ());
- this.to = null;
- this.yaw = location.getYaw();
- this.pitch = location.getPitch();
- this.distance = distance;
- }
- public Raycast(org.bukkit.entity.Entity entity, double distance) {
- this(entity.getLocation(), distance);
- }
- public RaycastHit getHit(World world) {
- return getHit(world, false);
- }
- public RaycastHit getHit(World world, boolean hitLiquid) {
- if (this.to == null) {
- return new RaycastHit(getHitDirection(Remap.<net.minecraft.world.World>cast(((CraftWorld) world).getHandle()), hitLiquid));
- }
- else {
- return new RaycastHit(getHit(Remap.<net.minecraft.world.World>cast(((CraftWorld) world).getHandle()), hitLiquid));
- }
- }
- private MovingObjectPosition getHit(net.minecraft.world.World world, boolean hitLiquid) {
- // the 4th param i think ignores block hits... but the arrow uses this function and sets that param to true so idk
- return world.func_72831_a(createVec(this.from), createVec(this.to), hitLiquid, !hitLiquid);
- }
- private MovingObjectPosition getHitDirection(net.minecraft.world.World world, boolean hitLiquid) {
- Vector3 from = this.from;
- return new universalelectricity.api.vector.Vector3(from.x, from.y, from.z).rayTraceBlocks(world, this.yaw, this.pitch, hitLiquid, this.distance);
- }
- private static Vec3 createVec(double x, double y, double z) {
- return Vec3.field_82592_a.func_72345_a(x, y, z);
- }
- private static Vec3 createVec(Vector3 vec) {
- return Vec3.field_82592_a.func_72345_a(vec.x, vec.y, vec.z);
- }
- public static final class RaycastHit {
- private final boolean isHitTile;
- private final boolean isHitEntity;
- private final Vector3 hitLocation;
- private final int blockHitSide;
- private final Entity hitEntity;
- RaycastHit(MovingObjectPosition pos) {
- if (pos == null || pos.field_72313_a == null) {
- this.isHitTile = false;
- this.isHitEntity = false;
- this.hitLocation = null;
- this.blockHitSide = -1;
- this.hitEntity = null;
- }
- else if (pos.field_72313_a == EnumMovingObjectType.TILE) {
- this.isHitTile = true;
- this.isHitEntity = false;
- this.hitLocation = new Vector3(
- pos.field_72311_b,
- pos.field_72312_c,
- pos.field_72309_d);
- this.blockHitSide = pos.field_72310_e;
- this.hitEntity = null;
- }
- else {
- this.isHitTile = true;
- this.isHitEntity = false;
- this.hitEntity = pos.field_72308_g;
- this.hitLocation = new Vector3(
- pos.field_72308_g.field_70165_t,
- pos.field_72308_g.field_70163_u,
- pos.field_72308_g.field_70161_v);
- this.blockHitSide = -1;
- }
- }
- public boolean hasHitTile() {
- return isHitTile;
- }
- public boolean hasHitEntity() {
- return isHitEntity;
- }
- public Vector3 getHitLocation() {
- return hitLocation;
- }
- public int getBlockHitSide() {
- return blockHitSide;
- }
- public Entity getHitEntity() {
- return this.hitEntity;
- }
- public boolean hasHit() {
- return this.isHitEntity || this.isHitTile;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment