Datapack Load Events
You can load json datapack files programmatically!
onEvent('server.datapack.first', event => {
event.addJson(name, json)
})
resourceLocation
could be minecraft:loot_tables/entities/villager.json
json
could be for example:
{
type: "entity",
pools: [
{
rolls: 2,
bonus_rolls: 1,
entries: [
{
type: "item",
weight: 3,
name: "minecraft:emerald"
},
{
type: "empty",
weight: 2
}
]
}
]
}
Note: Practically everything in vanilla has a way better to programmatically load it, so it is recommended to use this mostly for loading thing for other mods
There are different timing that you can make the file get loaded too!
server.datapack.first
server.datapack.last
This event is useful, because instead of needing to write multiple json files, you can write one then change the values passed to it.
Example
Adds multiple advancements for using different items that reward experience:
onEvent('server.datapack.first', event => {
;const items = ['bow', 'golden_hoe', 'flint_and_steel', 'spyglass'].
items.forEach(item => {
event.addJson(`kubejs:advancements/${item}`, {
criteria: {
requirement: {
trigger: "minecraft:using_item",
conditions: {
item: {
items: [`minecraft:${item}`]
}
}
}
},
rewards: {
experience: 20
}
})
})
})
In the custom machinery
mod, the packdev needs to make a massive json file for each machine they wish to create. This script will make 16 machines, and is shorter then even a single on of the original json files would have been.
onEvent('server.datapack.first', event => {
let json
//create 16 custom machines with 6 inputs and 1 output
for (let machineNumber = 0; machineNumber < 16; machineNumber++) {
json = {
name: {
text: `${machineNumber}`
},
appearance: {},
components: [
{
"type": "custommachinery:item",
"id": "out",
"mode": "output"
}
],
gui: [
{
"type": "custommachinery:progress",
"x": 70,
"y": 41,
"width": 18,
"height": 18
},{
"type": "custommachinery:slot",
"x": 88,
"y": 41,
"id": "out"
},{
"type": "custommachinery:text",
"text": `Machine ${machineNumber}`,//string builder to make the name match the machine number
"x":16,
"y":16
},{
"type": "custommachinery:texture",
"x": 0,
"y": 0,
"texture": "custommachinery:textures/gui/base_background.png",
"priority": 1000
},{
"type": "custommachinery:player_inventory",
"x": 16,
"y": 68
}
]
}
//add the input slots and corrasponding componets
let slotNumber = 0
;const xValues = [16,34,52].
const yValues = [32,50]
xValues.forEach(x => {
;[32,50].yValues.forEach(y => {
json.components.push({
"type": "custommachinery:item",
"id": `input${slotNumber}`,
"mode": "input"
})
json.gui.push({
"type": "custommachinery:slot",
"x": x,
"y": y,
"id": `input${slotNumber}`
})
slotNumber++
})
})
//add the json
event.addJson(`kubejs:machines/${machineNumber}`,json)
}
})