-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix earmuff behavior for long-running sounds
Previously the earmuffs would only affect sounds when they started, i.e. a sound started before putting on earmuffs would never get attenuated while a sound started while wearing earmuffs would be attenuated forever.
- Loading branch information
Showing
7 changed files
with
134 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/blusunrize/immersiveengineering/client/EarmuffHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* BluSunrize | ||
* Copyright (c) 2024 | ||
* | ||
* This code is licensed under "Blu's License of Common Sense" | ||
* Details can be found in the license file in the root folder of this project | ||
*/ | ||
|
||
package blusunrize.immersiveengineering.client; | ||
|
||
import blusunrize.immersiveengineering.api.Lib; | ||
import blusunrize.immersiveengineering.common.config.IEClientConfig; | ||
import blusunrize.immersiveengineering.common.items.EarmuffsItem; | ||
import blusunrize.immersiveengineering.common.util.ItemNBTHelper; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.resources.sounds.SoundInstance; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.sounds.SoundSource; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.neoforged.api.distmarker.Dist; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.fml.common.Mod.EventBusSubscriber; | ||
import net.neoforged.fml.common.Mod.EventBusSubscriber.Bus; | ||
import net.neoforged.neoforge.event.TickEvent.ClientTickEvent; | ||
import net.neoforged.neoforge.event.TickEvent.Phase; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@EventBusSubscriber(value = Dist.CLIENT, modid = Lib.MODID, bus = Bus.FORGE) | ||
public class EarmuffHandler | ||
{ | ||
private static final Map<SoundSource, Float> LAST_MULTIPLIERS = makeDefaultMultipliers(); | ||
private static Set<ResourceLocation> IGNORED_SOUNDS = Set.of(); | ||
|
||
/** | ||
* Only the volume multiplier for ticking sounds is updated tick-by-tick. For non-ticking sounds (e.g. records) we | ||
* need to force the volume update when necessary. | ||
*/ | ||
@SubscribeEvent | ||
public static void updateEarmuffMultipliers(ClientTickEvent ev) | ||
{ | ||
if(ev.phase!=Phase.START||ClientUtils.mc().player==null) | ||
return; | ||
Map<SoundSource, Float> newMultipliers = makeDefaultMultipliers(); | ||
ItemStack earmuffs = EarmuffsItem.EARMUFF_GETTERS.getFrom(ClientUtils.mc().player); | ||
if(!earmuffs.isEmpty()) | ||
for(SoundSource source : SoundSource.values()) | ||
if(EarmuffsItem.affectedSoundCategories.contains(source.getName())) | ||
if(!ItemNBTHelper.getBoolean(earmuffs, "IE:Earmuffs:Cat_"+source.getName())) | ||
{ | ||
// The max call is just a last safeguard against overly high attenuation (see documentation on | ||
// EarmuffsItem.MIN_MULTIPLIER). The workbench config UI should limit the attenuation by itself. | ||
final float newMultiplier = Math.max( | ||
EarmuffsItem.MIN_MULTIPLIER, EarmuffsItem.getVolumeMod(earmuffs) | ||
); | ||
newMultipliers.put(source, newMultiplier); | ||
} | ||
for(SoundSource source : SoundSource.values()) | ||
if(LAST_MULTIPLIERS.get(source).floatValue()!=newMultipliers.get(source)) | ||
{ | ||
LAST_MULTIPLIERS.put(source, newMultipliers.get(source)); | ||
Minecraft.getInstance().getSoundManager().updateSourceVolume( | ||
source, Minecraft.getInstance().options.getSoundSourceVolume(source) | ||
); | ||
} | ||
} | ||
|
||
public static float getVolumeMultiplier(SoundInstance sound) | ||
{ | ||
if(IGNORED_SOUNDS.contains(sound.getLocation())) | ||
return 1; | ||
else | ||
return LAST_MULTIPLIERS.get(sound.getSource()); | ||
} | ||
|
||
public static void onConfigUpdate() | ||
{ | ||
IGNORED_SOUNDS = IEClientConfig.earDefenders_SoundBlacklist.get().stream() | ||
.map(ResourceLocation::tryParse) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private static Map<SoundSource, Float> makeDefaultMultipliers() | ||
{ | ||
Map<SoundSource, Float> result = new EnumMap<>(SoundSource.class); | ||
for(SoundSource type : SoundSource.values()) | ||
result.put(type, 1f); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 0 additions & 109 deletions
109
src/main/java/blusunrize/immersiveengineering/common/util/sound/IEMuffledSound.java
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
src/main/java/blusunrize/immersiveengineering/common/util/sound/IEMuffledTickableSound.java
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/main/java/blusunrize/immersiveengineering/mixin/coremods/client/SoundEngineMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* BluSunrize | ||
* Copyright (c) 2024 | ||
* | ||
* This code is licensed under "Blu's License of Common Sense" | ||
* Details can be found in the license file in the root folder of this project | ||
*/ | ||
|
||
package blusunrize.immersiveengineering.mixin.coremods.client; | ||
|
||
import blusunrize.immersiveengineering.client.EarmuffHandler; | ||
import com.llamalad7.mixinextras.injector.ModifyExpressionValue; | ||
import com.llamalad7.mixinextras.injector.ModifyReturnValue; | ||
import net.minecraft.client.resources.sounds.SoundInstance; | ||
import net.minecraft.client.sounds.SoundEngine; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(SoundEngine.class) | ||
public class SoundEngineMixin | ||
{ | ||
@ModifyExpressionValue( | ||
method = "play", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/sounds/SoundEngine;calculateVolume(FLnet/minecraft/sounds/SoundSource;)F") | ||
) | ||
public float adjustVolumeAtStart(float original, SoundInstance sound) | ||
{ | ||
return original*EarmuffHandler.getVolumeMultiplier(sound); | ||
} | ||
|
||
@ModifyReturnValue(method = "calculateVolume(Lnet/minecraft/client/resources/sounds/SoundInstance;)F", at = @At("TAIL")) | ||
public float adjustVolumeForEarmuffs(float original, SoundInstance sound) | ||
{ | ||
return original*EarmuffHandler.getVolumeMultiplier(sound); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters