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-11-18 01:03:44 +01:00
|
|
|
import java.io.FileInputStream;
|
2013-05-14 17:34:26 +02:00
|
|
|
import java.net.URL;
|
2013-11-18 01:03:44 +01:00
|
|
|
import java.security.CodeSource;
|
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;
|
2013-11-18 01:03:44 +01:00
|
|
|
import java.util.List;
|
2013-04-14 17:55:51 +02:00
|
|
|
import java.util.Map;
|
2012-10-22 03:29:26 +02:00
|
|
|
import java.util.Random;
|
2013-11-18 01:03:44 +01:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
2012-10-22 03:29:26 +02:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
import mekanism.api.Coord4D;
|
2013-12-24 19:25:31 +01:00
|
|
|
import mekanism.client.HolidayManager;
|
|
|
|
import mekanism.client.MekanismClient;
|
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)
|
2014-03-08 02:00:25 +01:00
|
|
|
public class SoundHandler
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2013-03-22 02:28:36 +01:00
|
|
|
/** All the sound references in the Minecraft game. */
|
2013-12-04 22:53:58 +01:00
|
|
|
public Map<Object, Sound> sounds = Collections.synchronizedMap(new HashMap<Object, Sound>());
|
2014-03-08 02:00:25 +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;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-02 04:51:10 +01:00
|
|
|
public Minecraft mc = Minecraft.getMinecraft();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
/**
|
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);
|
2014-03-08 02:00:25 +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
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-10-31 19:26:04 +01:00
|
|
|
public void preloadSounds()
|
|
|
|
{
|
2013-11-18 01:03:44 +01:00
|
|
|
CodeSource src = getClass().getProtectionDomain().getCodeSource();
|
|
|
|
String corePath = src.getLocation().getFile().split("/mekanism/client")[0];
|
2013-11-18 01:07:18 +01:00
|
|
|
List<String> listings = listFiles(corePath.replace("%20", " ").replace(".jar!", ".jar").replace("file:", ""), "assets/mekanism/sound");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
for(String s : listings)
|
2013-11-17 19:39:40 +01:00
|
|
|
{
|
2013-12-24 19:25:31 +01:00
|
|
|
if(s.contains("etc") || s.contains("holiday"))
|
2013-11-18 01:16:40 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:16:40 +01:00
|
|
|
if(s.contains("/mekanism/sound/"))
|
|
|
|
{
|
|
|
|
s = s.split("/mekanism/sound/")[1];
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
preloadSound(s);
|
2013-11-17 19:39:40 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:25:52 +01:00
|
|
|
System.out.println("[Mekanism] Preloaded " + listings.size() + " object sounds.");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:07:18 +01:00
|
|
|
listings = listFiles(corePath.replace("%20", " ").replace(".jar!", ".jar").replace("file:", ""), "assets/mekanism/sound/etc");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
for(String s : listings)
|
2013-10-31 19:26:04 +01:00
|
|
|
{
|
2013-11-18 01:16:40 +01:00
|
|
|
if(s.contains("/mekanism/sound/etc/"))
|
|
|
|
{
|
|
|
|
s = s.split("/mekanism/sound/etc/")[1];
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
mc.sndManager.addSound("mekanism:etc/" + s);
|
2013-11-17 19:23:19 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:25:52 +01:00
|
|
|
System.out.println("[Mekanism] Initialized " + listings.size() + " sound effects.");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-24 19:25:31 +01:00
|
|
|
if(MekanismClient.holidays)
|
|
|
|
{
|
|
|
|
listings = listFiles(corePath.replace("%20", " ").replace(".jar!", ".jar").replace("file:", ""), "assets/mekanism/sound/holiday");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-24 19:25:31 +01:00
|
|
|
for(String s : listings)
|
|
|
|
{
|
|
|
|
if(s.contains("/mekanism/sound/"))
|
|
|
|
{
|
|
|
|
s = s.split("/mekanism/sound/")[1];
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-24 19:25:31 +01:00
|
|
|
if(!s.contains("holiday"))
|
|
|
|
{
|
|
|
|
s = "holiday/" + s;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-24 19:25:31 +01:00
|
|
|
preloadSound(s);
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 01:03:44 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
private List<String> listFiles(String path, String s)
|
|
|
|
{
|
|
|
|
List<String> names = new ArrayList<String>();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
File f = new File(path);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
if(!f.exists())
|
2013-11-17 19:39:40 +01:00
|
|
|
{
|
2013-11-18 01:03:44 +01:00
|
|
|
return names;
|
2013-11-17 19:39:40 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
if(!f.isDirectory())
|
2013-11-05 03:11:09 +01:00
|
|
|
{
|
2013-11-18 01:03:44 +01:00
|
|
|
try {
|
|
|
|
ZipInputStream zip = new ZipInputStream(new FileInputStream(path));
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
while(true)
|
2013-11-05 03:11:09 +01:00
|
|
|
{
|
2013-11-18 01:03:44 +01:00
|
|
|
ZipEntry e = zip.getNextEntry();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
if(e == null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
String name = e.getName();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
if(name.contains(s) && name.endsWith(".ogg"))
|
|
|
|
{
|
|
|
|
names.add(name);
|
|
|
|
}
|
2013-11-05 03:11:09 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
zip.close();
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
2013-11-05 03:11:09 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-17 19:23:19 +01:00
|
|
|
else {
|
2013-11-18 01:03:44 +01:00
|
|
|
f = new File(path + "/" + s);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
for(File file : f.listFiles())
|
|
|
|
{
|
|
|
|
if(file.getPath().contains(s) && file.getName().endsWith(".ogg"))
|
|
|
|
{
|
|
|
|
names.add(file.getName());
|
|
|
|
}
|
|
|
|
}
|
2013-11-17 19:23:19 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-18 01:03:44 +01:00
|
|
|
return names;
|
2013-10-31 19:26:04 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01: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-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;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-04-28 21:23:08 +02:00
|
|
|
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-12-04 22:53:58 +01:00
|
|
|
if(!sound.update(world))
|
2013-04-28 21:23:08 +02:00
|
|
|
{
|
|
|
|
soundsToRemove.add(sound);
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-22 04:03:54 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01: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();
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-04 22:53:58 +01:00
|
|
|
for(Sound sound : sounds.values())
|
|
|
|
{
|
|
|
|
if(sound.isPlaying)
|
|
|
|
{
|
2013-12-09 03:40:00 +01:00
|
|
|
sound.updateVolume();
|
2013-12-04 22:53:58 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-04-28 21:23:08 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01: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-12-04 22:53:58 +01:00
|
|
|
public Sound getFrom(Object obj)
|
2013-04-13 16:33:37 +02:00
|
|
|
{
|
|
|
|
synchronized(sounds)
|
|
|
|
{
|
2013-12-04 22:53:58 +01:00
|
|
|
return sounds.get(obj);
|
2012-11-07 21:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +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-12-04 22:53:58 +01:00
|
|
|
public void register(Object obj)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2013-12-04 22:53:58 +01:00
|
|
|
if(obj instanceof TileEntity && !(obj instanceof IHasSound))
|
2013-04-14 17:55:51 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-12-30 22:34:45 +01:00
|
|
|
synchronized(sounds)
|
2012-10-31 15:49:04 +01:00
|
|
|
{
|
2013-12-04 22:53:58 +01:00
|
|
|
if(getFrom(obj) == null)
|
2013-04-13 16:33:37 +02:00
|
|
|
{
|
2013-12-04 22:53:58 +01:00
|
|
|
if(obj instanceof TileEntity)
|
|
|
|
{
|
2013-12-24 19:25:31 +01:00
|
|
|
new TileSound(getIdentifier(), HolidayManager.filterSound(((IHasSound)obj).getSoundPath()), (TileEntity)obj);
|
2013-12-04 22:53:58 +01:00
|
|
|
}
|
2013-04-13 16:33:37 +02:00
|
|
|
}
|
2012-10-31 15:49:04 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +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);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
for(Sound sound : sounds.values())
|
|
|
|
{
|
|
|
|
if(sound.identifier.equals(toReturn))
|
|
|
|
{
|
|
|
|
return getIdentifier();
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-04-14 17:55:51 +02:00
|
|
|
return toReturn;
|
2012-12-30 22:34:45 +01:00
|
|
|
}
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01: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
|
|
|
|
*/
|
2013-12-20 22:09:09 +01:00
|
|
|
public void quickPlay(String soundPath, World world, Coord4D object)
|
2013-05-14 17:34:26 +02:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
URL url = getClass().getClassLoader().getResource("assets/mekanism/sound/" + soundPath);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-05-14 17:34:26 +02:00
|
|
|
if(url == null)
|
|
|
|
{
|
|
|
|
System.out.println("[Mekanism] Invalid sound file: " + soundPath);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
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);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-07-20 18:10:14 +02:00
|
|
|
public static SoundSystem getSoundSystem()
|
|
|
|
{
|
|
|
|
return Minecraft.getMinecraft().sndManager.sndSystem;
|
2013-05-14 17:34:26 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01: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;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-04-07 05:09:25 +02:00
|
|
|
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
|
|
|
}
|