use of javax.script.Invocable in project HeavenMS by ronancpl.
the class PortalScriptManager method getPortalScript.
private PortalScript getPortalScript(String scriptName) {
if (scripts.containsKey(scriptName)) {
return scripts.get(scriptName);
}
File scriptFile = new File("scripts/portal/" + scriptName + ".js");
if (!scriptFile.exists()) {
scripts.put(scriptName, null);
return null;
}
FileReader fr = null;
ScriptEngine portal = sef.getScriptEngine();
try {
fr = new FileReader(scriptFile);
((Compilable) portal).compile(fr).eval();
} catch (ScriptException | IOException | UndeclaredThrowableException e) {
FilePrinter.printError(FilePrinter.PORTAL + scriptName + ".txt", e);
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PortalScript script = ((Invocable) portal).getInterface(PortalScript.class);
scripts.put(scriptName, script);
return script;
}
use of javax.script.Invocable in project HeavenMS by ronancpl.
the class QuestScriptManager method end.
public void end(MapleClient c, short questid, int npc) {
MapleQuest quest = MapleQuest.getInstance(questid);
if (!c.getPlayer().getQuest(quest).getStatus().equals(MapleQuestStatus.Status.STARTED) || !c.getPlayer().getMap().containsNPC(npc)) {
dispose(c);
return;
}
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, false);
if (qms.containsKey(c)) {
return;
}
if (c.canClickNPC()) {
qms.put(c, qm);
Invocable iv = getInvocable("quest/" + questid + ".js", c);
if (iv == null) {
qm.dispose();
return;
}
engine.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
iv.invokeFunction("end", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}
use of javax.script.Invocable in project HeavenMS by ronancpl.
the class QuestScriptManager method start.
public void start(MapleClient c, short questid, int npc) {
MapleQuest quest = MapleQuest.getInstance(questid);
if (!c.getPlayer().getQuest(quest).getStatus().equals(MapleQuestStatus.Status.NOT_STARTED)) {
dispose(c);
return;
}
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, true);
if (qms.containsKey(c)) {
return;
}
if (c.canClickNPC()) {
qms.put(c, qm);
Invocable iv = getInvocable("quest/" + questid + ".js", c);
if (iv == null) {
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "Quest " + questid + " is uncoded.\r\n");
}
if (iv == null || QuestScriptManager.getInstance() == null) {
qm.dispose();
return;
}
engine.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
iv.invokeFunction("start", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}
use of javax.script.Invocable in project HeavenMS by ronancpl.
the class ReactorScriptManager method onHit.
public void onHit(MapleClient c, MapleReactor reactor) {
try {
Invocable iv = getInvocable("reactor/" + reactor.getId() + ".js", c);
if (iv == null)
return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
engine.put("rm", rm);
iv.invokeFunction("hit");
} catch (final NoSuchMethodException e) {
// do nothing, hit is OPTIONAL
} catch (final ScriptException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
}
}
use of javax.script.Invocable in project java8-tutorial by winterbe.
the class Nashorn6 method main.
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("load('res/nashorn6.js')");
Invocable invocable = (Invocable) engine;
Product product = new Product();
product.setName("Rubber");
product.setPrice(1.99);
product.setStock(1337);
ScriptObjectMirror result = (ScriptObjectMirror) invocable.invokeFunction("calculate", product);
System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
}
Aggregations