Получить список файлов в папке assets

Версия Minecraft
1.7.10
217
12
119
Нужно получить файлы с папки assets мода.
В дебаге все адекватно работает
Но вот после билда получить уже не могу

j:
try {
            InputStream stream = this.getClass().getResourceAsStream("/assets");
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            while (reader.ready()) {
                System.out.println(reader.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 
Решение
java:
public List<String> getResourcesByPath(String pathSources) {
        List<ModContainer> list = Loader.instance().getModList();
        ArrayList<String> listResources = new ArrayList<>();
        for (ModContainer container : list) {
            if (container.getModId().equals(Main.MODID)) {
                File jarFile = container.getSource();
                final String path = "assets/" + Main.MODID + "/" + pathSources;
                if (jarFile.isFile()) {
                    try {
                        final JarFile jar = new JarFile(jarFile);

                        final Enumeration<JarEntry> entries = jar.entries();
                        while (entries.hasMoreElements()) {
                            final String name...
Сомнительный конечно код, можно было намного проще сделать
Java:
final URI uri = Objects.requireNonNull(getClass().getResource(folder)).toURI();
if (uri.getScheme().equals("jar")) {
    try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
              
    }
}
Ну или вскрывай через zip
 
Сомнительный конечно код
Данный выигрышен только в случае когда ресурс перекрыватся другим ресурсом по тому же пути из другого jar в classpath. В случае ассетов такого не происходит - каждый имеет свой уникальный путь, так что через classpath все работать должно, главное к правильномому classloader'у обращаться, куда всё подключено.
 
java:
public List<String> getResourcesByPath(String pathSources) {
        List<ModContainer> list = Loader.instance().getModList();
        ArrayList<String> listResources = new ArrayList<>();
        for (ModContainer container : list) {
            if (container.getModId().equals(Main.MODID)) {
                File jarFile = container.getSource();
                final String path = "assets/" + Main.MODID + "/" + pathSources;
                if (jarFile.isFile()) {
                    try {
                        final JarFile jar = new JarFile(jarFile);

                        final Enumeration<JarEntry> entries = jar.entries();
                        while (entries.hasMoreElements()) {
                            final String name = entries.nextElement().getName();
                            // System.out.println("ALL NAME = "+ name);
                            if (name.startsWith(path + "/") && name.endsWith(".png")) {
                                listResources.add(FilenameUtils.getBaseName(name));
                            }
                        }
                        jar.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    final URL url = Main.class.getResource("/" + path);
                    if (url != null) {
                        try {
                            final File apps = new File(url.toURI());
                            for (File app : apps.listFiles()) {
                                listResources.add(FilenameUtils.getBaseName(app.getName()));
                            }

                        } catch (URISyntaxException ex) {
                            // never happens
                        }
                    }
                }

            }
        }
        return listResources;
    }
Решение
 
Назад
Сверху