Jump to content

Copying Files from Mod‘s resources folder to minecraft directory


The_Dead_2

Recommended Posts

 I‘m a little bit lost on this one. As the title says I‘m trying to copy files located in my mod‘s resources directory to a directory the mod creates inside the minecraft directory.

The mod I‘m currently working on gives modpack creators the ability to add certain things (e.g. advancements) to the game, a little bit like a data pack but with more features. And the mod works perfectly fine. The only problem I have, is that I want, at the first startup of the game, to create some example files inside the directory my mod creates and uses in the minecraft directory. I thought the easiest way to achieve this is by simply adding the examples as files inside my mod’s resources folder and copy them on the first startup into the mod’s directory inside the minecraft directory. Unfortunately I can’t seem to find the correct relative path to the mod’s resources folder. 
The build.gradle file defines the working directory as located in ‘run’ wich causes java to look inside the ‘run’ folder for relative paths. So even if you create a directory by simply stating:

https://share.icloud.com/photos/041c6hRFf2d1H1gd39ayK3Puw

it will create the file inside the run directory with specified name:

https://share.icloud.com/photos/07fTrJ_c5GSKsO9L1lLg-wFqQ

 

Changing the working directory inside the build.gradle resolves the problem in the dev environment, but when I build my mod as a .jar java looks for relative paths inside the minecraft directory.

I also tried different online examples on how to extract files from a jar by retrieving the location of the executing class but as it’s not the mod itself executing the program this results in a NullPointerException.

 

Does anyone has an idea, how I could resolve my problem? I thought about using minecrafts internal Resource Locations but I’m not quite sure how to achieve this.

 

This is my first minecraft mod I’m creating but not my first Java Project.

 

The file structure inside my mod:

https://share.icloud.com/photos/07e3YnmmqU4C9RmMAQmWFI6tQ

The method for copying the files:

https://share.icloud.com/photos/0b7m7DlQ5ezJJs3Uu9PYhhF_Q

This method works perfectly fine when using an absolute path, but this wouldn’t make sense in a finished mod. Running the method with a relative path i results in a NullPointerException.

 

Link to comment
Share on other sites

Don't post code or text as images.

Show the real code, preferably on github.

 

Your issue is you have to deal with 2 different environments.

In the dev environment, the files will be in the resources folder of your project.

In the real production environment they will be inside the jar file.

 

You should use java's Path abstraction and Forge's Mod information

e.g. something like (untested code)

        // Get your mod information from forge
        var modFile = ModList.get().getModFileById(MODID).getFile();
 
        // Process all files in the myexamples subfolder that end with .json
        var examples = modFile.findResource("myexamples");
        try (var paths = Files.list(examples)) {
            paths.filter(path -> path.endsWith(".json")).forEach(path -> {
                // Copy each path here
            });
        } catch (IOException e) {
            LOGGER.warn("Failed to copy examples", e);
        }

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

The portion of the code until the try block works perfectly fine. The try block itself gets skipped without throwing any error. Not quiet sure why. But I can use the first portion of the code with my original code combined. I just need to transfer the path to a uri an parse that into the „new File“ constructor.

 

Thx for the help.

Link to comment
Share on other sites

That's because my code contains a bug. I said it was untested. 🙂 

Path and String both have a method called endsWith() but they do very different things.

The code should be:

path.toString().endsWith(".json")

 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

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.