diff --git a/src/main/java/com/minelittlepony/unicopia/client/sound/BufferedExecutor.java b/src/main/java/com/minelittlepony/unicopia/client/sound/BufferedExecutor.java new file mode 100644 index 00000000..7a198cef --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/client/sound/BufferedExecutor.java @@ -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> 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); + } + }); + }); + } + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java b/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java index 5712d455..948936d1 100644 --- a/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java +++ b/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java @@ -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); }