Skip to content

Commit

Permalink
Restructure the Denizen entity system
Browse files Browse the repository at this point in the history
Yay! Custom entity possibilities!
  • Loading branch information
Morphan1 committed Feb 12, 2015
1 parent 930b241 commit d2bbceb
Show file tree
Hide file tree
Showing 31 changed files with 258 additions and 171 deletions.
Expand Up @@ -72,7 +72,7 @@ public boolean couldMatch(ScriptContainer scriptContainer, String s) {
@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = s.toLowerCase();
String ename = entity.getEntityType().name().toLowerCase();
String ename = entity.getEntityType().getLowercaseName();
String ename2 = entity.identifySimple().substring(2);
String ename3 = entity.identifySimpleType();
return lower.startsWith(ename + " teleports")
Expand Down
Expand Up @@ -54,7 +54,7 @@ public boolean couldMatch(ScriptContainer scriptContainer, String s) {
@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = s.toLowerCase();
String ename = vehicle.getEntityType().name().toLowerCase();
String ename = vehicle.getEntityType().getLowercaseName();
String ename2 = vehicle.identifySimple().substring(2);
String ename3 = vehicle.identifySimpleType();
return lower.startsWith(ename + " moves")
Expand Down
122 changes: 80 additions & 42 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
@@ -1,6 +1,8 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.npc.traits.HealthTrait;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizen.utilities.entity.EntityMovement;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
Expand Down Expand Up @@ -39,6 +41,7 @@
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -99,7 +102,7 @@ public static dEntity valueOf(String string, TagContext context) {
randomType = EntityType.values()[CoreUtilities.getRandom().nextInt(EntityType.values().length)];
}

return new dEntity(randomType, "RANDOM");
return new dEntity(DenizenEntityType.getByName(randomType.name()), "RANDOM");
}

///////
Expand Down Expand Up @@ -180,10 +183,9 @@ else if (entityGroup.matches("P@")) {
data2 = m.group(3);
}

for (EntityType type : EntityType.values()) {
if (type.name().equalsIgnoreCase(m.group(1)))
// Construct a new 'vanilla' unspawned dEntity
return new dEntity(type, data1, data2);
// Handle custom DenizenEntityTypes
if (DenizenEntityType.isRegistered(m.group(1))) {
return new dEntity(DenizenEntityType.getByName(m.group(1)), data1, data2);
}
}

Expand Down Expand Up @@ -233,8 +235,8 @@ public static boolean matches(String arg) {
m = entity_with_data.matcher(arg);
if (m.matches()) {
// Check first word with a valid entity_type (other groups are datas used in constructors)
for (EntityType type : EntityType.values())
if (type.name().equals(m.group(1))) return true;
if (DenizenEntityType.isRegistered(m.group(1)))
return true;
}

// No luck otherwise!
Expand All @@ -249,44 +251,78 @@ public static boolean matches(String arg) {
public dEntity(Entity entity) {
if (entity != null) {
this.entity = entity;
EntityScript = EntityScriptHelper.getEntityScript(entity);
entityScript = EntityScriptHelper.getEntityScript(entity);
this.uuid = entity.getUniqueId();
this.entity_type = entity.getType();
this.entity_type = DenizenEntityType.getByEntity(entity);
if (Depends.citizens != null && net.citizensnpcs.api.CitizensAPI.getNPCRegistry().isNPC(entity)) {
this.npc = new dNPC(net.citizensnpcs.api.CitizensAPI.getNPCRegistry().getNPC(entity));
}
} else dB.echoError("Entity referenced is null!");
}

@Deprecated
public dEntity(EntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.entity_type = DenizenEntityType.getByName(entityType.name());
} else dB.echoError("Entity_type referenced is null!");
}

@Deprecated
public dEntity(EntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}

@Deprecated
public dEntity(EntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.entity_type = DenizenEntityType.getByName(entityType.name());
this.data1 = data1;
} else dB.echoError("Entity_type referenced is null!");
}

@Deprecated
public dEntity(EntityType entityType, String data1, String data2) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.entity_type = DenizenEntityType.getByName(entityType.name());
this.data1 = data1;
this.data2 = data2;
} else dB.echoError("Entity_type referenced is null!");
}

public dEntity(DenizenEntityType entityType) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
} else dB.echoError("DenizenEntityType referenced is null!");
}

public dEntity(DenizenEntityType entityType, ArrayList<Mechanism> mechanisms) {
this(entityType);
this.mechanisms = mechanisms;
}

public dEntity(DenizenEntityType entityType, String data1) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.data1 = data1;
}
else dB.echoError("DenizenEntityType referenced is null!");
}

