refactor: use Invocable instead of ScriptEngine in script managers
- Make fields final - Inject event script variable earlier - Remove redundant fields
This commit is contained in:
@@ -51,7 +51,6 @@ import server.maps.MapleReactor;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.Pair;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptException;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
@@ -214,10 +213,10 @@ public class EventInstanceManager {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Object invokeScriptFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
if (!disposed) {
|
||||
return ((Invocable) em.getIv()).invokeFunction(name, args);
|
||||
return em.getIv().invokeFunction(name, args);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ import server.quest.MapleQuest;
|
||||
import tools.exceptions.EventInstanceInProgressException;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Semaphore;
|
||||
@@ -60,7 +59,7 @@ import java.util.logging.Logger;
|
||||
* @author Ronan
|
||||
*/
|
||||
public class EventManager {
|
||||
private ScriptEngine iv;
|
||||
private Invocable iv;
|
||||
private Channel cserv;
|
||||
private World wserv;
|
||||
private Server server;
|
||||
@@ -83,7 +82,7 @@ public class EventManager {
|
||||
|
||||
private static final int maxLobbys = 8; // an event manager holds up to this amount of concurrent lobbys
|
||||
|
||||
public EventManager(Channel cserv, ScriptEngine iv, String name) {
|
||||
public EventManager(Channel cserv, Invocable iv, String name) {
|
||||
this.server = Server.getInstance();
|
||||
this.iv = iv;
|
||||
this.cserv = cserv;
|
||||
@@ -102,7 +101,7 @@ public class EventManager {
|
||||
ess.dispose();
|
||||
|
||||
try {
|
||||
((Invocable) iv).invokeFunction("cancelSchedule", (Object) null);
|
||||
iv.invokeFunction("cancelSchedule", (Object) null);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@@ -166,7 +165,7 @@ public class EventManager {
|
||||
|
||||
private List<Integer> getLobbyRange() {
|
||||
try {
|
||||
List<Object> objects = (List<Object>) ((Invocable) iv).invokeFunction("setLobbyRange", (Object) null);
|
||||
List<Object> objects = (List<Object>) iv.invokeFunction("setLobbyRange", (Object) null);
|
||||
return convertToIntegerList(objects);
|
||||
} catch (ScriptException | NoSuchMethodException ex) { // they didn't define a lobby range
|
||||
List<Integer> defaultRange = new ArrayList<>();
|
||||
@@ -184,7 +183,7 @@ public class EventManager {
|
||||
public EventScheduledFuture schedule(final String methodName, final EventInstanceManager eim, long delay) {
|
||||
Runnable r = () -> {
|
||||
try {
|
||||
((Invocable) iv).invokeFunction(methodName, eim);
|
||||
iv.invokeFunction(methodName, eim);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
@@ -199,7 +198,7 @@ public class EventManager {
|
||||
public EventScheduledFuture scheduleAtTimestamp(final String methodName, long timestamp) {
|
||||
Runnable r = () -> {
|
||||
try {
|
||||
((Invocable) iv).invokeFunction(methodName, (Object) null);
|
||||
iv.invokeFunction(methodName, (Object) null);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
@@ -217,7 +216,7 @@ public class EventManager {
|
||||
return cserv;
|
||||
}
|
||||
|
||||
public ScriptEngine getIv() {
|
||||
public Invocable getIv() {
|
||||
return iv;
|
||||
}
|
||||
|
||||
@@ -368,7 +367,7 @@ public class EventManager {
|
||||
}
|
||||
|
||||
private EventInstanceManager createInstance(String name, Object... args) throws ScriptException, NoSuchMethodException {
|
||||
return (EventInstanceManager) ((Invocable) iv).invokeFunction(name, args);
|
||||
return (EventInstanceManager) iv.invokeFunction(name, args);
|
||||
}
|
||||
|
||||
private void registerEventInstance(String eventName, int lobbyId) {
|
||||
@@ -690,7 +689,7 @@ public class EventManager {
|
||||
registerEventInstance(eim.getName(), lobbyId);
|
||||
eim.setLeader(leader);
|
||||
|
||||
((Invocable) iv).invokeFunction("setup", eim);
|
||||
iv.invokeFunction("setup", eim);
|
||||
eim.setProperty("leader", ldr);
|
||||
|
||||
eim.startEvent();
|
||||
@@ -717,7 +716,7 @@ public class EventManager {
|
||||
return(new ArrayList<>());
|
||||
}
|
||||
try {
|
||||
Object p = ((Invocable) iv).invokeFunction("getEligibleParty", party.getPartyMembersOnline());
|
||||
Object p = iv.invokeFunction("getEligibleParty", party.getPartyMembersOnline());
|
||||
|
||||
if(p != null) {
|
||||
final List<MaplePartyCharacter> lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
|
||||
@@ -733,7 +732,7 @@ public class EventManager {
|
||||
|
||||
public void clearPQ(EventInstanceManager eim) {
|
||||
try {
|
||||
((Invocable) iv).invokeFunction("clearPQ", eim);
|
||||
iv.invokeFunction("clearPQ", eim);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
@@ -741,7 +740,7 @@ public class EventManager {
|
||||
|
||||
public void clearPQ(EventInstanceManager eim, MapleMap toMap) {
|
||||
try {
|
||||
((Invocable) iv).invokeFunction("clearPQ", eim, toMap);
|
||||
iv.invokeFunction("clearPQ", eim, toMap);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
||||
@@ -39,27 +39,29 @@ import java.util.logging.Logger;
|
||||
* @author Matze
|
||||
*/
|
||||
public class EventScriptManager extends AbstractScriptManager {
|
||||
private static final String INJECTED_VARIABLE = "em";
|
||||
private static EventEntry fallback;
|
||||
private final Map<String, EventEntry> events = new ConcurrentHashMap<>();
|
||||
private boolean active = false;
|
||||
|
||||
private class EventEntry {
|
||||
private static class EventEntry {
|
||||
|
||||
public EventEntry(ScriptEngine iv, EventManager em) {
|
||||
public EventEntry(Invocable iv, EventManager em) {
|
||||
this.iv = iv;
|
||||
this.em = em;
|
||||
}
|
||||
public ScriptEngine iv;
|
||||
public Invocable iv;
|
||||
public EventManager em;
|
||||
}
|
||||
|
||||
private static EventEntry fallback;
|
||||
private Map<String, EventEntry> events = new ConcurrentHashMap<>();
|
||||
private boolean active = false;
|
||||
|
||||
public EventScriptManager(Channel cserv, String[] scripts) {
|
||||
super();
|
||||
|
||||
public EventScriptManager(final Channel channel, String[] scripts) {
|
||||
for (String script : scripts) {
|
||||
if (!script.equals("")) {
|
||||
ScriptEngine iv = getScriptEngine("event/" + script + ".js");
|
||||
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
|
||||
if (!script.isEmpty()) {
|
||||
ScriptEngine engine = getInvocableScriptEngine("event/" + script + ".js");
|
||||
Invocable iv = (Invocable) engine;
|
||||
EventManager eventManager = new EventManager(channel, iv, script);
|
||||
engine.put(INJECTED_VARIABLE, eventManager);
|
||||
events.put(script, new EventEntry(iv, eventManager));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +84,7 @@ public class EventScriptManager extends AbstractScriptManager {
|
||||
public final void init() {
|
||||
for (EventEntry entry : events.values()) {
|
||||
try {
|
||||
entry.iv.put("em", entry.em);
|
||||
((Invocable) entry.iv).invokeFunction("init", (Object) null);
|
||||
entry.iv.invokeFunction("init", (Object) null);
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(EventScriptManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
System.out.println("Error on script: " + entry.em.getName());
|
||||
@@ -102,11 +103,15 @@ public class EventScriptManager extends AbstractScriptManager {
|
||||
Channel cserv = eventEntries.iterator().next().getValue().em.getChannelServer();
|
||||
for (Entry<String, EventEntry> entry : eventEntries) {
|
||||
String script = entry.getKey();
|
||||
ScriptEngine iv = getScriptEngine("event/" + script + ".js");
|
||||
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
|
||||
ScriptEngine engine = getInvocableScriptEngine("event/" + script + ".js");
|
||||
Invocable iv = (Invocable) engine;
|
||||
EventManager eventManager = new EventManager(cserv, iv, script);
|
||||
engine.put(INJECTED_VARIABLE, eventManager);
|
||||
events.put(script, new EventEntry(iv, eventManager));
|
||||
}
|
||||
}
|
||||
|
||||
// Is never being called
|
||||
public void reload() {
|
||||
cancel();
|
||||
reloadScripts();
|
||||
|
||||
Reference in New Issue
Block a user