Skip to content

Commit

Permalink
Fixing EventBuilder
Browse files Browse the repository at this point in the history
-> Using EventBuilder resulted in spam in console on proper usage.
EventBuilder is checking in static code if each Event has a _instantiate method to create it. Some events don't need to be created since convert() returns null or throws an exception. By removing the "warmup" for all events on classloading, the loads is distributed and the actual checks if _instantiate exists will only be done, when a convert() method actually needs to create an event.
-> instantiate() expects _instantiate() to return a Bukkit event, but all except one event return a BukkitMC*Event from CH. There is no need for EventBuilder to insert the Bukkit event into the BukkitMC*Event with reflection and overhead.
  • Loading branch information
Ecconia committed Jun 14, 2018
1 parent 5a50e50 commit f5a7ada
Showing 1 changed file with 2 additions and 37 deletions.
39 changes: 2 additions & 37 deletions src/main/java/com/laytonsmith/core/events/EventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.laytonsmith.PureUtilities.Common.StreamUtils;
import com.laytonsmith.abstraction.Implementation;
import com.laytonsmith.annotations.abstraction;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.CRE.CREPluginInternalException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
Expand All @@ -22,7 +19,6 @@ private EventBuilder() {
}

private static final Map<Class<BindableEvent>, Method> methods = new HashMap<Class<BindableEvent>, Method>();
private static final Map<Class<BindableEvent>, Constructor<? extends BindableEvent>> constructors = new HashMap<Class<BindableEvent>, Constructor<? extends BindableEvent>>();
private static final Map<Class<BindableEvent>, Class<BindableEvent>> eventImplementations = new HashMap<Class<BindableEvent>, Class<BindableEvent>>();

static {
Expand All @@ -39,8 +35,6 @@ private EventBuilder() {
}
}
eventImplementations.put(cinterface, c);
//Also, warm it up
warmup(cinterface);
}
}
}
Expand All @@ -64,7 +58,7 @@ private static void warmup(Class<? extends BindableEvent> clazz) {
if(method == null) {
StreamUtils.GetSystemErr().println("UNABLE TO CACHE A CONSTRUCTOR FOR " + clazz.getSimpleName()
+ ". Manual triggering will be impossible, and errors will occur"
+ " if an attempt is made. Did you forget to add"
+ " if an attempt is made. Did someone forget to add"
+ " public static <Event> _instantiate(...) to " + clazz.getSimpleName() + "?");
}
methods.put((Class<BindableEvent>) clazz, method);
Expand All @@ -76,41 +70,12 @@ public static <T extends BindableEvent> T instantiate(Class<? extends BindableEv
if(!methods.containsKey((Class<BindableEvent>) clazz)) {
warmup(clazz);
}
Object o = methods.get((Class<BindableEvent>) clazz).invoke(null, params);
//Now, we have an instance of the underlying object, which the instance
//of the event BindableEvent should know how to handle in a constructor.
if(!constructors.containsKey((Class<BindableEvent>) clazz)) {
Class bindableEvent = eventImplementations.get((Class<BindableEvent>) clazz);
Constructor constructor = null;
for(Constructor c : bindableEvent.getConstructors()) {
if(c.getParameterTypes().length == 1) {
//looks promising
if(c.getParameterTypes()[0].equals(o.getClass())) {
//This is it
constructor = c;
break;
}
}
}
if(constructor == null) {
throw new CREPluginInternalException("Cannot find an acceptable constructor that follows the format:"
+ " public " + bindableEvent.getClass().getSimpleName() + "(" + o.getClass().getSimpleName() + " event)."
+ " Please notify the plugin author of this error.", Target.UNKNOWN);
}
constructors.put((Class<BindableEvent>) clazz, constructor);
}
//Construct a new instance, then return it.
Constructor constructor = constructors.get((Class<BindableEvent>) clazz);
BindableEvent be = (BindableEvent) constructor.newInstance(o);
return (T) be;
return (T) methods.get((Class<BindableEvent>) clazz).invoke(null, params);

} catch(Exception e) {
e.printStackTrace();
}
return null;
}

// public static MCPlayerJoinEvent MCPlayerJoinEvent(MCPlayer player, String message){
// return instantiate(MCPlayerJoinEvent.class, player, message);
// }
}

0 comments on commit f5a7ada

Please sign in to comment.