Utils.java 7.7 KB

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