Jump to content

Recommended Posts

Posted

I get a little confused on this...

 

I want to use a custom asset (in this case a text file with a massive array in it).  It isn't suitable for a config file (too much info and not really user options).  But the file path to the assets of course varies from computer to computer. 

 

I want to use standard Java file methods, like FileReader and FileWriter, to access the file.

 

I think ResourceLocation can help, but can't quite figure out how to get the absolute file path needed for FileReader and FileWriter.

 

So assuming I put my array.txt file into my mod's asset directory what would I pass as the absolute path to the FileReader.  E.g.how do I make this work?:

        bufferedReader = new BufferedReader(new FileReader(whatDoIPutHere+"array.txt"));

 

Or instead of standard Java file access is there some sort of Forge file access I should use instead?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Is it a mod/resource supplied asset, or is it a config file that end users are expected to edit?

 

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Posted

Is it a mod/resource supplied asset, or is it a config file that end users are expected to edit?

It is supplied by me (the mod maker).  It is not a config file -- it is a large byte stream.  I would not consider it a user option.  It is not expected for users to edit although I would not prevent them from doing it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

You have 2 choices:

1

Your resource is clientside-only, then you can directly use the resource managers' getResource method and from the returning IResource, you can use the InputStream returned by getInputStream().

You can get the resource manager field in the Minecraft instance.

 

2

Your resource should be available to both, client and server. This one is tricky, since you don't have a resource manager on the server side.

On the client, you can use the first approach

On the server, you could either read the file from the classpath directly or you need to read the mods' archive directly and read your file from there. You can probably get the mods' archive as a file instance within the FMLPreInit event with getSourceFile()

 

Problem is, IDK if this will work on a dev environment, since there is no actual packed mod archive, but you can read the file from the classpath there if it's in a dev environment.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

I think I could solve the problem if I could get the absolute path of any other asset in the game.  At some point the Java must have that info, but when I tried to follow the code I got a little lost.

 

I guess I need to revert to more Java standard stuff, which I'm not that confident with (in terms of the JAR file navigation).  It seems that the classLoader getResourceAsStream() is intended for this sort of thing.  Need to see if I can adapt my working file based code to that...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Okay, I got it working using getResourceAsStream().  Basically you need to put it into an InputStream, put that into an InputStreamReader and put that into a BufferedReader.  The resource as stream can find files within your JAR assets folder and getting it into BufferedReader allows you to use readLine() and read() functions.

 

For example:

readIn = new BufferedReader(new InputStreamReader(getClass().getClassLoader()
      .getResourceAsStream("assets/mymodid/mycustomassets/"+parName+".txt"), "UTF-8"));

 

Thanks to a post by MultiMote on another thread for giving me confidence to pursue the getResourceAsStream().

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Okay, I got it working using getResourceAsStream().  Basically you need to put it into an InputStream, put that into an InputStreamReader and put that into a BufferedReader.  The resource as stream can find files within your JAR assets folder and getting it into BufferedReader allows you to use readLine() and read() functions.

 

For example:

readIn = new BufferedReader(new InputStreamReader(getClass().getClassLoader()
      .getResourceAsStream("assets/mymodid/mycustomassets/"+parName+".txt"), "UTF-8"));

 

Thanks to a post by MultiMote on another thread for giving me confidence to pursue the getResourceAsStream().

 

Does this work on a dev AND productive environment?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Does this work on a dev AND productive environment?

 

While I haven't tested yet, I was also worried about it, and an answer on StackOverflow (http://stackoverflow.com/questions/20389255/java-reading-a-resource-file-from-within-jar) said the following:

 

Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream:

 

InputStream in = getClass().getResourceAsStream("/file.txt"));
BufferredReader reader = new BufferedReader(new InputStreamReader(in));

 

As long as the file.txt resource is available on the classpath then this approach will work the same way regardless of whether the file.txt resource is in a classes/ directory or inside a jar.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.