Skip to content

Commit 2bd6731

Browse files
committed
Fixing EventBuilder
-> 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.
1 parent 9979054 commit 2bd6731

File tree

1 file changed

+2
-37
lines changed

1 file changed

+2
-37
lines changed

src/main/java/com/laytonsmith/core/events/EventBuilder.java

+2-37
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import com.laytonsmith.PureUtilities.Common.StreamUtils;
55
import com.laytonsmith.abstraction.Implementation;
66
import com.laytonsmith.annotations.abstraction;
7-
import com.laytonsmith.core.constructs.Target;
8-
import com.laytonsmith.core.exceptions.CRE.CREPluginInternalException;
9-
import java.lang.reflect.Constructor;
107
import java.lang.reflect.Method;
118
import java.lang.reflect.Modifier;
129
import java.util.HashMap;
@@ -22,7 +19,6 @@ private EventBuilder() {
2219
}
2320

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

2824
static {
@@ -39,8 +35,6 @@ private EventBuilder() {
3935
}
4036
}
4137
eventImplementations.put(cinterface, c);
42-
//Also, warm it up
43-
warmup(cinterface);
4438
}
4539
}
4640
}
@@ -64,7 +58,7 @@ private static void warmup(Class<? extends BindableEvent> clazz) {
6458
if(method == null) {
6559
StreamUtils.GetSystemErr().println("UNABLE TO CACHE A CONSTRUCTOR FOR " + clazz.getSimpleName()
6660
+ ". Manual triggering will be impossible, and errors will occur"
67-
+ " if an attempt is made. Did you forget to add"
61+
+ " if an attempt is made. Did someone forget to add"
6862
+ " public static <Event> _instantiate(...) to " + clazz.getSimpleName() + "?");
6963
}
7064
methods.put((Class<BindableEvent>) clazz, method);
@@ -76,41 +70,12 @@ public static <T extends BindableEvent> T instantiate(Class<? extends BindableEv
7670
if(!methods.containsKey((Class<BindableEvent>) clazz)) {
7771
warmup(clazz);
7872
}
79-
Object o = methods.get((Class<BindableEvent>) clazz).invoke(null, params);
80-
//Now, we have an instance of the underlying object, which the instance
81-
//of the event BindableEvent should know how to handle in a constructor.
82-
if(!constructors.containsKey((Class<BindableEvent>) clazz)) {
83-
Class bindableEvent = eventImplementations.get((Class<BindableEvent>) clazz);
84-
Constructor constructor = null;
85-
for(Constructor c : bindableEvent.getConstructors()) {
86-
if(c.getParameterTypes().length == 1) {
87-
//looks promising
88-
if(c.getParameterTypes()[0].equals(o.getClass())) {
89-
//This is it
90-
constructor = c;
91-
break;
92-
}
93-
}
94-
}
95-
if(constructor == null) {
96-
throw new CREPluginInternalException("Cannot find an acceptable constructor that follows the format:"
97-
+ " public " + bindableEvent.getClass().getSimpleName() + "(" + o.getClass().getSimpleName() + " event)."
98-
+ " Please notify the plugin author of this error.", Target.UNKNOWN);
99-
}
100-
constructors.put((Class<BindableEvent>) clazz, constructor);
101-
}
102-
//Construct a new instance, then return it.
103-
Constructor constructor = constructors.get((Class<BindableEvent>) clazz);
104-
BindableEvent be = (BindableEvent) constructor.newInstance(o);
105-
return (T) be;
73+
return (T) methods.get((Class<BindableEvent>) clazz).invoke(null, params);
10674

10775
} catch(Exception e) {
10876
e.printStackTrace();
10977
}
11078
return null;
11179
}
11280

113-
// public static MCPlayerJoinEvent MCPlayerJoinEvent(MCPlayer player, String message){
114-
// return instantiate(MCPlayerJoinEvent.class, player, message);
115-
// }
11681
}

0 commit comments

Comments
 (0)