= Changed lua plugins to run with arrays of output.

This commit is contained in:
Matthew Cech
2019-05-20 15:21:23 -07:00
parent fd847b49b6
commit 1df71ef61d
3 changed files with 40 additions and 8 deletions
+29 -2
View File
@@ -4,10 +4,13 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Vector;
import java.util.stream.Stream;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.jse.JsePlatform;
// To promote flexibility, plugins are lua file with predefined callbacks.
@@ -45,14 +48,38 @@ public class Plugin
}
}
public String Run(String args, PluginUser user)
public List<String> Run(String args, PluginUser user)
{
List<String> outputs = new Vector<String>();
try
{
LuaValue res = func_plugin.call(LuaValue.valueOf(args), user.AsLua());
if(!res.isnil())
return res.toString();
{
if(res.istable())
{
while(true)
{
LuaValue key = LuaValue.NIL;
while (true)
{
Varargs n = res.next(key);
key = n.arg1();
if (key.isnil())
break;
LuaValue value = n.arg(2);
outputs.add(value.toString());
}
}
}
else
{
outputs.add(res.toString());
}
}
}
catch(Exception e)
{
+7 -3
View File
@@ -4,6 +4,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import dataStructures.KittyUser;
@@ -37,14 +38,17 @@ public class PluginManager
}
}
// Runs all plugins, returning when it gets a non-nill result.
// Runs all plugins, returning when it gets a non-nill result. If there
// are mutliple strings returned in the list, it is because the plugin
// that was run returned multiple strings. Since plugins don't stack, it
// will never indicate that multiple
// Otherwise, returns null.
public String RunAll(String input, KittyUser user)
public List<String> RunAll(String input, KittyUser user)
{
for(int i = 0; i < plugins.size(); ++i)
{
Plugin plugin = plugins.get(i);
String out = plugin.Run(input, new PluginUser(user));
List<String> out = plugin.Run(input, new PluginUser(user));
if(out != null)
{
+4 -3
View File
@@ -18,6 +18,7 @@ import net.dv8tion.jda.core.hooks.ListenerAdapter;
import offline.*;
import utils.GlobalLog;
import net.dv8tion.jda.core.*;
import java.util.*;
// http://www.slf4j.org/ - this JDA logging tool has been disabled by specifying NOP implementation.
// This is the application entry point, and bot startup location!
@@ -86,10 +87,10 @@ public class Main extends ListenerAdapter
// Run plugins right before invoking the commands but after all other setup.
if(commandManager.InvokeOnNewThread(guild, channel, user, input, response) == false)
{
String pluginOutput = pluginManager.RunAll(input.message, user);
List<String> pluginOutput = pluginManager.RunAll(input.message, user);
if(pluginOutput != null)
response.Call(pluginOutput);
if(pluginOutput != null && pluginOutput.size() > 0)
response.Call(pluginOutput.get(0));
}
// Run any upkeep in post we need to