Skip to content

Commit

Permalink
Ignore chattriggerless NPCs in chattriggers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 27, 2013
1 parent 74f662e commit 4b782d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Expand Up @@ -56,7 +56,7 @@ public void onSpawn() {
*
* @param triggerName name of the Trigger, as specified by the Trigger. Case in-sensitive.
* @param toggle new state of the trigger
* @return
* @return output debug information.
*/
public String toggleTrigger(String triggerName, boolean toggle) {
if (enabled.containsKey(triggerName.toUpperCase())) {
Expand All @@ -78,6 +78,10 @@ public String toggleTrigger(String triggerName) {
else return triggerName + " trigger not found!";
}

public boolean hasTrigger(String triggerName) {
return enabled.containsKey(triggerName.toUpperCase()) && enabled.get(triggerName.toUpperCase());
}

public boolean isEnabled(String triggerName) {
if (!DenizenAPI.getDenizenNPC(npc).getAssignmentTrait().hasAssignment()) return false;
if (enabled.containsKey(triggerName.toUpperCase()))
Expand Down
Expand Up @@ -54,7 +54,7 @@ public void onEnable() {
public Boolean process(Player player, String message) {

// Check if there is an NPC within range of a player to chat to.
dNPC npc = Utilities.getClosestNPC(player.getLocation(), 25);
dNPC npc = Utilities.getClosestNPC_ChatTrigger(player.getLocation(), 25);
dPlayer denizenPlayer = dPlayer.mirrorBukkitPlayer(player);

// No NPC? Nothing else to do here.
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Expand Up @@ -13,6 +13,7 @@
import java.util.Set;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.npc.traits.TriggerTrait;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.TagManager;
Expand Down Expand Up @@ -274,6 +275,32 @@ public static dNPC getClosestNPC (Location location, int range) {
}
return closestNPC;
}
/**
* Finds the closest NPC to a particular location.
*
* @param location The location to find the closest NPC to.
* @param range The maximum range to look for the NPC.
*
* @return The closest NPC to the location, or null if no NPC was found
* within the range specified.
*/

public static dNPC getClosestNPC_ChatTrigger (Location location, int range) {
dNPC closestNPC = null;
double closestDistance = Math.pow(range, 2);
Iterator<dNPC> it = DenizenAPI.getSpawnedNPCs().iterator();
while (it.hasNext()) {
dNPC npc = it.next();
Location loc = npc.getLocation();
if (npc.getCitizen().hasTrait(TriggerTrait.class) && npc.getTriggerTrait().hasTrigger("CHAT") &&
loc.getWorld().equals(location.getWorld())
&& loc.distanceSquared(location) < closestDistance) {
closestNPC = npc;
closestDistance = npc.getLocation().distanceSquared(location);
}
}
return closestNPC;
}


/**
Expand Down

0 comments on commit 4b782d0

Please sign in to comment.