Utils.java 8.1 KB

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