#How to read files in KubeJS
15 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
You can read files, but with restrictions:
- You may only read files within the
minecraftfolder of your instance.Pathclass is blocked by class filter, so the only way to get a Path is via an implicit conversion from string, which sanitizes the path. - You may only read JSON files (with
JsonIO.read(path)) and NBT files (withNBTIO.read(path)).
Though if you want to "read the JEI blacklist", instead you can use KubeJS to remove items from JEI listing
try {
// Reading the JSON file using the allowed JsonIO class
let data = JsonIO.read("kubejs/assets/minecraft/neutron/disabled_items.json")
if (data) {
console.log("Successfully read neutron file!")
// Process your data here
console.log(data)
} else {
console.error("File was empty or could not be found.")
}
} catch (e) {
console.error("Error reading file: " + e)
}
Nvm, figured it out, For those wondering, it has to be an absolute path but not include minecraft as root dir, and it cant start with a "/" or "../" because its always absolute.
There are times like this when I wish KubeJS had real documentation
We're trying, but it's hard - documenting KubeJS means documenting a decent amount of Minecraft classes too
Also, was this generated by AI?
Just curious
Indeed, I used AI to save me time bc I wasnt sure what I needed to do exactly
Here's a simple snippet showing how to use JsonIO to read/write JSON files anywhere within the Minecraft folder:
// Reading the contents of the file from the given path
let config = JsonIO.read('kubejs/config/myawesomeconfig.json') // GSON format
let configObject = JSON.parse(config.asString()) // JS Object format
// Writing to an existing/new file
JsonIO.write('kubejs/config/myawesomeconfig.json', {settings: 'creeper', weirdblock: 'minecraft:end_gateway'})