Mixed multiple sounds playing when interacting with the spellbook's crafting grid

This commit is contained in:
Sollace 2022-08-28 18:47:33 +02:00
parent 6ed0bb1859
commit 3d704b7def
2 changed files with 25 additions and 1 deletions

View file

@ -0,0 +1,21 @@
package com.minelittlepony.unicopia.client.sound;
import java.util.*;
import java.util.concurrent.*;
public class BufferedExecutor {
private static final Executor EXECUTOR = CompletableFuture.delayedExecutor(50, TimeUnit.MILLISECONDS);
private static final Map<Integer, CompletableFuture<?>> PENDING_EXECUTIONS = new HashMap<>();
public static void bufferExecution(Object obj, Runnable action) {
synchronized (PENDING_EXECUTIONS) {
PENDING_EXECUTIONS.computeIfAbsent(obj.hashCode(), hash -> {
return CompletableFuture.runAsync(action, EXECUTOR).thenRun(() -> {
synchronized (PENDING_EXECUTIONS) {
PENDING_EXECUTIONS.remove(hash);
}
});
});
}
}
}

View file

@ -6,6 +6,7 @@ import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.client.sound.BufferedExecutor;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.item.URecipes;
@ -442,7 +443,9 @@ public class SpellbookScreenHandler extends ScreenHandler {
@Override
public void setStack(ItemStack stack) {
if (!stack.isEmpty() && !ItemStack.areEqual(stack, getStack())) {
player.playSound(stack.getItem() == UItems.BOTCHED_GEM ? USounds.GUI_ABILITY_FAIL : USounds.GUI_SPELL_CRAFT_SUCCESS, SoundCategory.MASTER, 1, 0.3F);
BufferedExecutor.bufferExecution(player, () -> {
player.playSound(stack.getItem() == UItems.BOTCHED_GEM ? USounds.GUI_ABILITY_FAIL : USounds.GUI_SPELL_CRAFT_SUCCESS, SoundCategory.MASTER, 1, 0.3F);
});
}
super.setStack(stack);
}