Skip to content

Commit

Permalink
Add PropertyParser
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Oct 21, 2013
1 parent 8cabab9 commit eb37a65
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -15,6 +15,7 @@
import net.aufdemrand.denizen.npc.traits.*;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.scripts.*;
import net.aufdemrand.denizen.scripts.commands.CommandRegistry;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptHelper;
Expand Down Expand Up @@ -103,6 +104,17 @@ public TriggerRegistry getTriggerRegistry() {
}


/*
* Denizen Property Parser
*/
private PropertyParser propertyParser;

private PropertyParser getPropertyParser() {
return propertyParser;
}



/*
* Denizen Managers
*/
Expand Down Expand Up @@ -236,8 +248,6 @@ public void onEnable() {
// Register CommandHandler with Citizens
Depends.citizens.registerCommandClass(CommandHandler.class);

dB.echoDebug(DebugElement.Footer);

try {
// Initialize the ObjectFetcher
ObjectFetcher.registerWithObjectFetcher(dItem.class); // i@
Expand Down Expand Up @@ -268,8 +278,15 @@ public void onEnable() {
// Initialize non-standard dMaterials
dMaterial._initialize();

// Fire the 'on Server Start' world event
// Initialize Property Parser
propertyParser = new PropertyParser();

dB.echoDebug(DebugElement.Footer);

// TODO: Figure out why we need this
ScriptHelper.reloadScripts();

// Fire the 'on Server Start' world event
ws_helper.serverStartEvent();
}

Expand Down
@@ -1,9 +1,105 @@
package net.aufdemrand.denizen.objects.properties;

// TODO: Whatever this is!
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.utilities.debugging.dB;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class PropertyParser {

public static String NONE = "";

// Keep track of which Property belongs to which dObject
static Map<Class<? extends dObject>, List<Class>> properties
= new HashMap<Class<? extends dObject>, List<Class>>();

// Keep track of the static 'describes' and 'getFrom' methods for each Property
static Map<Class, Method> describes = new WeakHashMap<Class, Method>();
static Map<Class, Method> getFrom = new WeakHashMap<Class, Method>();


public PropertyParser() {
// register core dEntity properties
registerProperty(EntityProfession.class, dEntity.class);
registerProperty(EntityAge.class, dEntity.class);
registerProperty(EntityFramed.class, dEntity.class);
registerProperty(EntityInfected.class, dEntity.class);

// register core dItem properties
registerProperty(ItemColor.class, dItem.class);

}

public void registerProperty(Class property, Class<? extends dObject> object) {
// Add property to the dObject's Properties list
List<Class> prop_list;

// Get current properties list, or make a new one
if (properties.containsKey(object))
prop_list = properties.get(object);
else prop_list = new ArrayList<Class>();

// Add this property to the list
prop_list.add(property);

// Put the list back into the Map
properties.put(object, prop_list);

// Cache methods used for fetching new properties
try {
describes.put(property, property.getMethod("describes", dObject.class));
getFrom.put(property, property.getMethod("getFrom", dObject.class));

} catch (NoSuchMethodException e) {
dB.echoError("Unable to register property '" + property.getSimpleName() + "'!");
}

}

public static String getPropertiesString(dObject object) {
StringBuilder prop_string = new StringBuilder();

// Iterate through each property associated with the dObject type, invoke 'describes'
// and if 'true', add property string from the property to the prop_string.
try {
for (Class property : properties.get(object.getClass())) {
if ((Boolean) describes.get(property).invoke(null, object))
prop_string.append(((Property) getFrom.get(property).invoke(null, object)).getPropertyString());
}

} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

// Return the list of properties
return prop_string.toString();
}

public static List<Property> getProperties(dObject object) {
List<Property> props = new ArrayList<Property>();

try {
for (Class property : properties.get(object.getClass())) {
if ((Boolean) describes.get(property).invoke(null, object))
props.add((Property) getFrom.get(property).invoke(null, object));
}

} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

return props;

}



}

0 comments on commit eb37a65

Please sign in to comment.