| 1 | package com.jellypudding.offlineclient.util;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.OfflineClient;
|
| 4 | import net.minecraft.client.Minecraft;
|
| 5 | import net.minecraft.client.gui.screens.Screen;
|
| 6 | import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
| 7 | import net.minecraft.client.gui.screens.inventory.ContainerScreen;
|
| 8 | import net.minecraft.client.gui.screens.inventory.DispenserScreen;
|
| 9 | import net.minecraft.client.gui.screens.inventory.HopperScreen;
|
| 10 | import net.minecraft.client.gui.screens.inventory.InventoryScreen;
|
| 11 | import net.minecraft.client.gui.screens.inventory.ShulkerBoxScreen;
|
| 12 | import net.minecraft.world.inventory.ContainerInput;
|
| 13 | import net.minecraft.world.item.Item;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 |
|
| 16 | import java.util.function.Predicate;
|
| 17 |
|
| 18 | public final class InventoryUtil {
|
| 19 |
|
| 20 | private static final Minecraft MC = OfflineClient.MC;
|
| 21 |
|
| 22 | // A hotbar slot maps to this network slot plus its index.
|
| 23 | private static final int HOTBAR_START = 36;
|
| 24 |
|
| 25 | public static final int HOTBAR_SIZE = 9;
|
| 26 |
|
| 27 | // Every slot of the survival inventory.
|
| 28 | public static final int WHOLE_INVENTORY = 36;
|
| 29 |
|
| 30 | public static final int OFFHAND_SLOT = 45;
|
| 31 |
|
| 32 | private InventoryUtil() {
|
| 33 | }
|
| 34 |
|
| 35 | // The hotbar sits after the rest in network slot order.
|
| 36 | // Container slots of the worn armour in the survival inventory. Helmet first.
|
| 37 | public static final int ARMOR_START = 5;
|
| 38 | public static final int CHEST_SLOT = ARMOR_START + 1;
|
| 39 |
|
| 40 | public static int networkSlot(int inventoryIndex) {
|
| 41 | return inventoryIndex < HOTBAR_SIZE ? HOTBAR_START + inventoryIndex : inventoryIndex;
|
| 42 | }
|
| 43 |
|
| 44 | public enum Swap {
|
| 45 | // Nothing moved.
|
| 46 | REFUSED,
|
| 47 | // The item moved and the cursor came away empty.
|
| 48 | DONE,
|
| 49 | // The item moved but something is still on the cursor.
|
| 50 | STRANDED
|
| 51 | }
|
| 52 |
|
| 53 | // The game throws the click away unless the survival inventory is the live menu.
|
| 54 | // The creative screen swaps in a menu of its own where the slot numbers differ.
|
| 55 | public static boolean canClick() {
|
| 56 | if (MC.player.containerMenu != MC.player.inventoryMenu) {
|
| 57 | return false;
|
| 58 | }
|
| 59 | return !(MC.gui.screen() instanceof AbstractContainerScreen)
|
| 60 | || MC.gui.screen() instanceof InventoryScreen;
|
| 61 | }
|
| 62 |
|
| 63 | // True when the survival inventory is up with nothing on the cursor.
|
| 64 | // Slot swaps and container clicks only land then.
|
| 65 | public static boolean inventoryFree() {
|
| 66 | return MC.gui.screen() == null
|
| 67 | && MC.player.containerMenu.containerId == 0
|
| 68 | && carried().isEmpty();
|
| 69 | }
|
| 70 |
|
| 71 | // Plain storage only. Crafting and anvil and trade and mount screens put
|
| 72 | // their own slots first and refuse a click into them.
|
| 73 | public static boolean isStorage(Screen screen) {
|
| 74 | return screen instanceof ContainerScreen
|
| 75 | || screen instanceof ShulkerBoxScreen
|
| 76 | || screen instanceof HopperScreen
|
| 77 | || screen instanceof DispenserScreen;
|
| 78 | }
|
| 79 |
|
| 80 | public static ItemStack carried() {
|
| 81 | return MC.player.containerMenu.getCarried();
|
| 82 | }
|
| 83 |
|
| 84 | // Moves one slot into another with the three clicks a player makes.
|
| 85 | public static Swap swap(int fromNetworkSlot, int toNetworkSlot) {
|
| 86 | click(fromNetworkSlot);
|
| 87 | if (carried().isEmpty()) {
|
| 88 | return Swap.REFUSED;
|
| 89 | }
|
| 90 | click(toNetworkSlot);
|
| 91 | if (!carried().isEmpty()) {
|
| 92 | // Whatever the new item displaced goes into the slot it came from.
|
| 93 | click(fromNetworkSlot);
|
| 94 | }
|
| 95 | return carried().isEmpty() ? Swap.DONE : Swap.STRANDED;
|
| 96 | }
|
| 97 |
|
| 98 | // A pickup click on the survival inventory container.
|
| 99 | public static void click(int networkSlot) {
|
| 100 | if (MC.player == null || MC.gameMode == null) {
|
| 101 | return;
|
| 102 | }
|
| 103 | MC.gameMode.handleContainerInput(0, networkSlot, 0, ContainerInput.PICKUP, MC.player);
|
| 104 | }
|
| 105 |
|
| 106 | public static int selectedSlot() {
|
| 107 | return MC.player.getInventory().getSelectedSlot();
|
| 108 | }
|
| 109 |
|
| 110 | // The first index up to the limit that passes the test. Minus one when none does.
|
| 111 | public static int findSlot(Predicate<ItemStack> test, int limit) {
|
| 112 | for (int i = 0; i < limit; i++) {
|
| 113 | if (test.test(MC.player.getInventory().getItem(i))) {
|
| 114 | return i;
|
| 115 | }
|
| 116 | }
|
| 117 | return -1;
|
| 118 | }
|
| 119 |
|
| 120 | public static int findSlot(Item item, int limit) {
|
| 121 | return findSlot(stack -> stack.is(item), limit);
|
| 122 | }
|
| 123 |
|
| 124 | // How many of an item the first slots up to the limit hold between them.
|
| 125 | public static int count(Item item, int limit) {
|
| 126 | int total = 0;
|
| 127 | for (int i = 0; i < limit; i++) {
|
| 128 | ItemStack stack = MC.player.getInventory().getItem(i);
|
| 129 | if (stack.is(item)) {
|
| 130 | total += stack.getCount();
|
| 131 | }
|
| 132 | }
|
| 133 | return total;
|
| 134 | }
|
| 135 |
|
| 136 | // The slot already in hand wins. Minus one when nothing matches.
|
| 137 | public static int hotbarSlot(Predicate<ItemStack> test) {
|
| 138 | int selected = selectedSlot();
|
| 139 | if (test.test(MC.player.getInventory().getItem(selected))) {
|
| 140 | return selected;
|
| 141 | }
|
| 142 | for (int i = 0; i < HOTBAR_SIZE; i++) {
|
| 143 | if (test.test(MC.player.getInventory().getItem(i))) {
|
| 144 | return i;
|
| 145 | }
|
| 146 | }
|
| 147 | return -1;
|
| 148 | }
|
| 149 |
|
| 150 | // An empty hotbar slot or minus one when every one is taken.
|
| 151 | public static int freeHotbarSlot() {
|
| 152 | return freeHotbarSlot(-1);
|
| 153 | }
|
| 154 |
|
| 155 | public static int freeHotbarSlot(int whenFull) {
|
| 156 | for (int i = 0; i < HOTBAR_SIZE; i++) {
|
| 157 | if (MC.player.getInventory().getItem(i).isEmpty()) {
|
| 158 | return i;
|
| 159 | }
|
| 160 | }
|
| 161 | return whenFull;
|
| 162 | }
|
| 163 |
|
| 164 | // Holds an item from anywhere in the inventory. A stack outside the hotbar
|
| 165 | // is moved in first and put back when the loan ends.
|
| 166 | public static final class HotbarLoan {
|
| 167 |
|
| 168 | private int previousSlot = -1;
|
| 169 | private int lentFrom = -1;
|
| 170 | private int lentTo = -1;
|
| 171 |
|
| 172 | // The hotbar slot the loan last selected.
|
| 173 | private int chosen = -1;
|
| 174 |
|
| 175 | // Selects the given inventory index and borrows it into the hotbar when
|
| 176 | // it is not already there. False when the swap could not be made.
|
| 177 | public boolean select(int inventorySlot) {
|
| 178 | if (MC.player == null || inventorySlot < 0) {
|
| 179 | return false;
|
| 180 | }
|
| 181 | int slot = inventorySlot;
|
| 182 | if (slot >= HOTBAR_SIZE) {
|
| 183 | if (lentFrom == slot) {
|
| 184 | // Already borrowed. A second swap would send it home again.
|
| 185 | slot = lentTo;
|
| 186 | } else {
|
| 187 | if (!canClick() || !carried().isEmpty()) {
|
| 188 | return false;
|
| 189 | }
|
| 190 | // Only one stack can be away from home at a time.
|
| 191 | if (lentFrom != -1 && !returnLoan()) {
|
| 192 | return false;
|
| 193 | }
|
| 194 | // A full hotbar means the held slot gives up its place.
|
| 195 | int hotbar = freeHotbarSlot(selectedSlot());
|
| 196 | MC.gameMode.handleContainerInput(0, slot, hotbar, ContainerInput.SWAP, MC.player);
|
| 197 | lentFrom = slot;
|
| 198 | lentTo = hotbar;
|
| 199 | slot = hotbar;
|
| 200 | }
|
| 201 | }
|
| 202 | if (previousSlot == -1) {
|
| 203 | previousSlot = selectedSlot();
|
| 204 | }
|
| 205 | chosen = slot;
|
| 206 | MC.player.getInventory().setSelectedSlot(slot);
|
| 207 | return true;
|
| 208 | }
|
| 209 |
|
| 210 | // True whilst the player is still on the slot the loan picked.
|
| 211 | public boolean stillMine() {
|
| 212 | return chosen != -1 && MC.player != null && selectedSlot() == chosen;
|
| 213 | }
|
| 214 |
|
| 215 | // Returns a borrowed stack and goes back to the slot the player had held.
|
| 216 | public void giveBack() {
|
| 217 | giveBack(true);
|
| 218 | }
|
| 219 |
|
| 220 | // A borrowed stack always goes home. The old slot only comes back when asked.
|
| 221 | public void giveBack(boolean reselect) {
|
| 222 | if (MC.player == null) {
|
| 223 | forget();
|
| 224 | return;
|
| 225 | }
|
| 226 | returnLoan();
|
| 227 | if (reselect && previousSlot != -1) {
|
| 228 | MC.player.getInventory().setSelectedSlot(previousSlot);
|
| 229 | }
|
| 230 | previousSlot = -1;
|
| 231 | chosen = -1;
|
| 232 | }
|
| 233 |
|
| 234 | // False when the swap could not be sent. The loan then stays open.
|
| 235 | private boolean returnLoan() {
|
| 236 | if (lentFrom == -1) {
|
| 237 | return true;
|
| 238 | }
|
| 239 | if (!canClick() || !carried().isEmpty()) {
|
| 240 | return false;
|
| 241 | }
|
| 242 | MC.gameMode.handleContainerInput(0, lentFrom, lentTo, ContainerInput.SWAP, MC.player);
|
| 243 | lentFrom = -1;
|
| 244 | lentTo = -1;
|
| 245 | return true;
|
| 246 | }
|
| 247 |
|
| 248 | // True whilst a stack is still out of its home slot.
|
| 249 | public boolean isLent() {
|
| 250 | return lentFrom != -1;
|
| 251 | }
|
| 252 |
|
| 253 | // Drops the bookkeeping without touching the inventory. For a respawn.
|
| 254 | public void forget() {
|
| 255 | previousSlot = -1;
|
| 256 | lentFrom = -1;
|
| 257 | lentTo = -1;
|
| 258 | chosen = -1;
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | // Remembers the hotbar slot a module took over. Each module owns its own instance.
|
| 263 | public static final class SlotSwap {
|
| 264 |
|
| 265 | private int previous = -1;
|
| 266 |
|
| 267 | // The slot this module last selected.
|
| 268 | private int taken = -1;
|
| 269 |
|
| 270 | public void select(int slot) {
|
| 271 | if (MC.player == null || slot < 0 || slot >= HOTBAR_SIZE) {
|
| 272 | return;
|
| 273 | }
|
| 274 | taken = slot;
|
| 275 | int selected = selectedSlot();
|
| 276 | if (selected == slot) {
|
| 277 | return;
|
| 278 | }
|
| 279 | if (previous == -1) {
|
| 280 | previous = selected;
|
| 281 | }
|
| 282 | MC.player.getInventory().setSelectedSlot(slot);
|
| 283 | sync();
|
| 284 | }
|
| 285 |
|
| 286 | public void restore() {
|
| 287 | if (previous != -1 && MC.player != null) {
|
| 288 | MC.player.getInventory().setSelectedSlot(previous);
|
| 289 | sync();
|
| 290 | }
|
| 291 | previous = -1;
|
| 292 | }
|
| 293 |
|
| 294 | // True whilst the player is still on the slot this module chose.
|
| 295 | public boolean stillMine() {
|
| 296 | return MC.player != null && taken != -1 && selectedSlot() == taken;
|
| 297 | }
|
| 298 |
|
| 299 | // Gives the slot back only if the player has not picked another since.
|
| 300 | public void restoreIfMine() {
|
| 301 | if (stillMine()) {
|
| 302 | restore();
|
| 303 | }
|
| 304 | forget();
|
| 305 | }
|
| 306 |
|
| 307 | // The instant mining path sends raw packets and never trips vanilla's own sync.
|
| 308 | private static void sync() {
|
| 309 | if (MC.gameMode != null && MC.player != null && MC.player.connection != null) {
|
| 310 | MC.gameMode.ensureHasSentCarriedItem();
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | public void forget() {
|
| 315 | previous = -1;
|
| 316 | taken = -1;
|
| 317 | }
|
| 318 |
|
| 319 | // The slot the player had before the first select. Minus one whilst none.
|
| 320 | public int previousSlot() {
|
| 321 | return previous;
|
| 322 | }
|
| 323 |
|
| 324 | public boolean isHolding() {
|
| 325 | return previous != -1;
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | // Remembers the slot a stack left on the cursor came from. The stack goes
|
| 330 | // home again on the first call where the inventory will take a click.
|
| 331 | public static final class StrandedStack {
|
| 332 |
|
| 333 | private int slot = -1;
|
| 334 |
|
| 335 | // Records where a stack left on the cursor belongs.
|
| 336 | public void hold(int networkSlot) {
|
| 337 | slot = networkSlot;
|
| 338 | }
|
| 339 |
|
| 340 | // True once the cursor is clear. Tries again on every call until it is.
|
| 341 | public boolean recover() {
|
| 342 | if (slot == -1 || carried().isEmpty()) {
|
| 343 | slot = -1;
|
| 344 | return true;
|
| 345 | }
|
| 346 | if (!canClick()) {
|
| 347 | return false;
|
| 348 | }
|
| 349 | click(slot);
|
| 350 | if (carried().isEmpty()) {
|
| 351 | slot = -1;
|
| 352 | return true;
|
| 353 | }
|
| 354 | return false;
|
| 355 | }
|
| 356 |
|
| 357 | // One last attempt and then the bookkeeping goes. For a disable or a respawn.
|
| 358 | public void giveBack() {
|
| 359 | recover();
|
| 360 | slot = -1;
|
| 361 | }
|
| 362 |
|
| 363 | public boolean isHolding() {
|
| 364 | return slot != -1;
|
| 365 | }
|
| 366 | }
|
| 367 | }
|
| 368 |
|