feat: implement withSwap method

This commit is contained in:
cattyn
2026-01-18 19:09:09 +03:00
parent 64a7206fc7
commit 2a8a6d1980
2 changed files with 23 additions and 12 deletions
@@ -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());
});
}
}
@@ -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<ResultType> HOTBAR_SCOPE = EnumSet.of(ResultType.OFFHAND, ResultType.HOTBAR);
public static final EnumSet<ResultType> 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<Result> 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<ResultType> scopes) {