Skip to content

Commit

Permalink
🍒 [2.7] Fixes an exception from being thrown on async events (#5980)
Browse files Browse the repository at this point in the history
Fixes an exception from being thrown on async events (#5699)

Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com>
Co-authored-by: Moderocky <admin@moderocky.com>
  • Loading branch information
3 people authored Sep 9, 2023
1 parent 7148bf2 commit 3f08853
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/main/java/ch/njol/skript/SkriptEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
*/
package ch.njol.skript;

import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.timings.SkriptTimings;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
Expand All @@ -36,16 +42,13 @@
import org.bukkit.plugin.RegisteredListener;
import org.eclipse.jdt.annotation.Nullable;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.timings.SkriptTimings;
import ch.njol.skript.util.Task;

public final class SkriptEventHandler {

Expand Down Expand Up @@ -110,12 +113,14 @@ private static List<Trigger> getTriggers(Class<? extends Event> event) {
*/
private static void check(Event event, EventPriority priority) {
List<Trigger> triggers = getTriggers(event.getClass());
if (triggers.isEmpty())
return;

if (Skript.logVeryHigh()) {
boolean hasTrigger = false;
for (Trigger trigger : triggers) {
SkriptEvent triggerEvent = trigger.getEvent();
if (triggerEvent.getEventPriority() == priority && triggerEvent.check(event)) {
if (triggerEvent.getEventPriority() == priority && Boolean.TRUE.equals(Task.callSync(() -> triggerEvent.check(event)))) {
hasTrigger = true;
break;
}
Expand All @@ -137,16 +142,31 @@ private static void check(Event event, EventPriority priority) {

for (Trigger trigger : triggers) {
SkriptEvent triggerEvent = trigger.getEvent();
if (triggerEvent.getEventPriority() != priority || !triggerEvent.check(event))
if (triggerEvent.getEventPriority() != priority)
continue;

logTriggerStart(trigger);
Object timing = SkriptTimings.start(trigger.getDebugLabel());

trigger.execute(event);

SkriptTimings.stop(timing);
logTriggerEnd(trigger);
// these methods need to be run on whatever thread the trigger is
Runnable execute = () -> {
logTriggerStart(trigger);
Object timing = SkriptTimings.start(trigger.getDebugLabel());
trigger.execute(event);
SkriptTimings.stop(timing);
logTriggerEnd(trigger);
};

if (trigger.getEvent().canExecuteAsynchronously()) {
// check should be performed on the main thread
if (Boolean.FALSE.equals(Task.callSync(() -> triggerEvent.check(event))))
continue;
execute.run();
} else { // Ensure main thread
Task.callSync(() -> {
if (!triggerEvent.check(event))
return null;
execute.run();
return null; // we don't care about a return value
});
}
}

logEventEnd();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ch/njol/skript/lang/SkriptEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ public boolean isEventPrioritySupported() {
return true;
}

/**
* Override this method to allow Skript to not force synchronization.
*/
public boolean canExecuteAsynchronously() {
return false;
}

/**
* Fixes patterns in event by modifying every {@link ch.njol.skript.patterns.TypePatternElement}
* to be nullable.
Expand Down

0 comments on commit 3f08853

Please sign in to comment.