#Read/write to file

116 messages · Page 1 of 1 (latest)

heavy spindle
#

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?

peak basaltBOT
#

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

plucky finchBOT
#

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'})
heavy spindle
#

Is json fast to read/write or would a diffrent format be better?

soft monolith
#

there's no different format.

heavy spindle
#

I thought it could do nbt as well?

soft monolith
#

??tips

plucky finchBOT
soft monolith
#

maybe it can? idk tbh

#

what does probe suggest for the JsonIO object thocc

heavy spindle
#

Will it create folders automatically?

soft monolith
heavy spindle
#

Can it create folders?

soft monolith
#

probably not, I'm not too sure

#

maybe there's a method to create folders, idk hmmm

#

what does probe suggest for the JsonIO object thocc

heavy spindle
soft monolith
#

scroll down hmmm

heavy spindle
soft monolith
#

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

heavy spindle
#

Ok ill wait, maybe someone has an idea

soft monolith
#

yeah

#

??xy

plucky finchBOT
# soft monolith ??xy

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.

soft monolith
#

oh wait

heavy spindle
#

Because if I include a folder in the installation of the modpack, it will overwrite it everytime

soft monolith
#

crap

#

I should maybe read the main post, discord likes scrolling to the bottom automatically

heavy spindle
#

Good idea I guess

wise reef
#

Could be that jsonio automatically creates the folders. Idk

plucky finchBOT
heavy spindle
#

Can I put the file into the save file?

#

Or what would be the path for the save file

soft monolith
#

??tryitandsee

plucky finchBOT
soft monolith
#

wdym

heavy spindle
#

But if I don't know the world name

#

This should work for every world

soft monolith
#

then don't put it in the save folder thocc

heavy spindle
#

But it has to have diffrent data for every save

soft monolith
#

then put it in the save folder

#

there's probs a way to get the world name cluedin

heavy spindle
#

Yeah that was my initial question...

heavy spindle
#

JsonIO.write('sky_technology/test.json', {settinga: 'creeper', weirdblock: 'minecraft:end_gateway'})

ocean plaza
#

you can probably get the folder path from server or level

heavy spindle
#

I looked with probejs, I found nothing

ocean plaza
#

should look at lexxie

heavy spindle
#

Whats the url for that again?

soft monolith
heavy spindle
#

Thanks a lot

ocean plaza
#

maybe console.log(event.server.getWorldPath("ROOT"))

heavy spindle
#

[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')?

ocean plaza
#

you need to load the java class LevelResource and use
$LevelResource.ROOT instead

heavy spindle
#

Stupid question, how do I load a java class? I never do that

ocean plaza
#

const $LevelResource = Java.loadClass("net.minecraft.world.level.storage.LevelResource")

this should be OUTSIDE any event

heavy spindle
#

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.

wise reef
#

Yes it can create a file

heavy spindle
#

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?

wise reef
#

To remove ethe dot couldn't you Just do String Manipulation?

wise reef
#

.slice should exist. And .toString() should work. Wierd

heavy spindle
#

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]

wise reef
#

Oh, it's an array? Seems Like you only need to remove the Last element. You need to use java code

heavy spindle
#

But when I put it in the path it behaves like a string

#

Verry weird

#

Also is .pop() not in rhino?

wise reef
#

You need to use java code

#

Probably

#

To remove the element

#

of the list

heavy spindle
#

path.remove(path.size() - 1)

#

Can't find function remove

#

This is java right?

#

I don't have that much experience with java

wise reef
heavy spindle
#

I think it says that for every array

wise reef
#

Oh, it does? Yeah then nvm

heavy spindle
#

[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?

ocean plaza
#

this is not an Array, it is a Path

try .PLAYER_DATA_DIR instead of .ROOT
I want to see which dir is this

heavy spindle
#

C:\Users\phili\curseforge\minecraft\Instances\Sky Technology\saves\Survival world\playerdata\sky_technology\test.json

ocean plaza
#

I'm sure you attach extra stuff on that

heavy spindle
#

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

ocean plaza
#

use .toUri()

heavy spindle
#

On what?

ocean plaza
#

send code

heavy spindle
#
    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'})
})```
ocean plaza
#

on console.log(path.toUri())

heavy spindle
#

[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?

ocean plaza
#

can you do a new console.log with
console.log(e.server.getServerDirectory().toUri())

heavy spindle
#

[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.

ocean plaza
#

ops this one is toURI()

#

Java is not consistent lmao

heavy spindle
#

20:25:17] [INFO] mystery_lands_loot.js#33: file:/C:/Users/phili/curseforge/minecraft/Instances/Sky%20Technology/ [java.net.URI]

heavy spindle
#

I found it

#

path.getParent() works

peak basaltBOT
#

Ticket re-opened!