Search in sources :

Example 1 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class ExecutorManager method reload.

@Override
public void reload() {
    FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory() || pathname.getName().endsWith(".js");
        }
    };
    jsExecutors.clear();
    for (File file : executorFolder.listFiles(filter)) {
        try {
            reloadExecutors(file, filter);
        } catch (ScriptException | IOException e) {
            e.printStackTrace();
            plugin.getLogger().warning("Could not load executor " + file.getName());
            continue;
        }
    }
    // manually add CMDOP after reload is done
    this.jsExecutors.put("CMDOP", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object e, Object... args) throws Exception {
            // /////////////////////////////
            Map<String, Object> variables = new HashMap<>();
            Map<String, Object> vars = ReflectionUtil.extractVariables(e);
            variables.putAll(vars);
            instance.extractCustomVariables(variables, e);
            // /////////////////////////////
            Object player = vars.get("player");
            if (player == null || !(player instanceof Player))
                return null;
            boolean wasOp = false;
            try {
                if (plugin.isServerThread()) {
                    wasOp = new IsOp((Player) player).call();
                } else {
                    wasOp = plugin.callSyncMethod(new IsOp((Player) player)).get();
                }
                if (!wasOp) {
                    if (plugin.isServerThread()) {
                        new SetOp((Player) player, true).call();
                    } else {
                        plugin.callSyncMethod(new SetOp((Player) player, true)).get();
                    }
                }
                if (args.length > 0) {
                    if (plugin.isServerThread()) {
                        new DispatchCommand((Player) player, String.valueOf(args[0])).call();
                    } else {
                        plugin.callSyncMethod(new DispatchCommand((Player) player, String.valueOf(args[0]))).get();
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (!wasOp) {
                    if (plugin.isServerThread()) {
                        new SetOp((Player) player, false).call();
                    } else {
                        plugin.callSyncMethod(new SetOp((Player) player, false)).get();
                    }
                }
            }
            return null;
        }
    });
}
Also used : Player(org.bukkit.entity.Player) IOException(java.io.IOException) IOException(java.io.IOException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) FileFilter(java.io.FileFilter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class TriggerTest method testSimpleIf.

@Test
public void testSimpleIf() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "x = 4.0;" + "IF x > 0.0;" + "    #TEST \"pass\";" + "ENDIF;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    executorMap.put("TEST", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
            Assert.assertEquals("pass", args[0]);
            return null;
        }
    });
    Map<String, Placeholder> placeholderMap = new HashMap<>();
    Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.startWithContext(null);
}
Also used : Placeholder(io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 3 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class TriggerTest method testShortCircuit.

@Test
public void testShortCircuit() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "IF player != null && player.health == 0.82;" + "    #TEST1 \"work\";" + "ENDIF;" + "IF player2 == null || player2.health == 0.82;" + "    #TEST2 \"work2\";" + "ENDIF;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<String, Executor>() {

        {
            put("TEST1", new Executor() {

                @Override
                protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
                    Assert.assertEquals("work", args[0]);
                    return null;
                }
            });
            put("TEST2", new Executor() {

                @Override
                protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
                    Assert.assertEquals("work2", args[0]);
                    return null;
                }
            });
        }
    };
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.getVars().put("player", new InTest());
    interpreter.getVars().put("player2", new InTest());
    interpreter.startWithContext(null);
    interpreter.getVars().remove("player");
    interpreter.getVars().remove("player2");
    interpreter.startWithContext(null);
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 4 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class TriggerTest method testIteration.

@Test
public void testIteration() throws Exception {
    CommonFunctions mockFunctions = mock(CommonFunctions.class);
    Player[] mockPlayer = new Player[2];
    mockPlayer[0] = mock(Player.class);
    mockPlayer[1] = mock(Player.class);
    when(mockPlayer[0].getName()).thenReturn("Player0");
    when(mockPlayer[1].getName()).thenReturn("Player1");
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "FOR player = getPlayers()\n" + "    #MESSAGE player.getName()\n" + "ENDFOR\n";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    executorMap.put("MESSAGE", new Executor() {

        int index = 0;

        @Override
        public Integer execute(boolean sync, Object context, Object... args) {
            Assert.assertEquals(mockPlayer[index].getName(), args[0]);
            index++;
            return null;
        }
    });
    Collection players = new ArrayList<Player>() {

        {
            add(mockPlayer[0]);
            add(mockPlayer[1]);
        }
    };
    when(mockFunctions.getPlayers()).thenReturn(players);
    Map<String, Object> map = new HashMap<String, Object>();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), mockFunctions);
    interpreter.startWithContext(null);
}
Also used : Player(org.bukkit.entity.Player) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) Collection(java.util.Collection) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 5 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class TriggerTest method testWhile.

@Test
public void testWhile() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "number = 1;" + "WHILE number < 3;" + "number = number + 1;" + "ENDWHILE;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.startWithContext(null);
    Assert.assertEquals(3, interpreter.getVars().get("number"));
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Aggregations

Executor (io.github.wysohn.triggerreactor.core.script.interpreter.Executor)22 HashMap (java.util.HashMap)22 CommonFunctions (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions)21 Interpreter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter)21 Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)21 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)21 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)21 Charset (java.nio.charset.Charset)21 Test (org.junit.Test)21 Placeholder (io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder)7 Player (org.bukkit.entity.Player)5 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ScriptException (javax.script.ScriptException)1 Location (org.bukkit.Location)1 ItemStack (org.bukkit.inventory.ItemStack)1