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:23] – Fixed Typo arbeetutorial:custom_resources [2024/06/29 07:10] (current) – [Reload Listeners 2: The Listener] Remove extra } daomephsta
Line 20: Line 20:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
-    ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener([...]);+    @Override 
 +    public void onInitialize() { 
 +        ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener([...]); 
 +    }
          
     [...]     [...]
Line 30: Line 33:
 We have now gone over the process of registering your reload listeners, but you have yet to actually //write// one, that part of the process will be discussed in this section (specifically, this section will discuss ''SimpleSynchronousResourceReloadListener'', async listeners will be discussed later). We have now gone over the process of registering your reload listeners, but you have yet to actually //write// one, that part of the process will be discussed in this section (specifically, this section will discuss ''SimpleSynchronousResourceReloadListener'', async listeners will be discussed later).
  
-By all laws of programmingyou should not be able to instantiate an interface in java, its methods are too abstract to callHowever, modders don't care about what actual programmers think so we will do it anyway+To simplify this tutorial, an [[https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html|anonymous class]] is used to implement ''SimpleSynchronousResourceReloadListener'', so that all the code is in one file.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
Line 40: Line 43:
  
     @Override     @Override
-    public void apply(ResourceManager manager) {+    public void reload(ResourceManager manager) {
         [...]         [...]
     }     }
Line 46: Line 49:
 </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 57:
 <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 67: Line 70:
         [...]         [...]
     }     }
-} 
 </code> </code>
  
Line 76: Line 78:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
-    ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new SimpleSynchronousResourceReloadListener() { +    @Override 
-        @Override +    public void onInitialize() { 
-        public Identifier getFabricId() { +        ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new SimpleSynchronousResourceReloadListener() { 
-            return new Identifier("tutorial", "my_resources"); +            @Override 
-        }+            public Identifier getFabricId() { 
 +                return new Identifier("tutorial", "my_resources"); 
 +            }
  
-        @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); 
 +                    }
                 }                 }
             }             }
-        } +        }); 
-    });+    }
     [...]     [...]
 } }
 </code> </code>
tutorial/custom_resources.1616927012.txt.gz · Last modified: 2021/03/28 10:23 by arbee