= Updated plugin subsystem formatting

This commit is contained in:
Matthew Cech
2019-06-19 20:05:23 -07:00
parent 51e40610a2
commit c7ad59b755
5 changed files with 24 additions and 21 deletions
+4 -4
View File
@@ -44,17 +44,17 @@ public class Plugin
} }
catch (IOException e) catch (IOException e)
{ {
PluginLog.Error(e.getMessage()); PluginLog.error(e.getMessage());
} }
} }
public List<String> Run(String args, PluginUser user) public List<String> run(String args, PluginUser user)
{ {
try try
{ {
List<String> outputs = new Vector<String>(); List<String> outputs = new Vector<String>();
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())
{ {
@@ -87,7 +87,7 @@ public class Plugin
} }
catch(Exception e) catch(Exception e)
{ {
PluginLog.Error("Failed to run plugin from file " + filepath); PluginLog.error("Failed to run plugin from file " + filepath);
} }
return null; return null;
+3 -3
View File
@@ -5,7 +5,7 @@ import utils.LogFilter;
public class PluginLog public class PluginLog
{ {
public static void Log(String s) { GlobalLog.Log(LogFilter.Plugin, s); } public static void log(String s) { GlobalLog.Log(LogFilter.Plugin, s); }
public static void Warn(String s) { GlobalLog.Warn(LogFilter.Plugin, s); } public static void warn(String s) { GlobalLog.Warn(LogFilter.Plugin, s); }
public static void Error(String s) { GlobalLog.Error(LogFilter.Plugin, s); } public static void error(String s) { GlobalLog.Error(LogFilter.Plugin, s); }
} }
+15 -12
View File
@@ -12,14 +12,11 @@ import dataStructures.KittyUser;
// Reads in, handles, and manipulates plugins. Plugins are loaded in the order they appear in the folder. // Reads in, handles, and manipulates plugins. Plugins are loaded in the order they appear in the folder.
public class PluginManager public class PluginManager
{ {
// Variables
public final String pluginFolder; public final String pluginFolder;
public ArrayList<Plugin> plugins; public ArrayList<Plugin> plugins;
public void AddPlugin(Path path) // Constructor
{
plugins.add(new Plugin(path));
}
public PluginManager(String folder) public PluginManager(String folder)
{ {
this.pluginFolder = folder; this.pluginFolder = folder;
@@ -29,30 +26,35 @@ public class PluginManager
{ {
try (Stream<Path> paths = Files.walk(Paths.get(this.pluginFolder))) try (Stream<Path> paths = Files.walk(Paths.get(this.pluginFolder)))
{ {
paths.filter(Files::isRegularFile).forEach((path)->{ AddPlugin(path); }); paths.filter(Files::isRegularFile).forEach((path)->{ addPlugin(path); });
} }
} }
catch(Exception e) catch(Exception e)
{ {
PluginLog.Error(e.getMessage()); PluginLog.error(e.getMessage());
} }
} }
public void addPlugin(Path path)
{
plugins.add(new Plugin(path));
}
// Runs all plugins, returning when it gets a non-nill result. If there // 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 // are mutliple strings returned in the list, it is because the plugin
// that was run returned multiple strings. Since plugins don't stack, it // that was run returned multiple strings. Since plugins don't stack, it
// will never indicate that multiple // will never indicate that multiple
// Otherwise, returns null. // Otherwise, returns null.
public List<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);
List<String> out = plugin.Run(input, new PluginUser(user)); List<String> out = plugin.run(input, new PluginUser(user));
if(out != null) if(out != null)
{ {
PluginLog.Log("Executed plugin at " + plugin.filepath); PluginLog.log("Executed plugin at " + plugin.filepath);
return out; return out;
} }
} }
@@ -60,9 +62,10 @@ public class PluginManager
return null; return null;
} }
public void PrintAll() // Dumps the contents of all plugins for debug
public void printAll()
{ {
for(int i = 0; i < plugins.size(); ++i) for(int i = 0; i < plugins.size(); ++i)
PluginLog.Log(plugins.get(i).contents.toString()); PluginLog.log(plugins.get(i).contents.toString());
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ import org.luaj.vm2.lib.jse.CoerceJavaToLua;
public class PluginStructure public class PluginStructure
{ {
public LuaValue AsLua() public LuaValue asLua()
{ {
return CoerceJavaToLua.coerce(this); return CoerceJavaToLua.coerce(this);
} }
+1 -1
View File
@@ -94,7 +94,7 @@ 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)
{ {
List<String> pluginOutput = pluginManager.RunAll(input.message, user); List<String> pluginOutput = pluginManager.runAll(input.message, user);
if(pluginOutput != null && pluginOutput.size() > 0) if(pluginOutput != null && pluginOutput.size() > 0)
{ {