flooferland

SoundsResourceReloader.java

Apr 22nd, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. package flooferland.darkwest.resource;
  2.  
  3. import com.google.gson.JsonIOException;
  4. import com.google.gson.JsonObject;
  5. import com.google.gson.JsonParseException;
  6. import com.google.gson.JsonParser;
  7. import flooferland.darkwest.DarkWestMod;
  8. import flooferland.darkwest.sound.ModSound;
  9. import flooferland.darkwest.sound.SoundInfo;
  10. import flooferland.darkwest.util.DarkWestEnv;
  11. import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
  12. import net.minecraft.registry.Registries;
  13. import net.minecraft.registry.Registry;
  14. import net.minecraft.resource.Resource;
  15. import net.minecraft.resource.ResourceManager;
  16. import net.minecraft.resource.SinglePreparationResourceReloader;
  17. import net.minecraft.sound.SoundEvent;
  18. import net.minecraft.util.Identifier;
  19. import net.minecraft.util.profiler.Profiler;
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.Optional;
  24.  
  25. public class SoundsResourceReloader extends SinglePreparationResourceReloader<HashMap<String, Float>> implements IdentifiableResourceReloadListener {
  26.     @Override
  27.     protected HashMap<String, Float> prepare(ResourceManager manager, Profiler profiler) {
  28.         HashMap<String, Float> lengths = new HashMap<>();
  29.         // if (DarkWestEnv.isDataGenerator()) return lengths;
  30.        
  31.         DarkWestMod.LOGGER.info("Loading sounds from disk..");
  32.  
  33.         // region | Reading the sounds.json
  34.         final JsonObject json;
  35.         {
  36.             Identifier soundsJsonId = new Identifier(DarkWestMod.MOD_ID, "sounds.json");
  37.             Optional<Resource> resource = manager.getResource(soundsJsonId);
  38.  
  39.             if (resource.isPresent()) {
  40.                 Resource loadedResource = resource.get();
  41.                 try {
  42.                     BufferedReader reader = loadedResource.getReader();
  43.                     json = JsonParser.parseReader(reader).getAsJsonObject();
  44.                     reader.close();
  45.                 } catch (IOException | JsonIOException e) {
  46.                     throw new RuntimeException("Error reading " + soundsJsonId + ": " + e);
  47.                 } catch (JsonParseException e) {
  48.                     throw new RuntimeException("Error parsing " + soundsJsonId + ": " + e);
  49.                 }
  50.             } else {
  51.                 throw new RuntimeException("Missing asset: " + soundsJsonId);
  52.             }
  53.         }
  54.         // endregion
  55.  
  56.         // Reading the length
  57.         json.asMap().forEach((key, value) -> {
  58.             float length = value.getAsJsonObject().get("length").getAsJsonPrimitive().getAsFloat();
  59.             lengths.put(key, length);
  60.  
  61.             // Safety check
  62.             if (length <= 0.03f) {
  63.                 DarkWestMod.LOGGER.error(String.format("Something went wrong retrieving the length for SoundEvent \"%s\"", key));
  64.             }
  65.         });
  66.        
  67.         DarkWestMod.LOGGER.info("Successfully loaded the sounds from disk!");
  68.  
  69.         // Returning all the lengths
  70.         return lengths;
  71.     }
  72.  
  73.     @Override
  74.     protected void apply(HashMap<String, Float> prepared, ResourceManager manager, Profiler profiler) {
  75.         ModSound.soundsFileData = prepared;
  76.         DarkWestMod.LOGGER.info("Beginning to register the mod's sounds..");
  77.  
  78.         for (ModSound sound : ModSound.values()) {
  79.             // Getting/registering data
  80.             if (Registries.SOUND_EVENT.get(sound.id) == null) {
  81.                 sound.event = SoundEvent.of(sound.id);
  82.                 Registry.register(Registries.SOUND_EVENT, sound.id, sound.event);
  83.             }
  84.  
  85.             // Guards
  86.             if (DarkWestEnv.isDataGenerator()) return;
  87.             if (ModSound.soundsFileData.isEmpty()) {
  88.                 throw new RuntimeException("soundsFileData is empty");
  89.             }
  90.  
  91.             // Retrieving the lengths
  92.             var length = ModSound.soundsFileData.get(sound.id.getPath());
  93.             if (length == null || length <= 0.03f) {
  94.                 throw new RuntimeException(String.format("Something went wrong retrieving the length from the file data for SoundEvent \"%s\"", sound.id));
  95.             }
  96.  
  97.             // Constructing a new mod sound
  98.             sound.info = new SoundInfo(sound.id, length);
  99.             sound.event = SoundEvent.of(sound.id);
  100.         }
  101.  
  102.         DarkWestMod.LOGGER.info("Registered the mod's sounds!");
  103.     }
  104.  
  105.     @Override
  106.     public Identifier getFabricId() {
  107.         return new Identifier(DarkWestMod.MOD_ID, "sound_loader");
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment