Skip to content

Commit

Permalink
Fix dynamic_sounds breaking on 1.16 (#259)
Browse files Browse the repository at this point in the history
An issue in Guava (google/guava#3081) causes the removal listener to fire even when entries haven't actually been removed. We filter them to get around this.
  • Loading branch information
Phoenix-Starlight committed Oct 13, 2023
1 parent 538d332 commit e7277b8
Showing 1 changed file with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.embeddedt.modernfix.common.mixin.perf.dynamic_sounds;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalCause;
import com.google.common.cache.RemovalNotification;
import com.mojang.blaze3d.audio.SoundBuffer;
import net.minecraft.client.sounds.SoundBufferLibrary;
Expand All @@ -26,12 +27,15 @@ public abstract class SoundBufferLibraryMixin {
@Shadow @Final @Mutable
private Map<ResourceLocation, CompletableFuture<SoundBuffer>> cache = CacheBuilder.newBuilder()
.expireAfterAccess(DynamicSoundHelpers.MAX_SOUND_LIFETIME_SECS, TimeUnit.SECONDS)
.concurrencyLevel(1)
// Excessive use of type hinting due to it assuming Object as the broadest correct type
.<ResourceLocation, CompletableFuture<SoundBuffer>>removalListener(this::onSoundRemoval)
.build()
.asMap();

private <K extends ResourceLocation, V extends CompletableFuture<SoundBuffer>> void onSoundRemoval(RemovalNotification<K, V> notification) {
if(notification.getCause() == RemovalCause.REPLACED && notification.getValue() == cache.get(notification.getKey()))
return;
notification.getValue().thenAccept(SoundBuffer::discardAlBuffer);
if(debugDynamicSoundLoading) {
K k = notification.getKey();
Expand Down

0 comments on commit e7277b8

Please sign in to comment.