public dEntity(DenizenEntityType entityType, String data1, String data2) {
if (entityType != null) {
this.entity = null;
this.entity_type = entityType;
this.data1 = data1;
this.data2 = data2;
} else dB.echoError("DenizenEntityType referenced is null!");
}

public dEntity(dNPC npc) {
if (Depends.citizens == null) {
return;
Expand All @@ -296,7 +332,7 @@ public dEntity(dNPC npc) {

if (npc.isSpawned()) {
this.entity = npc.getEntity();
this.entity_type = npc.getEntity().getType();
this.entity_type = DenizenEntityType.getByName(npc.getEntityType().name());
this.uuid = entity.getUniqueId();
}
} else dB.echoError("NPC referenced is null!");
Expand All @@ -309,24 +345,28 @@ public dEntity(dNPC npc) {
/////////////////

private Entity entity = null;
private EntityType entity_type = null;
private DenizenEntityType entity_type = null;
private String data1 = null;
private String data2 = null;
private DespawnedEntity despawned_entity = null;
private dNPC npc = null;
private UUID uuid = null;
private String EntityScript = null;
private String entityScript = null;

public EntityType getEntityType() {
public DenizenEntityType getEntityType() {
return entity_type;
}

public void setEntityScript(String EntityScript) {
this.EntityScript = EntityScript;
public EntityType getBukkitEntityType() {
return entity_type.getBukkitEntityType();
}

public void setEntityScript(String entityScript) {
this.entityScript = entityScript;
}

public String getEntityScript() {
return EntityScript;
return entityScript;
}

/**
Expand Down Expand Up @@ -538,7 +578,7 @@ public String getName() {
if (customName != null)
return customName;
}
return entity_type.name();
return entity_type.getName();
}

/**
Expand Down Expand Up @@ -663,7 +703,8 @@ public void spawnAt(Location location) {
// TODO: Build entity from custom script
}
// Else, use the entity_type specified/remembered
else entity = location.getWorld().spawnEntity(location, entity_type);
else
entity = entity_type.spawnNewEntity(location);

getLivingEntity().teleport(location);
getLivingEntity().getEquipment().setArmorContents(despawned_entity.equipment);
Expand All @@ -676,7 +717,7 @@ public void spawnAt(Location location) {

org.bukkit.entity.Entity ent = null;

if (entity_type.name().matches("PLAYER")) {
if (entity_type.getName().equals("PLAYER")) {
if (Depends.citizens == null) {
dB.echoError("Cannot spawn entity of type PLAYER!");
return;
Expand All @@ -688,7 +729,7 @@ public void spawnAt(Location location) {
uuid = entity.getUniqueId();
}
}
else if (entity_type.name().matches("FALLING_BLOCK")) {
else if (entity_type.getName().equals("FALLING_BLOCK")) {

Material material = null;

Expand Down Expand Up @@ -729,16 +770,13 @@ else if (entity_type.name().matches("FALLING_BLOCK")) {
}
else {

if (entity_type == EntityType.DROPPED_ITEM)
ent = location.getWorld().dropItem(location, new ItemStack(Material.STONE));
else
ent = location.getWorld().spawnEntity(location, entity_type);
ent = entity_type.spawnNewEntity(location);
entity = ent;
uuid = entity.getUniqueId();
if (EntityScript != null)
EntityScriptHelper.setEntityScript(entity, EntityScript);
if (entityScript != null)
EntityScriptHelper.setEntityScript(entity, entityScript);

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

// Give pig zombies golden swords by default, unless data2 specifies
// a different weapon
Expand All @@ -749,7 +787,7 @@ else if (entity_type.name().matches("FALLING_BLOCK")) {
((PigZombie) entity).getEquipment()
.setItemInHand(dItem.valueOf(data1).getItemStack());
}
else if (entity_type.name().matches("SKELETON")) {
else if (entity_type.getName().equals("SKELETON")) {

// Give skeletons bows by default, unless data2 specifies
// a different weapon
Expand Down Expand Up @@ -1029,8 +1067,8 @@ else if (isSpawned())
}

// Try to identify as an entity script
if (EntityScript != null)
return "e@" + EntityScript;
if (entityScript != null)
return "e@" + entityScript;

// Check if an entity_type is available
if (entity_type != null) {
Expand All @@ -1043,7 +1081,7 @@ else if (isSpawned())
if (properties.length() > 0) {
propertyOutput = "[" + properties.substring(0, properties.length() - 1) + "]";
}
return "e@" + entity_type.name() + propertyOutput;
return "e@" + entity_type.getLowercaseName() + propertyOutput;
}

return "null";
Expand All @@ -1062,12 +1100,12 @@ public String identifySimple() {
return "p@" + getPlayer().getName();

// Try to identify as an entity script
if (EntityScript != null)
return "e@" + EntityScript;
if (entityScript != null)
return "e@" + entityScript;

// Check if an entity_type is available
if (entity_type != null)
return "e@" + entity_type.name();
return "e@" + entity_type.getLowercaseName();

return "null";
}
Expand All @@ -1076,13 +1114,13 @@ public String identifySimple() {
public String identifyType() {
if (isNPC()) return "npc";
else if (isPlayer()) return "player";
else return "e@" + entity_type.name();
else return "e@" + entity_type.getName();
}

public String identifySimpleType() {
if (isNPC()) return "npc";
else if (isPlayer()) return "player";
else return entity_type.name();
else return entity_type.getLowercaseName();
}

@Override
Expand Down Expand Up @@ -1183,7 +1221,7 @@ public String getAttribute(Attribute attribute) {
// Returns the type of the entity.
// -->
if (attribute.startsWith("entity_type")) {
return new Element(entity_type.name()).getAttribute(attribute.fulfill(1));
return new Element(entity_type.getName()).getAttribute(attribute.fulfill(1));
}

// <--[tag]
Expand Down Expand Up @@ -1229,7 +1267,7 @@ public String getAttribute(Attribute attribute) {
// Returns the name of the entity script that spawned this entity, if any.
// -->
if (attribute.startsWith("script")) {
return new Element(EntityScript == null ? "null": EntityScript)
return new Element(entityScript == null ? "null": entityScript)
.getAttribute(attribute.fulfill(1));
}

Expand Down Expand Up @@ -2058,7 +2096,7 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
// Returns the entity's full description, including all properties.
// -->
if (attribute.startsWith("describe"))
return new Element("e@" + getEntityType().name().toLowerCase()
return new Element("e@" + getEntityType().getLowercaseName()
+ PropertyParser.getPropertiesString(this))
.getAttribute(attribute.fulfill(1));

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
Expand Down Expand Up @@ -495,8 +496,8 @@ public String getAttribute(Attribute attribute) {
// -->
if (attribute.startsWith("spawner_type")) {
if (getBlock().getState() instanceof CreatureSpawner) {
return new dEntity(((CreatureSpawner) getBlock().getState()).getSpawnedType())
.getAttribute(attribute.fulfill(1));
return new dEntity(DenizenEntityType.getByName(((CreatureSpawner) getBlock().getState())
.getSpawnedType().name())).getAttribute(attribute.fulfill(1));
}
else return "null";
}
Expand Down Expand Up @@ -971,8 +972,9 @@ else if (attribute.startsWith("entities")
if (Utilities.checkLocation(this, entity.getLocation(), radius)) {
dEntity current = new dEntity(entity);
if (!ent_list.isEmpty()) {
String type = current.getEntityType().getName();
for (String ent : ent_list) {
if ((entity.getType().name().equals(ent) ||
if ((type.equals(ent) ||
current.identify().equalsIgnoreCase(ent)) && entity.isValid()) {
found.add(current);
break;
Expand Down Expand Up @@ -1565,7 +1567,7 @@ public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("spawner_type") && mechanism.requireObject(dEntity.class)
&& getBlock().getState() instanceof CreatureSpawner) {
((CreatureSpawner) getBlock().getState()).setSpawnedType(value.asType(dEntity.class).getEntityType());
((CreatureSpawner) getBlock().getState()).setSpawnedType(value.asType(dEntity.class).getBukkitEntityType());
}

// <--[mechanism]
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dNPC.java
Expand Up @@ -1014,7 +1014,7 @@ public void adjust(Mechanism mechanism) {
// <n@npc.entity_type>
// -->
if (mechanism.matches("set_entity_type") && mechanism.requireObject(dEntity.class)) {
getCitizen().setBukkitEntityType(value.asType(dEntity.class).getEntityType());
getCitizen().setBukkitEntityType(value.asType(dEntity.class).getBukkitEntityType());
}

// <--[mechanism]
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -1559,7 +1559,7 @@ else if (getPlayerEntity().getFoodLevel() / maxHunger < 1)
return new Element(getPlayerEntity().getStatistic(statistic, ((dMaterial) obj).getMaterial()))
.getAttribute(attribute.fulfill(2));
else if (obj instanceof dEntity)
return new Element(getPlayerEntity().getStatistic(statistic, ((dEntity) obj).getEntityType()))
return new Element(getPlayerEntity().getStatistic(statistic, ((dEntity) obj).getBukkitEntityType()))
.getAttribute(attribute.fulfill(2));
else
return null;
Expand Down

0 comments on commit d2bbceb

Please sign in to comment.