Search in sources :

Example 51 with Invocable

use of javax.script.Invocable in project HeavenMS by ronancpl.

the class QuestScriptManager method end.

public void end(MapleClient c, byte mode, byte type, int selection) {
    Invocable iv = scripts.get(c);
    if (iv != null) {
        try {
            c.setClickedNPC();
            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);
            dispose(c);
        }
    }
}
Also used : Invocable(javax.script.Invocable) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Example 52 with Invocable

use of javax.script.Invocable in project HeavenMS by ronancpl.

the class QuestScriptManager method start.

public void start(MapleClient c, byte mode, byte type, int selection) {
    Invocable iv = scripts.get(c);
    if (iv != null) {
        try {
            c.setClickedNPC();
            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);
            dispose(c);
        }
    }
}
Also used : Invocable(javax.script.Invocable) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Example 53 with Invocable

use of javax.script.Invocable in project HeavenMS by ronancpl.

the class ReactorScriptManager method touching.

public synchronized void touching(MapleClient c, MapleReactor reactor, boolean touching) {
    try {
        Invocable iv = getInvocable("reactor/" + reactor.getId() + ".js", c);
        if (iv == null)
            return;
        ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
        engine.put("rm", rm);
        if (touching) {
            iv.invokeFunction("touch");
        } else {
            iv.invokeFunction("untouch");
        }
    } catch (final ScriptException | NoSuchMethodException | NullPointerException ute) {
        FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", ute);
    }
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException)

Example 54 with Invocable

use of javax.script.Invocable in project HeavenMS by ronancpl.

the class ReactorScriptManager method act.

public void act(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("act");
    } catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
        FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
    }
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException)

Example 55 with Invocable

use of javax.script.Invocable in project HeavenMS by ronancpl.

the class ItemScriptManager method getItemScript.

public void getItemScript(MapleClient c, String scriptName) {
    if (scripts.containsKey(scriptName)) {
        try {
            scripts.get(scriptName).invokeFunction("start", new ItemScriptMethods(c));
        } catch (ScriptException | NoSuchMethodException ex) {
            FilePrinter.printError(FilePrinter.ITEM + scriptName + ".txt", ex);
        }
        return;
    }
    File scriptFile = new File("scripts/item/" + scriptName + ".js");
    if (!scriptFile.exists()) {
        c.announce(MaplePacketCreator.enableActions());
        return;
    }
    FileReader fr = null;
    ScriptEngine portal = sef.getScriptEngine();
    try {
        fr = new FileReader(scriptFile);
        CompiledScript compiled = ((Compilable) portal).compile(fr);
        compiled.eval();
        final Invocable script = ((Invocable) portal);
        scripts.put(scriptName, script);
        script.invokeFunction("start", new ItemScriptMethods(c));
    } catch (final UndeclaredThrowableException | ScriptException ute) {
        FilePrinter.printError(FilePrinter.ITEM + scriptName + ".txt", ute);
    } catch (final Exception e) {
        FilePrinter.printError(FilePrinter.ITEM + scriptName + ".txt", e);
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : CompiledScript(javax.script.CompiledScript) Compilable(javax.script.Compilable) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

Invocable (javax.script.Invocable)74 ScriptException (javax.script.ScriptException)45 ScriptEngine (javax.script.ScriptEngine)34 ScriptEngineManager (javax.script.ScriptEngineManager)24 IOException (java.io.IOException)19 File (java.io.File)10 InputStreamReader (java.io.InputStreamReader)10 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)10 FileReader (java.io.FileReader)9 Reader (java.io.Reader)9 Compilable (javax.script.Compilable)8 CompiledScript (javax.script.CompiledScript)8 Bindings (javax.script.Bindings)7 Map (java.util.Map)5 BufferedReader (java.io.BufferedReader)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Writer (java.io.Writer)3 HashMap (java.util.HashMap)3 ScriptContext (javax.script.ScriptContext)3