Skip to content

Using the LuaValueMapper

Selena Rose Hammond edited this page Aug 22, 2025 · 3 revisions

Using the LuaValueMapper

The LuaValueMapper can be used for mapping Lua values from a Lua script to a Java class. (Be sure that the Java variables have the same name as the LUA variables)

Example usage

-- lua script
Test_String = "ABC"
Test_Boolean = true
Test_Int = 5


-- Be sure to return the LUA table
return {
    Test_String = Test_String,
    Test_Boolean = Test_Boolean,
    Test_Int = Test_Int
}
// Java mapper class
public class Mapped {

    public String Test_String;
    public boolean Test_Boolean;
    public int Test_Int;

}
// Calling the method somewhere

public void mapClass() {
    Mapped cls = LuaValueMapper.mapToClass(Mapped.class, FileManager.folderPath("test") + "Test.lua");
    // Using the mapped class
    MessageUtils.consoleSend(cls.Test_String);
}

I suggest you create an Enum of the Mapped classes

public enum ScriptSettings {

    TEST(MappingTest.class, "test", "Test.lua");

    private final File file;

    private final Class<?> clazz;

    <T> ScriptSettings(Class<T> clazz, String parent, String script) {
        this.clazz = clazz;
        file = FileManager.file(parent, script);
    }


    @SuppressWarnings("unchecked")
    public <T> T getScriptValues() {
        return (T) LuaValueMapper.mapToClass(clazz, file.getPath());
    }

}

Clone this wiki locally