Posted September 18, 20187 yr 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?
September 18, 20187 yr getAllResources may be what you're looking for. The official YouTuber Spaceboy Ross
September 18, 20187 yr 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
September 18, 20187 yr 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
September 18, 20187 yr 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
September 18, 20187 yr Well, then you cant do what you're trying to do. The official YouTuber Spaceboy Ross
September 18, 20187 yr 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 September 18, 20187 yr by Tschipp
September 19, 20187 yr Yeah, looks like you're already on the right track. Just wanted to add that Minecraft is just Java and so any file and folder processing you want to do you can do using standard Java methods to do so. Check out my tutorials here: http://jabelarminecraft.blogspot.com/
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.