Skip to content

Commit

Permalink
Cache some getMethod calls for performance. Add /loadtest command to …
Browse files Browse the repository at this point in the history
…Utils.dscript.
  • Loading branch information
aufdemrand committed Oct 23, 2013
1 parent 0567a46 commit fd4ad28
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 184 deletions.
26 changes: 3 additions & 23 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -24,7 +24,7 @@
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.scripts.requirements.RequirementRegistry;
import net.aufdemrand.denizen.scripts.triggers.TriggerRegistry;
import net.aufdemrand.denizen.tags.ObjectFetcher;
import net.aufdemrand.denizen.objects.ObjectFetcher;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.RuntimeCompiler;
import net.aufdemrand.denizen.utilities.ScoreboardHelper;
Expand Down Expand Up @@ -248,31 +248,11 @@ public void onEnable() {
// Register CommandHandler with Citizens
Depends.citizens.registerCommandClass(CommandHandler.class);

// Initialize ObjectFetcher
try {
// Initialize the ObjectFetcher
ObjectFetcher.registerWithObjectFetcher(dItem.class); // i@
ObjectFetcher.registerWithObjectFetcher(dCuboid.class); // cu@
ObjectFetcher.registerWithObjectFetcher(dEntity.class); // e@
ObjectFetcher.registerWithObjectFetcher(dInventory.class); // in@
ObjectFetcher.registerWithObjectFetcher(dColor.class); // co@
ObjectFetcher.registerWithObjectFetcher(dList.class); // li@/fl@
ObjectFetcher.registerWithObjectFetcher(dLocation.class); // l@
ObjectFetcher.registerWithObjectFetcher(dMaterial.class); // m@
ObjectFetcher.registerWithObjectFetcher(dNPC.class); // n@
ObjectFetcher.registerWithObjectFetcher(dPlayer.class); // p@
ObjectFetcher.registerWithObjectFetcher(dScript.class); // s@
ObjectFetcher.registerWithObjectFetcher(dWorld.class); // w@
ObjectFetcher.registerWithObjectFetcher(Element.class); // el@
ObjectFetcher.registerWithObjectFetcher(Duration.class); // d@
ObjectFetcher.registerWithObjectFetcher(dChunk.class); // ch@
ObjectFetcher._initialize();

// Initialize the NotableManager
NotableManager.registerWithNotableManager(dCuboid.class); // cuboid
NotableManager._initialize();

} catch (Exception e) {
// Shit!
e.printStackTrace();
}

// Initialize non-standard dMaterials
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/Duration.java
Expand Up @@ -83,7 +83,7 @@ public class Duration implements dObject {
* @param string the Argument value.
* @return a Duration, or null if incorrectly formatted.
*/
@ObjectFetcher("d")
@Fetchable("d")
public static Duration valueOf(String string) {
if (string == null) return null;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -54,7 +54,7 @@ public class Element implements dObject {
* @return a dScript dList
*
*/
@ObjectFetcher("el")
@Fetchable("el")
public static Element valueOf(String string) {
if (string == null) return null;

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Fetchable.java
@@ -0,0 +1,14 @@
package net.aufdemrand.denizen.objects;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

public @interface Fetchable {
String value();

}
104 changes: 96 additions & 8 deletions src/main/java/net/aufdemrand/denizen/objects/ObjectFetcher.java
@@ -1,14 +1,102 @@
package net.aufdemrand.denizen.objects;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import net.aufdemrand.denizen.objects.Fetchable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.utilities.debugging.dB;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public @interface ObjectFetcher {
String value();
/**
*
* @author Jeremy Schroeder
*
*/

public class ObjectFetcher {

// Keep track of each Class keyed by its 'object identifier' --> i@, e@, etc.
private static Map<String, Class> objects = new HashMap<String, Class>();

// Keep track of the static 'matches' and 'valueOf' methods for each dObject
static Map<Class, Method> matches = new WeakHashMap<Class, Method>();
static Map<Class, Method> valueof = new WeakHashMap<Class, Method>();

public static void _initialize() throws IOException, ClassNotFoundException, NoSuchMethodException {

// Initialize the ObjectFetcher
ObjectFetcher.registerWithObjectFetcher(dItem.class); // i@
ObjectFetcher.registerWithObjectFetcher(dCuboid.class); // cu@
ObjectFetcher.registerWithObjectFetcher(dEntity.class); // e@
ObjectFetcher.registerWithObjectFetcher(dInventory.class); // in@
ObjectFetcher.registerWithObjectFetcher(dColor.class); // co@
ObjectFetcher.registerWithObjectFetcher(dList.class); // li@/fl@
ObjectFetcher.registerWithObjectFetcher(dLocation.class); // l@
ObjectFetcher.registerWithObjectFetcher(dMaterial.class); // m@
ObjectFetcher.registerWithObjectFetcher(dNPC.class); // n@
ObjectFetcher.registerWithObjectFetcher(dPlayer.class); // p@
ObjectFetcher.registerWithObjectFetcher(dScript.class); // s@
ObjectFetcher.registerWithObjectFetcher(dWorld.class); // w@
ObjectFetcher.registerWithObjectFetcher(Element.class); // el@
ObjectFetcher.registerWithObjectFetcher(Duration.class); // d@
ObjectFetcher.registerWithObjectFetcher(dChunk.class); // ch@

if (fetchable_objects.isEmpty())
return;

Map<String, Class> adding = new HashMap<String, Class>();
for (Class dClass : fetchable_objects)
for (Method method : dClass.getMethods())
if (method.isAnnotationPresent(Fetchable.class)) {
String[] identifiers = method.getAnnotation(Fetchable.class).value().split(",");
for (String identifer : identifiers)
adding.put(identifer.trim().toLowerCase(), dClass);
}

objects.putAll(adding);
dB.echoApproval("Added objects to the ObjectFetcher " + adding.keySet().toString());
fetchable_objects.clear();
}

private static ArrayList<Class> fetchable_objects = new ArrayList<Class>();

public static void registerWithObjectFetcher(Class dObject) throws NoSuchMethodException {
fetchable_objects.add(dObject);
matches.put(dObject, dObject.getMethod("matches", String.class));
valueof.put(dObject, dObject.getMethod("valueOf", String.class));
}

public static boolean canFetch(String id) {
return objects.containsKey(id.toLowerCase());
}

public static Class getObjectClass(String id) {
if (canFetch(id))
return objects.get(id.toLowerCase());
else
return null;
}

public static boolean checkMatch(Class<? extends dObject> dClass, String value) {
try {
return (Boolean) matches.get(dClass).invoke(null, value);
} catch (Exception e) {
e.printStackTrace();
}

return false;

}

public static dObject getObjectFrom(Class<? extends dObject> dClass, String value) {
try {
return (dObject) valueof.get(dClass).invoke(null, value);
} catch (Exception e) {
e.printStackTrace();
}

return null;
}
}
36 changes: 10 additions & 26 deletions src/main/java/net/aufdemrand/denizen/objects/aH.java
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.entity.EntityType;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -74,6 +75,7 @@ public enum PrimitiveType { Float, Double, Integer, Boolean, String, Word, Perce
//////////////////

public static class Argument {

public String raw_value;
String prefix = null;
String value;
Expand All @@ -94,7 +96,6 @@ public Argument(String string) {
value = string.split(":", 2)[1];
}

// dB.log("Constructed Argument: " + prefix + ":" + value);
}


Expand All @@ -112,6 +113,7 @@ public boolean hasPrefix() {
return has_prefix;
}


public Argument getPrefix() {
if (prefix == null)
return null;
Expand Down Expand Up @@ -203,14 +205,7 @@ public boolean matchesPrimitive(PrimitiveType argumentType) {

// Check if this argument matches a certain dObject type
public boolean matchesArgumentType(Class<? extends dObject> dClass) {

try {
return (Boolean) dClass.getMethod("matches", String.class).invoke(null, value);
} catch (Exception e) {
e.printStackTrace();
}

return false;
return ObjectFetcher.checkMatch(dClass, value);
}


Expand Down Expand Up @@ -240,30 +235,19 @@ public Element asElement() {
return new Element(prefix, value);
}

public <T extends dObject> T asType(Class<? extends dObject> clazz) {

// dB.log("Calling asType: " + prefix + ":" + value + " " + clazz.getCanonicalName());
public <T extends dObject> T asType(Class<? extends dObject> clazz) {

try {
dObject arg = (dObject) clazz.getMethod("valueOf", String.class)
.invoke(null, value);
dObject arg = ObjectFetcher.getObjectFrom(clazz, value);

// dB.log("Created: " + clazz.cast(arg).debug());
if (arg != null) {
return (T) clazz.cast(arg).setPrefix(prefix);
}

} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
if (arg != null) {
return (T) clazz.cast(arg).setPrefix(prefix);
}

return null;
}


public void reportUnhandled() {
dB.echoError(dB.Messages.ERROR_UNKNOWN_ARGUMENT, raw_value);
}
Expand Down Expand Up @@ -626,7 +610,7 @@ public static boolean matchesItem(String arg) {
@Deprecated
public static boolean matchesContext(String arg) {
if (arg.toUpperCase().startsWith("CONTEXT:") ||
arg.toUpperCase().startsWith("DEFINE:")) return true;
arg.toUpperCase().startsWith("DEFINE:")) return true;
// TODO: Other matches____ do some actual checks, should this?.
return false;
}
Expand Down
17 changes: 1 addition & 16 deletions src/main/java/net/aufdemrand/denizen/objects/dChunk.java
@@ -1,29 +1,14 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.blocks.SafeBlock;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizen.utilities.depends.WorldGuardUtilities;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.minecraft.server.v1_6_R3.*;
import org.bukkit.*;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Sign;
import org.bukkit.craftbukkit.v1_6_R3.CraftChunk;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.potion.PotionEffect;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class dChunk extends CraftChunk implements dObject, Adjustable {

Expand All @@ -40,7 +25,7 @@ public class dChunk extends CraftChunk implements dObject, Adjustable {
* @return a dChunk, or null if incorrectly formatted
*
*/
@ObjectFetcher("ch")
@Fetchable("ch")
public static dChunk valueOf(String string) {
if (string == null) return null;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dColor.java
Expand Up @@ -25,7 +25,7 @@ public class dColor implements dObject {
* @return a Color, or null if incorrectly formatted
*
*/
@ObjectFetcher("co")
@Fetchable("co")
public static dColor valueOf(String string) {

string = string.toUpperCase().replace("CO@", "");
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dCuboid.java
Expand Up @@ -14,9 +14,7 @@
import net.aufdemrand.denizen.utilities.blocks.SafeBlock;
import net.aufdemrand.denizen.utilities.debugging.dB;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;

public class dCuboid implements dObject, Notable {
Expand All @@ -42,7 +40,7 @@ public class dCuboid implements dObject, Notable {
* @return a Location, or null if incorrectly formatted
*
*/
@ObjectFetcher("cu")
@Fetchable("cu")
public static dCuboid valueOf(String string) {
if (string == null) return null;

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -4,12 +4,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
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;
import net.aufdemrand.denizen.scripts.containers.core.EntityScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
Expand Down Expand Up @@ -83,7 +79,7 @@ public class dEntity implements dObject, Adjustable {
* @param string the string or dScript argument String
* @return a dEntity, or null
*/
@ObjectFetcher("e")
@Fetchable("e")
public static dEntity valueOf(String string) {
if (string == null) return null;

Expand Down
Expand Up @@ -83,7 +83,7 @@ public static dInventory valueOf(String string) {
* the specified inventory is invalid, this is null.
*
*/
@ObjectFetcher("in")
@Fetchable("in")
public static dInventory valueOf(String string, dPlayer player, dNPC npc) {

if (string == null) return null;
Expand Down

0 comments on commit fd4ad28

Please sign in to comment.