Utils.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package me.km.utils;
  2. import java.util.List;
  3. import net.minecraft.entity.Entity;
  4. import net.minecraft.entity.LivingEntity;
  5. import net.minecraft.entity.player.PlayerEntity;
  6. import net.minecraft.entity.player.ServerPlayerEntity;
  7. import net.minecraft.entity.projectile.ArrowEntity;
  8. import net.minecraft.entity.projectile.DamagingProjectileEntity;
  9. import net.minecraft.entity.projectile.ThrowableEntity;
  10. import net.minecraft.server.MinecraftServer;
  11. import net.minecraft.util.math.AxisAlignedBB;
  12. import net.minecraft.util.math.BlockPos;
  13. import net.minecraft.util.math.Vec3d;
  14. import net.minecraft.world.IWorld;
  15. import net.minecraft.util.DamageSource;
  16. import net.minecraft.util.math.RayTraceContext;
  17. import net.minecraft.util.math.RayTraceResult;
  18. import net.minecraft.world.World;
  19. public class Utils
  20. {
  21. public static PlayerEntity getPlayerFromProjectile(Entity ent)
  22. {
  23. if(ent instanceof ArrowEntity)
  24. {
  25. ArrowEntity arrow = (ArrowEntity) ent;
  26. return arrow.world.getPlayerByUuid(arrow.shootingEntity);
  27. }
  28. else if(ent instanceof ThrowableEntity)
  29. {
  30. ThrowableEntity thrown = (ThrowableEntity) ent;
  31. if(thrown.getThrower() instanceof PlayerEntity)
  32. {
  33. return (PlayerEntity) thrown.getThrower();
  34. }
  35. return null;
  36. }
  37. else if(ent instanceof DamagingProjectileEntity)
  38. {
  39. DamagingProjectileEntity fire = (DamagingProjectileEntity) ent;
  40. if(fire.shootingEntity instanceof PlayerEntity)
  41. {
  42. return (PlayerEntity) fire.shootingEntity;
  43. }
  44. return null;
  45. }
  46. return null;
  47. }
  48. public static PlayerEntity getDamager(DamageSource ds)
  49. {
  50. Entity ent = ds.getTrueSource();
  51. if(ent == null)
  52. {
  53. return null;
  54. }
  55. if(ent instanceof PlayerEntity)
  56. {
  57. return (PlayerEntity) ent;
  58. }
  59. return null;
  60. }
  61. private static AxisAlignedBB buildBoxAround(double x, double y, double z, double radius)
  62. {
  63. return new AxisAlignedBB(x - radius, y - radius, z - radius, x + radius, y + radius, z + radius);
  64. }
  65. @SuppressWarnings("unchecked")
  66. public static <T extends Entity> List<T> getEntities(IWorld w, double x, double y, double z, double radius, Class<T> type)
  67. {
  68. double sqareRadius = radius * radius;
  69. return w.getWorld().getEntitiesWithinAABB(type, buildBoxAround(x, y, z, radius), ent -> ent.getDistanceSq(x, y, z) <= sqareRadius);
  70. }
  71. public static List<PlayerEntity> getPlayers(IWorld w, double x, double y, double z, double radius)
  72. {
  73. double sqareRadius = radius * radius;
  74. return w.getWorld().getEntitiesWithinAABB(PlayerEntity.class, buildBoxAround(x, y, z, radius), p -> p.getDistanceSq(x, y, z) <= sqareRadius);
  75. }
  76. public static List<PlayerEntity> getPlayers(Entity not, double radius)
  77. {
  78. double sqareRadius = radius * radius;
  79. return not.world.getEntitiesWithinAABB(PlayerEntity.class, buildBoxAround(not.posX, not.posY, not.posZ, radius),
  80. p -> p != not && p.getDistanceSq(not) <= sqareRadius);
  81. }
  82. public static List<LivingEntity> getLiving(Entity not, double radius)
  83. {
  84. double sqareRadius = radius * radius;
  85. return not.world.getEntitiesWithinAABB(LivingEntity.class, buildBoxAround(not.posX, not.posY, not.posZ, radius),
  86. p -> p != not && p.getDistanceSq(not) <= sqareRadius);
  87. }
  88. public static <T extends Entity> T getEntity(IWorld w, double x, double y, double z, double radius, Class<T> type)
  89. {
  90. return getEntities(w, x, y, z, radius, type).stream()
  91. .min((e1, e2) -> Double.compare(e1.getDistanceSq(x, y, z), e2.getDistanceSq(x, y, z)))
  92. .orElse(null);
  93. }
  94. private static boolean doesIntersect(AxisAlignedBB bound, Vec3d start, Vec3d unit)
  95. {
  96. double lowerX = Double.NEGATIVE_INFINITY;
  97. double upperX = Double.POSITIVE_INFINITY;
  98. if(unit.x != 0)
  99. {
  100. if(unit.x > 0)
  101. {
  102. lowerX = (bound.minX - start.x) / unit.x;
  103. upperX = (bound.maxX - start.x) / unit.x;
  104. }
  105. else
  106. {
  107. lowerX = (bound.maxX - start.x) / unit.x;
  108. upperX = (bound.minX - start.x) / unit.x;
  109. }
  110. }
  111. double lowerY = Double.NEGATIVE_INFINITY;
  112. double upperY = Double.POSITIVE_INFINITY;
  113. if(unit.y != 0)
  114. {
  115. if(unit.y > 0)
  116. {
  117. lowerY = (bound.minY - start.y) / unit.y;
  118. upperY = (bound.maxY - start.y) / unit.y;
  119. }
  120. else
  121. {
  122. lowerY = (bound.maxY - start.y) / unit.y;
  123. upperY = (bound.minY - start.y) / unit.y;
  124. }
  125. }
  126. double lowerZ = Double.NEGATIVE_INFINITY;
  127. double upperZ = Double.POSITIVE_INFINITY;
  128. if(unit.z != 0)
  129. {
  130. if(unit.z > 0)
  131. {
  132. lowerZ = (bound.minZ - start.z) / unit.z;
  133. upperZ = (bound.maxZ - start.z) / unit.z;
  134. }
  135. else
  136. {
  137. lowerZ = (bound.maxZ - start.z) / unit.z;
  138. upperZ = (bound.minZ - start.z) / unit.z;
  139. }
  140. }
  141. return Math.max(Math.max(lowerX, lowerY), lowerZ) < Math.min(Math.min(upperX, upperY), upperZ);
  142. }
  143. private static Vec3d getTargetVector(Entity ent, double range)
  144. {
  145. if(range > 128)
  146. {
  147. range = 128;
  148. }
  149. World w = ent.getEntityWorld().getWorld();
  150. Vec3d start = ent.getEyePosition(1.0f);
  151. Vec3d look = ent.getLookVec();
  152. Vec3d end = start.add(look.x * range, look.y * range, look.z * range);
  153. RayTraceResult ray = w.rayTraceBlocks(new RayTraceContext(start, end, RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.NONE, ent));
  154. if(ray == null || ray.getHitVec() == null)
  155. {
  156. return end;
  157. }
  158. return ray.getHitVec();
  159. }
  160. @SuppressWarnings("unchecked")
  161. public static <T extends Entity> T getTargetedEntity(PlayerEntity p, double radius, Class<T> type)
  162. {
  163. World w = p.getEntityWorld().getWorld();
  164. Vec3d l = getTargetVector(p, radius);
  165. Vec3d eye = p.getEyePosition(1.0f);
  166. Vec3d unit = new Vec3d(l.x - eye.x, l.y - eye.y, l.z - eye.z);
  167. List<Entity> col = w.getEntitiesWithinAABBExcludingEntity(p, new AxisAlignedBB(eye.x, eye.y, eye.z, l.x, l.y, l.z));
  168. col.removeIf(ent -> !type.isAssignableFrom(ent.getClass()));
  169. // removes entities, which do not intersect with the sight vector
  170. col.removeIf(ent -> !doesIntersect(ent.getBoundingBox().grow(0.1), eye, unit));
  171. return (T) col.stream().sorted((Entity e1, Entity e2) ->
  172. {
  173. return Double.compare(e1.getDistanceSq(eye.x, eye.y, eye.z), e2.getDistanceSq(eye.x, eye.y, eye.z));
  174. }).findFirst().orElse(null);
  175. }
  176. // -------------------------------------------------------------------------
  177. // Player-Tools
  178. // -------------------------------------------------------------------------
  179. public static ServerPlayerEntity getPlayerByName(MinecraftServer server, String name)
  180. {
  181. String nameLower = name.toLowerCase();
  182. return server.getPlayerList().getPlayers().stream()
  183. .filter(pl -> pl.getName().getUnformattedComponentText().toLowerCase().contains(nameLower))
  184. .findFirst().orElse(null);
  185. }
  186. }