User Tools

Site Tools


tutorial:custom_resources

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:custom_resources [2021/03/28 10:09] – Fixed Typo arbeetutorial:custom_resources [2023/06/27 14:05] (current) mineblock11
Line 40: Line 40:
  
     @Override     @Override
-    public void apply(ResourceManager manager) {+    public void reload(ResourceManager manager) {
         [...]         [...]
     }     }
Line 46: Line 46:
 </code> </code>
  
-What you are seeing here is the actual resource reload listener (technically //a// resource reload listener, this is not the only type, however, it is the easiest to implement) that will be registered through ''ResourceManagerHelper''. As you can see there are two notable parts to it, the ''getFabricId'' and ''apply'' methods. The ''getFabricId'' method is the simpler of the two, you just need to return a unique identifier for the reload listener in question, nothing fancy. The apply method, however, is the main area of interest.+What you are seeing here is the actual resource reload listener (technically //a// resource reload listener, this is not the only type, however, it is the easiest to implement) that will be registered through ''ResourceManagerHelper''. As you can see there are two notable parts to it, the ''getFabricId'' and ''reload'' methods. The ''getFabricId'' method is the simpler of the two, you just need to return a unique identifier for the reload listener in question, nothing fancy. The reload method, however, is the main area of interest.
  
-The ''apply'' method supplies you with a ''ResourceManager'' with which to load whatever data you want within the constraints of the registry's resource type (remember, SERVER_DATA can only access data packs, CLIENT_RESOURCES can only access resource packs). What you do with the resource manager is really mostly up to you, however, if you are making some aspect of your mod data-driven then you probably want to read from a specific //folder//. Doing this is simple but it does require a bit of care.+The ''reload'' method supplies you with a ''ResourceManager'' with which to load whatever data you want within the constraints of the registry's resource type (remember, SERVER_DATA can only access data packs, CLIENT_RESOURCES can only access resource packs). What you do with the resource manager is really mostly up to you, however, if you are making some aspect of your mod data-driven then you probably want to read from a specific //folder//. Doing this is simple but it does require a bit of care.
  
 Firstly, you are going to want to clear or otherwise prepare to update anything that is storing the info you are going to be fetching through the manager, otherwise, your mod will likely break whenever ''/reload'' or ''F3 + T'' is used. Once that is done you should then use ''ResourceManager#findResources(String path, Predicate<String> pathPredicate)'' to fetch the files contained within whatever folder you want (note: the path is implicitly rooted in either the data or assets folder of all processed data/resource packs). This will get you a collection with the paths to all files that are within the specified path and meet the predicate's criteria (you are probably want to filter for a specific file type, ''.json'' in this tutorial), you will then need to iterate through the collection and do stuff with those paths. Firstly, you are going to want to clear or otherwise prepare to update anything that is storing the info you are going to be fetching through the manager, otherwise, your mod will likely break whenever ''/reload'' or ''F3 + T'' is used. Once that is done you should then use ''ResourceManager#findResources(String path, Predicate<String> pathPredicate)'' to fetch the files contained within whatever folder you want (note: the path is implicitly rooted in either the data or assets folder of all processed data/resource packs). This will get you a collection with the paths to all files that are within the specified path and meet the predicate's criteria (you are probably want to filter for a specific file type, ''.json'' in this tutorial), you will then need to iterate through the collection and do stuff with those paths.
Line 54: Line 54:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
     @Override     @Override
-    public void apply(ResourceManager manager) {+    public void reload(ResourceManager manager) {
         // Clear caches here         // Clear caches here
  
Line 60: Line 60:
             try(InputStream stream = manager.getResource(id).getInputStream()) {             try(InputStream stream = manager.getResource(id).getInputStream()) {
                 // Consume the stream however you want, medium, rare, or well done.                 // Consume the stream however you want, medium, rare, or well done.
-            } catch(Exception e) (+            } catch(Exception e) {
                 TUTORIAL_LOG.error("Error occurred while loading resource json " + id.toString(), e);                 TUTORIAL_LOG.error("Error occurred while loading resource json " + id.toString(), e);
             }             }
Line 83: Line 83:
  
         @Override         @Override
-        public void apply(ResourceManager manager) {+        public void reload(ResourceManager manager) {
             // Clear Caches Here             // Clear Caches Here
  
             for(Identifier id : manager.findResources("my_resource_folder", path -> path.endsWith(".json"))) {             for(Identifier id : manager.findResources("my_resource_folder", path -> path.endsWith(".json"))) {
-                try(IntputStream stream = manager.getResource(id).getInputStream()) {+                try(InputStream stream = manager.getResource(id).getInputStream()) {
                     // Consume the stream however you want, medium, rare, or well done.                     // Consume the stream however you want, medium, rare, or well done.
-                } catch(Exception e) ( +                } catch(Exception e) { 
-                    TUTORIAL_LOG.error("Error occurred while loading resource json " + id.toString(), e);+                    TUTORIAL_LOG.error("Error occurred while loading resource json" + id.toString(), e);
                 }                 }
             }             }
tutorial/custom_resources.1616926153.txt.gz · Last modified: 2021/03/28 10:09 by arbee