2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.client;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
2013-05-14 17:34:26 +02:00
|
|
|
import java.net.URL;
|
2012-11-07 21:01:46 +01:00
|
|
|
import java.util.ArrayList;
|
2012-12-30 22:34:45 +01:00
|
|
|
import java.util.Collections;
|
2013-04-14 17:55:51 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2012-10-22 03:29:26 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2013-05-14 17:34:26 +02:00
|
|
|
import mekanism.api.Object3D;
|
2013-04-01 01:12:10 +02:00
|
|
|
import mekanism.common.IActiveState;
|
2013-04-28 21:23:08 +02:00
|
|
|
import mekanism.common.Mekanism;
|
2013-06-29 01:43:45 +02:00
|
|
|
import net.minecraft.client.audio.SoundManager;
|
2013-02-22 04:03:54 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.world.World;
|
2013-04-07 05:09:25 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.ForgeSubscribe;
|
|
|
|
import net.minecraftforge.event.world.ChunkEvent;
|
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;
|
2013-04-13 16:33:37 +02:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
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
|
|
|
|
*
|
|
|
|
*/
|
2013-04-13 16:33:37 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
2012-10-22 03:29:26 +02:00
|
|
|
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. */
|
2013-04-14 17:55:51 +02:00
|
|
|
public Map<TileEntity, Sound> sounds = Collections.synchronizedMap(new HashMap<TileEntity, 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;
|
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
/**
|
|
|
|
* SoundHandler -- a class that handles all Sounds used by Mekanism.
|
|
|
|
*/
|
2012-10-22 03:29:26 +02:00
|
|
|
public SoundHandler()
|
|
|
|
{
|
2013-06-29 01:43:45 +02:00
|
|
|
soundSystem = SoundManager.sndSystem;
|
2013-04-28 21:23:08 +02:00
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
|
|
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-04-28 21:23:08 +02:00
|
|
|
if(soundSystem == null)
|
|
|
|
{
|
2013-06-29 01:43:45 +02:00
|
|
|
soundSystem = SoundManager.sndSystem;
|
2013-04-28 21:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(soundSystem != null)
|
2012-12-30 22:34:45 +01:00
|
|
|
{
|
2013-04-28 21:23:08 +02:00
|
|
|
if(!Mekanism.proxy.isPaused())
|
2012-12-30 22:34:45 +01:00
|
|
|
{
|
2013-04-28 21:23:08 +02:00
|
|
|
ArrayList<Sound> soundsToRemove = new ArrayList<Sound>();
|
|
|
|
World world = FMLClientHandler.instance().getClient().theWorld;
|
|
|
|
for(Sound sound : sounds.values())
|
2013-04-14 17:55:51 +02:00
|
|
|
{
|
2013-04-28 21:23:08 +02:00
|
|
|
if(FMLClientHandler.instance().getClient().thePlayer != null && world != null)
|
2013-02-22 04:03:54 +01:00
|
|
|
{
|
2013-06-15 17:35:14 +02:00
|
|
|
if(!(sound.tileEntity instanceof IHasSound))
|
2013-02-22 04:03:54 +01:00
|
|
|
{
|
2013-04-28 21:23:08 +02:00
|
|
|
soundsToRemove.add(sound);
|
|
|
|
continue;
|
2013-02-22 04:03:54 +01:00
|
|
|
}
|
2013-04-28 21:23:08 +02:00
|
|
|
else if(world.getBlockTileEntity(sound.tileEntity.xCoord, sound.tileEntity.yCoord, sound.tileEntity.zCoord) != sound.tileEntity)
|
|
|
|
{
|
|
|
|
soundsToRemove.add(sound);
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-28 22:04:16 +02:00
|
|
|
else if(!((IHasSound)sound.tileEntity).getSoundPath().equals(sound.soundPath))
|
2013-04-28 21:23:08 +02:00
|
|
|
{
|
|
|
|
soundsToRemove.add(sound);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(sound.tileEntity instanceof IActiveState)
|
|
|
|
{
|
|
|
|
if(((IActiveState)sound.tileEntity).getActive() != sound.isPlaying)
|
|
|
|
{
|
|
|
|
if(((IActiveState)sound.tileEntity).getActive())
|
|
|
|
{
|
|
|
|
sound.play();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sound.stopLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sound.isPlaying)
|
|
|
|
{
|
|
|
|
sound.updateVolume(FMLClientHandler.instance().getClient().thePlayer);
|
2013-02-22 04:03:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 16:33:37 +02:00
|
|
|
|
2013-04-28 21:23:08 +02:00
|
|
|
for(Sound sound : soundsToRemove)
|
2013-04-13 16:33:37 +02:00
|
|
|
{
|
2013-04-28 21:23:08 +02:00
|
|
|
sound.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
masterVolume = FMLClientHandler.instance().getClient().gameSettings.soundVolume;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(Sound sound : sounds.values())
|
|
|
|
{
|
|
|
|
if(sound.isPlaying)
|
|
|
|
{
|
|
|
|
sound.stopLoop();
|
|
|
|
}
|
2013-03-20 21:28:45 +01:00
|
|
|
}
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-28 21:23:08 +02:00
|
|
|
else {
|
|
|
|
Mekanism.proxy.unloadSoundHandler();
|
2013-04-13 16:33:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:57:55 +02:00
|
|
|
/**
|
|
|
|
* Gets a sound object from a specific TileEntity, null if there is none.
|
|
|
|
* @param tileEntity - the holder of the sound
|
|
|
|
* @return Sound instance
|
|
|
|
*/
|
2013-04-13 16:33:37 +02:00
|
|
|
public Sound getFrom(TileEntity tileEntity)
|
|
|
|
{
|
|
|
|
synchronized(sounds)
|
|
|
|
{
|
2013-04-14 17:55:51 +02:00
|
|
|
return sounds.get(tileEntity);
|
2012-11-07 21:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
/**
|
|
|
|
* Create and return an instance of a Sound.
|
2013-04-21 06:34:54 +02:00
|
|
|
* @param tileEntity - the holder of this sound.
|
2012-10-22 03:29:26 +02:00
|
|
|
* @return Sound instance
|
|
|
|
*/
|
2013-04-14 17:55:51 +02:00
|
|
|
public void register(TileEntity tileEntity)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2013-04-14 17:55:51 +02:00
|
|
|
if(!(tileEntity instanceof IHasSound))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(sounds)
|
2012-10-31 15:49:04 +01:00
|
|
|
{
|
2013-04-13 16:33:37 +02:00
|
|
|
if(getFrom(tileEntity) == null)
|
|
|
|
{
|
2013-04-14 17:55:51 +02:00
|
|
|
new Sound(getIdentifier(), ((IHasSound)tileEntity).getSoundPath(), tileEntity);
|
2013-04-13 16:33:37 +02:00
|
|
|
}
|
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-04-14 17:55:51 +02:00
|
|
|
String toReturn = "Mekanism_" + sounds.size() + "_" + new Random().nextInt(10000);
|
|
|
|
|
|
|
|
for(Sound sound : sounds.values())
|
|
|
|
{
|
|
|
|
if(sound.identifier.equals(toReturn))
|
|
|
|
{
|
|
|
|
return getIdentifier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
2013-04-07 05:09:25 +02:00
|
|
|
|
2013-05-14 17:34:26 +02:00
|
|
|
/**
|
|
|
|
* Plays a sound in a specific location.
|
|
|
|
* @param soundPath - sound path to play
|
|
|
|
* @param world - world to play in
|
|
|
|
* @param object - location to play
|
|
|
|
*/
|
|
|
|
public void quickPlay(String soundPath, World world, Object3D object)
|
|
|
|
{
|
|
|
|
URL url = getClass().getClassLoader().getResource("mods/mekanism/sound/" + soundPath);
|
|
|
|
|
|
|
|
if(url == null)
|
|
|
|
{
|
|
|
|
System.out.println("[Mekanism] Invalid sound file: " + soundPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
String s = soundSystem.quickPlay(false, url, soundPath, false, object.xCoord, object.yCoord, object.zCoord, 0, 16F);
|
|
|
|
soundSystem.setVolume(s, masterVolume);
|
|
|
|
}
|
|
|
|
|
2013-04-07 05:09:25 +02:00
|
|
|
@ForgeSubscribe
|
|
|
|
public void onChunkUnload(ChunkEvent.Unload event)
|
|
|
|
{
|
|
|
|
if(event.getChunk() != null)
|
|
|
|
{
|
|
|
|
for(Object obj : event.getChunk().chunkTileEntityMap.values())
|
|
|
|
{
|
|
|
|
if(obj instanceof TileEntity)
|
|
|
|
{
|
|
|
|
TileEntity tileEntity = (TileEntity)obj;
|
|
|
|
|
|
|
|
if(tileEntity instanceof IHasSound)
|
|
|
|
{
|
2013-04-13 16:33:37 +02:00
|
|
|
if(getFrom(tileEntity) != null)
|
2013-04-07 05:09:25 +02:00
|
|
|
{
|
2013-04-14 17:55:51 +02:00
|
|
|
if(sounds.containsKey(tileEntity))
|
2013-04-13 16:33:37 +02:00
|
|
|
{
|
|
|
|
getFrom(tileEntity).remove();
|
|
|
|
}
|
2013-04-07 05:09:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|