Skip to content
Permalink
Browse files

Add a maximum chase range

  • Loading branch information...
mcmonkey4eva committed Mar 30, 2016
1 parent ff86adc commit 25c58d2b9d2a3bf2dc1b69ae964317097a81041f
@@ -54,7 +54,6 @@ Created by mcmonkey4eva on behalf of the Citizens and Denizen teams.
- `/sentinel forgive PLAYERNAME/all` forgives attackers
- Other:
- old Sentry saves importer?
- Maximum chase range (waiting on Citizens2 issue 718)

**Future**:

@@ -410,6 +410,17 @@ else if (arg0.equals("respawntime") && sender.hasPermission("sentinel.respawntim
}
return true;
}
else if (arg0.equals("chaserange") && sender.hasPermission("sentinel.chaserange") && args.length > 1) {
try {
double d = Double.valueOf(args[1]);
sentinel.chaseRange = d;
sender.sendMessage(prefixGood + "Chase range set!");
}
catch (NumberFormatException ex) {
sender.sendMessage(prefixBad + "Invalid range number!");
}
return true;
}
else if (arg0.equals("invincible") && sender.hasPermission("sentinel.invincible")) {
sentinel.setInvincible(!sentinel.invincible);
if (sentinel.invincible) {
@@ -517,6 +528,7 @@ else if (arg0.equals("info") && sender.hasPermission("sentinel.info")) {
sender.sendMessage(prefixGood + "Fightback Enabled: " + ChatColor.AQUA + sentinel.fightback);
sender.sendMessage(prefixGood + "Ranged Chasing Enabled: " + ChatColor.AQUA + sentinel.rangedChase);
sender.sendMessage(prefixGood + "Close-Quarters Chasing Enabled: " + ChatColor.AQUA + sentinel.closeChase);
sender.sendMessage(prefixGood + "Maximum chase range: " + ChatColor.AQUA + sentinel.chaseRange);
sender.sendMessage(prefixGood + "Safe-Shot Enabled: " + ChatColor.AQUA + sentinel.safeShot);
return true;
}
@@ -546,6 +558,7 @@ else if (arg0.equals("stats") && sender.hasPermission("sentinel.info")) {
if (sender.hasPermission("sentinel.attackrate")) sender.sendMessage(prefixGood + "/sentinel attackrate RATE - Changes the rate at which the NPC attacks, in ticks.");
if (sender.hasPermission("sentinel.healrate")) sender.sendMessage(prefixGood + "/sentinel healrate RATE - Changes the rate at which the NPC heals, in ticks.");
if (sender.hasPermission("sentinel.respawntime")) sender.sendMessage(prefixGood + "/sentinel respawntime TIME - Changes the time it takes for the NPC to respawn, in ticks.");
if (sender.hasPermission("sentinel.chaserange")) sender.sendMessage(prefixGood + "/sentinel chaserange RANGE - Changes the maximum distance an NPC will run before returning to base.");
if (sender.hasPermission("sentinel.guard")) sender.sendMessage(prefixGood + "/sentinel guard [PLAYERNAME] - Makes the NPC guard a specific player. Don't specify a player to stop guarding.");
if (sender.hasPermission("sentinel.invincible")) sender.sendMessage(prefixGood + "/sentinel invincible - Toggles whether the NPC is invincible.");
if (sender.hasPermission("sentinel.fightback")) sender.sendMessage(prefixGood + "/sentinel fightback - Toggles whether the NPC will fight back.");
@@ -6,6 +6,9 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.Inventory;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.trait.waypoint.Waypoint;
import net.citizensnpcs.trait.waypoint.WaypointProvider;
import net.citizensnpcs.trait.waypoint.Waypoints;
import net.citizensnpcs.util.PlayerAnimation;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -143,6 +146,9 @@ public SentinelTrait() {
@Persist("respawnTime")
public long respawnTime = 100;

@Persist("chaseRange")
public double chaseRange = 100;

public UUID getGuarding() {
if (guardingLower == 0 && guardingUpper == 0) {
return null;
@@ -840,7 +846,7 @@ else if (isRegexTargeted(entity.getCustomName() == null ? entity.getType().name(
public LivingEntity findBestTarget() {
boolean ignoreGlow = usesSpectral();
double rangesquared = range * range;
Location pos = getLivingEntity().getEyeLocation();
Location pos = getGuardZone();
LivingEntity closest = null;
for (LivingEntity ent: getLivingEntity().getWorld().getLivingEntities()) {
if ((ignoreGlow && ent.isGlowing()) || ent.isDead()) {
@@ -904,7 +910,9 @@ else if (e.getLocation().distanceSquared(getLivingEntity().getLocation()) > rang
if (getGuarding() != null) {
Player player = Bukkit.getPlayer(getGuarding());
if (player != null) {
double dist = getLivingEntity().getLocation().distanceSquared(player.getLocation());
Location myLoc = getLivingEntity().getLocation();
Location theirLoc = player.getLocation();
double dist = theirLoc.getWorld().equals(myLoc.getWorld()) ? myLoc.distanceSquared(theirLoc) : MAX_DIST;
if (dist > 60 * 60) {
npc.teleport(player.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
}
@@ -915,6 +923,55 @@ else if (e.getLocation().distanceSquared(getLivingEntity().getLocation()) > rang
}
}
}
else if (chaseRange > 0) {
Location near = nearestPathPoint();
if (near != null && near.distanceSquared(getLivingEntity().getLocation()) > chaseRange * chaseRange) {
npc.getNavigator().setTarget(near);
}
}
}

private final static double MAX_DIST = 100000000;

public Location getGuardZone() {
if (getGuarding() != null) {
Player player = Bukkit.getPlayer(getGuarding());
if (player != null) {
return player.getLocation();
}
}
if (chaseRange > 0) {
Location goal = nearestPathPoint();
if (goal != null) {
return goal;
}
}
return getLivingEntity().getLocation();
}

public Location nearestPathPoint() {
if (!npc.hasTrait(Waypoints.class)) {
return null;
}
Waypoints wp = npc.getTrait(Waypoints.class);
if (!(wp.getCurrentProvider() instanceof WaypointProvider.EnumerableWaypointProvider)) {
return null;
}
Location baseloc = getLivingEntity().getLocation();
Location nearest = null;
double dist = MAX_DIST;
for (Waypoint wayp : ((WaypointProvider.EnumerableWaypointProvider) wp.getCurrentProvider()).waypoints()) {
Location l = wayp.getLocation();
if (!l.getWorld().equals(baseloc.getWorld())) {
continue;
}
double d = baseloc.distanceSquared(l);
if (d < dist) {
dist = d;
nearest = l;
}
}
return nearest;
}

@Override
@@ -38,6 +38,8 @@ permissions:
# /sentinel healrate RATE
sentinel.healrate: true
# /sentinel respawntime TIME
sentinel.chaserange: true
# /sentinel chaserange RANGE
sentinel.respawntime: true
# /sentinel guard PLAYER
sentinel.guard: true

0 comments on commit 25c58d2

Please sign in to comment.
You can’t perform that action at this time.