use of org.squiddev.cobalt.function.VarArgFunction in project allium by hugeblank.
the class CommandLib method create.
public static LuaLibrary create(Plugin plugin) {
LuaTable mt = new LuaTable();
mt.rawset("__index", new VarArgFunction() {
@Override
public Varargs invoke(LuaState state, Varargs args) throws LuaError {
if (isServerNull(plugin))
return Constants.NIL;
String command = args.arg(2).checkString();
CommandManager manager = Allium.SERVER.getCommandManager();
CommandDispatcher<ServerCommandSource> dispatcher = manager.getDispatcher();
for (CommandNode<?> child : dispatcher.getRoot().getChildren()) {
if (child.getName().equals(command)) {
return new ExecuteCommandFunction(plugin, command);
}
}
return Constants.NIL;
}
});
return LibBuilder.create("commands").set("exec", // commands.exec(String... command)
new ExecuteCommandFunction(plugin)::invoke).addMetatable(// commands.command(String... arguments)
mt).build();
}
use of org.squiddev.cobalt.function.VarArgFunction in project allium by hugeblank.
the class JavaLib method importClass.
private static Varargs importClass(LuaState state, Varargs args) throws LuaError {
Class<?> clazz = getClassOf(args.arg(1).checkString());
List<Method> staticMethods = new ArrayList<>();
List<Field> staticFields = new ArrayList<>();
for (Method declaredMethod : clazz.getDeclaredMethods()) {
int mods = declaredMethod.getModifiers();
if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && !Modifier.isAbstract(mods)) {
staticMethods.add(declaredMethod);
}
}
for (Field declaredField : clazz.getDeclaredFields()) {
int mods = declaredField.getModifiers();
if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && !Modifier.isAbstract(mods)) {
staticFields.add(declaredField);
}
}
LuaTable mt = new LuaTable();
mt.rawset("__call", new VarArgFunction() {
@Override
public Varargs invoke(LuaState state, Varargs args) throws LuaError {
return JavaLib.createInstance(state, ValueFactory.varargsOf(LuaString.valueOf(clazz.getName()), args.subargs(2)));
}
});
LuaTable impord = new StaticMethods(clazz, staticMethods, staticFields).create();
impord.setMetatable(mt);
return ValueFactory.varargsOf(impord);
}
Aggregations