#How to read files in KubeJS

15 messages · Page 1 of 1 (latest)

analog agate
#

How do I do it? Is it even possible? I want to read the JEI blacklist from the config folder and erase recipes from the items listed.

bold ravenBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

quick orchid
#

You can read files, but with restrictions:

  • You may only read files within the minecraft folder of your instance. Path class 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 (with NBTIO.read(path)).
#

Though if you want to "read the JEI blacklist", instead you can use KubeJS to remove items from JEI listing

analog agate
#
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

quick orchid
#

We're trying, but it's hard - documenting KubeJS means documenting a decent amount of Minecraft classes too

quick orchid
#

Just curious

analog agate
#

Indeed, I used AI to save me time bc I wasnt sure what I needed to do exactly

regal talonBOT
#

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'})


quick orchid
#

There

#

read will give you a Java Map, which you can operate on like on a JS object

#

readJson will give you a GSON JsonObject

#

You can stringify the read file and then parse it to get a JS object