Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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?

Posted

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.

dv9KfTp.png

A simple text document that will get compiled into the jar.

item 1
item 2
etc

Posted

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?

Posted

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.

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.