From 2a8a6d1980aa65f33ad04d224739bad2f3493e17 Mon Sep 17 00:00:00 2001 From: cattyn Date: Sun, 18 Jan 2026 19:09:09 +0300 Subject: [PATCH] feat: implement `withSwap` method --- .../features/modules/combat/KeyPearl.java | 10 +++----- .../oyvey/util/inventory/InventoryUtil.java | 25 +++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/me/alpha432/oyvey/features/modules/combat/KeyPearl.java b/src/main/java/me/alpha432/oyvey/features/modules/combat/KeyPearl.java index c750679..531435b 100644 --- a/src/main/java/me/alpha432/oyvey/features/modules/combat/KeyPearl.java +++ b/src/main/java/me/alpha432/oyvey/features/modules/combat/KeyPearl.java @@ -22,13 +22,9 @@ public class KeyPearl extends Module { if (nullCheck()) return; - int last = InventoryUtil.selected(); Result result = InventoryUtil.find(Items.ENDER_PEARL, inventory.getValue() ? FULL_SCOPE : HOTBAR_SCOPE); - if (!result.found()) - return; - - InventoryUtil.swap(result); - mc.gameMode.useItem(mc.player, result.hand()); - InventoryUtil.swapBack(result, last); + InventoryUtil.withSwap(result, () -> { + mc.gameMode.useItem(mc.player, result.hand()); + }); } } diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java b/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java index ffca877..4e076a1 100644 --- a/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java +++ b/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java @@ -12,10 +12,11 @@ import net.minecraft.world.item.ItemStack; import java.util.EnumSet; import java.util.List; import java.util.function.BiPredicate; +import java.util.function.Consumer; import java.util.function.Predicate; public final class InventoryUtil implements Util { - public static final Result NONE = new Result(-1, ItemStack.EMPTY, ResultType.NONE); + static final Result NONE = new Result(-1, ItemStack.EMPTY, ResultType.NONE); public static final EnumSet HOTBAR_SCOPE = EnumSet.of(ResultType.OFFHAND, ResultType.HOTBAR); public static final EnumSet INVENTORY_SCOPE = EnumSet.of(ResultType.OFFHAND, ResultType.INVENTORY); @@ -45,24 +46,38 @@ public final class InventoryUtil implements Util { mc.gameMode.handleInventoryMouseClick(id, slot, button, type, mc.player); } + public static void withSwap(Result result, Runnable action) { + withSwap(result, r -> action.run()); + } + + public static void withSwap(Result result, Consumer action) { + int lastSlot = selected(); + if (InventoryUtil.swap(result)) { + action.accept(result); + InventoryUtil.swapBack(result, lastSlot); + } + } + public static void swap(int to) { if (to < 0 || to > 8) return; mc.player.getInventory().setSelectedSlot(to); mc.gameMode.ensureHasSentCarriedItem(); } - public static void swap(Result result) { + public static boolean swap(Result result) { for (SwapStrategy strategy : STRATEGIES) { if (strategy.swap(result)) - return; + return true; } + return false; } - public static void swapBack(Result result, int last) { + public static boolean swapBack(Result result, int last) { for (SwapStrategy strategy : STRATEGIES) { if (strategy.swapBack(last, result)) - return; + return true; } + return false; } public static Result find(Item target, EnumSet scopes) {