Skip to content

Commit

Permalink
Fix setting target in "on entity targets entity" (although it still o…
Browse files Browse the repository at this point in the history
…nly works on a handful of monsters). Allow generic player dEntities.
  • Loading branch information
davidcernat committed Jul 29, 2013
1 parent 84ad2c5 commit 255f870
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
15 changes: 10 additions & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -212,12 +212,12 @@ else if (isSaved(m.group(2)))

if (m.group(2) != null) {

data1 = m.group(2).toUpperCase();
data1 = m.group(2);
}

if (m.group(3) != null) {

data2 = m.group(3).toUpperCase();
data2 = m.group(3);
}

for (EntityType type : EntityType.values()) {
Expand Down Expand Up @@ -385,7 +385,12 @@ public void spawnAt(Location location) {

org.bukkit.entity.Entity ent = null;

if (entity_type.name().matches("FALLING_BLOCK")) {
if (entity_type.name().matches("PLAYER")) {

NPC npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, data1);
npc.spawn(location);
}
else if (entity_type.name().matches("FALLING_BLOCK")) {

Material material = null;

Expand All @@ -395,7 +400,7 @@ public void spawnAt(Location location) {

// If we did not get a block with "RANDOM", or we got
// air or portals, keep trying
while (data1.equals("RANDOM") &&
while (data1.equalsIgnoreCase("RANDOM") &&
(material.isBlock() == false ||
material == Material.AIR ||
material == Material.PORTAL ||
Expand Down Expand Up @@ -689,7 +694,7 @@ public String getAttribute(Attribute attribute) {
dB.echoDebug("dEntity has returned null.");
return "null";
}

if (attribute.startsWith("get_vehicle")) {
if (getBukkitEntity().isInsideVehicle())
return new dEntity(getBukkitEntity().getVehicle())
Expand Down
Expand Up @@ -469,7 +469,7 @@ public String getAttribute(Attribute attribute) {

return new Element(getInventory().containsAtLeast
(dItem.valueOf(attribute.getContext(1)).getItemStack(), qty))
.getAttribute(attribute.fulfill(1));
.getAttribute(attribute.fulfill(qty == 1 ? 1 : 2));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dNPC.java
Expand Up @@ -21,17 +21,15 @@
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.util.Anchor;
import net.minecraft.server.v1_6_R2.EntityLiving;

import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class dNPC implements dObject {

Expand Down
Expand Up @@ -40,8 +40,6 @@ public static String getOutcome(long id) {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
String outcome = "false";
Boolean passively = false;

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments()))

Expand Down
Expand Up @@ -12,6 +12,8 @@
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.ai.TargetType;

/**
*
Expand All @@ -35,7 +37,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
}

else if (!scriptEntry.hasObject("entities")
&& arg.matchesPrefix("entity, entities, e")) {
&& arg.matchesPrefix("entity, entities, e, attacker, attackers, a")) {
// Entity dList arg
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}
Expand Down Expand Up @@ -85,13 +87,19 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept

for (dEntity entity : entities) {
if (entity.isNPC()) {
Navigator nav = entity.getNPC().getNavigator();

if (cancel.equals(false)) {
entity.getNPC().getNavigator()
.setTarget(target.getBukkitEntity(), true);
nav.setTarget(target.getBukkitEntity(), true);
}
else {
entity.getNPC().getNavigator()
.cancelNavigation();
// Only cancel navigation if the NPC is attacking something
if (nav.isNavigating()
&& nav.getTargetType().equals(TargetType.ENTITY)
&& nav.getEntityTarget().isAggressive()) {

nav.cancelNavigation();
}
}
}
else {
Expand Down
Expand Up @@ -141,7 +141,7 @@ public static String doEvents(List<String> eventNames, dNPC npc, Player player,

// Add the reqId to each of the entries
ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
InstantQueue.getQueue(null).addEntries(entries).start();
InstantQueue.getQueue(null).addEntries(entries).start();

if (DetermineCommand.hasOutcome(id))
determination = DetermineCommand.getOutcome(id);
Expand Down Expand Up @@ -574,7 +574,8 @@ else if (entity instanceof Player) {

if (CitizensAPI.getNPCRegistry().isNPC(damager)) {
subNPC = DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getNPC(entity));
context.put("damager", DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getNPC(damager)));
context.put("damager", DenizenAPI.getDenizenNPC
(CitizensAPI.getNPCRegistry().getNPC(damager)));
damagerType = "npc";

// If we had no NPC in our regular context, use this one
Expand All @@ -587,12 +588,12 @@ else if (damager instanceof Player) {
// If we had no player in our regular context, use this one
if (player == null) player = subPlayer;
}
else if (damager instanceof Projectile) {
context.put("shooter", new dEntity(((Projectile) damager).getShooter()));
context.put("damager", new dEntity(damager));
}
else {
context.put("damager", new dEntity(damager));

if (damager instanceof Projectile) {
context.put("shooter", new dEntity(((Projectile) damager).getShooter()));
}
}

events.add("entity damaged by entity");
Expand Down Expand Up @@ -744,6 +745,7 @@ public void entityTarget(EntityTargetEvent event) {
List<String> events = new ArrayList<String>();
events.add("entity targets");
events.add("entity targets because " + reason);
events.add(entityType + " targets");
events.add(entityType + " targets because " + reason);

if (target != null) {
Expand Down Expand Up @@ -772,13 +774,19 @@ public void entityTarget(EntityTargetEvent event) {

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
if (determination.toUpperCase().startsWith("TARGET")) {
dEntity newTarget = dEntity.valueOf(aH.getStringFrom(determination));

// If the determination matches a dEntity, change the event's target
//
// Note: this only works with a handful of monster types, like spiders
// and endermen for instance

else if (dEntity.matches(determination)) {
dEntity newTarget = dEntity.valueOf(determination);

if (newTarget.getBukkitEntity() != null) {
if (newTarget.isSpawned()) {
event.setTarget(newTarget.getBukkitEntity());
}
}
}
}

@EventHandler
Expand Down Expand Up @@ -1164,13 +1172,21 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
public void playerInteractEntity(PlayerInteractEntityEvent event) {

Entity entity = event.getRightClicked();
String entityType = entity.getType().name();
dNPC npc = null;
dItem item = new dItem(event.getPlayer().getItemInHand());

String determination;
Map<String, Object> context = new HashMap<String, Object>();
context.put("location", new dLocation(event.getRightClicked().getLocation()));

if (entity instanceof Player) {
if (CitizensAPI.getNPCRegistry().isNPC(entity)) {
npc = DenizenAPI.getDenizenNPC
(CitizensAPI.getNPCRegistry().getNPC(entity));
context.put("entity", npc);
entityType = "npc";
}
else if (entity instanceof Player) {
context.put("entity", new dPlayer((Player) entity));
}
else {
Expand All @@ -1179,35 +1195,35 @@ public void playerInteractEntity(PlayerInteractEntityEvent event) {

List<String> events = new ArrayList<String>();
events.add("player right clicks entity");
events.add("player right clicks " + entity.getType().name());
events.add("player right clicks " + entityType);
events.add("player right clicks entity with " +
item.identify());
events.add("player right clicks " + entity.getType().name() + " with " +
events.add("player right clicks " + entityType + " with " +
item.identify());

if (item.identify().equals(item.identify().split(":")[0]) == false) {

events.add("player right clicks entity with " +
item.identify().split(":")[0]);
events.add("player right clicks " + entity.getType().name() + " with " +
events.add("player right clicks " + entityType + " with " +
item.identify().split(":")[0]);
}

if (entity instanceof ItemFrame) {
dItem itemFrame = new dItem(((ItemFrame) entity).getItem());
context.put("itemframe", itemFrame);

events.add("player right clicks " + entity.getType().name() + " " +
events.add("player right clicks " + entityType + " " +
itemFrame.identify());

if (itemFrame.identify().equals(itemFrame.identify().split(":")[0]) == false) {

events.add("player right clicks " + entity.getType().name() + " " +
events.add("player right clicks " + entityType + " " +
itemFrame.identify().split(":")[0]);
}
}

determination = doEvents(events, null, event.getPlayer(), context);
determination = doEvents(events, npc, event.getPlayer(), context);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
Expand Down

0 comments on commit 255f870

Please sign in to comment.