| 1 | package net.minecraft.client.audio;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.gson.Gson;
|
| 5 | import com.google.gson.GsonBuilder;
|
| 6 | import java.io.FileNotFoundException;
|
| 7 | import java.io.IOException;
|
| 8 | import java.io.InputStream;
|
| 9 | import java.io.InputStreamReader;
|
| 10 | import java.io.Reader;
|
| 11 | import java.lang.reflect.ParameterizedType;
|
| 12 | import java.lang.reflect.Type;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Random;
|
| 16 | import java.util.Map.Entry;
|
| 17 | import net.minecraft.client.resources.IResource;
|
| 18 | import net.minecraft.client.resources.IResourceManager;
|
| 19 | import net.minecraft.client.resources.IResourceManagerReloadListener;
|
| 20 | import net.minecraft.client.settings.GameSettings;
|
| 21 | import net.minecraft.entity.player.EntityPlayer;
|
| 22 | import net.minecraft.util.ITickable;
|
| 23 | import net.minecraft.util.ResourceLocation;
|
| 24 | import org.apache.commons.io.IOUtils;
|
| 25 | import org.apache.commons.lang3.ArrayUtils;
|
| 26 | import org.apache.logging.log4j.LogManager;
|
| 27 | import org.apache.logging.log4j.Logger;
|
| 28 |
|
| 29 | public class SoundHandler implements IResourceManagerReloadListener, ITickable
|
| 30 | {
|
| 31 | private static final Logger logger = LogManager.getLogger();
|
| 32 | private static final Gson GSON = (new GsonBuilder()).registerTypeAdapter(SoundList.class, new SoundListSerializer()).create();
|
| 33 | private static final ParameterizedType TYPE = new ParameterizedType()
|
| 34 | {
|
| 35 | public Type[] getActualTypeArguments()
|
| 36 | {
|
| 37 | return new Type[] {String.class, SoundList.class};
|
| 38 | }
|
| 39 | public Type getRawType()
|
| 40 | {
|
| 41 | return Map.class;
|
| 42 | }
|
| 43 | public Type getOwnerType()
|
| 44 | {
|
| 45 | return null;
|
| 46 | }
|
| 47 | };
|
| 48 | public static final SoundPoolEntry missing_sound = new SoundPoolEntry(new ResourceLocation("meta:missing_sound"), 0.0D, 0.0D, false);
|
| 49 | private final SoundRegistry sndRegistry = new SoundRegistry();
|
| 50 | private final SoundManager sndManager;
|
| 51 | private final IResourceManager mcResourceManager;
|
| 52 |
|
| 53 | public SoundHandler(IResourceManager manager, GameSettings gameSettingsIn)
|
| 54 | {
|
| 55 | this.mcResourceManager = manager;
|
| 56 | this.sndManager = new SoundManager(this, gameSettingsIn);
|
| 57 | }
|
| 58 |
|
| 59 | public void onResourceManagerReload(IResourceManager resourceManager)
|
| 60 | {
|
| 61 | this.sndManager.reloadSoundSystem();
|
| 62 | this.sndRegistry.clearMap();
|
| 63 |
|
| 64 | for (String s : resourceManager.getResourceDomains())
|
| 65 | {
|
| 66 | try
|
| 67 | {
|
| 68 | for (IResource iresource : resourceManager.getAllResources(new ResourceLocation(s, "sounds.json")))
|
| 69 | {
|
| 70 | try
|
| 71 | {
|
| 72 | Map<String, SoundList> map = this.getSoundMap(iresource.getInputStream());
|
| 73 |
|
| 74 | for (Entry<String, SoundList> entry : map.entrySet())
|
| 75 | {
|
| 76 | this.loadSoundResource(new ResourceLocation(s, (String)entry.getKey()), (SoundList)entry.getValue());
|
| 77 | }
|
| 78 | }
|
| 79 | catch (RuntimeException runtimeexception)
|
| 80 | {
|
| 81 | logger.warn((String)"Invalid sounds.json", (Throwable)runtimeexception);
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 | catch (IOException var11)
|
| 86 | {
|
| 87 | ;
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | protected Map<String, SoundList> getSoundMap(InputStream stream)
|
| 93 | {
|
| 94 | Map map;
|
| 95 |
|
| 96 | try
|
| 97 | {
|
| 98 | map = (Map)GSON.fromJson((Reader)(new InputStreamReader(stream)), TYPE);
|
| 99 | }
|
| 100 | finally
|
| 101 | {
|
| 102 | IOUtils.closeQuietly(stream);
|
| 103 | }
|
| 104 |
|
| 105 | return map;
|
| 106 | }
|
| 107 |
|
| 108 | private void loadSoundResource(ResourceLocation location, SoundList sounds)
|
| 109 | {
|
| 110 | boolean flag = !this.sndRegistry.containsKey(location);
|
| 111 | SoundEventAccessorComposite soundeventaccessorcomposite;
|
| 112 |
|
| 113 | if (!flag && !sounds.canReplaceExisting())
|
| 114 | {
|
| 115 | soundeventaccessorcomposite = (SoundEventAccessorComposite)this.sndRegistry.getObject(location);
|
| 116 | }
|
| 117 | else
|
| 118 | {
|
| 119 | if (!flag)
|
| 120 | {
|
| 121 | logger.debug("Replaced sound event location {}", new Object[] {location});
|
| 122 | }
|
| 123 |
|
| 124 | soundeventaccessorcomposite = new SoundEventAccessorComposite(location, 1.0D, 1.0D, sounds.getSoundCategory());
|
| 125 | this.sndRegistry.registerSound(soundeventaccessorcomposite);
|
| 126 | }
|
| 127 |
|
| 128 | for (final SoundList.SoundEntry soundlist$soundentry : sounds.getSoundList())
|
| 129 | {
|
| 130 | String s = soundlist$soundentry.getSoundEntryName();
|
| 131 | ResourceLocation resourcelocation = new ResourceLocation(s);
|
| 132 | final String s1 = s.contains(":") ? resourcelocation.getResourceDomain() : location.getResourceDomain();
|
| 133 | ISoundEventAccessor<SoundPoolEntry> isoundeventaccessor;
|
| 134 |
|
| 135 | switch (soundlist$soundentry.getSoundEntryType())
|
| 136 | {
|
| 137 | case FILE:
|
| 138 | ResourceLocation resourcelocation1 = new ResourceLocation(s1, "sounds/" + resourcelocation.getResourcePath() + ".ogg");
|
| 139 | InputStream inputstream = null;
|
| 140 |
|
| 141 | try
|
| 142 | {
|
| 143 | inputstream = this.mcResourceManager.getResource(resourcelocation1).getInputStream();
|
| 144 | }
|
| 145 | catch (FileNotFoundException var18)
|
| 146 | {
|
| 147 | logger.warn("File {} does not exist, cannot add it to event {}", new Object[] {resourcelocation1, location});
|
| 148 | continue;
|
| 149 | }
|
| 150 | catch (IOException ioexception)
|
| 151 | {
|
| 152 | logger.warn((String)("Could not load sound file " + resourcelocation1 + ", cannot add it to event " + location), (Throwable)ioexception);
|
| 153 | continue;
|
| 154 | }
|
| 155 | finally
|
| 156 | {
|
| 157 | IOUtils.closeQuietly(inputstream);
|
| 158 | }
|
| 159 |
|
| 160 | isoundeventaccessor = new SoundEventAccessor(new SoundPoolEntry(resourcelocation1, (double)soundlist$soundentry.getSoundEntryPitch(), (double)soundlist$soundentry.getSoundEntryVolume(), soundlist$soundentry.isStreaming()), soundlist$soundentry.getSoundEntryWeight());
|
| 161 | break;
|
| 162 |
|
| 163 | case SOUND_EVENT:
|
| 164 | isoundeventaccessor = new ISoundEventAccessor<SoundPoolEntry>()
|
| 165 | {
|
| 166 | final ResourceLocation field_148726_a = new ResourceLocation(s1, soundlist$soundentry.getSoundEntryName());
|
| 167 | public int getWeight()
|
| 168 | {
|
| 169 | SoundEventAccessorComposite soundeventaccessorcomposite1 = (SoundEventAccessorComposite)SoundHandler.this.sndRegistry.getObject(this.field_148726_a);
|
| 170 | return soundeventaccessorcomposite1 == null ? 0 : soundeventaccessorcomposite1.getWeight();
|
| 171 | }
|
| 172 | public SoundPoolEntry cloneEntry()
|
| 173 | {
|
| 174 | SoundEventAccessorComposite soundeventaccessorcomposite1 = (SoundEventAccessorComposite)SoundHandler.this.sndRegistry.getObject(this.field_148726_a);
|
| 175 | return soundeventaccessorcomposite1 == null ? SoundHandler.missing_sound : soundeventaccessorcomposite1.cloneEntry();
|
| 176 | }
|
| 177 | };
|
| 178 |
|
| 179 | break;
|
| 180 | default:
|
| 181 | throw new IllegalStateException("IN YOU FACE");
|
| 182 | }
|
| 183 |
|
| 184 | soundeventaccessorcomposite.addSoundToEventPool(isoundeventaccessor);
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | public SoundEventAccessorComposite getSound(ResourceLocation location)
|
| 189 | {
|
| 190 | return (SoundEventAccessorComposite)this.sndRegistry.getObject(location);
|
| 191 | }
|
| 192 |
|
| 193 | /**
|
| 194 | * Play a sound
|
| 195 | */
|
| 196 | public void playSound(ISound sound)
|
| 197 | {
|
| 198 | this.sndManager.playSound(sound);
|
| 199 | }
|
| 200 |
|
| 201 | /**
|
| 202 | * Plays the sound in n ticks
|
| 203 | */
|
| 204 | public void playDelayedSound(ISound sound, int delay)
|
| 205 | {
|
| 206 | this.sndManager.playDelayedSound(sound, delay);
|
| 207 | }
|
| 208 |
|
| 209 | public void setListener(EntityPlayer player, float p_147691_2_)
|
| 210 | {
|
| 211 | this.sndManager.setListener(player, p_147691_2_);
|
| 212 | }
|
| 213 |
|
| 214 | public void pauseSounds()
|
| 215 | {
|
| 216 | this.sndManager.pauseAllSounds();
|
| 217 | }
|
| 218 |
|
| 219 | public void stopSounds()
|
| 220 | {
|
| 221 | this.sndManager.stopAllSounds();
|
| 222 | }
|
| 223 |
|
| 224 | public void unloadSounds()
|
| 225 | {
|
| 226 | this.sndManager.unloadSoundSystem();
|
| 227 | }
|
| 228 |
|
| 229 | /**
|
| 230 | * Like the old updateEntity(), except more generic.
|
| 231 | */
|
| 232 | public void update()
|
| 233 | {
|
| 234 | this.sndManager.updateAllSounds();
|
| 235 | }
|
| 236 |
|
| 237 | public void resumeSounds()
|
| 238 | {
|
| 239 | this.sndManager.resumeAllSounds();
|
| 240 | }
|
| 241 |
|
| 242 | public void setSoundLevel(SoundCategory category, float volume)
|
| 243 | {
|
| 244 | if (category == SoundCategory.MASTER && volume <= 0.0F)
|
| 245 | {
|
| 246 | this.stopSounds();
|
| 247 | }
|
| 248 |
|
| 249 | this.sndManager.setSoundCategoryVolume(category, volume);
|
| 250 | }
|
| 251 |
|
| 252 | public void stopSound(ISound p_147683_1_)
|
| 253 | {
|
| 254 | this.sndManager.stopSound(p_147683_1_);
|
| 255 | }
|
| 256 |
|
| 257 | /**
|
| 258 | * Returns a random sound from one or more categories
|
| 259 | */
|
| 260 | public SoundEventAccessorComposite getRandomSoundFromCategories(SoundCategory... categories)
|
| 261 | {
|
| 262 | List<SoundEventAccessorComposite> list = Lists.<SoundEventAccessorComposite>newArrayList();
|
| 263 |
|
| 264 | for (ResourceLocation resourcelocation : this.sndRegistry.getKeys())
|
| 265 | {
|
| 266 | SoundEventAccessorComposite soundeventaccessorcomposite = (SoundEventAccessorComposite)this.sndRegistry.getObject(resourcelocation);
|
| 267 |
|
| 268 | if (ArrayUtils.contains(categories, soundeventaccessorcomposite.getSoundCategory()))
|
| 269 | {
|
| 270 | list.add(soundeventaccessorcomposite);
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | if (list.isEmpty())
|
| 275 | {
|
| 276 | return null;
|
| 277 | }
|
| 278 | else
|
| 279 | {
|
| 280 | return (SoundEventAccessorComposite)list.get((new Random()).nextInt(list.size()));
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | public boolean isSoundPlaying(ISound sound)
|
| 285 | {
|
| 286 | return this.sndManager.isSoundPlaying(sound);
|
| 287 | }
|
| 288 | }
|
| 289 |
|