Posted June 6, 201510 yr I am working on a mod, that requires a text file to be loaded into an array. To get the file loaded, i need to be able to get the directory of the file, i plan on putting it into the config folder. So how do i get the config directory? I figure it is either in minecraft, forge, fml, or a java import.
June 6, 201510 yr Don't do that man. Use Forges Configuration file. It REALLY is GOOD once you learn to use it, and allows you to do ANYTHING. Spend some time looking into it, worth it. As to finding path: public static File getMcDir() { if (MinecraftServer.getServer() != null && MinecraftServer.getServer().isDedicatedServer()) { return new File("."); } return Minecraft.getMinecraft().mcDataDir; } This will give you main MC folder - either main server folder or .minecraft for client.jar. Anything else is pure java. (Actulla above is too - pure java). 1.7.10 is no longer supported by forge, you are on your own.
June 6, 201510 yr Easiest way to get the configuration file is from FMLPreInitializationEvent: @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { Config config = new Configuration(new File(event.getModConfigurationDirectory().getAbsolutePath() + "additional/path/to/your/config.cfg")); } I just put my "modid/modid.cfg" as the additional path. http://i.imgur.com/NdrFdld.png[/img]
June 6, 201510 yr Author Thinking i should use a text file from the resource files area, so it is in the jar. How would i change this to load from say src/main/resources/myfile.txt Planning on adding versionchecker, so this method would be preferred. This is the code i was gonna use for the file in the cinfig directory. public String[] readLines(String filename) throws IOException { FileReader fileReader = new FileReader(filename); BufferedReader bufferedReader = new BufferedReader(fileReader); List<String> lines = new ArrayList<String>(); String line = null; while ((line = bufferedReader.readLine()) != null) { lines.add(line); } bufferedReader.close(); return lines.toArray(new String[lines.size()]); } Also thank you for the quick replies. It is very much appreciated.
June 6, 201510 yr Is there some reason you must write your own file read/write code, rather than using the Config class from Forge? You can read or write anything to that file, even arrays of Strings, or you could simply get the config directory like I showed you in my last post and use that to open a file directly - you don't have to use the Forge Config class to interact with files. http://i.imgur.com/NdrFdld.png[/img]
June 6, 201510 yr Author My original idea was to have 2 files, the mod jar, and a list of things in a txt file. But after thinking about adding versionchecker, it seems easier to just have the jar. I have used the configs files in my other mod. I just don't want to hard code the list. Would rather it be in a text file, and then load it into an array, for access by a function. So how would i load a text file from the jar?
June 6, 201510 yr Have you looked at Version Checker? It's very easy to use - you just put a .json versionlist online and send a single inter-mod communication message. Or, if you really want to implement your own version checking system, Jabelar has a tutorial on that topic on his blog. http://i.imgur.com/NdrFdld.png[/img]
June 6, 201510 yr Author Yes i've used version checker before. I'm wanting to load a file from the src/main/resources directory of eclipse, instead of a file outside of the jar. That way when i update, and versionchecker downloads the new jar, the text file is updated, without the user having to move it to another folder. I know this is not related to the original question. This is what i want to be able to load into an array. A simple text document that will get compiled into the jar. item 1 item 2 etc
June 6, 201510 yr I'm not quite sure what you are trying to accomplish with this particular line of reasoning, but you'd need to figure out a way to open the .jar file contents (e.g. 7-zip utility to open it as an archive) and find the file you want within that directory structure. Is this text file supposed to contain configuration settings for your mod that are not user-modifiable? If so, you are still hard-coding it because you have to change the text file - why not just set the values you want in your code? If it is supposed to be modifiable by users, why place it in your .jar file when you can use the mod configuration directory and open / modify whatever file you want there? http://i.imgur.com/NdrFdld.png[/img]
June 6, 201510 yr Author It's just supposed to be lines of text, like a bunch of insults or random quotes. Nothing to do with configurations. More than likely this text file would be the thing getting updated most often, and could have hundreds of lines of text.
June 6, 201510 yr Couldn't you just use your language file, then? http://i.imgur.com/NdrFdld.png[/img]
June 6, 201510 yr Author hmm, i hadn't thought of that file. I could just use a deliminator to separate the lines, and import them into an array. Thank you so much, for pointing that out to me.
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.