diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java b/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java new file mode 100644 index 0000000..e2ed743 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/InventoryUtil.java @@ -0,0 +1,64 @@ +package me.alpha432.oyvey.util.inventory; + +import me.alpha432.oyvey.util.traits.Util; +import net.minecraft.world.inventory.ClickType; +import net.minecraft.world.item.ItemStack; + +import java.util.Set; +import java.util.function.BiPredicate; +import java.util.function.Predicate; + +public final class InventoryUtil implements Util { + public static final Result NONE = new Result(-1, ItemStack.EMPTY, ResultType.NONE); + + private InventoryUtil() { + throw new AssertionError(); + } + + public static ItemStack cursor() { + return mc.player.containerMenu.getCarried(); + } + + public static int selected() { + return mc.player.getInventory().getSelectedSlot(); + } + + public static void click(int slot, int button, ClickType type) { + int id = mc.player.containerMenu.containerId; + mc.gameMode.handleInventoryMouseClick(id, slot, button, type, mc.player); + } + + public static void swap(int to) { + if (to < 0 || to > 8) return; + mc.player.getInventory().setSelectedSlot(to); + mc.gameMode.ensureHasSentCarriedItem(); + } + + public static Result find(Predicate predicate, ResultType... types) { + final Set set = Set.of(types); + return find((item, type) -> set.contains(type) && predicate.test(item)); + } + + public static Result find(BiPredicate predicate) { + ItemStack offhand = mc.player.getOffhandItem(); + if (predicate.test(offhand, ResultType.OFFHAND)) { + return Result.fromOffhand(offhand); + } + + for (int i = 0; i < 9; i++) { + ItemStack item = mc.player.getInventory().getItem(i); + if (predicate.test(item, ResultType.HOTBAR)) { + return new Result(i, item, ResultType.HOTBAR); + } + } + + for (int i = 9; i < 36; i++) { + ItemStack item = mc.player.getInventory().getItem(i); + if (predicate.test(item, ResultType.INVENTORY)) { + return new Result(i, item, ResultType.INVENTORY); + } + } + + return NONE; + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/Result.java b/src/main/java/me/alpha432/oyvey/util/inventory/Result.java new file mode 100644 index 0000000..08d9223 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/Result.java @@ -0,0 +1,22 @@ +package me.alpha432.oyvey.util.inventory; + +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.ItemStack; + +public record Result(int slot, ItemStack stack, ResultType type) { + static Result fromOffhand(ItemStack stack) { + return new Result(-1, stack, ResultType.OFFHAND); + } + + public InteractionHand getHand() { + return type == ResultType.OFFHAND ? InteractionHand.OFF_HAND : InteractionHand.MAIN_HAND; + } + + public boolean holding() { + return type == ResultType.OFFHAND || type == ResultType.HOTBAR && slot == InventoryUtil.selected(); + } + + public boolean found() { + return type != ResultType.NONE; + } +} diff --git a/src/main/java/me/alpha432/oyvey/util/inventory/ResultType.java b/src/main/java/me/alpha432/oyvey/util/inventory/ResultType.java new file mode 100644 index 0000000..f73c447 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/inventory/ResultType.java @@ -0,0 +1,8 @@ +package me.alpha432.oyvey.util.inventory; + +public enum ResultType { + HOTBAR, + INVENTORY, + OFFHAND, + NONE +} diff --git a/src/main/resources/oyvey.accesswidener b/src/main/resources/oyvey.accesswidener index 8b9085f..a60449a 100644 --- a/src/main/resources/oyvey.accesswidener +++ b/src/main/resources/oyvey.accesswidener @@ -5,3 +5,4 @@ accessible field net/minecraft/network/protocol/game/ServerboundInteractPacket e accessible method net/minecraft/network/protocol/game/ServerboundInteractPacket$Action getType ()Lnet/minecraft/network/protocol/game/ServerboundInteractPacket$ActionType; accessible class net/minecraft/network/protocol/game/ServerboundInteractPacket$ActionType accessible field net/minecraft/network/protocol/game/ServerboundInteractPacket action Lnet/minecraft/network/protocol/game/ServerboundInteractPacket$Action; +accessible method net/minecraft/client/multiplayer/MultiPlayerGameMode ensureHasSentCarriedItem ()V \ No newline at end of file