| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.utils.player;
|
| 7 |
|
| 8 | import meteordevelopment.meteorclient.mixininterface.IMultiPlayerGameMode;
|
| 9 | import net.minecraft.world.entity.EquipmentSlot;
|
| 10 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 11 | import net.minecraft.world.inventory.ContainerInput;
|
| 12 | import net.minecraft.world.item.Item;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import org.jetbrains.annotations.Range;
|
| 16 |
|
| 17 | import java.util.function.Predicate;
|
| 18 |
|
| 19 | import static meteordevelopment.meteorclient.MeteorClient.mc;
|
| 20 |
|
| 21 | /**
|
| 22 | * Util class for dealing inventories and the items in them.
|
| 23 | * See {@link SlotUtils} for details on how slots are referenced.
|
| 24 | */
|
| 25 | public class InvUtils {
|
| 26 | private static final Action ACTION = new Action();
|
| 27 | public static int previousSlot = -1;
|
| 28 |
|
| 29 | private InvUtils() {
|
| 30 | }
|
| 31 |
|
| 32 | // Predicates
|
| 33 |
|
| 34 | private static Predicate<ItemStack> isOneOf(Item... items) {
|
| 35 | return itemStack -> {
|
| 36 | for (var item : items) if (itemStack.is(item)) return true;
|
| 37 | return false;
|
| 38 | };
|
| 39 | }
|
| 40 |
|
| 41 | public static boolean testInMainHand(Predicate<ItemStack> predicate) {
|
| 42 | return predicate.test(mc.player.getMainHandItem());
|
| 43 | }
|
| 44 |
|
| 45 | public static boolean testInMainHand(Item... items) {
|
| 46 | return testInMainHand(isOneOf(items));
|
| 47 | }
|
| 48 |
|
| 49 | public static boolean testInOffHand(Predicate<ItemStack> predicate) {
|
| 50 | return predicate.test(mc.player.getOffhandItem());
|
| 51 | }
|
| 52 |
|
| 53 | public static boolean testInOffHand(Item... items) {
|
| 54 | return testInOffHand(isOneOf(items));
|
| 55 | }
|
| 56 |
|
| 57 | public static boolean testInHands(Predicate<ItemStack> predicate) {
|
| 58 | return testInMainHand(predicate) || testInOffHand(predicate);
|
| 59 | }
|
| 60 |
|
| 61 | public static boolean testInHands(Item... items) {
|
| 62 | return testInMainHand(items) || testInOffHand(items);
|
| 63 | }
|
| 64 |
|
| 65 | public static boolean testInHotbar(Predicate<ItemStack> predicate) {
|
| 66 | if (testInHands(predicate)) return true;
|
| 67 |
|
| 68 | for (int i = SlotUtils.HOTBAR_START; i <= SlotUtils.HOTBAR_END; i++) {
|
| 69 | ItemStack stack = mc.player.getInventory().getItem(i);
|
| 70 | if (predicate.test(stack)) return true;
|
| 71 | }
|
| 72 |
|
| 73 | return false;
|
| 74 | }
|
| 75 |
|
| 76 | public static boolean testInHotbar(Item... items) {
|
| 77 | return testInHotbar(isOneOf(items));
|
| 78 | }
|
| 79 |
|
| 80 | // Finding items
|
| 81 |
|
| 82 | public static FindItemResult findEmpty() {
|
| 83 | return find(ItemStack::isEmpty);
|
| 84 | }
|
| 85 |
|
| 86 | public static FindItemResult findInHotbar(Item... items) {
|
| 87 | return findInHotbar(isOneOf(items));
|
| 88 | }
|
| 89 |
|
| 90 | public static FindItemResult findInHotbar(Predicate<ItemStack> isGood) {
|
| 91 | if (testInOffHand(isGood)) {
|
| 92 | return new FindItemResult(SlotUtils.OFFHAND, mc.player.getOffhandItem().getCount());
|
| 93 | }
|
| 94 |
|
| 95 | if (testInMainHand(isGood)) {
|
| 96 | return new FindItemResult(mc.player.getInventory().getSelectedSlot(), mc.player.getMainHandItem().getCount());
|
| 97 | }
|
| 98 |
|
| 99 | return find(isGood, 0, 8);
|
| 100 | }
|
| 101 |
|
| 102 | public static FindItemResult find(Item... items) {
|
| 103 | return find(isOneOf(items));
|
| 104 | }
|
| 105 |
|
| 106 | public static FindItemResult find(Predicate<ItemStack> isGood) {
|
| 107 | if (mc.player == null) return new FindItemResult(0, 0);
|
| 108 | return find(isGood, 0, mc.player.getInventory().getContainerSize());
|
| 109 | }
|
| 110 |
|
| 111 | public static FindItemResult find(Predicate<ItemStack> isGood, int start, int end) {
|
| 112 | if (mc.player == null) return new FindItemResult(0, 0);
|
| 113 |
|
| 114 | int slot = -1, count = 0;
|
| 115 |
|
| 116 | for (int i = start; i <= end; i++) {
|
| 117 | ItemStack stack = mc.player.getInventory().getItem(i);
|
| 118 |
|
| 119 | if (isGood.test(stack)) {
|
| 120 | if (slot == -1) slot = i;
|
| 121 | count += stack.getCount();
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | return new FindItemResult(slot, count);
|
| 126 | }
|
| 127 |
|
| 128 | public static FindItemResult findFastestTool(BlockState state) {
|
| 129 | float bestScore = 1;
|
| 130 | int slot = -1;
|
| 131 |
|
| 132 | for (int i = 0; i < 9; i++) {
|
| 133 | ItemStack stack = mc.player.getInventory().getItem(i);
|
| 134 | if (!stack.isCorrectToolForDrops(state)) continue;
|
| 135 |
|
| 136 | float score = stack.getDestroySpeed(state);
|
| 137 | if (score > bestScore) {
|
| 138 | bestScore = score;
|
| 139 | slot = i;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | return new FindItemResult(slot, 1);
|
| 144 | }
|
| 145 |
|
| 146 | // Interactions
|
| 147 |
|
| 148 | public static boolean swap(int slot, boolean swapBack) {
|
| 149 | if (slot == SlotUtils.OFFHAND) return true;
|
| 150 | if (slot < 0 || slot > 8) return false;
|
| 151 | if (swapBack && previousSlot == -1) previousSlot = mc.player.getInventory().getSelectedSlot();
|
| 152 | else if (!swapBack) previousSlot = -1;
|
| 153 |
|
| 154 | mc.player.getInventory().setSelectedSlot(slot);
|
| 155 | ((IMultiPlayerGameMode) mc.gameMode).meteor$syncSelected();
|
| 156 | return true;
|
| 157 | }
|
| 158 |
|
| 159 | public static boolean swapBack() {
|
| 160 | if (previousSlot == -1) return false;
|
| 161 |
|
| 162 | boolean return_ = swap(previousSlot, false);
|
| 163 | previousSlot = -1;
|
| 164 | return return_;
|
| 165 | }
|
| 166 |
|
| 167 | public static Action move() {
|
| 168 | ACTION.type = ContainerInput.PICKUP;
|
| 169 | ACTION.two = true;
|
| 170 | return ACTION;
|
| 171 | }
|
| 172 |
|
| 173 | public static Action click() {
|
| 174 | ACTION.type = ContainerInput.PICKUP;
|
| 175 | return ACTION;
|
| 176 | }
|
| 177 |
|
| 178 | /**
|
| 179 | * When writing code with quickSwap, both to and from should provide the ID of a slot, not the index.
|
| 180 | * From should be the slot in the hotbar, to should be the slot you're switching an item from.
|
| 181 | */
|
| 182 | public static Action quickSwap() {
|
| 183 | ACTION.type = ContainerInput.SWAP;
|
| 184 | return ACTION;
|
| 185 | }
|
| 186 |
|
| 187 | public static Action shiftClick() {
|
| 188 | ACTION.type = ContainerInput.QUICK_MOVE;
|
| 189 | return ACTION;
|
| 190 | }
|
| 191 |
|
| 192 | public static Action drop() {
|
| 193 | ACTION.type = ContainerInput.THROW;
|
| 194 | ACTION.data = 1;
|
| 195 | return ACTION;
|
| 196 | }
|
| 197 |
|
| 198 | public static Action dropOne() {
|
| 199 | ACTION.type = ContainerInput.THROW;
|
| 200 | ACTION.data = 0;
|
| 201 | return ACTION;
|
| 202 | }
|
| 203 |
|
| 204 | public static void dropHand() {
|
| 205 | if (!mc.player.containerMenu.getCarried().isEmpty())
|
| 206 | mc.gameMode.handleContainerInput(mc.player.containerMenu.containerId, AbstractContainerMenu.SLOT_CLICKED_OUTSIDE, 0, ContainerInput.PICKUP, mc.player);
|
| 207 | }
|
| 208 |
|
| 209 | public static class Action {
|
| 210 | private ContainerInput type = null;
|
| 211 | private boolean two = false;
|
| 212 | private int from = -1;
|
| 213 | private int to = -1;
|
| 214 | private int data = 0;
|
| 215 |
|
| 216 | private boolean isRecursive = false;
|
| 217 |
|
| 218 | private Action() {
|
| 219 | }
|
| 220 |
|
| 221 | // From
|
| 222 |
|
| 223 | public Action fromId(int id) {
|
| 224 | from = id;
|
| 225 | return this;
|
| 226 | }
|
| 227 |
|
| 228 | /**
|
| 229 | * @param index The index of one of the slots within the inventory
|
| 230 | */
|
| 231 | public Action from(int index) {
|
| 232 | return fromId(SlotUtils.indexToId(index));
|
| 233 | }
|
| 234 |
|
| 235 | public Action fromHotbar(@Range(from = 0, to = 8) int i) {
|
| 236 | return from(SlotUtils.HOTBAR_START + i);
|
| 237 | }
|
| 238 |
|
| 239 | public Action fromOffhand() {
|
| 240 | return from(SlotUtils.OFFHAND);
|
| 241 | }
|
| 242 |
|
| 243 | public Action fromMain(@Range(from = 0, to = 26) int i) {
|
| 244 | return from(SlotUtils.MAIN_START + i);
|
| 245 | }
|
| 246 |
|
| 247 | /**
|
| 248 | * @param i The entity slot id of one of the four humanoid armor pieces, as defined in {@link EquipmentSlot}
|
| 249 | */
|
| 250 | public Action fromArmor(int i) {
|
| 251 | return from(SlotUtils.ARMOR_START + (3 - i));
|
| 252 | }
|
| 253 |
|
| 254 | // To
|
| 255 |
|
| 256 | public void toId(int id) {
|
| 257 | to = id;
|
| 258 | run();
|
| 259 | }
|
| 260 |
|
| 261 | /**
|
| 262 | * @param index The index of one of the slots within the inventory
|
| 263 | */
|
| 264 | public void to(int index) {
|
| 265 | toId(SlotUtils.indexToId(index));
|
| 266 | }
|
| 267 |
|
| 268 | public void toHotbar(@Range(from = 0, to = 8) int i) {
|
| 269 | to(SlotUtils.HOTBAR_START + i);
|
| 270 | }
|
| 271 |
|
| 272 | public void toOffhand() {
|
| 273 | to(SlotUtils.OFFHAND);
|
| 274 | }
|
| 275 |
|
| 276 | public void toMain(@Range(from = 0, to = 26) int i) {
|
| 277 | to(SlotUtils.MAIN_START + i);
|
| 278 | }
|
| 279 |
|
| 280 | /**
|
| 281 | * @param i The entity slot id of one of the four humanoid armor pieces, as defined in {@link EquipmentSlot}
|
| 282 | */
|
| 283 | public void toArmor(int i) {
|
| 284 | to(SlotUtils.ARMOR_START + (3 - i));
|
| 285 | }
|
| 286 |
|
| 287 | // Slot
|
| 288 |
|
| 289 | public void slotId(int id) {
|
| 290 | from = to = id;
|
| 291 | run();
|
| 292 | }
|
| 293 |
|
| 294 | /**
|
| 295 | * @param index The index of one of the slots within the inventory
|
| 296 | */
|
| 297 | public void slot(int index) {
|
| 298 | slotId(SlotUtils.indexToId(index));
|
| 299 | }
|
| 300 |
|
| 301 | public void slotHotbar(@Range(from = 0, to = 8) int i) {
|
| 302 | slot(SlotUtils.HOTBAR_START + i);
|
| 303 | }
|
| 304 |
|
| 305 | public void slotOffhand() {
|
| 306 | slot(SlotUtils.OFFHAND);
|
| 307 | }
|
| 308 |
|
| 309 | public void slotMain(@Range(from = 0, to = 26) int i) {
|
| 310 | slot(SlotUtils.MAIN_START + i);
|
| 311 | }
|
| 312 |
|
| 313 | /**
|
| 314 | * @param i The entity slot id of one of the four humanoid armor pieces, as defined in {@link EquipmentSlot}
|
| 315 | */
|
| 316 | public void slotArmor(int i) {
|
| 317 | slot(SlotUtils.ARMOR_START + (3 - i));
|
| 318 | }
|
| 319 |
|
| 320 | // Other
|
| 321 |
|
| 322 | private void run() {
|
| 323 | boolean hadEmptyCursor = mc.player.containerMenu.getCarried().isEmpty();
|
| 324 |
|
| 325 | if (type == ContainerInput.SWAP) {
|
| 326 | data = from;
|
| 327 | from = to;
|
| 328 | }
|
| 329 |
|
| 330 | if (type != null && from != -1 && to != -1) {
|
| 331 | click(from);
|
| 332 | if (two) click(to);
|
| 333 | }
|
| 334 |
|
| 335 | ContainerInput preType = type;
|
| 336 | boolean preTwo = two;
|
| 337 | int preFrom = from;
|
| 338 | int preTo = to;
|
| 339 |
|
| 340 | type = null;
|
| 341 | two = false;
|
| 342 | from = -1;
|
| 343 | to = -1;
|
| 344 | data = 0;
|
| 345 |
|
| 346 | if (!isRecursive && hadEmptyCursor && preType == ContainerInput.PICKUP && preTwo && (preFrom != -1 && preTo != -1) && !mc.player.containerMenu.getCarried().isEmpty()) {
|
| 347 | isRecursive = true;
|
| 348 | InvUtils.click().slotId(preFrom);
|
| 349 | isRecursive = false;
|
| 350 | }
|
| 351 | }
|
| 352 |
|
| 353 | private void click(int id) {
|
| 354 | mc.gameMode.handleContainerInput(mc.player.containerMenu.containerId, id, data, type, mc.player);
|
| 355 | }
|
| 356 | }
|
| 357 | }
|
| 358 |
|