CompiledScript script = ((Compilable) scriptEngine).compile(reader); Bindings sb = new SimpleBindings(); script.eval(sb); // Put the Lua functions into the sb environment LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function LuaValue b = onTalk.call(luaDog); // Call the function System.out.println("onTalk answered: " + b); LuaFunction onWalk = (LuaFunction) sb.get("onWalk"); LuaValue[] dogs = { luaDog }; Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative System.out.println("onWalk returned: " + dist);
public LuaMap merge(LuaMap map2, LuaFunction func) { HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(map.size() + map2.map.size()); target.putAll(map); boolean hasFunc = !(func == null || func.isnil()); for (Entry<LuaValue,LuaValue> entry : map2.map.entrySet()) { if (hasFunc) { LuaValue value = map.get(entry.getKey()); if (value != null) { Varargs ret = func.invoke(value, entry.getValue()); target.put(entry.getKey(), (LuaValue)ret); continue; } } target.put(entry.getKey(), entry.getValue()); } return new LuaMap(instance, target); }
public LuaMap merge(LuaMap map2, LuaFunction func) { HashMap<LuaValue,LuaValue> target = new HashMap<LuaValue,LuaValue>(map.size() + map2.map.size()); target.putAll(map); boolean hasFunc = !(func == null || func.isnil()); for (Entry<LuaValue,LuaValue> entry : map2.map.entrySet()) { if (hasFunc) { LuaValue value = map.get(entry.getKey()); if (value != null) { Varargs ret = func.invoke(value, entry.getValue()); target.put(entry.getKey(), (LuaValue)ret); continue; } } target.put(entry.getKey(), entry.getValue()); } return new LuaMap(instance, target); }
@Override public boolean execute(CommandSender sender, String command, String[] args) { if (!this.testPermissionSilent(sender)) { sender.sendMessage(ERROR_NO_PERMISSION); return true; } try { if (args.length > maxArgs && maxArgs >= 0) { sender.sendMessage(ERROR_TOO_MANY_ARGS); } else if (args.length < minArgs) { sender.sendMessage(ERROR_MISSING_ARGS); } else { if (runAsync) { BukkitScheduler scheduler = Bukkit.getScheduler(); scheduler.runTaskAsynchronously(plugin, () -> function.invoke(new LuaValue[]{new CommandEvent(sender, command, args)})); } else { function.invoke(new LuaValue[]{new CommandEvent(sender, command, args)}); } } } catch (LukkitPluginException e) { e.printStackTrace(); LuaEnvironment.addError(e); } return true; }
@Override public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { List<String> def = super.tabComplete(sender, alias, args); if (tabComleteFunction != null) { LuaValue val = tabComleteFunction.invoke( CoerceJavaToLua.coerce(sender), CoerceJavaToLua.coerce(alias), CoerceJavaToLua.coerce(args) ).arg1(); if (val != LuaValue.NIL) { LuaTable tbl = val.checktable(); Object o = Utilities.convertTable(tbl); if (o instanceof List) return (List<String>) o; } } return def; }