Does anyone have an idea how I could write to a file and read from that file? I want to make a folder in the modpacks directory, and then save a file in there, to store stuff. I want to make an idle game and I can't just save everything inside the players persistant data because those will be houndreds of diffrent values that have to be stored. Also if I store it like an array there, can I read it and js will know its an array and allow me to do stuff like .push?
#Read/write to file
116 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
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
config = JsonIO.read('kubejs/config/myawesomeconfig.json')
// Writing to an existing/new file
JsonIO.write('kubejs/config/myawesomeconfig.json', {settinga: 'creeper', weirdblock: 'minecraft:end_gateway'})
Is json fast to read/write or would a diffrent format be better?
there's no different format.
I thought it could do nbt as well?
??tips
WIP
Will it create folders automatically?
no
Can it create folders?
probably not, I'm not too sure
maybe there's a method to create folders, idk 
what does probe suggest for the JsonIO object
scroll down 
okay yeah no there's no way to create folders using that
class filter blocks FS classes, so that isn't an option either iirc
Ok ill wait, maybe someone has an idea
The XY problem is when you really want to do X, and you think that Y can achieve X. However, you can't get Y to work, and so ask for help exclusively about Y.
This can lead to a lot of confusion from the people trying to help you, as Y can seem like a very random thing to want to do, and a lot of the time isn't the best way to achieve X anyway.
Its fine to ask about Y, just always include some context about X so you can be put on the right track if Y won't do X well, or there is an easier way to do X.
oh wait
Because if I include a folder in the installation of the modpack, it will overwrite it everytime
crap
I should maybe read the main post, discord likes scrolling to the bottom automatically
Good idea I guess
Could be that jsonio automatically creates the folders. Idk
Can I put the file into the save file?
Or what would be the path for the save file
??tryitandsee
then don't put it in the save folder 
But it has to have diffrent data for every save
Yeah that was my initial question...
[15:09:39] [ERROR] ! java.nio.file.NoSuchFileException: C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\sky_technology\test.json
JsonIO.write('sky_technology/test.json', {settinga: 'creeper', weirdblock: 'minecraft:end_gateway'})
you can probably get the folder path from server or level
I looked with probejs, I found nothing
should look at lexxie
Whats the url for that again?
A small repository of Forge/Fabric JavaDocs for quick and easy use.
Thanks a lot
maybe console.log(event.server.getWorldPath("ROOT"))
[16:29:39] [ERROR] ! mystery_lands_loot.js#30: Error occurred while handling event 'ItemEvents.rightClicked': Can't find method net.minecraft.server.MinecraftServer.m_129843_(string).
e.entity.tell(Text.of(e.server.getWorldPath("ROOT")))
I am looking at the java docs and trying diffrent things, but everything leads to the same error
It says getWorldPath(LevelResource p_129844_), so do I have to do something like LevelResource.of('Root')?
you need to load the java class LevelResource and use
$LevelResource.ROOT instead
Stupid question, how do I load a java class? I never do that

