feat: implement withSwap method
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user