2013-08-27 00:57:08 +02:00
|
|
|
package mekanism.client.sound;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
2013-10-31 19:26:04 +01:00
|
|
|
import java.io.File;
|
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-07-20 18:10:14 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
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
|
|
|
|
{
|
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-04-28 21:23:08 +02:00
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
2013-10-31 19:26:04 +01:00
|
|
|
|
2013-04-28 21:23:08 +02:00
|
|
|
System.out.println("[Mekanism] Successfully set up SoundHandler.");
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
|
2013-10-31 19:26:04 +01:00
|
|
|
public void preloadSounds()
|
|
|
|
{
|
|
|
|
URL url = getClass().getClassLoader().getResource("assets/mekanism/sound");
|
|
|
|
File dir = new File(url.getFile());
|
|
|
|
|
|
|
|
for(File file : dir.listFiles())
|
|
|
|
{
|
|
|
|
if(!file.isDirectory() && file.getName().endsWith(".ogg"))
|
|
|
|
{
|
|
|
|
preloadSound(file.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 19:46:14 +01:00
|
|
|
private void preloadSound(String sound)
|
2013-10-31 19:26:04 +01:00
|
|
|
{
|
|
|
|
String id = "pre_" + sound;
|
|
|
|
URL url = getClass().getClassLoader().getResource("assets/mekanism/sound/" + sound);
|
|
|
|
|
|
|
|
if(getSoundSystem() != null)
|
|
|
|
{
|
|
|
|
getSoundSystem().newSource(false, id, url, sound, true, 0, 0, 0, 0, 16F);
|
|
|
|
getSoundSystem().activate(id);
|
|
|
|
getSoundSystem().removeSource(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-20 18:10:14 +02:00
|
|
|
if(getSoundSystem() != 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)
|
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
URL url = getClass().getClassLoader().getResource("assets/mekanism/sound/" + soundPath);
|
2013-05-14 17:34:26 +02:00
|
|
|
|
|
|
|
if(url == null)
|
|
|
|
{
|
|
|
|
System.out.println("[Mekanism] Invalid sound file: " + soundPath);
|
|
|
|
}
|
|
|
|
|
2013-07-20 18:10:14 +02:00
|
|
|
String s = getSoundSystem().quickPlay(false, url, soundPath, false, object.xCoord, object.yCoord, object.zCoord, 0, 16F);
|
|
|
|
getSoundSystem().setVolume(s, masterVolume);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SoundSystem getSoundSystem()
|
|
|
|
{
|
|
|
|
return Minecraft.getMinecraft().sndManager.sndSystem;
|
2013-05-14 17:34:26 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|