Alright, so after a whole morning of tries, I finally got it working!
Let's say I have this object inside my GLM json
{
"item": "minecraft:emerald",
"chance": 0.0265,
"functions": [
{
"function": "minecraft:set_count",
"count": {
"type": "minecraft:uniform",
"min": 2.0,
"max": 7.0
},
"add": false
}
]
}
where I am saying that the emerald has a 2.65% chacne of being inside the chest loot. But, I want that the stack, if any, must be between 2 and 7 items. Sure I can add my "min" and "max" parameters, but since we already have a function that does this, why not using it? So I added the vanilla set_count function, using the exact same syntax as any vanilla loot table. In the deserializer (the class that extends GlobalLootModifierSerializer) I can read the functions array by doing so
var functionsArray = jsonObject.getAsJsonArray("functions");
if(functionsArray != null) {
var functions = Deserializers.createFunctionSerializer().create().fromJson(functionsArray, LootItemFunction[].class);
}
This will return a LootItemFunction array that is the actual list of functions that are specified inside the JSON, correctly deserialized. So we can store these functions and use them when adding the ItemStack to the generated loot, by doing a simple for loop
var stack = new ItemStack(item);
if(functions != null && !functions.isEmpty()) {
functions.forEach(function -> function.apply(stack, context));
}
By doing that the function will be applied to the item of the GLM! Of course we are not limited by the set_count, but we can use any vanilla function as we want (I think even custom functions, but I'm not sure about this since I haven't any of these).
To correctly serialize said functions, inside the write method, we can use this function
Deserializers.createFunctionSerializer().create().toJsonTree(functions)
assuming that functions is a variable containing all of the functions previously deserialized