Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * Created by LynxPlay on 28.10.2016.
- */
- public class EntityUtil {
- /**
- * @param locations The locations which will be used as the center
- * @param radius The max. radius in which the entiys must be (from the center)
- * @param entityClass The class of the entiys. Used to filter the list. Entiy will
- * @param <T>
- * @return A list of entities extending T
- */
- public static <T extends Entity> List<T> getNearbyEntitys(final double radius, final Class<T> entityClass, Collection<Location> locations) {
- if (locations.isEmpty()) return new ArrayList<>();
- return locations.stream().findAny().get().getWorld().getEntitiesByClass(entityClass).stream()
- .filter(entity -> isNearby(entity, radius, locations))
- .map(t -> entityClass.cast(t))
- .collect(Collectors.toList());
- }
- public static <T extends Entity> List<T> getNearbyEntitys(final double radius, final Class<T> entityClass, Location... locations) {
- return getNearbyEntitys(radius , entityClass , Arrays.asList(locations));
- }
- /**
- *
- * @param world the world the entity is in
- * @param uuid the entitys uuid
- * @param entityClass the entitys class
- * @return the entity and if not found null
- */
- public static <T extends Entity> T getFrom(World world , UUID uuid , Class<T> entityClass) {
- return world.getEntitiesByClass(entityClass).stream().filter(e -> e != null && e.getUniqueId().equals(uuid)).findFirst().orElse(null);
- }
- /**
- * @param player The player to check
- * @return Whether the player is a real player or just a npc
- */
- public static boolean isRealPlayer(Player player) {
- return player.getAddress() != null;
- }
- /**
- * @param player The player to heal
- * @param amount The health to heal
- */
- public static void heal(Player player, int amount) {
- player.setHealth(Math.min(player.getHealth() + amount, player.getMaxHealth()));
- }
- private static boolean isNearby(Entity entity, final double radius, Collection<Location> locations) {
- return locations.stream().filter(location -> location.distance(entity.getLocation()) <= radius).findAny().isPresent();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement