= Changed lua plugins to run with arrays of output.
This commit is contained in:
@@ -4,10 +4,13 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.luaj.vm2.Globals;
|
import org.luaj.vm2.Globals;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
|
import org.luaj.vm2.Varargs;
|
||||||
import org.luaj.vm2.lib.jse.JsePlatform;
|
import org.luaj.vm2.lib.jse.JsePlatform;
|
||||||
|
|
||||||
// To promote flexibility, plugins are lua file with predefined callbacks.
|
// 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
|
try
|
||||||
{
|
{
|
||||||
LuaValue res = func_plugin.call(LuaValue.valueOf(args), user.AsLua());
|
LuaValue res = func_plugin.call(LuaValue.valueOf(args), user.AsLua());
|
||||||
|
|
||||||
if(!res.isnil())
|
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)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import dataStructures.KittyUser;
|
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.
|
// 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)
|
for(int i = 0; i < plugins.size(); ++i)
|
||||||
{
|
{
|
||||||
Plugin plugin = plugins.get(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)
|
if(out != null)
|
||||||
{
|
{
|
||||||
|
|||||||
+4
-3
@@ -18,6 +18,7 @@ import net.dv8tion.jda.core.hooks.ListenerAdapter;
|
|||||||
import offline.*;
|
import offline.*;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import net.dv8tion.jda.core.*;
|
import net.dv8tion.jda.core.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
// http://www.slf4j.org/ - this JDA logging tool has been disabled by specifying NOP implementation.
|
// 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!
|
// 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.
|
// Run plugins right before invoking the commands but after all other setup.
|
||||||
if(commandManager.InvokeOnNewThread(guild, channel, user, input, response) == false)
|
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)
|
if(pluginOutput != null && pluginOutput.size() > 0)
|
||||||
response.Call(pluginOutput);
|
response.Call(pluginOutput.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run any upkeep in post we need to
|
// Run any upkeep in post we need to
|
||||||
|
|||||||
Reference in New Issue
Block a user