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



×
×
  • Create New...

Important Information

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