Java Library
The following functions are provided to scripts via the java global.
java.cast(object, class)
Casts a Java object object to class.
Parameters
object-userdata [instance]: An object instance.class-userdata [class]: A class that the object instance is cast to.
Returns
userdata [instance]: The casted object.
Usage
java.coerce(tableLike, class)
Coerces tableLike to a table.
Parameters
tableLike-userdata [instance]: A table-like object instance (one ofMap,List, orSet).class-userdata [class]: A class representing both the type oftableLike, and the types of the elements withintableLike.
Returns
table: A table from the values contained withintableLike.
Usage
Given the Java class:
package com.example;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.lang.Integer;
import java.lang.String;
public class CoerceExample {
public static List<Integer> getList() {
return List.of(3, 1, 4, 1, 5, 9);
}
public static Set<Integer> getSet() {
return Set.of(3, 1, 4, 5, 9);
}
public static Map<String, Integer> getMap() {
Map<String, Integer> map = new HashMap<>();
map.put("hello", 5);
map.put("world!", 6);
return map;
}
}The usage for each type would be:
local List = require("java.util.List")
local Set = require("java.util.Set")
local Map = require("java.util.Map")
local Integer = require("java.lang.Integer")
local String = require("java.lang.String")
local CoerceExample = require("com.example.CoerceExample")
local list = java.coerce(CoerceExample.getList(), List[{Integer}])
local set = java.coerce(CoerceExample.getSet(), Set[{Integer}])
local map = java.coerce(CoerceExample.getMap(), Map[{String, Integer}])java.wrap(value, class)
Wraps a Lua value in a provided class. Seldom useful due to type conversion being automatically handled by Allium.
Parameters
value-boolean | number | string | userdata [instance]: Lua value (or Java object) to be wrapped.class-userdata [class]: Class for value to be wrapped in.
Returns
userdata [instance] - Wrapped value of type provided by class.
Usage
local Double = require("java.lang.Double")
local wrappedPi = java.wrap(3.14159265, Double) java.instanceOf(object, class)
Check whether the given object is of the given class.
Parameters
object-userdata [instance]: The object instance to test.class-userdata [class]: The class that the object instance is tested against.
Returns
boolean: Whether or notobjectis of typeclass
Usage
local Blocks = require("net.minecraft.world.level.block.Blocks")
local VegetationBlock = require("net.minecraft.world.level.block.VegetationBlock")
local result
if java.instanceOf(Blocks.ALLIUM, VegetationBlock) then
result = "is"
else
result = "is not"
end
print("Allium", result, "a VegetationBlock.")INFO
In Minecraft's code, Blocks.ALLIUM is of type FlowerBlock, which inherits from VegetationBlock. Therefore in this example, java.instanceOf should return true.
java.callWith(function, paramTypes, params...)
Invokes a java method (either static or instance) with the given paramTypes, using the given lua params.... Useful for when generic types abstract away the type, confusing Allium's automatic method resolution.
TIP
If you get an error that Allium can't find a method that is present, and one of the parameters types is T (or some other letter, generally); use this function.
Parameters
function-function: The method to be called.paramTypes-table<userdata [class]>: The list of classes representing each parameter.params...-any: A variable amount of parameters to be passed to the method. If this method is an instance method, supply the instance that the method should be called on. Supplying the type of the instance inparamTypesis not necessary.
Returns
any...: Technically a variable number of arguments, but is generally just the single return value of thefunctionbeing called.
Usage
Parses a table, converting it into a text component. Example derived from Bouquet API - component.lua.
local ComponentSerialization = require("net.minecraft.network.chat.ComponentSerialization")
local LuaOps = require("dev.hugeblank.bouquet.util.LuaOps")
local LuaValue = require("org.squiddev.cobalt.LuaValue")
local data = {
text = "Hello world!",
color = "green",
hover_event = { action = "show_text", value = "Secret message!" }
}
local dataResult = java.callWith(ComponentSerialization.CODEC.parse, {LuaOps, LuaValue}, ComponentSerialization.CODEC, LuaOps(script:getState()), data)
if result:isError() and dataResult:error():isPresent() then
error(dataResult:error():get():message())
elseif dataResult:isError() then
error("An unknown error occurred during parsing.")
end
local component = dataResult:getOrThrow() -- This would be sent in a command response, or something similar.java.getRawClass(className)
Gets a raw class or interface representation as an EClass (enhanced representation of the standard Class type). Seldom useful due to Allium's automatic conversion of userdata [class] to EClass and Class types where necessary.
Parameters
className-string: The class name, as it would be provided torequire().
Returns
userdata [instance]: anEClassinstance representing the class matchingclassName.
Usage
local eClass = java.getRawClass("net.minecraft.world.level.block.Blocks")
eClass:fields():forEach(function(field)
print(field:name())
end)java.throw(exception)
Throw an exception in Java.
Parameters
exception-userdata [instance]: The exception instance to be thrown.
Usage
java.extendClass(superclass, interfaces, access)
Provides a class builder extending from the given superclass. Optionally applies interfaces, and is created with the given access.
Parameters
superclass-userdata [class]: The parent class of the class being built.interfaces-table<userdata [class]>?: An optional table of interfaces to be applied to the class.access-{ static = boolean?, interface = boolean?, abstract = boolean? }?: An optional table of flags to define properties of the class.
Returns
userdata [instance]: A class builder.
Usage
See Class Building - Standard Class Builder.
Primitives
The java global also provides all of the primitives as userdata [class] for use in class building.
They are:
java.booleanjava.bytejava.shortjava.intjava.longjava.floatjava.doublejava.char