    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 s1 = 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 soundpoolentry = new SoundPoolEntry(s1, url);
    081            ((List)this.nameToSoundPoolEntriesMapping.get(par1Str)).add(soundpoolentry);
    082            this.allSoundPoolEntries.add(soundpoolentry);
    083            ++this.numberOfSoundPoolEntries;
    084            return soundpoolentry;
    085        }
    086        catch (Exception malformedurlexception)
    087        {
    088            malformedurlexception.printStackTrace();
    089            throw new RuntimeException(malformedurlexception);
    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 list = (List)this.nameToSoundPoolEntriesMapping.get(par1Str);
    099        return list == null ? null : (SoundPoolEntry)list.get(this.rand.nextInt(list.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}