2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.client;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
2012-11-07 21:01:46 +01:00
|
|
|
import java.util.ArrayList;
|
2012-12-30 22:34:45 +01:00
|
|
|
import java.util.Collections;
|
2012-11-07 21:01:46 +01:00
|
|
|
import java.util.List;
|
2012-10-22 03:29:26 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2013-02-22 04:03:54 +01:00
|
|
|
import mekanism.api.IActiveState;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.world.World;
|
2012-10-22 03:29:26 +02:00
|
|
|
import paulscode.sound.SoundSystem;
|
2012-12-20 22:53:39 +01:00
|
|
|
import cpw.mods.fml.client.FMLClientHandler;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-11-05 20:29:04 +01:00
|
|
|
* SoundHandler - a class that handles all Sounds used by Mekanism.
|
2012-10-22 03:29:26 +02:00
|
|
|
* Runs off of PaulsCode's SoundSystem.
|
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class SoundHandler
|
|
|
|
{
|
|
|
|
/** The PaulsCode SoundSystem */
|
|
|
|
public SoundSystem soundSystem;
|
|
|
|
|
2013-03-22 02:28:36 +01:00
|
|
|
/** All the sound references in the Minecraft game. */
|
2012-12-30 22:34:45 +01:00
|
|
|
public List<Sound> sounds = Collections.synchronizedList(new ArrayList<Sound>());
|
2012-11-07 21:01:46 +01:00
|
|
|
|
2013-03-22 02:28:36 +01:00
|
|
|
/** The current base volume Minecraft is using. */
|
2012-12-09 06:24:27 +01:00
|
|
|
public float masterVolume = 0;
|
|
|
|
|
2012-11-05 20:29:04 +01:00
|
|
|
/** SoundHandler -- a class that handles all Sounds used by Mekanism. */
|
2012-10-22 03:29:26 +02:00
|
|
|
public SoundHandler()
|
|
|
|
{
|
|
|
|
if(soundSystem == null)
|
|
|
|
{
|
|
|
|
soundSystem = FMLClientHandler.instance().instance().getClient().sndManager.sndSystem;
|
2012-11-05 20:29:04 +01:00
|
|
|
System.out.println("[Mekanism] Successfully set up SoundHandler.");
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 02:28:36 +01:00
|
|
|
/**
|
|
|
|
* Ticks the sound handler. Should be called every Minecraft tick, or 20 times per second.
|
|
|
|
*/
|
2012-11-07 21:01:46 +01:00
|
|
|
public void onTick()
|
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(sounds)
|
2012-11-07 21:01:46 +01:00
|
|
|
{
|
2013-02-22 04:03:54 +01:00
|
|
|
ArrayList<Sound> soundsToRemove = new ArrayList<Sound>();
|
2012-12-30 22:34:45 +01:00
|
|
|
for(Sound sound : sounds)
|
|
|
|
{
|
|
|
|
if(FMLClientHandler.instance().getClient().thePlayer != null && FMLClientHandler.instance().getClient().theWorld != null)
|
|
|
|
{
|
2013-02-25 21:02:05 +01:00
|
|
|
if(sound.isPlaying)
|
|
|
|
{
|
|
|
|
sound.updateVolume(FMLClientHandler.instance().getClient().thePlayer);
|
|
|
|
}
|
2013-02-22 04:03:54 +01:00
|
|
|
|
|
|
|
TileEntity tileEntity = FMLClientHandler.instance().getClient().theWorld.getBlockTileEntity(sound.xCoord, sound.yCoord, sound.zCoord);
|
|
|
|
|
2013-03-22 02:28:36 +01:00
|
|
|
if(tileEntity instanceof IActiveState && tileEntity instanceof IHasSound)
|
2013-02-22 04:03:54 +01:00
|
|
|
{
|
2013-03-22 02:28:36 +01:00
|
|
|
if(((IHasSound)tileEntity).getSound().soundPath != sound.soundPath)
|
2013-03-20 21:28:45 +01:00
|
|
|
{
|
|
|
|
soundsToRemove.add(sound);
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-22 04:03:54 +01:00
|
|
|
if(((IActiveState)tileEntity).getActive() != sound.isPlaying)
|
|
|
|
{
|
|
|
|
if(((IActiveState)tileEntity).getActive())
|
|
|
|
{
|
|
|
|
sound.play();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sound.stopLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 21:28:45 +01:00
|
|
|
else if(tileEntity == null)
|
|
|
|
{
|
|
|
|
soundsToRemove.add(sound);
|
|
|
|
}
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 21:28:45 +01:00
|
|
|
for(Sound sound : soundsToRemove)
|
|
|
|
{
|
|
|
|
System.out.println("[Mekanism] Removed dead sound '" + sound.identifier + ".'");
|
|
|
|
sound.remove();
|
2013-03-22 02:28:36 +01:00
|
|
|
|
|
|
|
TileEntity tileEntity = FMLClientHandler.instance().getClient().theWorld.getBlockTileEntity(sound.xCoord, sound.yCoord, sound.zCoord);
|
|
|
|
|
|
|
|
if(tileEntity instanceof IHasSound)
|
|
|
|
{
|
|
|
|
((IHasSound)tileEntity).removeSound();
|
|
|
|
}
|
2013-03-20 21:28:45 +01:00
|
|
|
}
|
|
|
|
|
2012-12-30 22:34:45 +01:00
|
|
|
masterVolume = FMLClientHandler.instance().getClient().gameSettings.soundVolume;
|
2012-11-07 21:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
/**
|
|
|
|
* Create and return an instance of a Sound.
|
2012-10-22 03:29:26 +02:00
|
|
|
* @param name - unique identifier for this sound
|
|
|
|
* @param path - bundled path to the sound effect
|
|
|
|
* @param world - world to play sound in
|
2013-03-11 18:49:01 +01:00
|
|
|
* @param x - x coordinate
|
|
|
|
* @param y - y coordinate
|
|
|
|
* @param z - z coordinate
|
2012-10-22 03:29:26 +02:00
|
|
|
* @return Sound instance
|
|
|
|
*/
|
2013-01-05 05:58:53 +01:00
|
|
|
public Sound getSound(String path, World world, int x, int y, int z)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(sounds)
|
2012-10-31 15:49:04 +01:00
|
|
|
{
|
2012-12-31 20:33:36 +01:00
|
|
|
return new Sound(getIdentifier(), path, world, x, y, z);
|
2012-10-31 15:49:04 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-30 22:34:45 +01:00
|
|
|
* Get a unique identifier for a sound effect instance by combining the mod's name,
|
|
|
|
* Mekanism, the new sound's unique position on the 'sounds' ArrayList, and a random
|
|
|
|
* number between 0 and 10,000. Example: "Mekanism_6_6123"
|
2012-10-22 03:29:26 +02:00
|
|
|
* @return unique identifier
|
|
|
|
*/
|
2012-12-30 22:34:45 +01:00
|
|
|
public String getIdentifier()
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(sounds)
|
|
|
|
{
|
2013-03-22 02:28:36 +01:00
|
|
|
return "Mekanism_" + sounds.size() + "_" + new Random().nextInt(10000);
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
}
|