Tschipp Posted September 18, 2018 Posted September 18, 2018 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? Quote
Spaceboy Ross Posted September 18, 2018 Posted September 18, 2018 getAllResources may be what you're looking for. Quote The official YouTuber Spaceboy Ross
Tschipp Posted September 18, 2018 Author Posted September 18, 2018 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 Quote
Spaceboy Ross Posted September 18, 2018 Posted September 18, 2018 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. Quote The official YouTuber Spaceboy Ross
Tschipp Posted September 18, 2018 Author Posted September 18, 2018 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 Quote
Spaceboy Ross Posted September 18, 2018 Posted September 18, 2018 Well, then you cant do what you're trying to do. Quote The official YouTuber Spaceboy Ross
Tschipp Posted September 18, 2018 Author Posted September 18, 2018 (edited) 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, 2018 by Tschipp Quote
jabelar Posted September 19, 2018 Posted September 19, 2018 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. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
Recommended Posts
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.