Jump to content

[1.12.2] Getting all files in custom assets folder


Tschipp

Recommended Posts

I'm trying to get (and read the content) of all files contained in a custom asset folder.

The folders location is something like:

"assets/modid/customfolder/"

 

The files of the folder can change have names that cannot be hardcoded. I'd like to somehow get all these files and read their content.

I know that I can get the input stream from any resource using

Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation).getInputStream();

but this doesn't work for folders. Is it possible to get all the contents of the folder?

Link to comment
Share on other sites

5 minutes ago, Spaceboy Ross said:

getAllResources may be what you're looking for.

Doesn't seem to do it, unless I'm doing it wrong.

As a test, I tried to list all files in the lang folder of my mod, I tried both this:

Minecraft.getMinecraft().getResourceManager().getAllResources(new ResourceLocation(MODID, "assets/modid/lang/"));

and this:

Minecraft.getMinecraft().getResourceManager().getAllResources(new ResourceLocation(MODID, "lang/"));

 

Both threw a FileNotFoundException

 

Link to comment
Share on other sites

4 minutes ago, Spaceboy Ross said:

It should look something like this.


Minecraft.getMinecraft().getResourceManager().getAllResources(new ResourceLocation(MODID,"dir.subdir"));

You cannot access it like a path, you have to access it like a package.

You sure? 

Doing both

Minecraft.getMinecraft().getResourceManager().getAllResources(new ResourceLocation(MODID, "assets.modid.lang"));

or

Minecraft.getMinecraft().getResourceManager().getAllResources(new ResourceLocation(MODID, "lang"));

results in the same exception as before

Link to comment
Share on other sites

1 hour ago, Spaceboy Ross said:

Well, then you cant do what you're trying to do.

I think I actually just found a solution. I "borrowed" this code from CraftingManager.class and edited it a bit:

		FileSystem filesystem = null;

		URL url = MainModClass.class.getResource("/assets/modid/lang/");

		try
		{
			if (url != null)
			{
				URI uri = url.toURI();
				Path path = null;

				if ("file".equals(uri.getScheme()))
				{
					path = Paths.get(MainModClass.class.getResource("/assets/modid/lang").toURI());
				}
				else
				{

					filesystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
					path = filesystem.getPath("/assets/modid/lang");

				}
				
				Iterator<Path> it = Files.walk(path).iterator();
				
				while(it.hasNext())
					System.out.println(it.next());
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

This now prints out all files in the lang folder. Works in the dev environment and when the mod is built into a jar, just tested.

Edited by Tschipp
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.