mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-25 14:24:35 +00:00
Pretty satisfactory implentation now, approaching point of being able to merge into 8.0.
This commit is contained in:
1 parent
be3120800f
commit
9db096f9bf
20 files changed
+468
-1078
No files matched your search
@@ -1,36 +1,18 @@
|
||||
package mekanism.client.sound;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URL;
|
||||
import java.security.CodeSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import mekanism.api.Coord4D;
|
||||
import mekanism.api.MekanismConfig.client;
|
||||
import mekanism.client.HolidayManager;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.ObfuscatedNames;
|
||||
import mekanism.common.util.MekanismUtils;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.ISound;
|
||||
import net.minecraft.client.audio.ITickableSound;
|
||||
import net.minecraft.client.audio.PositionedSoundRecord;
|
||||
import net.minecraft.client.audio.SoundCategory;
|
||||
import net.minecraft.client.audio.SoundManager;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.world.ChunkEvent;
|
||||
import paulscode.sound.SoundSystem;
|
||||
import cpw.mods.fml.client.FMLClientHandler;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@@ -43,298 +25,80 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class SoundHandler
|
||||
{
|
||||
/** All the sound references in the Minecraft game. */
|
||||
public Map<Object, SoundMap> soundMaps = Collections.synchronizedMap(new HashMap<Object, SoundMap>());
|
||||
public Map<String, Map<String, IResettableSound>> soundMaps = new HashMap<String, Map<String, IResettableSound>>();
|
||||
|
||||
public static Map<ISound, String> invPlayingSounds;
|
||||
|
||||
public static Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
public static final String CHANNEL_TILE_DEFAULT = "tile";
|
||||
public static final String CHANNEL_JETPACK = "jetpack";
|
||||
public static final String CHANNEL_GASMASK = "gasMask";
|
||||
public static final String CHANNEL_FLAMETHROWER = "flamethrower";
|
||||
|
||||
/**
|
||||
* SoundHandler -- a class that handles all Sounds used by Mekanism.
|
||||
*/
|
||||
public SoundHandler()
|
||||
public enum Channel
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
JETPACK("jetpack"),
|
||||
GASMASK("gasMask"),
|
||||
FLAMETHROWER("flamethrower");
|
||||
|
||||
Mekanism.logger.info("Successfully set up SoundHandler.");
|
||||
String channelName;
|
||||
|
||||
private Channel(String name)
|
||||
{
|
||||
channelName = name;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return channelName;
|
||||
}
|
||||
}
|
||||
|
||||
public void preloadSounds()
|
||||
public boolean hasSound(EntityPlayer player, Channel channel)
|
||||
{
|
||||
CodeSource src = getClass().getProtectionDomain().getCodeSource();
|
||||
String corePath = src.getLocation().getFile().split("/mekanism/client")[0];
|
||||
List<String> listings = listFiles(corePath.replace("%20", " ").replace(".jar!", ".jar").replace("file:", ""), "assets/mekanism/sounds");
|
||||
String name = player.getCommandSenderName();
|
||||
Map<String, IResettableSound> map = getMap(name);
|
||||
IResettableSound sound = map.get(channel.getName());
|
||||
|
||||
for(String s : listings)
|
||||
return sound != null;
|
||||
}
|
||||
|
||||
public void addSound(EntityPlayer player, Channel channel, IResettableSound newSound, boolean replace)
|
||||
{
|
||||
String name = player.getCommandSenderName();
|
||||
Map<String, IResettableSound> map = getMap(name);
|
||||
IResettableSound sound = map.get(channel.getName());
|
||||
if(sound == null || replace)
|
||||
{
|
||||
if(s.contains("etc") || s.contains("holiday"))
|
||||
map.put(channel.getName(), newSound);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean playSound(EntityPlayer player, Channel channel)
|
||||
{
|
||||
String name = player.getCommandSenderName();
|
||||
Map<String, IResettableSound> map = getMap(name);
|
||||
IResettableSound sound = map.get(channel.getName());
|
||||
if(sound != null)
|
||||
{
|
||||
if(sound.isDonePlaying() && !getSoundMap().containsKey(sound))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(s.contains("/mekanism/sounds/"))
|
||||
{
|
||||
s = s.split("/mekanism/sounds/")[1];
|
||||
}
|
||||
|
||||
preloadSound(s);
|
||||
}
|
||||
|
||||
Mekanism.logger.info("Preloaded " + listings.size() + " object sounds.");
|
||||
|
||||
if(client.holidays)
|
||||
{
|
||||
listings = listFiles(corePath.replace("%20", " ").replace(".jar!", ".jar").replace("file:", ""), "assets/mekanism/sounds/holiday");
|
||||
|
||||
for(String s : listings)
|
||||
{
|
||||
if(s.contains("/mekanism/sounds/"))
|
||||
{
|
||||
s = s.split("/mekanism/sounds/")[1];
|
||||
}
|
||||
|
||||
if(!s.contains("holiday"))
|
||||
{
|
||||
s = "holiday/" + s;
|
||||
}
|
||||
|
||||
preloadSound(s);
|
||||
sound.reset();
|
||||
Mekanism.logger.info("Playing sound " + sound);
|
||||
playSound(sound);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<String> listFiles(String path, String s)
|
||||
public Map<String, IResettableSound> getMap(String name)
|
||||
{
|
||||
List<String> names = new ArrayList<String>();
|
||||
|
||||
File f = new File(path);
|
||||
|
||||
if(!f.exists())
|
||||
Map<String, IResettableSound> map = soundMaps.get(name);
|
||||
if(map == null)
|
||||
{
|
||||
return names;
|
||||
map = new HashMap<String, IResettableSound>();
|
||||
soundMaps.put(name, map);
|
||||
}
|
||||
|
||||
if(!f.isDirectory())
|
||||
{
|
||||
try {
|
||||
ZipInputStream zip = new ZipInputStream(new FileInputStream(path));
|
||||
|
||||
while(true)
|
||||
{
|
||||
ZipEntry e = zip.getNextEntry();
|
||||
|
||||
if(e == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
String name = e.getName();
|
||||
|
||||
if(name.contains(s) && name.endsWith(".ogg"))
|
||||
{
|
||||
names.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
zip.close();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else {
|
||||
f = new File(path + "/" + s);
|
||||
|
||||
for(File file : f.listFiles())
|
||||
{
|
||||
if(file.getPath().contains(s) && file.getName().endsWith(".ogg"))
|
||||
{
|
||||
names.add(file.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return names;
|
||||
return map;
|
||||
}
|
||||
|
||||
private void preloadSound(String sound)
|
||||
{
|
||||
String id = "pre_" + sound;
|
||||
URL url = getClass().getClassLoader().getResource("assets/mekanism/sounds/" + sound);
|
||||
|
||||
if(getSoundSystem() != null)
|
||||
{
|
||||
getSoundSystem().newSource(false, id, url, sound, true, 0, 0, 0, 0, 16F);
|
||||
getSoundSystem().activate(id);
|
||||
getSoundSystem().removeSource(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ticks the sound handler. Should be called every Minecraft tick, or 20 times per second.
|
||||
*/
|
||||
public void onTick()
|
||||
{
|
||||
synchronized(soundMaps)
|
||||
{
|
||||
if(getSoundSystem() != null)
|
||||
{
|
||||
if(!Mekanism.proxy.isPaused())
|
||||
{
|
||||
ArrayList<Sound> soundsToRemove = new ArrayList<Sound>();
|
||||
World world = FMLClientHandler.instance().getClient().theWorld;
|
||||
|
||||
if(FMLClientHandler.instance().getClient().thePlayer != null && world != null)
|
||||
{
|
||||
for(SoundMap map : soundMaps.values())
|
||||
{
|
||||
for(Sound sound : map)
|
||||
{
|
||||
if(!sound.update(world))
|
||||
{
|
||||
soundsToRemove.add(sound);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(sound.isPlaying)
|
||||
{
|
||||
sound.updateVolume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(Sound sound : soundsToRemove)
|
||||
{
|
||||
sound.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(SoundMap map : soundMaps.values())
|
||||
{
|
||||
map.stopLoops();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Mekanism.proxy.unloadSoundHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSound(Object ref, String channel)
|
||||
{
|
||||
if(soundMaps.get(ref) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
soundMaps.get(ref).remove(channel);
|
||||
|
||||
if(soundMaps.get(ref).isEmpty())
|
||||
{
|
||||
soundMaps.remove(ref);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerSound(Object ref, String channel, Sound sound)
|
||||
{
|
||||
if(soundMaps.get(ref) == null)
|
||||
{
|
||||
soundMaps.put(ref, new SoundMap(ref, channel, sound));
|
||||
return;
|
||||
}
|
||||
|
||||
soundMaps.get(ref).add(channel, sound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a sound object from a specific TileEntity, null if there is none.
|
||||
* @param tileEntity - the holder of the sound
|
||||
* @return Sound instance
|
||||
*/
|
||||
public SoundMap getMap(Object ref)
|
||||
{
|
||||
synchronized(soundMaps)
|
||||
{
|
||||
return soundMaps.get(ref);
|
||||
}
|
||||
}
|
||||
|
||||
public Sound getSound(Object ref, String channel)
|
||||
{
|
||||
if(soundMaps.get(ref) == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return soundMaps.get(ref).getSound(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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"
|
||||
* @return unique identifier
|
||||
*/
|
||||
public String getIdentifier(Object obj)
|
||||
{
|
||||
synchronized(soundMaps)
|
||||
{
|
||||
String toReturn = "Mekanism_" + getActiveSize() + "_" + new Random().nextInt(10000);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
}
|
||||
|
||||
public int getActiveSize()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
for(SoundMap map : soundMaps.values())
|
||||
{
|
||||
count += map.size();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, Coord4D object)
|
||||
{
|
||||
URL url = getClass().getClassLoader().getResource("assets/mekanism/sounds/" + soundPath);
|
||||
|
||||
if(url == null)
|
||||
{
|
||||
Mekanism.logger.info("Invalid sound file: " + soundPath);
|
||||
}
|
||||
|
||||
String s = getSoundSystem().quickPlay(false, url, soundPath, false, object.xCoord, object.yCoord, object.zCoord, 0, 16F);
|
||||
getSoundSystem().setVolume(s, getMasterVolume());
|
||||
}
|
||||
|
||||
public float getMasterVolume()
|
||||
{
|
||||
return FMLClientHandler.instance().getClient().gameSettings.getSoundLevel(SoundCategory.MASTER);
|
||||
}
|
||||
|
||||
public static SoundSystem getSoundSystem()
|
||||
{
|
||||
try {
|
||||
return (SoundSystem)MekanismUtils.getPrivateValue(getSoundManager(), SoundManager.class, ObfuscatedNames.SoundManager_sndSystem);
|
||||
} catch(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static SoundManager getSoundManager()
|
||||
{
|
||||
try {
|
||||
@@ -343,44 +107,32 @@ public class SoundHandler
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSystemLoaded()
|
||||
|
||||
//Fudge required because sound thread gets behind and the biMap crashes when rapidly toggling sounds.
|
||||
public static Map<ISound, String> getSoundMap()
|
||||
{
|
||||
if(invPlayingSounds == null)
|
||||
try {
|
||||
return (Boolean)MekanismUtils.getPrivateValue(getSoundManager(), net.minecraft.client.audio.SoundManager.class, new String[] {"loaded"});
|
||||
invPlayingSounds = (Map<ISound, String>)MekanismUtils.getPrivateValue(getSoundManager(), net.minecraft.client.audio.SoundManager.class, ObfuscatedNames.SoundManager_invPlayingSounds);
|
||||
} catch(Exception e) {
|
||||
return false;
|
||||
invPlayingSounds = null;
|
||||
}
|
||||
return invPlayingSounds;
|
||||
|
||||
}
|
||||
|
||||
public static boolean canRestartSound(ITickableSound sound)
|
||||
{
|
||||
return sound.isDonePlaying() && !getSoundMap().containsKey(sound);
|
||||
}
|
||||
|
||||
public static void playSound(String sound)
|
||||
{
|
||||
mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation(sound), 1.0F));
|
||||
playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation(sound), 1.0F));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onChunkUnload(ChunkEvent.Unload event)
|
||||
public static void playSound(ISound sound)
|
||||
{
|
||||
if(event.getChunk() != null)
|
||||
{
|
||||
for(Object obj : event.getChunk().chunkTileEntityMap.values())
|
||||
{
|
||||
if(obj instanceof TileEntity)
|
||||
{
|
||||
TileEntity tileEntity = (TileEntity)obj;
|
||||
|
||||
if(tileEntity instanceof IHasSound)
|
||||
{
|
||||
if(getMap(tileEntity) != null)
|
||||
{
|
||||
if(soundMaps.containsKey(tileEntity))
|
||||
{
|
||||
getMap(tileEntity).kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
mc.getSoundHandler().playSound(sound);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user