Skip to content

Commit

Permalink
monitor handled listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Apr 28, 2020
1 parent 2eaf5e3 commit 863a128
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
11 changes: 3 additions & 8 deletions src/main/java/com/mengcraft/script/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredListener;

import javax.script.Bindings;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;

/**
* Created on 16-10-17.
Expand Down Expand Up @@ -42,7 +41,7 @@ protected void remove(HandledListener listener) {
}
}

public HandledListener add(ScriptPlugin script, Consumer<Event> executor, int order) {
public HandledListener add(ScriptPlugin script, Bindings executor, int order) {
CustomRegisteredListener custom = handledExecutors.computeIfAbsent(Utils.getEventPriority(order),
priority -> new CustomRegisteredListener(priority, false));
if (custom.isEmpty()) {
Expand Down Expand Up @@ -77,11 +76,7 @@ private CustomRegisteredListener(EventPriority priority, boolean ignoreCancelled
public void callEvent(Event event) {
if (clz == event.getClass()) {
for (HandledListener listener : executors) {
try {
listener.getExecutor().accept(event);
} catch (Exception e) {
listener.getPlugin().getLogger().log(Level.SEVERE, name(), e);
}
listener.handle(event);
}
}
}
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/mengcraft/script/HandledListener.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mengcraft.script;

import com.mengcraft.script.util.Utils;
import lombok.EqualsAndHashCode;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;

import javax.script.Bindings;
import java.util.Comparator;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.logging.Level;

/**
* Created on 16-10-17.
Expand All @@ -15,30 +17,40 @@
public class HandledListener {

private final UUID id = UUID.randomUUID();// Use random id to func
private final Consumer<Event> executor;
private final Bindings executor;
private final EventListener managedListener;
private final int priority;
private final ScriptPlugin plugin;
private final EventPriority eventPriority;

public HandledListener(EventListener managedListener, ScriptPlugin plugin, Consumer<Event> executor, int priority, EventPriority eventPriority) {
public HandledListener(EventListener managedListener, ScriptPlugin plugin, Bindings executor, int priority, EventPriority eventPriority) {
this.managedListener = managedListener;
this.plugin = plugin;
this.executor = executor;
this.priority = priority;
this.eventPriority = eventPriority;
}

public void handle(Event event) {
long millis = System.currentTimeMillis();
try {
Utils.invoke(executor, event);
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, String.format("Exception occurred while handle %s\n%s", event.getEventName(), executor.toString()), e);
} finally {
millis = System.currentTimeMillis() - millis;
if (millis >= 5) {
plugin.getLogger().warning(String.format("Consume too much time to handle %s. (%s millis)\n%s", event.getEventName(), millis, executor.toString()));
}
}
}

public void remove() {
if (plugin.remove(this)) {
managedListener.remove(this);
}
}

public Consumer<Event> getExecutor() {
return executor;
}

public ScriptPlugin getPlugin() {
return plugin;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/mengcraft/script/ScriptPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ public HandledPlaceholder addPlaceholder(String id, HandledPlaceholder.Func func
throw new IllegalStateException("id " + id + " conflict");
}

public HandledListener addListener(String event, Consumer<Event> i) {
return addListener(event, i, -1);
public HandledListener addListener(String event, Bindings executor) {
return addListener(event, executor, -1);
}

public HandledListener addListener(String eventName, Consumer<Event> executor, int priority) {
public HandledListener addListener(String eventName, Bindings executor, int priority) {
Preconditions.checkState(isHandled(), "unloaded");
EventListener handle = EventMapping.INSTANCE.getListener(eventName);
HandledListener add = handle.add(this, executor, priority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void loadListener(ScriptPlugin plugin, ScriptEngine ctx) {
if (handle != null && EventMapping.INSTANCE.initialized(handle)) {
var obj = ctx.get("handle");
if (obj != null) {
plugin.addListener(handle, ((Invocable) ctx).getInterface(obj, Consumer.class));
plugin.addListener(handle, (Bindings) obj);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/mengcraft/script/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.script.ScriptEngine;
import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -19,6 +20,7 @@ public class Utils {
private static final Yaml YAML = new Yaml();
private static Function<Object, Bindings> java_from_invoker;
private static Consumer<Bindings> function_invoker;
private static BiConsumer<Bindings, Object> function_alt_invoker;

public static Yaml getYaml() {
return YAML;
Expand Down Expand Up @@ -50,12 +52,19 @@ public static void setup(ScriptEngine js) {
bindings = js.createBindings();
js.eval("function accept(a){a();}", bindings);
function_invoker = ((Invocable) js).getInterface(bindings, Consumer.class);
bindings = js.createBindings();
js.eval("function accept(a, b){a(b);}", bindings);
function_alt_invoker = ((Invocable) js).getInterface(bindings, BiConsumer.class);
}

public static void invoke(Bindings bindings) {
function_invoker.accept(bindings);
}

public static <T> void invoke(Bindings invokable, T obj) {
function_alt_invoker.accept(invokable, obj);
}

@SneakyThrows
public static <T> Bindings fromJava(T obj) {
return java_from_invoker.apply(obj);
Expand Down

0 comments on commit 863a128

Please sign in to comment.