Skip to content

Commit

Permalink
Add dEntity Properties 'infected, age, profession, and framed'
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Oct 21, 2013
1 parent 6091eed commit 8cabab9
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 115 deletions.
108 changes: 29 additions & 79 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -6,10 +6,7 @@

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.properties.EntityAge;
import net.aufdemrand.denizen.objects.properties.EntityInfected;
import net.aufdemrand.denizen.objects.properties.EntityProfessional;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
Expand Down Expand Up @@ -373,8 +370,11 @@ public boolean isLivingEntity() {
*/

public NPC getNPC() {

return npc;
if (npc != null)
return npc;
else if (entity != null && CitizensAPI.getNPCRegistry().isNPC(entity))
return CitizensAPI.getNPCRegistry().getNPC(entity);
else return null;
}

/**
Expand All @@ -384,8 +384,11 @@ public NPC getNPC() {
*/

public dNPC getDenizenNPC() {

return new dNPC(npc);
if (npc != null)
return new dNPC(npc);
else if (entity != null && CitizensAPI.getNPCRegistry().isNPC(entity))
return new dNPC(CitizensAPI.getNPCRegistry().getNPC(entity));
else return null;
}

/**
Expand All @@ -395,7 +398,9 @@ public dNPC getDenizenNPC() {
*/

public boolean isNPC() {
return npc != null;
if (npc != null) return true;
else if (entity != null && CitizensAPI.getNPCRegistry().isNPC(entity)) return true;
else return false;
}

/**
Expand Down Expand Up @@ -506,7 +511,7 @@ public dInventory getInventory() {
public dInventory getEquipment() {
if (isLivingEntity())
return new dInventory(InventoryType.CRAFTING)
.add(getLivingEntity().getEquipment().getArmorContents());
.add(getLivingEntity().getEquipment().getArmorContents());
else return null;
}

Expand Down Expand Up @@ -900,50 +905,6 @@ public int comparesTo(dEntity entity) {
}



///////////////
// Properties
////////////

private List<Property> properties = new ArrayList<Property>();

public String describe() {
properties.clear();

StringBuilder property_string = new StringBuilder();

for (Property property : properties)
property_string.append(property.getPropertyString());

return identify() + '[' + property_string.toString() + ']';
}

public boolean isInfected() {
return EntityInfected.describes(this);
}

public EntityInfected getInfected() {
return EntityInfected.getFrom(this);
}

public boolean isProfessional() {
return EntityProfessional.describes(this);
}

public EntityProfessional getProfessional() {
return EntityProfessional.getFrom(this);
}

public boolean isAgeable() {
return EntityAge.describes(this);
}

public EntityAge getAgeable() {
return EntityAge.getFrom(this);
}



/////////////////////
// dObject Methods
///////////////////
Expand Down Expand Up @@ -1178,7 +1139,7 @@ public String getAttribute(Attribute attribute) {
// if none.
// -->
else if (attribute.startsWith("equipment.chestplate") ||
attribute.startsWith("equipment.chest")) {
attribute.startsWith("equipment.chest")) {
if (getLivingEntity().getEquipment().getChestplate() != null) {
return new dItem(getLivingEntity().getEquipment().getChestplate())
.getAttribute(attribute.fulfill(2));
Expand All @@ -1193,7 +1154,7 @@ else if (attribute.startsWith("equipment.chestplate") ||
// if none.
// -->
else if (attribute.startsWith("equipment.helmet") ||
attribute.startsWith("equipment.head")) {
attribute.startsWith("equipment.head")) {
if (getLivingEntity().getEquipment().getHelmet() != null) {
return new dItem(getLivingEntity().getEquipment().getHelmet())
.getAttribute(attribute.fulfill(2));
Expand All @@ -1208,7 +1169,7 @@ else if (attribute.startsWith("equipment.helmet") ||
// if none.
// -->
else if (attribute.startsWith("equipment.leggings") ||
attribute.startsWith("equipment.legs")) {
attribute.startsWith("equipment.legs")) {
if (getLivingEntity().getEquipment().getLeggings() != null) {
return new dItem(getLivingEntity().getEquipment().getLeggings())
.getAttribute(attribute.fulfill(2));
Expand Down Expand Up @@ -1236,7 +1197,7 @@ else if (attribute.startsWith("equipment")) {
// if none.
// -->
if (attribute.startsWith("item_in_hand") ||
attribute.startsWith("iteminhand"))
attribute.startsWith("iteminhand"))
return new dItem(getLivingEntity().getEquipment().getItemInHand())
.getAttribute(attribute.fulfill(1));

Expand Down Expand Up @@ -1416,7 +1377,7 @@ else return new Element("null")
// Otherwise, returns null.
// -->
if (attribute.startsWith("get_shooter") ||
attribute.startsWith("shooter")) {
attribute.startsWith("shooter")) {
if (isProjectile() && hasShooter())
return getShooter().getAttribute(attribute.fulfill(1));
else return new Element("null")
Expand Down Expand Up @@ -1730,30 +1691,19 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
.getAttribute(attribute.fulfill(1));



/////////////////////
// PROPERTY ATTRIBUTES
/////////////////

if (attribute.startsWith("infected")) {
if (EntityInfected.describes(this))
return EntityInfected.getFrom(this)
.getAttribute(attribute.fulfill(1));
else return Attribute.RETURN_NULL;
}

if (attribute.startsWith("professional")) {
if (EntityProfessional.describes(this))
return EntityProfessional.getFrom(this)
.getAttribute(attribute.fulfill(1));
else return Attribute.RETURN_NULL;
}
if (attribute.startsWith("describe"))
return new Element("e@" + getEntityType().name().toLowerCase()
+ '[' + PropertyParser.getPropertiesString(this) + ']')
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("age")) {
if (EntityAge.describes(this))
return EntityAge.getFrom(this)
.getAttribute(attribute.fulfill(1));
else return Attribute.RETURN_NULL;
// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) return returned;
}

return new Element(identify()).getAttribute(attribute);
Expand All @@ -1770,7 +1720,7 @@ public void adjust(Mechanism mechanism, Element value) {
}

if (mechanism.matches("remove_when_far_away"))
getLivingEntity().setRemoveWhenFarAway(value.asBoolean());
getLivingEntity().setRemoveWhenFarAway(value.asBoolean());

if (mechanism.matches("custom_name"))
getLivingEntity().setCustomName(value.asString());
Expand Down
Expand Up @@ -3,22 +3,25 @@

import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.trait.Age;
import org.bukkit.entity.*;

public class EntityAge implements Property {

public static boolean describes(dEntity entity) {
return (entity.getBukkitEntity() instanceof Ageable)
|| entity.getBukkitEntity().getType() == EntityType.ZOMBIE;
public static boolean describes(dObject entity) {
if (!(entity instanceof dEntity)) return false;
// Check if entity is Ageable, or a Zombie
return (((dEntity) entity).getBukkitEntity() instanceof Ageable)
|| ((dEntity) entity).getBukkitEntity().getType() == EntityType.ZOMBIE;
}

public static EntityAge getFrom(dEntity entity) {
public static EntityAge getFrom(dObject entity) {
if (!describes(entity)) return null;

else return new EntityAge(entity);
else return new EntityAge((dEntity) entity);
}


Expand All @@ -41,9 +44,12 @@ public boolean isBaby() {
}

public void setBaby(boolean bool) {
dB.log(ageable.isNPC() + " <--- is NPC?");
if (ageable.isNPC()) {
NPC ageable_npc = ageable.getNPC();
ageable_npc.getTrait(Age.class).setAge(bool ? 0 : 1);
if (!ageable_npc.hasTrait(Age.class))
ageable_npc.addTrait(Age.class);
ageable_npc.getTrait(Age.class).setAge(bool ? -24000 : 1);

} else {
if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE)
Expand Down Expand Up @@ -71,10 +77,10 @@ public void setAge(int val) {
}

public int getAge() {
if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE)
return ((Zombie) ageable.getBukkitEntity()).isBaby() ? 0 : 1;
else
return ((Ageable) ageable.getBukkitEntity()).getAge();
if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE)
return ((Zombie) ageable.getBukkitEntity()).isBaby() ? 0 : 1;
else
return ((Ageable) ageable.getBukkitEntity()).getAge();
}

public void setLock(boolean bool) {
Expand All @@ -95,7 +101,10 @@ public boolean getLock() {

@Override
public String getPropertyString() {
return getPropertyId() + '=' + getAge() + ';';
if (getAge() != 1)
return getPropertyId() + '=' + (getAge() == 0
? "baby" : getAge() + ';');
else return PropertyParser.NONE;
}

@Override
Expand All @@ -119,7 +128,7 @@ public String getAttribute(Attribute attribute) {
// @description
// If the entity is ageable, returns the entity's age number.
// -->
if (attribute.startsWith("is_baby"))
if (attribute.startsWith("age"))
return new Element(getAge())
.getAttribute(attribute.fulfill(1));

Expand All @@ -134,7 +143,7 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));


return new Element(getAge()).getAttribute(attribute);
return null;
}

}

0 comments on commit 8cabab9

Please sign in to comment.