Advertisement
LynxPlay

EntityUtil.java

Apr 19th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import org.bukkit.Location;
  2. import org.bukkit.World;
  3. import org.bukkit.entity.Entity;
  4. import org.bukkit.entity.Player;
  5.  
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8.  
  9. /**
  10.  * Created by LynxPlay on 28.10.2016.
  11.  */
  12. public class EntityUtil {
  13.  
  14.     /**
  15.      * @param locations         The locations which will be used as the center
  16.      * @param radius      The max. radius in which the entiys must be (from the center)
  17.      * @param entityClass The class of the entiys. Used to filter the list. Entiy will
  18.      * @param <T>
  19.      * @return A list of entities extending T
  20.      */
  21.     public static <T extends Entity> List<T> getNearbyEntitys(final double radius, final Class<T> entityClass, Collection<Location> locations) {
  22.         if (locations.isEmpty()) return new ArrayList<>();
  23.  
  24.         return locations.stream().findAny().get().getWorld().getEntitiesByClass(entityClass).stream()
  25.                 .filter(entity -> isNearby(entity, radius, locations))
  26.                 .map(t -> entityClass.cast(t))
  27.                 .collect(Collectors.toList());
  28.     }
  29.  
  30.     public static <T extends Entity> List<T> getNearbyEntitys(final double radius, final Class<T> entityClass, Location... locations) {
  31.         return getNearbyEntitys(radius , entityClass , Arrays.asList(locations));
  32.     }
  33.  
  34.     /**
  35.      *
  36.      * @param world the world the entity is in
  37.      * @param uuid the entitys uuid
  38.      * @param entityClass the entitys class
  39.      * @return the entity and if not found null
  40.      */
  41.     public static <T extends Entity> T getFrom(World world , UUID uuid , Class<T> entityClass) {
  42.         return world.getEntitiesByClass(entityClass).stream().filter(e -> e != null && e.getUniqueId().equals(uuid)).findFirst().orElse(null);
  43.     }
  44.  
  45.     /**
  46.      * @param player The player to check
  47.      * @return Whether the player is a real player or just a npc
  48.      */
  49.     public static boolean isRealPlayer(Player player) {
  50.         return player.getAddress() != null;
  51.     }
  52.  
  53.     /**
  54.      * @param player The player to heal
  55.      * @param amount The health to heal
  56.      */
  57.     public static void heal(Player player, int amount) {
  58.         player.setHealth(Math.min(player.getHealth() + amount, player.getMaxHealth()));
  59.     }
  60.  
  61.     private static boolean isNearby(Entity entity, final double radius, Collection<Location> locations) {
  62.         return locations.stream().filter(location -> location.distance(entity.getLocation()) <= radius).findAny().isPresent();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement