Skip to content

Commit

Permalink
Add filter on tag player.target
Browse files Browse the repository at this point in the history
Now we can specify a npc/<entity>/<entity.type> as target.
few examples
<p@player.target[npc] will target only npc
<p@player.target[player] will target only npc
<p@player.target[e@123|e@321|zombie] will target only entity 123 or 321
or zombie
  • Loading branch information
blankiito committed Dec 11, 2013
1 parent cd548f2 commit 486a5fc
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.citizensnpcs.api.CitizensAPI;

import org.bukkit.Achievement;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -414,23 +415,25 @@ public String getAttribute(Attribute attribute) {
/////////////////

// <--[tag]
// @attribute <p@player.target>
// @attribute <p@player.target[npc/<entity>/<entity.type>|..]>
// @returns dEntity
// @description
// Returns the entity that the player is looking at, within a maximum range of 50 blocks,
// or null if the player is not looking at an entity.
// The player can use a list of entities or entity types to filter the possible targets.
// -->

if (attribute.startsWith("target")) {
int range = 50;
int attribs = 1;

// <--[tag]
// @attribute <p@player.target.within[#]>
// @attribute <p@player.target[npc/<entity>/<entity.type>|..].within[#]>
// @returns dEntity
// @description
// Returns the entity that the player is looking at within the specified range limit,
// or null if the player is not looking at an entity.
// The player can use a list of entities or entity types to filter the possible targets.
if (attribute.getAttribute(2).startsWith("within") &&
attribute.hasContext(2) &&
aH.matchesInteger(attribute.getContext(2))) {
Expand All @@ -442,7 +445,32 @@ public String getAttribute(Attribute attribute) {
ArrayList<LivingEntity> possibleTargets = new ArrayList<LivingEntity>();
for (Entity entity : entities) {
if (entity instanceof LivingEntity) {
possibleTargets.add((LivingEntity) entity);

// if we have a context for entity types, check the entity
if (attribute.hasContext(1)) {
for (String ent : attribute.getContext(1).split("\\|")) {
boolean valid = false;

if (ent.equalsIgnoreCase("npc") && CitizensAPI.getNPCRegistry().isNPC(entity)) {
valid = true;
}
else if (dEntity.matches(ent)) {
// only accept generic entities that are not npcs
if (dEntity.valueOf(ent).isGeneric()) {
if (!CitizensAPI.getNPCRegistry().isNPC(entity)) {
valid = true;
}
}
else {
valid = true;
}
}
if (valid) possibleTargets.add((LivingEntity) entity);
}
} else { // no entity type specified
possibleTargets.add((LivingEntity) entity);
entity.getType();
}
}
}

Expand Down Expand Up @@ -478,8 +506,8 @@ public String getAttribute(Attribute attribute) {
ey = l.getY();
ez = l.getZ();

if ((bx - .75 <= ex && ex <= bx + 1.75) &&
(bz - .75 <= ez && ez <= bz + 1.75) &&
if ((bx - .50 <= ex && ex <= bx + 1.50) &&
(bz - .50 <= ez && ez <= bz + 1.50) &&
(by - 1 <= ey && ey <= by + 2.5)) {
// Entity is close enough, so return it
return new dEntity(possibleTarget).getAttribute(attribute.fulfill(attribs));
Expand Down

0 comments on commit 486a5fc

Please sign in to comment.