Skip to content

Commit

Permalink
Big optimisation for asynchronous placeholder requests
Browse files Browse the repository at this point in the history
  • Loading branch information
szumielxd authored Jan 29, 2021
1 parent 4aac4ec commit 47cf812
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package me.szumielxd.PlaceholderSK.placeholderAPI;

import org.bukkit.Bukkit;
//import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventException;
import org.bukkit.event.Listener;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.szumielxd.PlaceholderSK.PlaceholderSK;
import me.szumielxd.PlaceholderSK.utils.ReflectionUtils;
import me.szumielxd.PlaceholderSK.skript.events.SKPlaceholderRequestEvent;
//import me.szumielxd.PlaceholderSK.utils.ReflectionUtils;

public class PAPIListener extends PlaceholderExpansion {


private String identifier;
private final String identifier;
private final SKPlaceholderRequestEvent skEvent;
private final Listener listener;


public PAPIListener(String prefix) {
public PAPIListener(SKPlaceholderRequestEvent skEvent, String prefix) {
this.skEvent = skEvent;
this.identifier = prefix;
this.listener = new Listener() {};
}


Expand Down Expand Up @@ -46,16 +53,21 @@ public String getPlugin() {

@Override
public String onPlaceholderRequest(Player p, String param) {
PAPIEvent event;
if(Bukkit.isPrimaryThread()) {
PAPIEvent event = new PAPIEvent(p, identifier, param);
/*if(Bukkit.isPrimaryThread()) {
event = new PAPIEvent(p, identifier, param);
}else {
event = new PAPIEvent(p, identifier, param, true);
}
ReflectionUtils utils = PlaceholderSK.getInstance().getReflectionUtils();
Object res = utils.getField(Bukkit.getServer(), "console");
boolean running = (boolean) utils.invokeMethod(res, "isRunning");
if(running) Bukkit.getPluginManager().callEvent(event);
if(running) Bukkit.getPluginManager().callEvent(event);*/
try {
this.skEvent.getExecutor().execute(this.listener, event);
} catch (EventException e) {
e.printStackTrace();
}
return event.getResult();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.szumielxd.PlaceholderSK.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptConfig;
//import ch.njol.skript.SkriptConfig;
import ch.njol.skript.SkriptEventHandler;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand All @@ -12,17 +12,17 @@
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.Task;
//import ch.njol.skript.util.Task;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.szumielxd.PlaceholderSK.PlaceholderSK;
//import me.szumielxd.PlaceholderSK.PlaceholderSK;
import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIEvent;
import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIListener;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.Callable;
//import java.util.concurrent.Callable;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -84,11 +84,11 @@ public void execute(final Listener l, final Event e) throws EventException {
Trigger tr = triggers.get(ev.getPrefix());
if(tr != null) {

if(!ev.isAsynchronous()) {
//if(!ev.isAsynchronous()) {
SkriptEventHandler.logTriggerStart(tr);
tr.execute(e);
SkriptEventHandler.logTriggerEnd(tr);
} else {
/*} else {
Task.callSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
Expand All @@ -98,12 +98,16 @@ public Void call() throws Exception {
return null;
}
});
}
}*/
}
}
return;
}
};

public EventExecutor getExecutor() {
return this.executor;
}

@Override
public String toString(Event e, boolean debug) {
Expand All @@ -113,11 +117,11 @@ public String toString(Event e, boolean debug) {
@Override
public void register(Trigger tr) {
triggers.put(prefix, tr);
PAPIListener l = new PAPIListener(prefix);
PAPIListener l = new PAPIListener(this, prefix);
listeners.put(prefix, l);
l.register();
if (!registeredExecutor) {
Bukkit.getPluginManager().registerEvent(PAPIEvent.class, new Listener() {}, SkriptConfig.defaultEventPriority.value(), executor, PlaceholderSK.getInstance(), true);
//Bukkit.getPluginManager().registerEvent(PAPIEvent.class, new Listener() {}, SkriptConfig.defaultEventPriority.value(), executor, PlaceholderSK.getInstance(), true);
registeredExecutor = true;
}
}
Expand Down

0 comments on commit 47cf812

Please sign in to comment.