| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.google.gson.Gson;
|
| 5 | import com.google.gson.GsonBuilder;
|
| 6 | import com.google.gson.reflect.TypeToken;
|
| 7 | import com.mojang.blaze3d.audio.ListenerTransform;
|
| 8 | import com.mojang.logging.LogUtils;
|
| 9 | import java.io.IOException;
|
| 10 | import java.io.Reader;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.HashMap;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Map.Entry;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.SharedConstants;
|
| 19 | import net.minecraft.client.Camera;
|
| 20 | import net.minecraft.client.Options;
|
| 21 | import net.minecraft.client.resources.sounds.Sound;
|
| 22 | import net.minecraft.client.resources.sounds.SoundEventRegistration;
|
| 23 | import net.minecraft.client.resources.sounds.SoundEventRegistrationSerializer;
|
| 24 | import net.minecraft.client.resources.sounds.SoundInstance;
|
| 25 | import net.minecraft.client.resources.sounds.TickableSoundInstance;
|
| 26 | import net.minecraft.client.sounds.SoundManager.Preparations.1;
|
| 27 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 28 | import net.minecraft.network.chat.ComponentUtils;
|
| 29 | import net.minecraft.resources.Identifier;
|
| 30 | import net.minecraft.server.packs.resources.Resource;
|
| 31 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 32 | import net.minecraft.server.packs.resources.ResourceProvider;
|
| 33 | import net.minecraft.server.packs.resources.SimplePreparableReloadListener;
|
| 34 | import net.minecraft.sounds.SoundSource;
|
| 35 | import net.minecraft.util.GsonHelper;
|
| 36 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 37 | import net.minecraft.util.profiling.Zone;
|
| 38 | import net.minecraft.util.valueproviders.ConstantFloat;
|
| 39 | import org.jspecify.annotations.Nullable;
|
| 40 | import org.slf4j.Logger;
|
| 41 |
|
| 42 | @Environment(EnvType.CLIENT)
|
| 43 | public class SoundManager extends SimplePreparableReloadListener<SoundManager.Preparations> {
|
| 44 | public static final Identifier EMPTY_SOUND_LOCATION = Identifier.withDefaultNamespace("empty");
|
| 45 | public static final Sound EMPTY_SOUND = new Sound(EMPTY_SOUND_LOCATION, ConstantFloat.of(1.0F), ConstantFloat.of(1.0F), 1, Sound.Type.FILE, false, false, 16);
|
| 46 | public static final Identifier INTENTIONALLY_EMPTY_SOUND_LOCATION = Identifier.withDefaultNamespace("intentionally_empty");
|
| 47 | public static final WeighedSoundEvents INTENTIONALLY_EMPTY_SOUND_EVENT = new WeighedSoundEvents(INTENTIONALLY_EMPTY_SOUND_LOCATION, null);
|
| 48 | public static final Sound INTENTIONALLY_EMPTY_SOUND = new Sound(
|
| 49 | INTENTIONALLY_EMPTY_SOUND_LOCATION, ConstantFloat.of(1.0F), ConstantFloat.of(1.0F), 1, Sound.Type.FILE, false, false, 16
|
| 50 | );
|
| 51 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 52 | private static final String SOUNDS_PATH = "sounds.json";
|
| 53 | private static final Gson GSON = new GsonBuilder().registerTypeAdapter(SoundEventRegistration.class, new SoundEventRegistrationSerializer()).create();
|
| 54 | private static final TypeToken<Map<String, SoundEventRegistration>> SOUND_EVENT_REGISTRATION_TYPE = new TypeToken<Map<String, SoundEventRegistration>>() {};
|
| 55 | private final Map<Identifier, WeighedSoundEvents> registry = Maps.newHashMap();
|
| 56 | private final SoundEngine soundEngine;
|
| 57 | private final Map<Identifier, Resource> soundCache = new HashMap<>();
|
| 58 |
|
| 59 | public SoundManager(final Options options) {
|
| 60 | this.soundEngine = new SoundEngine(this, options, ResourceProvider.fromMap(this.soundCache));
|
| 61 | }
|
| 62 |
|
| 63 | protected SoundManager.Preparations prepare(final ResourceManager manager, final ProfilerFiller profiler) {
|
| 64 | SoundManager.Preparations preparations = new SoundManager.Preparations();
|
| 65 |
|
| 66 | try (Zone ignored = profiler.zone("list")) {
|
| 67 | preparations.listResources(manager);
|
| 68 | }
|
| 69 |
|
| 70 | for (String namespace : manager.getNamespaces()) {
|
| 71 | try (Zone ignored = profiler.zone(namespace)) {
|
| 72 | for (Resource resource : manager.getResourceStack(Identifier.fromNamespaceAndPath(namespace, "sounds.json"))) {
|
| 73 | profiler.push(resource.sourcePackId());
|
| 74 |
|
| 75 | try (Reader reader = resource.openAsReader()) {
|
| 76 | profiler.push("parse");
|
| 77 | Map<String, SoundEventRegistration> map = GsonHelper.fromJson(GSON, reader, SOUND_EVENT_REGISTRATION_TYPE);
|
| 78 | profiler.popPush("register");
|
| 79 |
|
| 80 | for (Entry<String, SoundEventRegistration> entry : map.entrySet()) {
|
| 81 | preparations.handleRegistration(Identifier.fromNamespaceAndPath(namespace, entry.getKey()), entry.getValue());
|
| 82 | }
|
| 83 |
|
| 84 | profiler.pop();
|
| 85 | } catch (RuntimeException e) {
|
| 86 | LOGGER.warn("Invalid {} in resourcepack: '{}'", "sounds.json", resource.sourcePackId(), e);
|
| 87 | }
|
| 88 |
|
| 89 | profiler.pop();
|
| 90 | }
|
| 91 | } catch (IOException var21) {
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return preparations;
|
| 96 | }
|
| 97 |
|
| 98 | protected void apply(final SoundManager.Preparations preparations, final ResourceManager manager, final ProfilerFiller profiler) {
|
| 99 | preparations.apply(this.registry, this.soundCache, this.soundEngine);
|
| 100 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 101 | for (Identifier location : this.registry.keySet()) {
|
| 102 | WeighedSoundEvents event = this.registry.get(location);
|
| 103 | if (!ComponentUtils.isTranslationResolvable(event.getSubtitle()) && BuiltInRegistries.SOUND_EVENT.containsKey(location)) {
|
| 104 | LOGGER.error("Missing subtitle {} for sound event: {}", event.getSubtitle(), location);
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | if (LOGGER.isDebugEnabled()) {
|
| 110 | for (Identifier location : this.registry.keySet()) {
|
| 111 | if (!BuiltInRegistries.SOUND_EVENT.containsKey(location)) {
|
| 112 | LOGGER.debug("Not having sound event for: {}", location);
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | this.soundEngine.reload();
|
| 118 | }
|
| 119 |
|
| 120 | public List<String> getAvailableSoundDevices() {
|
| 121 | return this.soundEngine.getAvailableSoundDevices();
|
| 122 | }
|
| 123 |
|
| 124 | public ListenerTransform getListenerTransform() {
|
| 125 | return this.soundEngine.getListenerTransform();
|
| 126 | }
|
| 127 |
|
| 128 | private static boolean validateSoundResource(final Sound sound, final Identifier eventLocation, final ResourceProvider resourceProvider) {
|
| 129 | Identifier soundPath = sound.getPath();
|
| 130 | if (resourceProvider.getResource(soundPath).isEmpty()) {
|
| 131 | LOGGER.warn("File {} does not exist, cannot add it to event {}", soundPath, eventLocation);
|
| 132 | return false;
|
| 133 | } else {
|
| 134 | return true;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | public @Nullable WeighedSoundEvents getSoundEvent(final Identifier location) {
|
| 139 | return this.registry.get(location);
|
| 140 | }
|
| 141 |
|
| 142 | public Collection<Identifier> getAvailableSounds() {
|
| 143 | return this.registry.keySet();
|
| 144 | }
|
| 145 |
|
| 146 | public void queueTickingSound(final TickableSoundInstance instance) {
|
| 147 | this.soundEngine.queueTickingSound(instance);
|
| 148 | }
|
| 149 |
|
| 150 | public SoundEngine.PlayResult play(final SoundInstance instance) {
|
| 151 | return this.soundEngine.play(instance);
|
| 152 | }
|
| 153 |
|
| 154 | public void playDelayed(final SoundInstance instance, final int delay) {
|
| 155 | this.soundEngine.playDelayed(instance, delay);
|
| 156 | }
|
| 157 |
|
| 158 | public void updateSource(final Camera camera) {
|
| 159 | this.soundEngine.updateSource(camera);
|
| 160 | }
|
| 161 |
|
| 162 | public void pauseAllExcept(final SoundSource... ignoredSources) {
|
| 163 | this.soundEngine.pauseAllExcept(ignoredSources);
|
| 164 | }
|
| 165 |
|
| 166 | public void stop() {
|
| 167 | this.soundEngine.stopAll();
|
| 168 | }
|
| 169 |
|
| 170 | public void destroy() {
|
| 171 | this.soundEngine.destroy();
|
| 172 | }
|
| 173 |
|
| 174 | public void emergencyShutdown() {
|
| 175 | this.soundEngine.emergencyShutdown();
|
| 176 | }
|
| 177 |
|
| 178 | public void tick(final boolean paused) {
|
| 179 | this.soundEngine.tick(paused);
|
| 180 | }
|
| 181 |
|
| 182 | public void resume() {
|
| 183 | this.soundEngine.resume();
|
| 184 | }
|
| 185 |
|
| 186 | public void refreshCategoryVolume(final SoundSource category) {
|
| 187 | this.soundEngine.refreshCategoryVolume(category);
|
| 188 | }
|
| 189 |
|
| 190 | public void stop(final SoundInstance soundInstance) {
|
| 191 | this.soundEngine.stop(soundInstance);
|
| 192 | }
|
| 193 |
|
| 194 | public void updateCategoryVolume(final SoundSource source, final float gain) {
|
| 195 | this.soundEngine.updateCategoryVolume(source, gain);
|
| 196 | }
|
| 197 |
|
| 198 | public boolean isActive(final SoundInstance instance) {
|
| 199 | return this.soundEngine.isActive(instance);
|
| 200 | }
|
| 201 |
|
| 202 | public void addListener(final SoundEventListener listener) {
|
| 203 | this.soundEngine.addEventListener(listener);
|
| 204 | }
|
| 205 |
|
| 206 | public void removeListener(final SoundEventListener listener) {
|
| 207 | this.soundEngine.removeEventListener(listener);
|
| 208 | }
|
| 209 |
|
| 210 | public void stop(final @Nullable Identifier sound, final @Nullable SoundSource source) {
|
| 211 | this.soundEngine.stop(sound, source);
|
| 212 | }
|
| 213 |
|
| 214 | public String getChannelDebugString() {
|
| 215 | return this.soundEngine.getChannelDebugString();
|
| 216 | }
|
| 217 |
|
| 218 | public void getSoundCacheDebugStats(final SoundBufferLibrary.DebugOutput output) {
|
| 219 | this.soundEngine.getSoundCacheDebugStats(output);
|
| 220 | }
|
| 221 |
|
| 222 | public void reload() {
|
| 223 | this.soundEngine.reload();
|
| 224 | }
|
| 225 |
|
| 226 | @Environment(EnvType.CLIENT)
|
| 227 | protected static class Preparations {
|
| 228 | private final Map<Identifier, WeighedSoundEvents> registry = Maps.newHashMap();
|
| 229 | private Map<Identifier, Resource> soundCache = Map.of();
|
| 230 |
|
| 231 | private void listResources(final ResourceManager resourceManager) {
|
| 232 | this.soundCache = Sound.SOUND_LISTER.listMatchingResources(resourceManager);
|
| 233 | }
|
| 234 |
|
| 235 | private void handleRegistration(final Identifier eventLocation, final SoundEventRegistration soundEventRegistration) {
|
| 236 | WeighedSoundEvents registration = this.registry.get(eventLocation);
|
| 237 | boolean missesRegistration = registration == null;
|
| 238 | if (missesRegistration || soundEventRegistration.isReplace()) {
|
| 239 | if (!missesRegistration) {
|
| 240 | SoundManager.LOGGER.debug("Replaced sound event location {}", eventLocation);
|
| 241 | }
|
| 242 |
|
| 243 | registration = new WeighedSoundEvents(eventLocation, soundEventRegistration.getSubtitle());
|
| 244 | this.registry.put(eventLocation, registration);
|
| 245 | }
|
| 246 |
|
| 247 | ResourceProvider cachedProvider = ResourceProvider.fromMap(this.soundCache);
|
| 248 |
|
| 249 | for (Sound sound : soundEventRegistration.getSounds()) {
|
| 250 | Identifier soundLocation = sound.getLocation();
|
| 251 | Weighted<Sound> weighted;
|
| 252 | switch (sound.getType()) {
|
| 253 | case FILE:
|
| 254 | if (!SoundManager.validateSoundResource(sound, eventLocation, cachedProvider)) {
|
| 255 | continue;
|
| 256 | }
|
| 257 |
|
| 258 | weighted = sound;
|
| 259 | break;
|
| 260 | case SOUND_EVENT:
|
| 261 | weighted = new 1(this, soundLocation, sound);
|
| 262 | break;
|
| 263 | default:
|
| 264 | throw new IllegalStateException("Unknown SoundEventRegistration type: " + sound.getType());
|
| 265 | }
|
| 266 |
|
| 267 | registration.addSound(weighted);
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | public void apply(final Map<Identifier, WeighedSoundEvents> registry, final Map<Identifier, Resource> soundCache, final SoundEngine engine) {
|
| 272 | registry.clear();
|
| 273 | soundCache.clear();
|
| 274 | soundCache.putAll(this.soundCache);
|
| 275 |
|
| 276 | for (Entry<Identifier, WeighedSoundEvents> entry : this.registry.entrySet()) {
|
| 277 | registry.put(entry.getKey(), entry.getValue());
|
| 278 | entry.getValue().preloadIfRequired(engine);
|
| 279 | }
|
| 280 | }
|
| 281 | }
|
| 282 | }
|
| 283 |
|