Skip to content

Commit

Permalink
More work on dObect Properties. Add some additional meta.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Oct 2, 2013
1 parent 1ede811 commit 17d629b
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 99 deletions.
18 changes: 18 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Duration.java
Expand Up @@ -19,6 +19,11 @@
*/
public class Duration implements dObject {


/////////////////////
// STATIC METHODS AND FIELDS
/////////////////

// Use regex pattern matching to easily determine if a string
// value is a valid Duration.
final static Pattern match =
Expand All @@ -32,6 +37,19 @@ public class Duration implements dObject {
final public static Duration ZERO = new Duration(0);


/////////////////////
// OBJECT FETCHER
/////////////////

// <--[language]
// @name d@
// @description
// d@ refers to the object type of a 'Duration'. The 'd@' is notation for Denizen's Object
// Fetcher. The only valid constructor for a dWorld is the name of the world it should be
// associated with. For example, to reference the world named 'world1', use w@world1.
// World names are case insensitive.
// -->

/**
* Gets a Duration Object from a dScript argument. Durations must be a positive
* number. Can specify the unit of time by using one of the following: T=ticks, M=minutes,
Expand Down
153 changes: 63 additions & 90 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -4,6 +4,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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;
Expand Down Expand Up @@ -59,42 +60,6 @@ public class dEntity implements dObject {
Pattern.CASE_INSENSITIVE);


/////////////////////
// STATIC METHODS
/////////////////

public static Map<String, dEntity> uniqueObjects = new HashMap<String, dEntity>();

public static boolean isSaved(String id) {
return uniqueObjects.containsKey(id.toUpperCase());
}

public static boolean isSaved(dEntity entity) {
return uniqueObjects.containsValue(entity);
}

public static dEntity getSaved(String id) {
if (uniqueObjects.containsKey(id.toUpperCase()))
return uniqueObjects.get(id.toUpperCase());
else return null;
}

public static String getSaved(dEntity entity) {
for (Map.Entry<String, dEntity> i : uniqueObjects.entrySet())
if (i.getValue() == entity) return i.getKey();
return null;
}

public static void saveAs(dEntity entity, String id) {
if (entity == null) return;
uniqueObjects.put(id.toUpperCase(), entity);
}

public static void remove(String id) {
uniqueObjects.remove(id.toUpperCase());
}


//////////////////
// OBJECT FETCHER
////////////////
Expand Down Expand Up @@ -192,8 +157,8 @@ else if (entityGroup.matches("P@")) {
if (entity != null) return new dEntity(entity);
}

else if (isSaved(m.group(2)))
return getSaved(m.group(2));
// else if (isSaved(m.group(2)))
// return getSaved(m.group(2));
}
}

Expand Down Expand Up @@ -512,27 +477,6 @@ public boolean hasShooter() {
return isProjectile() && getProjectile().getShooter() != null;
}

/**
* Get this dEntity as an Ageable
*
* @return The Ageable
*/

public Ageable getAgeable() {

return (Ageable) entity;
}

/**
* Check whether this dEntity is an Ageable
*
* @return true or false
*/

public boolean isAgeable() {
return entity instanceof Ageable;
}

/**
* Whether this entity identifies as a generic
* entity type, for instance "e@cow", instead of
Expand Down Expand Up @@ -804,19 +748,13 @@ public boolean isSpawned() {
}

public boolean isValid() {

return entity.isValid();
}

public void remove() {
entity.remove();
}

public dEntity rememberAs(String id) {
dEntity.saveAs(this, id);
return this;
}

public void teleport(Location location) {
if (isNPC())
getNPC().teleport(location, TeleportCause.PLUGIN);
Expand Down Expand Up @@ -912,6 +850,24 @@ public DespawnedEntity(dEntity entity) {



public int comparesTo(dEntity entity) {
// If provided is unique, and both are the same unique entity, return 1.
if (entity.isUnique() && entity.identify().equals(identify())) return 1;

// If provided isn't unique...
if (!entity.isUnique()) {
// Return 1 if this object isn't unique either, but matches
if (!isUnique() && entity.identify().equals(identify()))
return 1;
// Return 1 if the provided object isn't unique, but whose entity_type
// matches this object, even if this object is unique.
if (entity_type == entity.entity_type) return 1;
}

return 0;
}



///////////////
// Properties
Expand Down Expand Up @@ -946,11 +902,19 @@ public EntityProfessional getProfessional() {
return EntityProfessional.getFrom(this);
}

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

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



//////////////////////////////
// DSCRIPT ARGUMENT METHODS
/////////////////////////
/////////////////////
// dObject Methods
///////////////////

private String prefix = "Entity";

Expand All @@ -975,23 +939,6 @@ public String debug() {
return "<G>" + prefix + "='<Y>" + identify() + "<G>' ";
}

public int comparesTo(dEntity entity) {
// If provided is unique, and both are the same unique entity, return 1.
if (entity.isUnique() && entity.identify().equals(identify())) return 1;

// If provided isn't unique...
if (!entity.isUnique()) {
// Return 1 if this object isn't unique either, but matches
if (!isUnique() && entity.identify().equals(identify()))
return 1;
// Return 1 if the provided object isn't unique, but whose entity_type
// matches this object, even if this object is unique.
if (entity_type == entity.entity_type) return 1;
}

return 0;
}

@Override
public String identify() {

Expand All @@ -1003,10 +950,9 @@ else if (isPlayer())
return "p@" + getPlayer().getName();
}

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

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

else if (isSpawned())
return "e@" + entity.getEntityId();
Expand All @@ -1025,7 +971,7 @@ public String toString() {

@Override
public boolean isUnique() {
return (isPlayer() || isNPC() || isSaved(this) || isSpawned());
return (isPlayer() || isNPC() || isSpawned()); // || isSaved()
}

@Override
Expand Down Expand Up @@ -1656,7 +1602,34 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
return new Element(entity instanceof Tameable)
.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;
}






return new Element(identify()).getAttribute(attribute);
}



}
9 changes: 9 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -39,6 +39,15 @@ public static dPlayer mirrorBukkitPlayer(OfflinePlayer player) {
// OBJECT FETCHER
/////////////////

// <--[language]
// @name p@
// @description
// p@ refers to the object type of a dPlayer. The 'p@' is notation for Denizen's Object
// Fetcher. The only valid constructor for a dPlayer is the name of the player the object should be
// associated with. For example, to reference the player named 'mythan', use p@mythan. Player names
// are case insensitive.
// -->

@ObjectFetcher("p")
public static dPlayer valueOf(String string) {
if (string == null) return null;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dWorld.java
Expand Up @@ -16,13 +16,33 @@

public class dWorld implements dObject {


/////////////////////
// STATIC METHODS
/////////////////

static Map<String, dWorld> worlds = new HashMap<String, dWorld>();

public static dWorld mirrorBukkitWorld(World world) {
if (world == null) return null;
if (worlds.containsKey(world.getName())) return worlds.get(world.getName());
else return new dWorld(world);
}


/////////////////////
// OBJECT FETCHER
/////////////////

// <--[language]
// @name w@
// @description
// w@ refers to the object type of a dWorld. The 'w@' is notation for Denizen's Object
// Fetcher. The only valid constructor for a dWorld is the name of the world it should be
// associated with. For example, to reference the world named 'world1', use w@world1.
// World names are case insensitive.
// -->

@ObjectFetcher("w")
public static dWorld valueOf(String string) {
if (string == null) return null;
Expand Down

0 comments on commit 17d629b

Please sign in to comment.