Search in sources :

Example 1 with VarArgFunction

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();
}
Also used : CommandManager(net.minecraft.server.command.CommandManager) CommandNode(com.mojang.brigadier.tree.CommandNode) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) VarArgFunction(org.squiddev.cobalt.function.VarArgFunction)

Example 2 with VarArgFunction

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);
}
Also used : ArrayList(java.util.ArrayList) VarArgFunction(org.squiddev.cobalt.function.VarArgFunction)

Aggregations

VarArgFunction (org.squiddev.cobalt.function.VarArgFunction)2 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)1 CommandNode (com.mojang.brigadier.tree.CommandNode)1 ArrayList (java.util.ArrayList)1 CommandManager (net.minecraft.server.command.CommandManager)1