Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.12.2] Getting all files in custom assets folder

Featured Replies

Posted

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?

  • Author
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

 

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.

The official YouTuber Spaceboy Ross

  • Author
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

  • Author
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

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.