Authors: JanHolger, Digital
- Create JVMs.
- Communicate between Lua <-> Java.
Check out Releases for a download to Windows & Linux builds.
- Download OnsetJavaPlugin.dll (Windows) & OnsetJavaPlugin.so (Linux) from Releases (HERE) and place inside plugins folder.
- Ensure Java 8 JDK/JRE 64bit is installed.
- Enable "OnsetJavaPlugin" as a plugin inside server_config.json.
- Lua String -> String (java.lang.String)
- Lua Int -> Integer (java.lang.Integer)
- Lua Number -> Double (java.lang.Double)
- Lua Bool -> Boolean (java.lang.Boolean)
- Lua Table -> Map (java.util.HashMap)
- Lua Function -> LuaFunction (lua.LuaFunction, lua function support interface required)
- String (java.lang.String)
- Integer (java.lang.Integer)
- Double (java.lang.Double)
- Boolean (java.lang.Boolean)
- List (java.util.List)
- Map (java.util.Map)
We will be adding more data types later on.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.OnfireNetwork</groupId>
<artifactId>OnsetJavaPlugin</artifactId>
<version>2357546bd5</version>
</dependency>
</dependencies>Java:
public static void example(LuaFunction fn){
fn.call("Hello from Java!");
fn.close(); // Always call this when you don't need the function anymore to free memory
}Lua:
CallJavaStaticMethod(java, "example/Example", "example", "(Llua/LuaFunction;)V", function(msg)
print(msg)
end)Create a new JVM with a jar. Returns JVM ID.
local jvmID = CreateJava(path)- path Classpath for the jvm. This parameter is optional. When not provided it will include the "java" directory aswell as all jar files inside or "." when "java" doesn't exist.
Destroy a JVM.
DestroyJava(jvmID)- jvmID ID to the JVM you have created using CreateJava.
Call a java static method. Can return information from the Java method, check out Data Types for the types we currently support.
local dataFromJava = CallJavaStaticMethod(jvmID, className, methodName, methodSignature, args...)- jvmID ID to the JVM you have created using CreateJava. Example: 1
- className Class name of the class you want to call a method in, must include package path as well. Example: dev/joseph/Test (dev.joseph.Test).
- methodName Static method name you want to call. Example: returnTestInt
- methodSignature Signature of the method you want to call. Example: (Ljava/lang/Integer;)I
- args (Optional) Pass arguments into the method.
Links a Java class so the native methods below can be used.
LinkJavaAdapter(jvmID, className)- jvmID ID to the JVM you have created using CreateJava. Example: 1
- className Class name of the class you want to call a method in, must include package path as well. Example: dev/joseph/Adapter (dev.joseph.Adapter).
You can use a native adapter to call lua functions.
package example;
public class Adapter {
public native static void callEvent(String event, Object... args);
public native static Object[] callGlobalFunction(String packageName, String functionName, Object... args);
}LinkJavaAdapter(java, "example/Adapter")Java:
Adapter.callEvent("testCallEvent", "hi", Integer.valueOf(123), Boolean.valueOf(true));Lua:
AddEvent('testCallEvent', function(s,i,b)
print(s)
print(i)
print(b)
end)Make sure to use this method only on the main thread. Using it outside the mainthread can result in unexpected behavior.
Java:
Adapter.callGlobalFunction("AddPlayerChatAll", "Hello World");