feat(InventoryUtil): add basic util for inventory search & interaction

This commit is contained in:
cattyn
2025-12-25 15:41:41 +03:00
parent af5aed78f2
commit 1362662f66
4 changed files with 95 additions and 0 deletions
@@ -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<ItemStack> predicate, ResultType... types) {
final Set<ResultType> set = Set.of(types);
return find((item, type) -> set.contains(type) && predicate.test(item));
}
public static Result find(BiPredicate<ItemStack, ResultType> 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;
}
}
@@ -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;
}
}
@@ -0,0 +1,8 @@
package me.alpha432.oyvey.util.inventory;
public enum ResultType {
HOTBAR,
INVENTORY,
OFFHAND,
NONE
}
+1
View File
@@ -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