const $LevelResource = Java.loadClass("net.minecraft.world.level.storage.LevelResource")
this should be OUTSIDE any event
Ok thanks a lot
[18:43:39] [ERROR] ! Error occurred while handling event 'ItemEvents.rightClicked': java.lang.StackOverflowError
[18:43:39] [ERROR] ! java.lang.StackOverflowError
[18:43:39] [ERROR] ! at TRANSFORMER/[email protected]/dev.latvian.mods.kubejs.util.UtilsJS.wrap(UtilsJS.java:282)
[18:43:39] [ERROR] ! at TRANSFORMER/[email protected]/dev.latvian.mods.kubejs.bindings.TextWrapper.of(TextWrapper.java:30)
[18:43:39] [ERROR] ! at TRANSFORMER/[email protected]/dev.latvian.mods.kubejs.bindings.TextWrapper.of(TextWrapper.java:106)
Ah ok, when just cnosole.logging it it works
Thanks a lot
[18:44:48] [INFO] mystery_lands_loot.js#32: [Users, phili, curseforge, minecraft, Instances, Sky Technology, saves, Survival world, .] [java.util.ArrayList]
Following problem, this: ItemEvents.rightClicked('kubejs:mystery_lands_pickaxe_lv2', e => { let path = e.server.getWorldPath($LevelResource.ROOT) console.log(path) JsonIO.write(`${path}/sky_technology/test.json`, {settinga: 'creeper', weirdblock: 'minecraft:end_gateway'}) }) leads to this error
[18:52:15] [ERROR] ! Error occurred while handling event 'ItemEvents.rightClicked': java.nio.file.NoSuchFileException: C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\saves\Survival world\.\sky_technology\test.json
[18:52:15] [ERROR] ! java.nio.file.NoSuchFileException: C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\saves\Survival world\.\sky_technology\test.json```
For some reason there is a `.\`in there that I don't need, and I would just use .slice, but it says it cannot find function in object. Doing .tostring() before that changes nothing.
And it can't create a folder, idk if it can even create a file.
Yes it can create a file
Ok, if there is no other way, I'll just name the files sky_technology_xy
But the main problem is the .\
private final Path outputFolder
Can this maybe make a folder?
To remove ethe dot couldn't you Just do String Manipulation?
...
.slice should exist. And .toString() should work. Wierd
Even werider, when I console.log it I get this:
[18:52:15] [INFO] mystery_lands_loot.js#32: [Users, phili, curseforge, minecraft, Instances, Sky Technology, saves, Survival world, .] [java.util.ArrayList]
Oh, it's an array? Seems Like you only need to remove the Last element. You need to use java code
But when I put it in the path it behaves like a string
Verry weird
Also is .pop() not in rhino?
path.remove(path.size() - 1)
Can't find function remove
This is java right?
I don't have that much experience with java
I've also found that through a search so that should be correct.
Seems Like it doesn't need Java code, Just thought, because it Said Java here
I think it says that for every array
Oh, it does? Yeah then nvm
[19:16:22] [ERROR] ! mystery_lands_loot.js#32: Error occurred while handling event 'ItemEvents.rightClicked': TypeError: Cannot find function splice in object C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\saves\Survival world..
[19:16:22] [ERROR] ! mystery_lands_loot.js#32: Error occurred while handling event 'ItemEvents.rightClicked': TypeError: Cannot find function splice in object C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\saves\Survival world..
Splice doesn't work either
Is it possible that I just can't edit this?
this is not an Array, it is a Path
try .PLAYER_DATA_DIR instead of .ROOT
I want to see which dir is this
C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\saves\Survival world\playerdata\sky_technology\test.json
I'm sure you attach extra stuff on that
Yes, I copy it from the error of this
JsonIO.write(${path}/sky_technology/test.json, {settinga: 'creeper', weirdblock: 'minecraft:end_gateway'})
Becuase if I just console.log it, it looks like an array
use .toUri()
On what?
send code
let path = e.server.getWorldPath($LevelResource.PLAYER_DATA_DIR)
console.log(path)
JsonIO.write(`${path}/sky_technology/test.json`, {settinga: 'creeper', weirdblock: 'minecraft:end_gateway'})
})```
on console.log(path.toUri())
[20:21:50] [INFO] mystery_lands_loot.js#32: file:///C:/Users/phili/curseforge/minecraft/Instances/Sky%20Technology/saves/Survival%20world/playerdata/ [java.net.URI]
What does toUri do?
can you do a new console.log with
console.log(e.server.getServerDirectory().toUri())
[20:24:26] [ERROR] ! mystery_lands_loot.js#33: Error occurred while handling event 'ItemEvents.rightClicked': TypeError: Cannot find function toUri in object C:\Users\phili\curseforge\minecraft\Instances\Sky Technology.
20:25:17] [INFO] mystery_lands_loot.js#33: file:/C:/Users/phili/curseforge/minecraft/Instances/Sky%20Technology/ [java.net.URI]
Ticket re-opened!
