bobmarley12345

World.rayTraceBlocks_do_do / World.func_72831_a wrapper

Nov 5th, 2021 (edited)
1,383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. package reghzy.api.utils;
  2.  
  3. import net.minecraft.entity.Entity;
  4. import net.minecraft.util.EnumMovingObjectType;
  5. import net.minecraft.util.MovingObjectPosition;
  6. import net.minecraft.util.Vec3;
  7. import org.bukkit.Location;
  8. import org.bukkit.World;
  9. import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
  10. import reghzy.api.utils.types.Vector3;
  11.  
  12. import javax.annotation.Nonnull;
  13.  
  14. public final class Raycast {
  15.     public final Vector3 from;
  16.     public final Vector3 to;
  17.     public final float yaw;
  18.     public final float pitch;
  19.     private final double distance;
  20.  
  21.     public Raycast(@Nonnull Vector3 from, @Nonnull Vector3 to) {
  22.         this.from = from;
  23.         this.to = to;
  24.         this.yaw = 0;
  25.         this.pitch = 0;
  26.         this.distance = 0;
  27.     }
  28.  
  29.     public Raycast(@Nonnull Location location, double distance) {
  30.         this.from = new Vector3(location.getX(), location.getY(), location.getZ());
  31.         this.to = null;
  32.         this.yaw = location.getYaw();
  33.         this.pitch = location.getPitch();
  34.         this.distance = distance;
  35.     }
  36.  
  37.     public Raycast(org.bukkit.entity.Entity entity, double distance) {
  38.         this(entity.getLocation(), distance);
  39.     }
  40.  
  41.     public RaycastHit getHit(World world) {
  42.         return getHit(world, false);
  43.     }
  44.  
  45.     public RaycastHit getHit(World world, boolean hitLiquid) {
  46.         if (this.to == null) {
  47.             return new RaycastHit(getHitDirection(Remap.<net.minecraft.world.World>cast(((CraftWorld) world).getHandle()), hitLiquid));
  48.         }
  49.         else {
  50.             return new RaycastHit(getHit(Remap.<net.minecraft.world.World>cast(((CraftWorld) world).getHandle()), hitLiquid));
  51.         }
  52.     }
  53.  
  54.     private MovingObjectPosition getHit(net.minecraft.world.World world, boolean hitLiquid) {
  55.         // the 4th param i think ignores block hits... but the arrow uses this function and sets that param to true so idk
  56.         return world.func_72831_a(createVec(this.from), createVec(this.to), hitLiquid, !hitLiquid);
  57.     }
  58.  
  59.     private MovingObjectPosition getHitDirection(net.minecraft.world.World world, boolean hitLiquid) {
  60.         Vector3 from = this.from;
  61.         return new universalelectricity.api.vector.Vector3(from.x, from.y, from.z).rayTraceBlocks(world, this.yaw, this.pitch, hitLiquid, this.distance);
  62.     }
  63.  
  64.     private static Vec3 createVec(double x, double y, double z) {
  65.         return Vec3.field_82592_a.func_72345_a(x, y, z);
  66.     }
  67.  
  68.     private static Vec3 createVec(Vector3 vec) {
  69.         return Vec3.field_82592_a.func_72345_a(vec.x, vec.y, vec.z);
  70.     }
  71.  
  72.     public static final class RaycastHit {
  73.         private final boolean isHitTile;
  74.         private final boolean isHitEntity;
  75.         private final Vector3 hitLocation;
  76.         private final int blockHitSide;
  77.         private final Entity hitEntity;
  78.  
  79.         RaycastHit(MovingObjectPosition pos) {
  80.             if (pos == null || pos.field_72313_a == null) {
  81.                 this.isHitTile = false;
  82.                 this.isHitEntity = false;
  83.                 this.hitLocation = null;
  84.                 this.blockHitSide = -1;
  85.                 this.hitEntity = null;
  86.             }
  87.             else if (pos.field_72313_a == EnumMovingObjectType.TILE) {
  88.                 this.isHitTile = true;
  89.                 this.isHitEntity = false;
  90.                 this.hitLocation = new Vector3(
  91.                         pos.field_72311_b,
  92.                         pos.field_72312_c,
  93.                         pos.field_72309_d);
  94.                 this.blockHitSide = pos.field_72310_e;
  95.                 this.hitEntity = null;
  96.             }
  97.             else {
  98.                 this.isHitTile = true;
  99.                 this.isHitEntity = false;
  100.                 this.hitEntity = pos.field_72308_g;
  101.                 this.hitLocation = new Vector3(
  102.                         pos.field_72308_g.field_70165_t,
  103.                         pos.field_72308_g.field_70163_u,
  104.                         pos.field_72308_g.field_70161_v);
  105.                 this.blockHitSide = -1;
  106.             }
  107.         }
  108.  
  109.         public boolean hasHitTile() {
  110.             return isHitTile;
  111.         }
  112.  
  113.         public boolean hasHitEntity() {
  114.             return isHitEntity;
  115.         }
  116.  
  117.         public Vector3 getHitLocation() {
  118.             return hitLocation;
  119.         }
  120.  
  121.         public int getBlockHitSide() {
  122.             return blockHitSide;
  123.         }
  124.  
  125.         public Entity getHitEntity() {
  126.             return this.hitEntity;
  127.         }
  128.  
  129.         public boolean hasHit() {
  130.             return this.isHitEntity || this.isHitTile;
  131.         }
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment