Skip to content

Commit

Permalink
Add new entity target mapping API
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Nov 24, 2015
1 parent 1bc890b commit a6fb623
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/net/citizensnpcs/api/ai/NavigatorParameters.java
Expand Up @@ -3,6 +3,10 @@
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.entity.Entity;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

import net.citizensnpcs.api.ai.event.CancelReason;
Expand All @@ -20,6 +24,7 @@ public class NavigatorParameters implements Cloneable {
private AttackStrategy defaultStrategy;
private double distanceMargin = 2F;
private List<BlockExaminer> examiners = Lists.newArrayList();
private Function<Entity, Location> mapper;
private double pathDistanceMargin = 1F;
private float range;
private List<Runnable> runCallbacks = Lists.newArrayListWithExpectedSize(3);
Expand Down Expand Up @@ -231,6 +236,26 @@ public NavigatorParameters distanceMargin(double newMargin) {
return this;
}

/**
* Gets the target location mapper. This is a function that maps from a target entity to the location the NPC should
* pathfind to. The default mapper returns the location using {@link Entity#getLocation(Location)}.
*/
public Function<Entity, Location> entityTargetLocationMapper() {
return mapper != null ? mapper : DEFAULT_MAPPER;
}

/**
* Set the target location mapper.
*
* @param mapper
* The new mapper
* @see #entityTargetLocationMapper(Function)
*/
public NavigatorParameters entityTargetLocationMapper(Function<Entity, Location> mapper) {
this.mapper = mapper;
return this;
}

/**
* Adds the given {@link BlockExaminer}.
*
Expand Down Expand Up @@ -450,4 +475,13 @@ public NavigatorParameters useNewPathfinder(boolean use) {
useNewPathfinder = use;
return this;
}

private static final Function<org.bukkit.entity.Entity, Location> DEFAULT_MAPPER = new Function<Entity, Location>() {
Location location = new Location(null, 0, 0, 0);

@Override
public Location apply(Entity input) {
return input.getLocation(location);
}
};
}

0 comments on commit a6fb623

Please sign in to comment.