mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-25 14:24:35 +00:00
Improvements to fix for #15; smooth out sounds and detect ongoing attempts to muffle
The previous commit made the assumption that detection of sound being complete was fast enough that restarting a well-designed sound would be seamless -- this is not true. As such, had to change sounds to run for as long as machine is active on repeat with ongoing detection of muffling attempts. In addition, if sound was INITIALLY muffled, it would never have been unmuffled, so this also fixes that problem.
This commit is contained in:
1 parent
c78ae6dd1d
commit
f675590372
18 files changed
+332
-86
No files matched your search
@@ -1,30 +1,54 @@
|
||||
package mekanism.client.sound;
|
||||
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.Upgrade;
|
||||
import mekanism.common.base.IUpgradeTile;
|
||||
import mekanism.common.config.MekanismConfig;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.ISound;
|
||||
import net.minecraft.client.audio.PositionedSoundRecord;
|
||||
import net.minecraft.client.audio.*;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.client.ForgeHooksClient;
|
||||
import net.minecraftforge.client.event.sound.PlaySoundEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.EventPriority;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
// SoundHandler is the central point for sounds on Mek client side. Some sounds, such as hand-held items
|
||||
// are instantiated the first time user uses one and stays in memory (though muted) until the player
|
||||
// leaves the server. Block/tile-based sounds are periodically restarted while machine is active; this ensures
|
||||
// that thousands of idle machines aren't filling up memory with sounds that are rarely running.
|
||||
// SoundHandler is the central point for sounds on Mek client side. There are roughly three classes of sounds to deal
|
||||
// with:
|
||||
// 1. One-shot sounds like GUI interactions; play the sound and done
|
||||
//
|
||||
// As of 1.12.2, this design closely mirrors that of Minecraft sounds and should be generally compatible with any
|
||||
// other mod that interacts with the sound system (such as mufflers)
|
||||
// 2. Long-lived player item sounds (such as jetpacks): long-running sounds that may flip on/off quickly based
|
||||
// on user action. We follow the minecart model for these sounds; starting a sound and then muting when not in use.
|
||||
//
|
||||
// 3. Tile entity sounds: long-running, repeating sounds that run while a fixed tile is active. These are sounds that
|
||||
// users want to be able to mute effectively.
|
||||
//
|
||||
// All sounds, when initially started can be intercepted on the Forge event bus and wrapped by various muting/manipulation
|
||||
// mods. For item sounds, we don't want to them to be manipulated, since the flipping on/off is too prone to weird timing
|
||||
// issues. For long-running sounds, we need a way to honor these attempted manipulations, without allowing them to become
|
||||
// the permanent state of the sound (which is what happens by default). To accomplish this, we have our own wrapper that
|
||||
// intercepts new repeating sounds from Mek and ensures that they periodically poll for any muting/manipulation so that
|
||||
// it the object can dynamically adjust to conditions.
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class SoundHandler {
|
||||
private static IdentityHashMap<EntityPlayer, Boolean> jetpackSounds = new IdentityHashMap();
|
||||
private static IdentityHashMap<EntityPlayer, Boolean> gasmaskSounds = new IdentityHashMap<>();
|
||||
|
||||
private static Map<Long, ISound> soundMap = new HashMap<>();
|
||||
|
||||
public static void startSound(EntityPlayer player, String soundName) {
|
||||
ISound soundToPlay = null;
|
||||
|
||||
@@ -38,18 +62,217 @@ public class SoundHandler {
|
||||
}
|
||||
|
||||
if (soundToPlay != null) {
|
||||
Mekanism.logger.info("Starting sound object for {}: {}", player.getName(), soundName);
|
||||
Minecraft.getMinecraft().getSoundHandler().playSound(soundToPlay);
|
||||
}
|
||||
}
|
||||
|
||||
public static void playSound(SoundEvent sound)
|
||||
{
|
||||
public static void playSound(SoundEvent sound) {
|
||||
playSound(PositionedSoundRecord.getMasterRecord(sound, MekanismConfig.client.baseSoundVolume));
|
||||
}
|
||||
|
||||
public static void playSound(ISound sound)
|
||||
{
|
||||
public static void playSound(ISound sound) {
|
||||
Minecraft.getMinecraft().getSoundHandler().playSound(sound);
|
||||
}
|
||||
|
||||
public static ISound startTileSound(ResourceLocation soundLoc, float volume, BlockPos pos) {
|
||||
// First, check to see if there's already a sound playing at the desired location
|
||||
ISound s = soundMap.get(pos.toLong());
|
||||
if (s == null || !Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(s)) {
|
||||
// No sound playing, start one up - we assume that tile sounds will play until explicitly stopped
|
||||
s = new PositionedSoundRecord(soundLoc, SoundCategory.BLOCKS, volume, 1.0f,
|
||||
true, 0, ISound.AttenuationType.LINEAR,
|
||||
pos.getX() + 0.5f, pos.getY() + 0.5f, pos.getZ() + 0.5f);
|
||||
|
||||
// Force the underlying sound to get initialized
|
||||
// TODO: Understand what this is doing exactly
|
||||
s.createAccessor(Minecraft.getMinecraft().getSoundHandler());
|
||||
|
||||
// Start the sound
|
||||
playSound(s);
|
||||
|
||||
// N.B. By the time playSound returns, our expectation is that our wrapping-detector handler has fired
|
||||
// and dealt with any muting interceptions and, CRITICALLY, updated the soundMap with the final ISound.
|
||||
s = soundMap.get(pos.toLong());
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void stopTileSound(BlockPos pos) {
|
||||
long posKey = pos.toLong();
|
||||
ISound s = soundMap.get(posKey);
|
||||
if (s != null) {
|
||||
// TODO: Saw some code that suggests there is a soundmap MC already tracks; investigate further
|
||||
// and maybe we can avoid this dedicated soundmap
|
||||
Minecraft.getMinecraft().getSoundHandler().stopSound(s);
|
||||
soundMap.remove(posKey);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean IN_MUFFLED_CHECK = false;
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.LOWEST)
|
||||
public static void onTilePlaySound(PlaySoundEvent event) {
|
||||
// Ignore any sound event which is null or is happening in a muffled check
|
||||
ISound resultSound = event.getResultSound();
|
||||
if (resultSound == null || IN_MUFFLED_CHECK) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore any sound event outside this mod namespace
|
||||
ResourceLocation soundLoc = event.getSound().getSoundLocation();
|
||||
if (!soundLoc.getNamespace().equals("mekanism")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore any Mek sounds that aren't either a player or block event
|
||||
SoundCategory category = event.getSound().getCategory();
|
||||
if (!category.equals(SoundCategory.PLAYERS) && !category.equals(SoundCategory.BLOCKS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If this is a Mek player sound, unwrap any muffling that other mods may have attempted. I haven't
|
||||
// sorted out a good way to deal with long-lived, non-repeating, dynamic volume sounds -- something
|
||||
// to investigate in the future.
|
||||
if (event.getSound() instanceof PlayerSound) {
|
||||
event.setResultSound(event.getSound());
|
||||
return;
|
||||
}
|
||||
|
||||
// At this point, we've got a known block Mekanism sound. We want to re-wrap the original
|
||||
// using the (possibly) muffled sound as the initial volume. The TileSound will then periodically poll
|
||||
// to see if the volume should be adjusted
|
||||
resultSound = new TileSound(event.getSound(), resultSound.getVolume());
|
||||
event.setResultSound(resultSound);
|
||||
|
||||
|
||||
// Finally, update our soundMap so that we can actually have a shot at stopping this sound; note that we also
|
||||
// need to "unoffset" the sound position so that we build the correct key for the sound map
|
||||
// Aside: I really, really, wish Forge returned the final result sound as part of playSound :/
|
||||
BlockPos pos = new BlockPos(resultSound.getXPosF() - 0.5f, resultSound.getYPosF() - 0.5f, resultSound.getZPosF() -0.5);
|
||||
soundMap.put(pos.toLong(), resultSound);
|
||||
}
|
||||
|
||||
private static class TileSound implements ITickableSound {
|
||||
|
||||
private ISound original;
|
||||
private float volume;
|
||||
private boolean donePlaying = false;
|
||||
|
||||
// Choose an interval between 60-80 ticks (3-4 seconds) to check for muffling changes. We do this
|
||||
// to ensure that not every tile sound tries to run on the same tick and thus create
|
||||
// uneven spikes of CPU usage
|
||||
private int checkInterval = 60 + ThreadLocalRandom.current().nextInt(20);
|
||||
|
||||
private Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
TileSound(ISound original, float volume) {
|
||||
this.original = original;
|
||||
this.volume = volume;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
// Every configured interval, see if we need to adjust muffling
|
||||
if (mc.world.getTotalWorldTime() % checkInterval == 0) {
|
||||
|
||||
// Run the event bus with the original sound. Note that we must making sure to set the GLOBAL/STATIC
|
||||
// flag that ensures we don't wrap already muffled sounds. This is...NOT ideal and makes some
|
||||
// significant (hopefully well-informed) assumptions about locking/ordering of all these calls.
|
||||
IN_MUFFLED_CHECK = true;
|
||||
ISound s = ForgeHooksClient.playSound(mc.getSoundHandler().sndManager, original);
|
||||
IN_MUFFLED_CHECK = false;
|
||||
|
||||
if (s == this) {
|
||||
// No filtering done, use the original sound's volume
|
||||
volume = original.getVolume() * getMufflingFactor();
|
||||
} else if (s == null){
|
||||
// Full on mute; go ahead and shutdown
|
||||
donePlaying = true;
|
||||
} else {
|
||||
// Altered sound returned; adjust volume
|
||||
volume = s.getVolume() * getMufflingFactor();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private float getMufflingFactor() {
|
||||
// Pull the TE from the sound position and see if supports muffling upgrades. If it does, calculate what
|
||||
// percentage of the original volume should be muted
|
||||
TileEntity te = mc.world.getTileEntity(new BlockPos(original.getXPosF(), original.getYPosF(), original.getZPosF()));
|
||||
if (te != null && te instanceof IUpgradeTile && ((IUpgradeTile) te).getComponent().supports(Upgrade.MUFFLING)) {
|
||||
int mufflerCount = ((IUpgradeTile) te).getComponent().getUpgrades(Upgrade.MUFFLING);
|
||||
return Math.max(0.001f, 1.0f - (mufflerCount / (float)Upgrade.MUFFLING.getMax()));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDonePlaying() {
|
||||
return donePlaying;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getVolume() {
|
||||
return volume;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getSoundLocation() {
|
||||
return original.getSoundLocation();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public SoundEventAccessor createAccessor(net.minecraft.client.audio.SoundHandler handler) {
|
||||
return original.createAccessor(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getSound() {
|
||||
return original.getSound();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SoundCategory getCategory() {
|
||||
return original.getCategory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRepeat() {
|
||||
return original.canRepeat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRepeatDelay() {
|
||||
return original.getRepeatDelay();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getPitch() {
|
||||
return original.getPitch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXPosF() {
|
||||
return original.getXPosF();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYPosF() {
|
||||
return original.getYPosF();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getZPosF() {
|
||||
return original.getZPosF();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttenuationType getAttenuationType() {
|
||||
return original.getAttenuationType();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user