Skip to content

Commit

Permalink
Add special data values to dMaterial. Allow special data values for F…
Browse files Browse the repository at this point in the history
…allingBlock in dEntity.
  • Loading branch information
davidcernat committed Jul 1, 2013
1 parent 36e0a22 commit f5501e7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
66 changes: 45 additions & 21 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -184,20 +184,26 @@ else if (isSaved(m.group(2)))
// Match Entity_Type

m = entity_with_data.matcher(string);

String data = null;

if (m.matches()) {

String data1 = null;
String data2 = null;

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

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

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

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

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

Expand Down Expand Up @@ -251,11 +257,20 @@ public dEntity(EntityType entityType) {
} else dB.echoError("Entity_type referenced is null!");
}

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

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

Expand All @@ -267,7 +282,8 @@ public dEntity(EntityType entityType, String data) {

private Entity entity = null;
private EntityType entity_type = null;
private String data = null;
private String data1 = null;
private String data2 = null;
private DespawnedEntity despawned_entity = null;

public Entity getBukkitEntity() {
Expand Down Expand Up @@ -312,19 +328,19 @@ public void spawnAt(Location location) {

Material material = null;

if (data != null && dMaterial.matches(data)) {
if (data1 != null && dMaterial.matches(data1)) {

material = dMaterial.valueOf(data).getMaterial();
material = dMaterial.valueOf(data1).getMaterial();

// If we did not get a block with "RANDOM", or we got
// air or portals, keep trying
while (data.equals("RANDOM") &&
while (data1.equals("RANDOM") &&
(material.isBlock() == false ||
material == Material.AIR ||
material == Material.PORTAL ||
material == Material.ENDER_PORTAL)) {

material = dMaterial.valueOf(data).getMaterial();
material = dMaterial.valueOf(data1).getMaterial();
}
}

Expand All @@ -334,8 +350,16 @@ public void spawnAt(Location location) {
material = Material.SAND;
}

byte materialData = 0;

// Get special data value from data2 if it is a valid integer
if (data2 != null && aH.matchesInteger(data2)) {

materialData = (byte) aH.getIntegerFrom(data2);
}

// This is currently the only way to spawn a falling block
ent = location.getWorld().spawnFallingBlock(location, material, (byte) 0);
ent = location.getWorld().spawnFallingBlock(location, material, materialData);
entity = ent;
}

Expand All @@ -349,28 +373,28 @@ public void spawnAt(Location location) {
// way that uses reflection
//
// Otherwise, just use entity-specific methods manually
if (data != null) {
if (data1 != null) {

try {

// Allow creepers to be powered
if (ent instanceof Creeper && data.equalsIgnoreCase("POWERED")) {
if (ent instanceof Creeper && data1.equalsIgnoreCase("POWERED")) {
((Creeper) entity).setPowered(true);
}
else if (ent instanceof Enderman && dMaterial.matches(data)) {
((Enderman) entity).setCarriedMaterial(dMaterial.valueOf(data).getMaterialData());
else if (ent instanceof Enderman && dMaterial.matches(data1)) {
((Enderman) entity).setCarriedMaterial(dMaterial.valueOf(data1).getMaterialData());
}
else if (ent instanceof Ocelot) {
setSubtype(Ocelot.class, "Type", "setCatType", data);
setSubtype(Ocelot.class, "Type", "setCatType", data1);
}
else if (ent instanceof Skeleton) {
setSubtype(Skeleton.class, "SkeletonType", "setSkeletonType", data);
setSubtype(Skeleton.class, "SkeletonType", "setSkeletonType", data1);
}
else if (ent instanceof Slime && aH.matchesInteger(data)) {
((Slime) entity).setSize(aH.getIntegerFrom(data));
else if (ent instanceof Slime && aH.matchesInteger(data1)) {
((Slime) entity).setSize(aH.getIntegerFrom(data1));
}
else if (ent instanceof Villager) {
setSubtype(Villager.class, "Profession", "setProfession", data);
setSubtype(Villager.class, "Profession", "setProfession", data1);
}

} catch (Exception e) {
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Expand Up @@ -11,7 +11,7 @@

public class dMaterial implements dObject {

final static Pattern materialPattern = Pattern.compile("(\\w+):?(\\w+)?");
final static Pattern materialPattern = Pattern.compile("(\\w+):?(\\d+)?");

//////////////////
// OBJECT FETCHER
Expand All @@ -36,17 +36,24 @@ public static dMaterial valueOf(String string) {

if (m.matches()) {

int data = 0;

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

data = aH.getIntegerFrom(m.group(2));
}

if (aH.matchesInteger(m.group(1))) {

return new dMaterial(aH.getIntegerFrom(m.group(1)));
return new dMaterial(aH.getIntegerFrom(m.group(1)), data);
}
else {

for (Material material : Material.values()) {

if (material.name().equalsIgnoreCase(m.group(1))) {

return new dMaterial(material);
return new dMaterial(material, data);
}
}
}
Expand Down Expand Up @@ -86,36 +93,45 @@ public dMaterial(Material material) {
this.material = material;
}

public dMaterial(Material material, int data) {
this.material = material;
this.data = (byte) data;
}

public dMaterial(int id) {
this.material = Material.getMaterial(id);
}

public dMaterial(int id, int data) {
this.material = Material.getMaterial(id);
this.data = (byte) data;
}

/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////

// Bukkit Material associated

private Material material;
private byte data = 0;

public Material getMaterial() {
return material;
}

public MaterialData getMaterialData() {
return new MaterialData(material, (byte) 0);
return new MaterialData(material, data);
}


@Override
public String getPrefix() {
// TODO Auto-generated method stub
return null;
}

@Override
public String debug() {
// TODO Auto-generated method stub
return null;
}

Expand Down

0 comments on commit f5501e7

Please sign in to comment.