package com.agricraft.agricore.util;

import com.agricraft.agricore.core.AgriCore;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Set;
import java.util.function.Predicate;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;

public class ResourceHelper {

    private static final Reflections REFLECTIONS = new Reflections(null, new ResourcesScanner());

    public static Set<String> findResources(Predicate<String> nameFilter) {
        return REFLECTIONS.getResources(nameFilter::test);
    }

    /**
     * Copies a file from inside the jar to the specified location outside the
     * jar, retaining the file name. The default copy action is to not overwrite
     * an existing file.
     *
     * @param from the location of the internal resource.
     * @param to the location to copy the resource to.
     * @param overwrite if the copy task should overwrite existing files.
     */
    public static void copyResource(String from, Path to, boolean overwrite) {
        try {
            if (overwrite || !Files.exists(to)) {
                Files.createDirectories(to.getParent());
                Files.copy(getResourceAsStream(from), to, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (Exception e) {
            AgriCore.getLogger("AgriCraft").error(
                    "Unable to copy Jar resource: \"{0}\" to: \"{1}\"!",
                    from,
                    to
            );
            e.printStackTrace();
        }
    }

    /**
     * Retrieves the requested resource by using the current thread's class
     * loader or the AgriCore class loader.
     *
     * @param location the location of the desired resource stream.
     * @return the resource, as a stream.
     */
    public static InputStream getResourceAsStream(String location) {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(location);
        return in != null ? in : ResourceHelper.class.getResourceAsStream(location);
    }

}

@@ -222,16 +222,52 @@
 il.Set;%0A
+import java.util.function.Function;%0A
 import j
@@ -416,125 +416,1480 @@
 r %7B%0A
-%0A    private static final Reflections REFLECTIONS = new Reflections(null, new ResourcesScanner());%0A%0A    public static
+    /**%0A     * Copies all resources in the class path matching the given name predicate, as well as the file structure predicate to a new directory%0A     *%0A     * @param nameFilter the predicate for the file name%0A     * @param dirFilter the predicate for the file (directory) structure%0A     * @param toFunction function specifying the destination where each file should be copied to%0A     * @param overwrite specifies if files which already exist should be overwritten or not%0A     */%0A    public static void copyResources(Predicate%3CString%3E nameFilter, Predicate%3CString%3E dirFilter, Function%3CString, Path%3E toFunction, boolean overwrite) %7B%0A        ResourceHelper helper = new ResourceHelper();%0A        helper.findResources(nameFilter).stream()%0A                .filter(dirFilter)%0A                .forEach(r -%3E helper.copyResource(r, toFunction.apply(r), overwrite)%0A                );%0A    %7D%0A%0A    /**%0A     * Reflections instance used to find jsons%0A     */%0A    private final Reflections reflections;%0A%0A    /**%0A     * The Reflections object can use quite some memory, therefore we instantiate a new one when we need it,%0A     * and discard it afterwards%0A     */%0A    protected ResourceHelper() %7B%0A        this.reflections = new Reflections(null, new ResourcesScanner());%0A    %7D%0A%0A    /**%0A     * Finds all resources in the class path matching the given predicate%0A     * @param nameFilter file name predicate%0A     * @return set of all filenames matching the name filter%0A     */%0A    protected
  Set
@@ -1962,19 +1962,24 @@
 urn 
-REFLECTIONS
+this.reflections
 .get
@@ -2410,36 +2410,32 @@
     */%0A    p
-ublic static
+rotected
  void copyRe
@@ -2632,16 +2632,21 @@
 es.copy(
+this.
 getResou
@@ -3248,28 +3248,24 @@
 */%0A    p
-ublic static
+rotected
  InputSt

Optimize ResourceHelper memory usage