Skip to content

Commit

Permalink
Add getClosestPlayer() utility. Add <npc.closest.player> tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jun 10, 2013
1 parent 0894f5f commit af7e644
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Expand Up @@ -189,8 +189,6 @@ public void onDeath(EntityDamageEvent event) {
// Check if the event pertains to this NPC
if (event.getEntity() != npc.getBukkitEntity() || dying) return;

dB.echoApproval("Health left is: " + this.getHealth());

// Make sure this is a killing blow
if (this.getHealth() - event.getDamage() > 0)
return;
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/net/aufdemrand/denizen/tags/core/NPCTags.java
Expand Up @@ -6,6 +6,8 @@
import net.aufdemrand.denizen.npc.traits.AssignmentTrait;
import net.aufdemrand.denizen.npc.traits.NicknameTrait;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.citizensnpcs.api.ai.event.NavigationBeginEvent;
import net.citizensnpcs.api.ai.event.NavigationCancelEvent;
Expand Down Expand Up @@ -34,16 +36,30 @@ public void npcTags(ReplaceableTagEvent event) {
if (n == null) return; // to avoid exceptions in scripts with no NPC attached

String type = event.getType() != null ? event.getType().toUpperCase() : "";
String typeContext = event.getTypeContext() != null ? event.getTypeContext() : "";
String subType = event.getSubType() != null ? event.getSubType().toUpperCase() : "";

if (type.equals("NAME")) {
event.setReplaced(ChatColor.stripColor(n.getName()));
if (subType.equals("NICKNAME")) {
if (n.getCitizen().hasTrait(NicknameTrait.class))
event.setReplaced(n.getCitizen().getTrait(NicknameTrait.class).getNickname());
}
}

else if (type.equalsIgnoreCase("CLOSEST"))
{
int range = 100;

if (aH.matchesInteger(typeContext))
range = aH.getIntegerFrom(typeContext);

if (subType.equalsIgnoreCase("PLAYER")) {
event.setReplaced(String.valueOf(Utilities.getClosestPlayer(n.getLocation(), range).getName()));
}

} else if (type.equals("HEALTH")) {
}

else if (type.equals("HEALTH")) {

if (subType.equals("MAX"))
event.setReplaced(String.valueOf(n.getHealthTrait().getMaxhealth()));
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Expand Up @@ -316,6 +316,36 @@ public static void faceLocation(Entity from, Location at) {
public static void faceEntity(Entity entity, Entity target) {
faceLocation(entity, target.getLocation());
}


/**
* Finds the closest Player to a particular location.
*
* @param location The location to find the closest Player to.
* @param range The maximum range to look for the Player.
*
* @return The closest Player to the location, or null if no Player was found
* within the range specified.
*/

@SuppressWarnings("unchecked")
public static Player getClosestPlayer (Location location, int range) {

Player closestPlayer = null;
double closestDistance = Math.pow(range, 2);
List playerList = new ArrayList(Arrays.asList(Bukkit.getOnlinePlayers()));
Iterator<Player> it = playerList.iterator();
while (it.hasNext()) {
Player player = it.next();
Location loc = player.getLocation();
if (loc.getWorld().equals(location.getWorld())
&& loc.distanceSquared(location) < closestDistance) {
closestPlayer = player;
closestDistance = player.getLocation().distanceSquared(location);
}
}
return closestPlayer;
}


/**
Expand All @@ -329,9 +359,10 @@ public static void faceEntity(Entity entity, Entity target) {
*/

public static dNPC getClosestNPC (Location location, int range) {

dNPC closestNPC = null;
double closestDistance = Math.pow(range, 2);
Iterator<dNPC> it = DenizenAPI.getSpawnedNPCs().iterator();
Iterator<dNPC> it = DenizenAPI.getSpawnedNPCs().iterator();
while (it.hasNext()) {
dNPC npc = it.next();
Location loc = npc.getLocation();
Expand All @@ -355,6 +386,7 @@ public static dNPC getClosestNPC (Location location, int range) {
*/

public static Set<dNPC> getClosestNPCs (Location location, int maxRange) {

maxRange = (int) Math.pow(maxRange, 2);
Set<dNPC> closestNPCs = new HashSet<dNPC> ();
Iterator<dNPC> it = DenizenAPI.getSpawnedNPCs().iterator();
Expand All @@ -379,6 +411,7 @@ public static Set<dNPC> getClosestNPCs (Location location, int maxRange) {
*/

public static double normalizeYaw(double yaw) {

yaw = (yaw - 90) % 360;
if (yaw < 0) yaw += 360.0;
return yaw;
Expand Down

0 comments on commit af7e644

Please sign in to comment.