From 3d704b7def2860c8d2bf88ad3e2a86868370eb51 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 28 Aug 2022 18:47:33 +0200 Subject: [PATCH] Mixed multiple sounds playing when interacting with the spellbook's crafting grid --- .../client/sound/BufferedExecutor.java | 21 +++++++++++++++++++ .../container/SpellbookScreenHandler.java | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/client/sound/BufferedExecutor.java 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); }