Skip to content

Commit

Permalink
Fix dEntity, FlagCommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Aug 17, 2013
1 parent 962ce1b commit 3da1f16
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 58 deletions.
10 changes: 2 additions & 8 deletions src/main/java/net/aufdemrand/denizen/objects/aH.java
Expand Up @@ -98,13 +98,6 @@ public String getValue() {
return value;
}

public String getPrefixAndValue() {
if (has_prefix)
return prefix + ":" + value;
else
return value;
}

public boolean matchesEnum(Enum[] values) {
for (Enum value : values)
if (value.name().replace("_", "").equalsIgnoreCase(this.value.replace("_", "")))
Expand All @@ -114,8 +107,9 @@ public boolean matchesEnum(Enum[] values) {
}

public boolean matchesPrefix(String values) {
if (!hasPrefix()) return false;
for (String value : values.split(","))
if (value.trim().equalsIgnoreCase((prefix != null ? prefix : this.value)))
if (value.trim().equalsIgnoreCase(prefix))
return true;

return false;
Expand Down
86 changes: 38 additions & 48 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -118,15 +118,16 @@ public static dEntity valueOf(String string) {

// Choose a random entity type if "RANDOM" is used
if (string.equalsIgnoreCase("RANDOM")) {

EntityType randomType = null;

// When selecting a random entity type, ignore invalid or inappropriate ones
while (randomType == null || randomType.name().matches("^(COMPLEX_PART|DROPPED_ITEM|ENDER_CRYSTAL" +
"|ENDER_DRAGON|FISHING_HOOK|ITEM_FRAME|LIGHTNING|PAINTING" +
"|PLAYER|UNKNOWN|WEATHER|WITHER|WITHER_SKULL)$"))
while (randomType == null ||
randomType.name().matches("^(COMPLEX_PART|DROPPED_ITEM|ENDER_CRYSTAL|ENDER_DRAGON|FISHING_HOOK|ITEM_FRAME|LIGHTNING|PAINTING|PLAYER|UNKNOWN|WEATHER|WITHER|WITHER_SKULL)$")) {

randomType = EntityType.values()[Utilities.getRandom().nextInt(EntityType.values().length)];
}

// Return the entity
return new dEntity(randomType, "RANDOM");
}

Expand All @@ -135,18 +136,20 @@ public static dEntity valueOf(String string) {

// Make sure string matches what this interpreter can accept.


Matcher m;
m = entity_by_id.matcher(string);

if (m.matches()) {

String entityGroup = m.group(1).toUpperCase();

// NPC entity
if (entityGroup.matches("N@")) {
NPC returnable = CitizensAPI.getNPCRegistry()
.getById(Integer.valueOf(m.group(2)));

if (returnable != null) return new dEntity(returnable);
if (returnable != null) return new dEntity(returnable.getBukkitEntity());
else dB.echoError("Invalid NPC! '" + entityGroup
+ "' could not be found. Has it been despawned or killed?");
}
Expand Down Expand Up @@ -262,16 +265,6 @@ public static boolean matches(String arg) {
// CONSTRUCTORS
//////////////////

dNPC built_from_npc = null;

public dEntity(NPC npc) {
if (npc != null) {
built_from_npc = dNPC.mirrorCitizensNPC(npc);
this.entity = npc.getBukkitEntity();
this.entity_type = (npc.getBukkitEntity() != null ? npc.getBukkitEntity().getType() : null);
} else dB.echoError("NPC referenced for dEntity is null!");
}

public dEntity(Entity entity) {
if (entity != null) {
this.entity = entity;
Expand Down Expand Up @@ -341,7 +334,7 @@ public boolean isLivingEntity() {

public NPC getNPC() {

return built_from_npc.getCitizen();
return CitizensAPI.getNPCRegistry().getNPC(getBukkitEntity());
}

/**
Expand All @@ -351,17 +344,19 @@ public NPC getNPC() {
*/

public boolean isNPC() {
if (built_from_npc != null) return true;
else return false;
if (CitizensAPI.getNPCRegistry().isNPC(getBukkitEntity()))
return true;
return false;
}

/**
* Get the Player corresponding to this entity
*
* @return The Player
*/

public Player getPlayer() {

return (Player) getBukkitEntity();
}

Expand Down Expand Up @@ -390,7 +385,7 @@ public boolean isGeneric() {
return true;
return false;
}

/**
* Get the location of this entity
*
Expand All @@ -402,10 +397,10 @@ public dLocation getLocation() {
if (!isGeneric()) {
return new dLocation(getBukkitEntity().getLocation());
}

return null;
}

/**
* Get the eye location of this entity
*
Expand All @@ -417,23 +412,16 @@ public dLocation getEyeLocation() {
if (!isGeneric() && isLivingEntity()) {
return new dLocation(getLivingEntity().getEyeLocation());
}

return null;
}

public void spawnAt(Location location) {
// If the entity is already spawned, teleport it.
if (entity != null && isUnique()) entity.teleport(location);

else if (built_from_npc != null) {
built_from_npc.getCitizen().teleport(location, TeleportCause.COMMAND);
entity_type = built_from_npc.getEntityType();
entity = built_from_npc.getEntity();
}

else {
if (entity_type != null) {

if (despawned_entity != null) {
// If entity had a custom_script, use the script to rebuild the base entity.
if (despawned_entity.custom_script != null)
Expand All @@ -453,7 +441,7 @@ else if (built_from_npc != null) {
org.bukkit.entity.Entity ent = null;

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

NPC npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, data1);
npc.spawn(location);
}
Expand Down Expand Up @@ -499,9 +487,9 @@ else if (entity_type.name().matches("FALLING_BLOCK")) {

ent = location.getWorld().spawnEntity(location, entity_type);
entity = ent;

if (entity_type.name().matches("PIG_ZOMBIE")) {

// Give pig zombies golden swords by default, unless data2 specifies
// a different weapon
if (dItem.matches(data1) == false) {
Expand All @@ -512,7 +500,7 @@ else if (entity_type.name().matches("FALLING_BLOCK")) {
.setItemInHand(dItem.valueOf(data1).getItemStack());
}
else if (entity_type.name().matches("SKELETON")) {

// Give skeletons bows by default, unless data2 specifies
// a different weapon
if (dItem.matches(data2) == false) {
Expand Down Expand Up @@ -634,10 +622,10 @@ public void target(LivingEntity target) {
// If the target is not null, cast it to an NMS EntityLiving
// as well for one of the two methods below
EntityLiving nmsTarget = target != null ? ((CraftLivingEntity) target).getHandle()
: null;

: null;
((CraftCreature) entity).getHandle().
setGoalTarget(nmsTarget);
setGoalTarget(nmsTarget);

((CraftCreature) entity).getHandle().
setGoalTarget(((CraftLivingEntity) target).getHandle());
Expand Down Expand Up @@ -753,17 +741,19 @@ public int comparesTo(dEntity entity) {
@Override
public String identify() {

// Check if entity is a NPC or Player
if (built_from_npc != null)
return "n@" + getNPC().getId();

if (getBukkitEntity() != null && getBukkitEntity() instanceof Player)
return "p@" + ((Player) getBukkitEntity()).getName();
// Check if entity is a Player or NPC
if (getBukkitEntity() != null) {
if (isNPC())
return "n@" + getNPC().getId();
else if (getBukkitEntity() instanceof Player)
return "p@" + ((Player) getBukkitEntity()).getName();
}

// Check if entity is a 'saved entity'
if (isSaved(this))
return "e@" + getSaved(this);


else if (isSpawned())
return "e@" + getBukkitEntity().getEntityId();

Expand Down Expand Up @@ -796,12 +786,12 @@ public String getAttribute(Attribute attribute) {
dB.echoDebug("dEntity has returned null.");
return "null";
}

// <--
// <entity> -> dEntity
// Returns the dEntity of the entity.
// -->

// <--
// <entity.get_vehicle> -> dEntity
// If the entity is in a vehicle, returns the vehicle as a
Expand Down Expand Up @@ -887,7 +877,7 @@ public String getAttribute(Attribute attribute) {
if (attribute.startsWith("eye_location"))
return new dLocation(getEyeLocation())
.getAttribute(attribute.fulfill(1));

// <--
// <entity.location> -> dLocation
// Returns the dLocation of the entity.
Expand Down Expand Up @@ -1062,7 +1052,7 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
for (org.bukkit.potion.PotionEffect effect : getLivingEntity().getActivePotionEffects())
if (effect.getType().equals(org.bukkit.potion.PotionType.valueOf(attribute.getContext(1))))
returnElement = true;
else if (!getLivingEntity().getActivePotionEffects().isEmpty()) returnElement = true;
else if (!getLivingEntity().getActivePotionEffects().isEmpty()) returnElement = true;
return new Element(returnElement).getAttribute(attribute.fulfill(1));
}

Expand Down
Expand Up @@ -102,7 +102,7 @@ public boolean execute(ScriptEntry scriptEntry) {
continue;
}

m = definition_pattern.matcher(arg.getValue());
m = definition_pattern.matcher(arg.raw_value);
sb = new StringBuffer();
while (m.find()) {
if (scriptEntry.getResidingQueue().hasContext(m.group(1).toLowerCase()))
Expand Down Expand Up @@ -138,7 +138,7 @@ else if (arg.matchesPrefix("npc, npcid") && !if_ignore) {
dB.echoDebug("...replacing the linked NPC.");
String value = TagManager.tag(scriptEntry.getPlayer(), scriptEntry.getNPC(), arg.getValue(), false);
dNPC npc = dNPC.valueOf(arg.getValue());
if (!npc.isValid()) {
if (npc == null || !npc.isValid()) {
dB.echoError(value + " is an invalid NPC!");
return false;
}
Expand Down
Expand Up @@ -76,6 +76,7 @@ else if (!scriptEntry.hasObject("action")
// Check for flag_name:value/action
else if (!scriptEntry.hasObject("action")
&& arg.raw_value.split(":", 3).length == 2) {

String[] flagArgs = arg.raw_value.split(":", 2);
scriptEntry.addObject("flag_name", new Element(flagArgs[0].toUpperCase()));

Expand Down

0 comments on commit 3da1f16

Please sign in to comment.