|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.dynamic_sounds; |
| 2 | + |
| 3 | +import com.google.common.cache.CacheBuilder; |
| 4 | +import com.google.common.cache.RemovalNotification; |
| 5 | +import com.mojang.blaze3d.audio.SoundBuffer; |
| 6 | +import net.minecraft.client.sounds.SoundBufferLibrary; |
| 7 | +import net.minecraft.resources.ResourceLocation; |
| 8 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 9 | +import org.embeddedt.modernfix.dynamicresources.DynamicSoundHelpers; |
| 10 | +import org.embeddedt.modernfix.ModernFix; |
| 11 | +import org.spongepowered.asm.mixin.Final; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.Mutable; |
| 14 | +import org.spongepowered.asm.mixin.Shadow; |
| 15 | + |
| 16 | +import java.util.concurrent.CompletableFuture; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +@Mixin(SoundBufferLibrary.class) |
| 21 | +@ClientOnlyMixin |
| 22 | +public abstract class SoundBufferLibraryMixin { |
| 23 | + |
| 24 | + private static final boolean debugDynamicSoundLoading = Boolean.getBoolean("modernfix.debugDynamicSoundLoading"); |
| 25 | + |
| 26 | + @Shadow @Final @Mutable |
| 27 | + private Map<ResourceLocation, CompletableFuture<SoundBuffer>> cache = CacheBuilder.newBuilder() |
| 28 | + .expireAfterAccess(DynamicSoundHelpers.MAX_SOUND_LIFETIME_SECS, TimeUnit.SECONDS) |
| 29 | + // Excessive use of type hinting due to it assuming Object as the broadest correct type |
| 30 | + .<ResourceLocation, CompletableFuture<SoundBuffer>>removalListener(this::onSoundRemoval) |
| 31 | + .build() |
| 32 | + .asMap(); |
| 33 | + |
| 34 | + private <K extends ResourceLocation, V extends CompletableFuture<SoundBuffer>> void onSoundRemoval(RemovalNotification<K, V> notification) { |
| 35 | + notification.getValue().thenAccept(SoundBuffer::discardAlBuffer); |
| 36 | + if(debugDynamicSoundLoading) { |
| 37 | + K k = notification.getKey(); |
| 38 | + if(k == null) |
| 39 | + return; |
| 40 | + ModernFix.LOGGER.warn("Evicted sound {}", k); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
0 commit comments