use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class JavaPluginBridge method createInterrupterForInv.
@Override
public ProcessInterrupter createInterrupterForInv(Object e, Interpreter interpreter, Map<UUID, Long> cooldowns, Map<IInventory, InventoryTrigger> inventoryMap) {
return new ProcessInterrupter() {
@Override
public boolean onNodeProcess(Node node) {
if (interpreter.isCooldown()) {
if (e instanceof InventoryInteractEvent) {
HumanEntity he = ((InventoryInteractEvent) e).getWhoClicked();
if (he instanceof Player) {
Player player = (Player) he;
UUID uuid = player.getUniqueId();
cooldowns.put(uuid, interpreter.getCooldownEnd());
}
}
return false;
}
// is still running after the inventory is closed.
if (e instanceof InventoryOpenEvent || e instanceof InventoryClickEvent) {
Inventory inv = ((InventoryEvent) e).getInventory();
// it's not GUI so stop execution
if (!inventoryMap.containsKey(new BukkitInventory(inv)))
return true;
}
return false;
}
@Override
public boolean onCommand(Object context, String command, Object[] args) {
if ("CALL".equals(command)) {
if (args.length < 1)
throw new RuntimeException("Need parameter [String] or [String, boolean]");
if (args[0] instanceof String) {
Trigger trigger = getNamedTriggerManager().getTriggerForName((String) args[0]);
if (trigger == null)
throw new RuntimeException("No trigger found for Named Trigger " + args[0]);
if (args.length > 1 && args[1] instanceof Boolean) {
trigger.setSync((boolean) args[1]);
} else {
trigger.setSync(true);
}
if (trigger.isSync()) {
trigger.activate(e, interpreter.getVars());
} else {
// use snapshot to avoid concurrent modification
trigger.activate(e, new HashMap<>(interpreter.getVars()));
}
return true;
} else {
throw new RuntimeException("Parameter type not match; it should be a String." + " Make sure to put double quotes, if you provided String literal.");
}
} else if ("CANCELEVENT".equals(command)) {
if (!interpreter.isSync())
throw new RuntimeException("CANCELEVENT is illegal in async mode!");
if (context instanceof Cancellable) {
((Cancellable) context).setCancelled(true);
return true;
} else {
throw new RuntimeException(context + " is not a Cancellable event!");
}
}
return false;
}
};
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class ParserTest method testFor.
@Test
public void testFor() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "" + "FOR i = 0:10;" + " #MESSAGE \"test i=\"+i;" + "ENDFOR;";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Queue<Node> queue = new LinkedList<Node>();
serializeNode(queue, root);
assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "i")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "0")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "10")), queue.poll());
assertEquals(new Node(new Token(Type.ITERATOR, "<ITERATOR>")), queue.poll());
assertEquals(new Node(new Token(Type.STRING, "test i=")), queue.poll());
assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "i")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "+")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "FOR")), queue.poll());
assertEquals(new Node(new Token(Type.ROOT, "<ROOT>")), queue.poll());
assertEquals(0, queue.size());
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node 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);
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node 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);
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node 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);
}
Aggregations