Skip to content

Commit 1031c8b

Browse files
committed
Rewriting ManualTrigger to send serverwide
ManualTrigger was only able to trigger an event if there was a bind() which used that event in MS. If one intends to send an only serverwide message. it was not possible. General rewrite as cleanup. Features stay the same.
1 parent 45ca3f1 commit 1031c8b

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

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

+30-26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.laytonsmith.core.constructs.Target;
1111
import com.laytonsmith.core.events.BoundEvent.Priority;
1212
import com.laytonsmith.core.exceptions.CRE.CREBindException;
13+
import com.laytonsmith.core.exceptions.CRE.CRECastException;
1314
import com.laytonsmith.core.exceptions.CRE.CREEventException;
15+
import com.laytonsmith.core.exceptions.CRE.CREIllegalArgumentException;
1416
import com.laytonsmith.core.extensions.Extension;
1517
import com.laytonsmith.core.extensions.ExtensionManager;
1618
import com.laytonsmith.core.extensions.ExtensionTracker;
@@ -140,42 +142,44 @@ public static SortedSet<BoundEvent> GetEvents(Driver type) {
140142
}
141143

142144
public static void ManualTrigger(String eventName, CArray object, Target t, boolean serverWide) {
143-
for(Driver type : event_handles.keySet()) {
145+
// Get the event
146+
Event event = EventList.getEvent(eventName);
147+
if(event == null) {
148+
// Abort if event is not existing
149+
throw new CREIllegalArgumentException("Non existant event is being triggered: " + eventName, t);
150+
}
151+
152+
BindableEvent convertedEvent = null;
153+
try {
154+
// Either an exception will be thrown, or null returned
155+
convertedEvent = event.convert(object, t);
156+
} catch(UnsupportedOperationException ex) {
157+
}
158+
if(convertedEvent == null) {
159+
throw new CREBindException(eventName + " doesn't support the use of trigger() yet.", t);
160+
}
161+
162+
// By Javadoc, if supportsExternal() false there is no use to call it serverwide
163+
if(serverWide && event.supportsExternal()) {
164+
event.manualTrigger(convertedEvent);
165+
} else {
144166
SortedSet<BoundEvent> toRun = new TreeSet<>();
145-
SortedSet<BoundEvent> bounded = GetEvents(type);
146-
Event driver = EventList.getEvent(type, eventName);
147-
if(bounded != null) {
148-
for(BoundEvent b : bounded) {
149-
if(b.getEventName().equalsIgnoreCase(eventName)) {
167+
for(Driver type : event_handles.keySet()) {
168+
for(BoundEvent boundEvent: GetEvents(type)) {
169+
if(boundEvent.getEventName().equalsIgnoreCase(eventName)) {
150170
try {
151-
BindableEvent convertedEvent = null;
152-
try {
153-
convertedEvent = driver.convert(object, t);
154-
} catch(UnsupportedOperationException ex) {
155-
// The event will stay null, and be caught below
156-
}
157-
if(convertedEvent == null) {
158-
throw new CREBindException(eventName + " doesn't support the use of trigger() yet.", t);
159-
} else if(driver.matches(b.getPrefilter(), convertedEvent)) {
160-
toRun.add(b);
171+
if(event.matches(boundEvent.getPrefilter(), convertedEvent)) {
172+
toRun.add(boundEvent);
161173
}
162174
} catch(PrefilterNonMatchException ex) {
163175
//Not running this one
164176
}
165177
}
166178
}
167179
}
168-
//If it's not a serverwide event, or this event doesn't support external events.
180+
169181
if(!toRun.isEmpty()) {
170-
if(!serverWide || !driver.supportsExternal()) {
171-
FireListeners(toRun, driver, driver.convert(object, t));
172-
} else {
173-
//It's serverwide, so we can just trigger it normally with the driver, and it should trickle back down to us
174-
driver.manualTrigger(driver.convert(object, t));
175-
}
176-
} else {
177-
//They have fired a non existant event
178-
ConfigRuntimeException.DoWarning(ConfigRuntimeException.CreateUncatchableException("Non existant event is being triggered: " + eventName, object.getTarget()));
182+
FireListeners(toRun, event, convertedEvent);
179183
}
180184
}
181185
}

0 commit comments

Comments
 (0)