Skip to content

Commit 0dd74c1

Browse files
committed
Sort inventory no longer makes noises if not necessary (#23)
Signed-off-by: Hendrix-Shen <[email protected]>
1 parent 9625f79 commit 0dd74c1

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/main/java/com/plusls/ommc/config/Configs.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import net.minecraft.client.Minecraft;
1414
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
1515
import net.minecraft.client.resources.language.I18n;
16-
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
1716
import net.minecraft.core.BlockPos;
18-
import net.minecraft.sounds.SoundEvents;
1917
import net.minecraft.world.entity.Entity;
2018
import net.minecraft.world.phys.BlockHitResult;
2119
import net.minecraft.world.phys.HitResult;
@@ -36,6 +34,7 @@
3634
import java.util.ArrayList;
3735
import java.util.List;
3836
import java.util.Objects;
37+
import java.util.Optional;
3938

4039
public class Configs {
4140
private static final List<String> OLD_WORLD_EATER_MINE_HELPER_WHITELIST = new ArrayList<>();
@@ -282,11 +281,7 @@ public static void init(@NotNull ConfigManager cm) {
282281
sortInventory.getKeybind().setSettings(KeybindSettings.GUI);
283282

284283
sortInventory.getKeybind().setCallback((keyAction, iKeybind) -> {
285-
if (SortInventoryUtil.sort()) {
286-
Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
287-
} else {
288-
Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.DISPENSER_FAIL, 1.0F));
289-
}
284+
Optional.ofNullable(SortInventoryUtil.sort()).ifPresent(Runnable::run);
290285
return false;
291286
});
292287

src/main/java/com/plusls/ommc/feature/sortInventory/SortInventoryUtil.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen;
1212
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
1313
import net.minecraft.client.player.LocalPlayer;
14+
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
1415
import net.minecraft.nbt.CompoundTag;
16+
import net.minecraft.sounds.SoundEvents;
1517
import net.minecraft.util.Tuple;
1618
import net.minecraft.world.entity.player.Inventory;
1719
import net.minecraft.world.entity.player.Player;
@@ -155,12 +157,12 @@ public static Tuple<Integer, Integer> getSortRange(AbstractContainerMenu screenH
155157
return new Tuple<>(l, r);
156158
}
157159

158-
public static boolean sort() {
160+
public static @Nullable Runnable sort() {
159161
Minecraft client = Minecraft.getInstance();
160162

161163
if (!(client.screen instanceof AbstractContainerScreen<?>) ||
162164
client.screen instanceof CreativeModeInventoryScreen) {
163-
return false;
165+
return null;
164166
}
165167

166168
AbstractContainerScreen<?> handledScreen = (AbstractContainerScreen<?>) client.screen;
@@ -170,20 +172,20 @@ public static boolean sort() {
170172
Slot mouseSlot = ((AccessorAbstractContainerScreen) handledScreen).invokeFindSlot(x, y);
171173

172174
if (mouseSlot == null) {
173-
return false;
175+
return null;
174176
}
175177

176178
LocalPlayer player = client.player;
177179

178180
if (client.gameMode == null || player == null) {
179-
return false;
181+
return null;
180182
}
181183

182184
AbstractContainerMenu screenHandler = player.containerMenu;
183185
Tuple<Integer, Integer> sortRange = SortInventoryUtil.getSortRange(screenHandler, mouseSlot);
184186

185187
if (sortRange == null) {
186-
return false;
188+
return null;
187189
}
188190

189191
List<ItemStack> itemStacks = Lists.newArrayList();
@@ -199,7 +201,11 @@ public static boolean sort() {
199201
List<Tuple<Integer, Integer>> swapQueue = SortInventoryUtil.quickSort(itemStacks, sortRange.getA(),
200202
sortRange.getB());
201203
SortInventoryUtil.doClick(player, screenHandler.containerId, client.gameMode, mergeQueue, swapQueue);
202-
return !mergeQueue.isEmpty() || !swapQueue.isEmpty();
204+
return (!mergeQueue.isEmpty() || !swapQueue.isEmpty()) ?
205+
() -> Minecraft.getInstance().getSoundManager()
206+
.play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)) :
207+
() -> Minecraft.getInstance().getSoundManager()
208+
.play(SimpleSoundInstance.forUI(SoundEvents.DISPENSER_FAIL, 1.0F));
203209
}
204210

205211
public static void doClick(Player player, int syncId, @NotNull MultiPlayerGameMode interactionManager,

0 commit comments

Comments
 (0)