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:
@@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package scripting;
|
||||
|
||||
import client.MapleClient;
|
||||
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
|
||||
import tools.FilePrinter;
|
||||
|
||||
import javax.script.*;
|
||||
@@ -40,7 +41,7 @@ public abstract class AbstractScriptManager {
|
||||
sef = new ScriptEngineManager().getEngineByName("graal.js").getFactory();
|
||||
}
|
||||
|
||||
protected ScriptEngine getScriptEngine(String path) {
|
||||
protected ScriptEngine getInvocableScriptEngine(String path) {
|
||||
path = "scripts/" + path;
|
||||
File scriptFile = new File(path);
|
||||
if (!scriptFile.exists()) {
|
||||
@@ -48,7 +49,11 @@ public abstract class AbstractScriptManager {
|
||||
}
|
||||
|
||||
ScriptEngine engine = sef.getScriptEngine();
|
||||
applyLooserGraalSecurity(engine);
|
||||
if (!(engine instanceof GraalJSScriptEngine graalScriptEngine)) {
|
||||
throw new IllegalStateException("ScriptEngineFactory did not provide a GraalJSScriptEngine");
|
||||
}
|
||||
|
||||
enableScriptHostAccess(graalScriptEngine);
|
||||
|
||||
try (FileReader fr = new FileReader(scriptFile)) {
|
||||
engine.eval(fr);
|
||||
@@ -57,21 +62,24 @@ public abstract class AbstractScriptManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
return engine;
|
||||
return graalScriptEngine;
|
||||
}
|
||||
|
||||
protected ScriptEngine getScriptEngine(String path, MapleClient c) {
|
||||
protected ScriptEngine getInvocableScriptEngine(String path, MapleClient c) {
|
||||
ScriptEngine engine = c.getScriptEngine("scripts/" + path);
|
||||
if (engine == null) {
|
||||
engine = getScriptEngine(path);
|
||||
engine = getInvocableScriptEngine(path);
|
||||
c.setScriptEngine(path, engine);
|
||||
}
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
private void applyLooserGraalSecurity(ScriptEngine scriptEngine) {
|
||||
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
/**
|
||||
* Allow usage of "Java.type()" in script to look up host class
|
||||
*/
|
||||
private void enableScriptHostAccess(GraalJSScriptEngine engine) {
|
||||
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
bindings.put("polyglot.js.allowHostAccess", true);
|
||||
bindings.put("polyglot.js.allowHostClassLookup", true);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -27,28 +27,18 @@ import scripting.AbstractScriptManager;
|
||||
import tools.FilePrinter;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapScriptManager extends AbstractScriptManager {
|
||||
private static final MapScriptManager instance = new MapScriptManager();
|
||||
|
||||
private final Map<String, Invocable> scripts = new HashMap<>();
|
||||
|
||||
private static MapScriptManager instance = new MapScriptManager();
|
||||
|
||||
public static MapScriptManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Map<String, Invocable> scripts = new HashMap<>();
|
||||
private final ScriptEngineFactory sef;
|
||||
|
||||
private MapScriptManager() {
|
||||
ScriptEngineManager sem = new ScriptEngineManager();
|
||||
sef = sem.getEngineByName("graal.js").getFactory();
|
||||
}
|
||||
|
||||
public void reloadScripts() {
|
||||
scripts.clear();
|
||||
@@ -76,20 +66,18 @@ public class MapScriptManager extends AbstractScriptManager {
|
||||
}
|
||||
|
||||
try {
|
||||
iv = (Invocable) getScriptEngine("map/" + mapScriptPath + ".js");
|
||||
iv = (Invocable) getInvocableScriptEngine("map/" + mapScriptPath + ".js");
|
||||
if (iv == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
scripts.put(mapScriptPath, iv);
|
||||
((Invocable) iv).invokeFunction("start", new MapScriptMethods(c));
|
||||
iv.invokeFunction("start", new MapScriptMethods(c));
|
||||
return true;
|
||||
} catch (final UndeclaredThrowableException | ScriptException ute) {
|
||||
FilePrinter.printError(FilePrinter.MAP_SCRIPT + mapScriptPath + ".txt", ute);
|
||||
} catch (final Exception e) {
|
||||
FilePrinter.printError(FilePrinter.MAP_SCRIPT + mapScriptPath + ".txt", e);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,6 @@ import tools.MaplePacketCreator;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -42,23 +41,22 @@ import java.util.Map;
|
||||
* @author Matze
|
||||
*/
|
||||
public class NPCScriptManager extends AbstractScriptManager {
|
||||
private static final NPCScriptManager instance = new NPCScriptManager();
|
||||
|
||||
private static NPCScriptManager instance = new NPCScriptManager();
|
||||
private final Map<MapleClient, NPCConversationManager> cms = new HashMap<>();
|
||||
private final Map<MapleClient, Invocable> scripts = new HashMap<>();
|
||||
|
||||
public static NPCScriptManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Map<MapleClient, NPCConversationManager> cms = new HashMap<>();
|
||||
private Map<MapleClient, ScriptEngine> scripts = new HashMap<>();
|
||||
|
||||
public boolean isNpcScriptAvailable(MapleClient c, String fileName) {
|
||||
ScriptEngine iv = null;
|
||||
ScriptEngine engine = null;
|
||||
if (fileName != null) {
|
||||
iv = getScriptEngine("npc/" + fileName + ".js", c);
|
||||
engine = getInvocableScriptEngine("npc/" + fileName + ".js", c);
|
||||
}
|
||||
|
||||
return iv != null;
|
||||
return engine != null;
|
||||
}
|
||||
|
||||
public boolean start(MapleClient c, int npc, MapleCharacter chr) {
|
||||
@@ -83,30 +81,29 @@ public class NPCScriptManager extends AbstractScriptManager {
|
||||
|
||||
public void start(String filename, MapleClient c, int npc, List<MaplePartyCharacter> chrs) {
|
||||
try {
|
||||
NPCConversationManager cm = new NPCConversationManager(c, npc, chrs, true);
|
||||
final NPCConversationManager cm = new NPCConversationManager(c, npc, chrs, true);
|
||||
cm.dispose();
|
||||
if (cms.containsKey(c)) {
|
||||
return;
|
||||
}
|
||||
cms.put(c, cm);
|
||||
ScriptEngine iv = getScriptEngine("npc/" + filename + ".js", c);
|
||||
ScriptEngine engine = getInvocableScriptEngine("npc/" + filename + ".js", c);
|
||||
|
||||
if (iv == null) {
|
||||
if (engine == null) {
|
||||
c.getPlayer().dropMessage(1, "NPC " + npc + " is uncoded.");
|
||||
cm.dispose();
|
||||
return;
|
||||
}
|
||||
iv.put("cm", cm);
|
||||
scripts.put(c, iv);
|
||||
engine.put("cm", cm);
|
||||
|
||||
Invocable invocable = (Invocable) engine;
|
||||
scripts.put(c, invocable);
|
||||
try {
|
||||
((Invocable) iv).invokeFunction("start", chrs);
|
||||
invocable.invokeFunction("start", chrs);
|
||||
} catch (final NoSuchMethodException nsme) {
|
||||
nsme.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (final UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", ute);
|
||||
dispose(c);
|
||||
} catch (final Exception e) {
|
||||
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", e);
|
||||
dispose(c);
|
||||
@@ -115,38 +112,40 @@ public class NPCScriptManager extends AbstractScriptManager {
|
||||
|
||||
private boolean start(MapleClient c, int npc, int oid, String fileName, MapleCharacter chr, boolean itemScript, String engineName) {
|
||||
try {
|
||||
NPCConversationManager cm = new NPCConversationManager(c, npc, oid, fileName, itemScript);
|
||||
final NPCConversationManager cm = new NPCConversationManager(c, npc, oid, fileName, itemScript);
|
||||
if (cms.containsKey(c)) {
|
||||
dispose(c);
|
||||
}
|
||||
if (c.canClickNPC()) {
|
||||
cms.put(c, cm);
|
||||
ScriptEngine iv = null;
|
||||
ScriptEngine engine = null;
|
||||
if (!itemScript) {
|
||||
if (fileName != null) {
|
||||
iv = getScriptEngine("npc/" + fileName + ".js", c);
|
||||
engine = getInvocableScriptEngine("npc/" + fileName + ".js", c);
|
||||
}
|
||||
} else {
|
||||
if (fileName != null) { // thanks MiLin for drafting NPC-based item scripts
|
||||
iv = getScriptEngine("item/" + fileName + ".js", c);
|
||||
engine = getInvocableScriptEngine("item/" + fileName + ".js", c);
|
||||
}
|
||||
}
|
||||
if (iv == null) {
|
||||
iv = getScriptEngine("npc/" + npc + ".js", c);
|
||||
if (engine == null) {
|
||||
engine = getInvocableScriptEngine("npc/" + npc + ".js", c);
|
||||
cm.resetItemScript();
|
||||
}
|
||||
if (iv == null) {
|
||||
if (engine == null) {
|
||||
dispose(c);
|
||||
return false;
|
||||
}
|
||||
iv.put(engineName, cm);
|
||||
engine.put(engineName, cm);
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
scripts.put(c, iv);
|
||||
c.setClickedNPC();
|
||||
try {
|
||||
((Invocable) iv).invokeFunction("start");
|
||||
iv.invokeFunction("start");
|
||||
} catch (final NoSuchMethodException nsme) {
|
||||
try {
|
||||
((Invocable) iv).invokeFunction("start", chr);
|
||||
iv.invokeFunction("start", chr);
|
||||
} catch (final NoSuchMethodException nsma) {
|
||||
nsma.printStackTrace();
|
||||
}
|
||||
@@ -155,25 +154,20 @@ public class NPCScriptManager extends AbstractScriptManager {
|
||||
c.announce(MaplePacketCreator.enableActions());
|
||||
}
|
||||
return true;
|
||||
} catch (final UndeclaredThrowableException | ScriptException ute) {
|
||||
} catch (final Exception ute) {
|
||||
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", ute);
|
||||
dispose(c);
|
||||
|
||||
return false;
|
||||
} catch (final Exception e) {
|
||||
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", e);
|
||||
dispose(c);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void action(MapleClient c, byte mode, byte type, int selection) {
|
||||
ScriptEngine iv = scripts.get(c);
|
||||
Invocable iv = scripts.get(c);
|
||||
if (iv != null) {
|
||||
try {
|
||||
c.setClickedNPC();
|
||||
((Invocable) iv).invokeFunction("action", mode, type, selection);
|
||||
iv.invokeFunction("action", mode, type, selection);
|
||||
} catch (ScriptException | NoSuchMethodException t) {
|
||||
if (getCM(c) != null) {
|
||||
FilePrinter.printError(FilePrinter.NPC + getCM(c).getNpc() + ".txt", t);
|
||||
|
||||
@@ -27,37 +27,26 @@ import server.maps.MaplePortal;
|
||||
import tools.FilePrinter;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PortalScriptManager extends AbstractScriptManager {
|
||||
|
||||
private static final PortalScriptManager instance = new PortalScriptManager();
|
||||
|
||||
|
||||
private final Map<String, Invocable> scripts = new HashMap<>();
|
||||
|
||||
public static PortalScriptManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Map<String, ScriptEngine> scripts = new HashMap<>();
|
||||
private final ScriptEngineFactory sef;
|
||||
|
||||
private PortalScriptManager() {
|
||||
ScriptEngineManager sem = new ScriptEngineManager();
|
||||
sef = sem.getEngineByName("graal.js").getFactory();
|
||||
}
|
||||
|
||||
private ScriptEngine getPortalScript(String scriptName) {
|
||||
private Invocable getPortalScript(String scriptName) {
|
||||
String scriptPath = "portal/" + scriptName + ".js";
|
||||
ScriptEngine iv = scripts.get(scriptPath);
|
||||
Invocable iv = scripts.get(scriptPath);
|
||||
if (iv != null) {
|
||||
return iv;
|
||||
}
|
||||
|
||||
iv = getScriptEngine(scriptPath);
|
||||
iv = (Invocable) getInvocableScriptEngine(scriptPath);
|
||||
if (iv == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -68,14 +57,12 @@ public class PortalScriptManager extends AbstractScriptManager {
|
||||
|
||||
public boolean executePortalScript(MaplePortal portal, MapleClient c) {
|
||||
try {
|
||||
ScriptEngine iv = getPortalScript(portal.getScriptName());
|
||||
Invocable iv = getPortalScript(portal.getScriptName());
|
||||
if (iv != null) {
|
||||
boolean couldWarp = (boolean) ((Invocable) iv).invokeFunction("enter", new PortalPlayerInteraction(c, portal));
|
||||
boolean couldWarp = (boolean) iv.invokeFunction("enter", new PortalPlayerInteraction(c, portal));
|
||||
return couldWarp;
|
||||
}
|
||||
} catch (UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.PORTAL + portal.getScriptName() + ".txt", ute);
|
||||
} catch (final Exception e) {
|
||||
} catch (Exception e) {
|
||||
FilePrinter.printError(FilePrinter.PORTAL + portal.getScriptName() + ".txt", e);
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -39,22 +39,22 @@ import java.util.Map;
|
||||
* @author RMZero213
|
||||
*/
|
||||
public class QuestScriptManager extends AbstractScriptManager {
|
||||
private static QuestScriptManager instance = new QuestScriptManager();
|
||||
|
||||
public static QuestScriptManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
private static final QuestScriptManager instance = new QuestScriptManager();
|
||||
|
||||
private Map<MapleClient, QuestActionManager> qms = new HashMap<>();
|
||||
private Map<MapleClient, ScriptEngine> scripts = new HashMap<>();
|
||||
private final Map<MapleClient, QuestActionManager> qms = new HashMap<>();
|
||||
private final Map<MapleClient, Invocable> scripts = new HashMap<>();
|
||||
|
||||
public static QuestScriptManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private ScriptEngine getQuestScriptEngine(MapleClient c, short questid) {
|
||||
ScriptEngine iv = getScriptEngine("quest/" + questid + ".js", c);
|
||||
if (iv == null && GameConstants.isMedalQuest(questid)) {
|
||||
iv = getScriptEngine("quest/medalQuest.js", c); // start generic medal quest
|
||||
ScriptEngine engine = getInvocableScriptEngine("quest/" + questid + ".js", c);
|
||||
if (engine == null && GameConstants.isMedalQuest(questid)) {
|
||||
engine = getInvocableScriptEngine("quest/medalQuest.js", c); // start generic medal quest
|
||||
}
|
||||
|
||||
return iv;
|
||||
return engine;
|
||||
}
|
||||
|
||||
public void start(MapleClient c, short questid, int npc) {
|
||||
@@ -72,17 +72,19 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
return;
|
||||
}
|
||||
|
||||
ScriptEngine iv = getQuestScriptEngine(c, questid);
|
||||
if (iv == null) {
|
||||
ScriptEngine engine = getQuestScriptEngine(c, questid);
|
||||
if (engine == null) {
|
||||
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "START Quest " + questid + " is uncoded.");
|
||||
qm.dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
iv.put("qm", qm);
|
||||
engine.put("qm", qm);
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
scripts.put(c, iv);
|
||||
c.setClickedNPC();
|
||||
((Invocable) iv).invokeFunction("start", (byte) 1, (byte) 0, 0);
|
||||
iv.invokeFunction("start", (byte) 1, (byte) 0, 0);
|
||||
}
|
||||
} catch (final UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
|
||||
@@ -94,19 +96,16 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
}
|
||||
|
||||
public void start(MapleClient c, byte mode, byte type, int selection) {
|
||||
ScriptEngine iv = scripts.get(c);
|
||||
Invocable iv = scripts.get(c);
|
||||
if (iv != null) {
|
||||
try {
|
||||
c.setClickedNPC();
|
||||
((Invocable) iv).invokeFunction("start", mode, type, selection);
|
||||
} catch (final UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", ute);
|
||||
dispose(c);
|
||||
} catch (final Throwable t) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
|
||||
iv.invokeFunction("start", mode, type, selection);
|
||||
} catch (final Exception e) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", e);
|
||||
dispose(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void end(MapleClient c, short questid, int npc) {
|
||||
@@ -128,17 +127,19 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
return;
|
||||
}
|
||||
|
||||
ScriptEngine iv = getQuestScriptEngine(c, questid);
|
||||
if (iv == null) {
|
||||
ScriptEngine engine = getQuestScriptEngine(c, questid);
|
||||
if (engine == null) {
|
||||
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "END Quest " + questid + " is uncoded.");
|
||||
qm.dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
iv.put("qm", qm);
|
||||
engine.put("qm", qm);
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
scripts.put(c, iv);
|
||||
c.setClickedNPC();
|
||||
((Invocable) iv).invokeFunction("end", (byte) 1, (byte) 0, 0);
|
||||
iv.invokeFunction("end", (byte) 1, (byte) 0, 0);
|
||||
}
|
||||
} catch (final UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
|
||||
@@ -150,19 +151,16 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
}
|
||||
|
||||
public void end(MapleClient c, byte mode, byte type, int selection) {
|
||||
ScriptEngine iv = scripts.get(c);
|
||||
Invocable iv = scripts.get(c);
|
||||
if (iv != null) {
|
||||
try {
|
||||
c.setClickedNPC();
|
||||
((Invocable) iv).invokeFunction("end", mode, type, selection);
|
||||
} catch (final UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", ute);
|
||||
dispose(c);
|
||||
} catch (final Throwable t) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
|
||||
iv.invokeFunction("end", mode, type, selection);
|
||||
} catch (final Exception e) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", e);
|
||||
dispose(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void raiseOpen(MapleClient c, short questid, int npc) {
|
||||
@@ -174,17 +172,19 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
if (c.canClickNPC()) {
|
||||
qms.put(c, qm);
|
||||
|
||||
ScriptEngine iv = getQuestScriptEngine(c, questid);
|
||||
if (iv == null) {
|
||||
ScriptEngine engine = getQuestScriptEngine(c, questid);
|
||||
if (engine == null) {
|
||||
//FilePrinter.printError(FilePrinter.QUEST_UNCODED, "RAISE Quest " + questid + " is uncoded.");
|
||||
qm.dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
iv.put("qm", qm);
|
||||
engine.put("qm", qm);
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
scripts.put(c, iv);
|
||||
c.setClickedNPC();
|
||||
((Invocable) iv).invokeFunction("raiseOpen");
|
||||
iv.invokeFunction("raiseOpen");
|
||||
}
|
||||
} catch (final UndeclaredThrowableException ute) {
|
||||
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
|
||||
@@ -195,13 +195,13 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void dispose(QuestActionManager qm, MapleClient c) {
|
||||
qms.remove(c);
|
||||
scripts.remove(c);
|
||||
c.getPlayer().setNpcCooldown(System.currentTimeMillis());
|
||||
resetContext("quest/" + qm.getQuest() + ".js", c);
|
||||
c.getPlayer().flushDelayedUpdateQuests();
|
||||
}
|
||||
public void dispose(QuestActionManager qm, MapleClient c) {
|
||||
qms.remove(c);
|
||||
scripts.remove(c);
|
||||
c.getPlayer().setNpcCooldown(System.currentTimeMillis());
|
||||
resetContext("quest/" + qm.getQuest() + ".js", c);
|
||||
c.getPlayer().flushDelayedUpdateQuests();
|
||||
}
|
||||
|
||||
public void dispose(MapleClient c) {
|
||||
QuestActionManager qm = qms.get(c);
|
||||
@@ -210,12 +210,12 @@ public class QuestScriptManager extends AbstractScriptManager {
|
||||
}
|
||||
}
|
||||
|
||||
public QuestActionManager getQM(MapleClient c) {
|
||||
return qms.get(c);
|
||||
}
|
||||
|
||||
public void reloadQuestScripts() {
|
||||
scripts.clear();
|
||||
qms.clear();
|
||||
}
|
||||
public QuestActionManager getQM(MapleClient c) {
|
||||
return qms.get(c);
|
||||
}
|
||||
|
||||
public void reloadQuestScripts() {
|
||||
scripts.clear();
|
||||
qms.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,6 @@ import server.partyquest.MapleCarnivalFactory.MCSkill;
|
||||
import tools.MaplePacketCreator;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
@@ -59,11 +58,11 @@ import java.util.logging.Logger;
|
||||
* @author Ronan
|
||||
*/
|
||||
public class ReactorActionManager extends AbstractPlayerInteraction {
|
||||
private MapleReactor reactor;
|
||||
private ScriptEngine iv;
|
||||
private final MapleReactor reactor;
|
||||
private final Invocable iv;
|
||||
private ScheduledFuture<?> sprayTask = null;
|
||||
|
||||
public ReactorActionManager(MapleClient c, MapleReactor reactor, ScriptEngine iv) {
|
||||
public ReactorActionManager(MapleClient c, MapleReactor reactor, Invocable iv) {
|
||||
super(c);
|
||||
this.reactor = reactor;
|
||||
this.iv = iv;
|
||||
@@ -320,7 +319,7 @@ public class ReactorActionManager extends AbstractPlayerInteraction {
|
||||
public ScheduledFuture<?> schedule(final String methodName, final EventInstanceManager eim, long delay) {
|
||||
return TimerManager.getInstance().schedule(() -> {
|
||||
try {
|
||||
((Invocable) iv).invokeFunction(methodName, eim);
|
||||
iv.invokeFunction(methodName, eim);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
@@ -330,7 +329,7 @@ public class ReactorActionManager extends AbstractPlayerInteraction {
|
||||
public ScheduledFuture<?> scheduleAtTimestamp(final String methodName, long timestamp) {
|
||||
return TimerManager.getInstance().scheduleAtTimestamp(() -> {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -43,23 +43,25 @@ import java.util.Map;
|
||||
* @author Lerk
|
||||
*/
|
||||
public class ReactorScriptManager extends AbstractScriptManager {
|
||||
|
||||
private static ReactorScriptManager instance = new ReactorScriptManager();
|
||||
private static final ReactorScriptManager instance = new ReactorScriptManager();
|
||||
|
||||
private final Map<Integer, List<ReactorDropEntry>> drops = new HashMap<>();
|
||||
|
||||
public static ReactorScriptManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Map<Integer, List<ReactorDropEntry>> drops = new HashMap<>();
|
||||
|
||||
public void onHit(MapleClient c, MapleReactor reactor) {
|
||||
try {
|
||||
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
|
||||
if (iv == null) return;
|
||||
|
||||
ScriptEngine engine = getInvocableScriptEngine("reactor/" + reactor.getId() + ".js", c);
|
||||
if (engine == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
|
||||
iv.put("rm", rm);
|
||||
((Invocable) iv).invokeFunction("hit");
|
||||
engine.put("rm", rm);
|
||||
iv.invokeFunction("hit");
|
||||
} catch (final NoSuchMethodException e) {} //do nothing, hit is OPTIONAL
|
||||
|
||||
catch (final ScriptException | NullPointerException e) {
|
||||
@@ -69,12 +71,15 @@ public class ReactorScriptManager extends AbstractScriptManager {
|
||||
|
||||
public void act(MapleClient c, MapleReactor reactor) {
|
||||
try {
|
||||
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
|
||||
if (iv == null) return;
|
||||
|
||||
ScriptEngine engine = getInvocableScriptEngine("reactor/" + reactor.getId() + ".js", c);
|
||||
if (engine == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
|
||||
iv.put("rm", rm);
|
||||
((Invocable) iv).invokeFunction("act");
|
||||
engine.put("rm", rm);
|
||||
iv.invokeFunction("act");
|
||||
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
|
||||
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
|
||||
}
|
||||
@@ -115,15 +120,18 @@ public class ReactorScriptManager extends AbstractScriptManager {
|
||||
|
||||
private void touching(MapleClient c, MapleReactor reactor, boolean touching) {
|
||||
try {
|
||||
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
|
||||
if (iv == null) return;
|
||||
|
||||
ScriptEngine engine = getInvocableScriptEngine("reactor/" + reactor.getId() + ".js", c);
|
||||
if (engine == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Invocable iv = (Invocable) engine;
|
||||
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
|
||||
iv.put("rm", rm);
|
||||
engine.put("rm", rm);
|
||||
if (touching) {
|
||||
((Invocable) iv).invokeFunction("touch");
|
||||
iv.invokeFunction("touch");
|
||||
} else {
|
||||
((Invocable) iv).invokeFunction("untouch");
|
||||
iv.invokeFunction("untouch");
|
||||
}
|
||||
} catch (final ScriptException | NoSuchMethodException | NullPointerException ute) {
|
||||
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", ute);
|
||||
|
||||
Reference in New Issue
Block a user