001package net.minecraft.client.audio;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.io.File;
006import java.net.MalformedURLException;
007import java.net.URL;
008import java.util.ArrayList;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012import java.util.Random;
013
014@SideOnly(Side.CLIENT)
015public class SoundPool
016{
017    /** The RNG used by SoundPool. */
018    private Random rand = new Random();
019
020    /**
021     * Maps a name (can be sound/newsound/streaming/music/newmusic) to a list of SoundPoolEntry's.
022     */
023    private Map nameToSoundPoolEntriesMapping = new HashMap();
024
025    /** A list of all SoundPoolEntries that have been loaded. */
026    private List allSoundPoolEntries = new ArrayList();
027
028    /**
029     * The number of soundPoolEntry's. This value is computed but never used (should be equal to
030     * allSoundPoolEntries.size()).
031     */
032    public int numberOfSoundPoolEntries = 0;
033    public boolean isGetRandomSound = true;
034
035    /**
036     * Adds a sound to this sound pool.
037     */
038    public SoundPoolEntry addSound(String par1Str, File par2File)
039    {
040        try 
041        {
042            return addSound(par1Str, par2File.toURI().toURL());
043        }
044        catch (MalformedURLException ex)
045        {
046            ex.printStackTrace();
047            throw new RuntimeException(ex);
048        }
049    }
050    
051    /**
052     * URL version of addSound, as the back-end sound engine has full support for various types of URLs
053     * 
054     * @param par1Str The name of the sound to add
055     * @param url The url of the sound resource
056     * @return A SoundPoolEntry for the newly added sound
057     */
058    public SoundPoolEntry addSound(String par1Str, URL url)
059    {
060        try
061        {
062            String var3 = par1Str;
063            par1Str = par1Str.substring(0, par1Str.indexOf("."));
064
065            if (this.isGetRandomSound)
066            {
067                while (Character.isDigit(par1Str.charAt(par1Str.length() - 1)))
068                {
069                    par1Str = par1Str.substring(0, par1Str.length() - 1);
070                }
071            }
072
073            par1Str = par1Str.replaceAll("/", ".");
074
075            if (!this.nameToSoundPoolEntriesMapping.containsKey(par1Str))
076            {
077                this.nameToSoundPoolEntriesMapping.put(par1Str, new ArrayList());
078            }
079
080            SoundPoolEntry var4 = new SoundPoolEntry(var3, url);
081            ((List)this.nameToSoundPoolEntriesMapping.get(par1Str)).add(var4);
082            this.allSoundPoolEntries.add(var4);
083            ++this.numberOfSoundPoolEntries;
084            return var4;
085        }
086        catch (Exception var5)
087        {
088            var5.printStackTrace();
089            throw new RuntimeException(var5);
090        }
091    }
092
093    /**
094     * gets a random sound from the specified (by name, can be sound/newsound/streaming/music/newmusic) sound pool.
095     */
096    public SoundPoolEntry getRandomSoundFromSoundPool(String par1Str)
097    {
098        List var2 = (List)this.nameToSoundPoolEntriesMapping.get(par1Str);
099        return var2 == null ? null : (SoundPoolEntry)var2.get(this.rand.nextInt(var2.size()));
100    }
101
102    /**
103     * Gets a random SoundPoolEntry.
104     */
105    public SoundPoolEntry getRandomSound()
106    {
107        return this.allSoundPoolEntries.isEmpty() ? null : (SoundPoolEntry)this.allSoundPoolEntries.get(this.rand.nextInt(this.allSoundPoolEntries.size()));
108    }
109}