Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package flooferland.darkwest.resource;
- import com.google.gson.JsonIOException;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParseException;
- import com.google.gson.JsonParser;
- import flooferland.darkwest.DarkWestMod;
- import flooferland.darkwest.sound.ModSound;
- import flooferland.darkwest.sound.SoundInfo;
- import flooferland.darkwest.util.DarkWestEnv;
- import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
- import net.minecraft.registry.Registries;
- import net.minecraft.registry.Registry;
- import net.minecraft.resource.Resource;
- import net.minecraft.resource.ResourceManager;
- import net.minecraft.resource.SinglePreparationResourceReloader;
- import net.minecraft.sound.SoundEvent;
- import net.minecraft.util.Identifier;
- import net.minecraft.util.profiler.Profiler;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Optional;
- public class SoundsResourceReloader extends SinglePreparationResourceReloader<HashMap<String, Float>> implements IdentifiableResourceReloadListener {
- @Override
- protected HashMap<String, Float> prepare(ResourceManager manager, Profiler profiler) {
- HashMap<String, Float> lengths = new HashMap<>();
- // if (DarkWestEnv.isDataGenerator()) return lengths;
- DarkWestMod.LOGGER.info("Loading sounds from disk..");
- // region | Reading the sounds.json
- final JsonObject json;
- {
- Identifier soundsJsonId = new Identifier(DarkWestMod.MOD_ID, "sounds.json");
- Optional<Resource> resource = manager.getResource(soundsJsonId);
- if (resource.isPresent()) {
- Resource loadedResource = resource.get();
- try {
- BufferedReader reader = loadedResource.getReader();
- json = JsonParser.parseReader(reader).getAsJsonObject();
- reader.close();
- } catch (IOException | JsonIOException e) {
- throw new RuntimeException("Error reading " + soundsJsonId + ": " + e);
- } catch (JsonParseException e) {
- throw new RuntimeException("Error parsing " + soundsJsonId + ": " + e);
- }
- } else {
- throw new RuntimeException("Missing asset: " + soundsJsonId);
- }
- }
- // endregion
- // Reading the length
- json.asMap().forEach((key, value) -> {
- float length = value.getAsJsonObject().get("length").getAsJsonPrimitive().getAsFloat();
- lengths.put(key, length);
- // Safety check
- if (length <= 0.03f) {
- DarkWestMod.LOGGER.error(String.format("Something went wrong retrieving the length for SoundEvent \"%s\"", key));
- }
- });
- DarkWestMod.LOGGER.info("Successfully loaded the sounds from disk!");
- // Returning all the lengths
- return lengths;
- }
- @Override
- protected void apply(HashMap<String, Float> prepared, ResourceManager manager, Profiler profiler) {
- ModSound.soundsFileData = prepared;
- DarkWestMod.LOGGER.info("Beginning to register the mod's sounds..");
- for (ModSound sound : ModSound.values()) {
- // Getting/registering data
- if (Registries.SOUND_EVENT.get(sound.id) == null) {
- sound.event = SoundEvent.of(sound.id);
- Registry.register(Registries.SOUND_EVENT, sound.id, sound.event);
- }
- // Guards
- if (DarkWestEnv.isDataGenerator()) return;
- if (ModSound.soundsFileData.isEmpty()) {
- throw new RuntimeException("soundsFileData is empty");
- }
- // Retrieving the lengths
- var length = ModSound.soundsFileData.get(sound.id.getPath());
- if (length == null || length <= 0.03f) {
- throw new RuntimeException(String.format("Something went wrong retrieving the length from the file data for SoundEvent \"%s\"", sound.id));
- }
- // Constructing a new mod sound
- sound.info = new SoundInfo(sound.id, length);
- sound.event = SoundEvent.of(sound.id);
- }
- DarkWestMod.LOGGER.info("Registered the mod's sounds!");
- }
- @Override
- public Identifier getFabricId() {
- return new Identifier(DarkWestMod.MOD_ID, "sound_loader");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment