| 1 | /*
|
| 2 | * This file is part of the Combatant Client distribution.
|
| 3 | * Copyright (c) 2026 pivosos2007.
|
| 4 | *
|
| 5 | * Licensed under the GNU General Public License v3.0.
|
| 6 | */
|
| 7 |
|
| 8 | package combatant.client.util.player.inventory;
|
| 9 |
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.multiplayer.MultiPlayerGameMode;
|
| 12 | import net.minecraft.client.player.LocalPlayer;
|
| 13 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 14 | import net.minecraft.network.protocol.game.*;
|
| 15 | import net.minecraft.world.Container;
|
| 16 | import net.minecraft.world.InteractionHand;
|
| 17 | import net.minecraft.world.InteractionResult;
|
| 18 | import net.minecraft.world.entity.player.Input;
|
| 19 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 20 | import net.minecraft.world.inventory.ContainerInput;
|
| 21 | import net.minecraft.world.inventory.Slot;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import combatant.client.events.EventHandler;
|
| 24 | import combatant.client.events.impl.GameTickEvent;
|
| 25 | import combatant.client.events.impl.MovementInputEvent;
|
| 26 | import combatant.client.events.impl.PacketEvent;
|
| 27 | import combatant.client.events.impl.SprintControlEvent;
|
| 28 | import combatant.client.mixins.accessors.MultiPlayerGameModeAccessor;
|
| 29 | import combatant.client.mixins.accessors.PlayerInventoryAccessor;
|
| 30 | import combatant.client.mixins.accessors.SlotAccessor;
|
| 31 |
|
| 32 | import java.util.ArrayDeque;
|
| 33 | import java.util.Deque;
|
| 34 | import java.util.Set;
|
| 35 | import java.util.UUID;
|
| 36 | import java.util.function.BooleanSupplier;
|
| 37 | import java.util.function.Predicate;
|
| 38 |
|
| 39 | public final class InventorySwap {
|
| 40 | public static final InventorySwap INSTANCE = new InventorySwap();
|
| 41 |
|
| 42 | private static final int OFFHAND_SCREEN_SLOT = 45;
|
| 43 | private static final int OFFHAND_SWAP_BUTTON = 40;
|
| 44 |
|
| 45 | private final Minecraft mc = Minecraft.getInstance();
|
| 46 | private final Deque<QueuedAction> strictQueue = new ArrayDeque<>();
|
| 47 | private int lastServerSelectedSlot = -1;
|
| 48 | private int savedSelectedSlot = -1;
|
| 49 | private int internalSwapDepth;
|
| 50 | private HotbarLease hotbarLease;
|
| 51 | private InventorySwapPolicy defaultPolicy = InventorySwapPolicy.NONE;
|
| 52 | private InventorySearchScope defaultScope = InventorySearchScope.FULL;
|
| 53 | private InventorySwapVisibility defaultVisibility = InventorySwapVisibility.SILENT;
|
| 54 | private boolean restoreByDefault = true;
|
| 55 | private boolean preferHotbar = true;
|
| 56 | private int legitWaitTicks = 2;
|
| 57 | private int strictInventoryWaitTicks = 1;
|
| 58 | private int strictMovementLockTicks = 2;
|
| 59 | private int packetTick;
|
| 60 | private int lastUseItemTick = Integer.MIN_VALUE;
|
| 61 | private int lastEntityActionTick = Integer.MIN_VALUE;
|
| 62 | private int lastBlockActionTick = Integer.MIN_VALUE;
|
| 63 | private boolean lastInputMoving;
|
| 64 | private int movementLockTicks;
|
| 65 | private boolean sendingLockedInput;
|
| 66 |
|
| 67 | private InventorySwap() {
|
| 68 | }
|
| 69 |
|
| 70 | public static int mapInventoryToScreenSlot(int inventorySlot) {
|
| 71 | LocalPlayer player = INSTANCE.mc.player;
|
| 72 | AbstractContainerMenu handler = player != null ? player.containerMenu : null;
|
| 73 | Container inventory = player != null ? player.getInventory() : null;
|
| 74 | return mapInventoryToScreenSlot(handler, inventory, inventorySlot);
|
| 75 | }
|
| 76 |
|
| 77 | public static int mapInventoryToScreenSlot(AbstractContainerMenu handler, Container playerInventory, int inventorySlot) {
|
| 78 | if (inventorySlot < 0 || inventorySlot >= 41) return -1;
|
| 79 | if (handler != null && playerInventory != null) {
|
| 80 | for (Slot slot : handler.slots) {
|
| 81 | if (slot == null || slot.container != playerInventory) continue;
|
| 82 | if (((SlotAccessor) slot).combatant$getIndex() == inventorySlot) {
|
| 83 | return slot.index;
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 | return legacyPlayerScreenSlot(inventorySlot);
|
| 88 | }
|
| 89 |
|
| 90 | public static int mapHotbarToScreenSlot(int hotbarSlot) {
|
| 91 | if (!isHotbarSlot(hotbarSlot)) return -1;
|
| 92 | return mapInventoryToScreenSlot(hotbarSlot);
|
| 93 | }
|
| 94 |
|
| 95 | public static int playerInventoryStart(AbstractContainerMenu handler) {
|
| 96 | if (handler == null) return -1;
|
| 97 | LocalPlayer player = INSTANCE.mc.player;
|
| 98 | Container inventory = player != null ? player.getInventory() : null;
|
| 99 | if (handler != null && inventory != null) {
|
| 100 | int min = Integer.MAX_VALUE;
|
| 101 | for (Slot slot : handler.slots) {
|
| 102 | if (slot == null || slot.container != inventory) continue;
|
| 103 | int index = ((SlotAccessor) slot).combatant$getIndex();
|
| 104 | if (index >= 9 && index < 36) {
|
| 105 | min = Math.min(min, slot.index);
|
| 106 | }
|
| 107 | }
|
| 108 | if (min != Integer.MAX_VALUE) return min;
|
| 109 | }
|
| 110 | return handler.slots.size() - 36;
|
| 111 | }
|
| 112 |
|
| 113 | public static int hotbarStart(AbstractContainerMenu handler) {
|
| 114 | if (handler == null) return -1;
|
| 115 | LocalPlayer player = INSTANCE.mc.player;
|
| 116 | Container inventory = player != null ? player.getInventory() : null;
|
| 117 | if (handler != null && inventory != null) {
|
| 118 | int min = Integer.MAX_VALUE;
|
| 119 | for (Slot slot : handler.slots) {
|
| 120 | if (slot == null || slot.container != inventory) continue;
|
| 121 | int index = ((SlotAccessor) slot).combatant$getIndex();
|
| 122 | if (index >= 0 && index < 9) {
|
| 123 | min = Math.min(min, slot.index);
|
| 124 | }
|
| 125 | }
|
| 126 | if (min != Integer.MAX_VALUE) return min;
|
| 127 | }
|
| 128 | return playerInventoryStart(handler) + 27;
|
| 129 | }
|
| 130 |
|
| 131 | private static int legacyPlayerScreenSlot(int inventorySlot) {
|
| 132 | if (inventorySlot == 40) return OFFHAND_SCREEN_SLOT;
|
| 133 | return inventorySlot < 9 ? inventorySlot + 36 : inventorySlot;
|
| 134 | }
|
| 135 |
|
| 136 | private static boolean isHotbarSlot(int slot) {
|
| 137 | return slot >= 0 && slot <= 8;
|
| 138 | }
|
| 139 |
|
| 140 | private static boolean isSwapButton(int button) {
|
| 141 | return isHotbarSlot(button) || button == OFFHAND_SWAP_BUTTON;
|
| 142 | }
|
| 143 |
|
| 144 | @EventHandler
|
| 145 | private void onPacketSend(PacketEvent.Send event) {
|
| 146 | Object packet = event.getPacket();
|
| 147 | if (packet instanceof ServerboundSetCarriedItemPacket selectedSlot) {
|
| 148 | lastServerSelectedSlot = selectedSlot.getSlot();
|
| 149 | } else if (packet instanceof ServerboundMovePlayerPacket || packet instanceof ServerboundClientTickEndPacket) {
|
| 150 | packetTick++;
|
| 151 | } else if (packet instanceof ServerboundPlayerInputPacket(Input input)) {
|
| 152 | boolean moving = input.forward() || input.backward() || input.left() || input.right();
|
| 153 | if (isMovementLocked() && !sendingLockedInput && moving && mc.getConnection() != null) {
|
| 154 | event.cancel();
|
| 155 | sendingLockedInput = true;
|
| 156 | try {
|
| 157 | mc.getConnection().send(new ServerboundPlayerInputPacket(
|
| 158 | new Input(false, false, false, false, false, false, false)
|
| 159 | ));
|
| 160 | } finally {
|
| 161 | sendingLockedInput = false;
|
| 162 | }
|
| 163 | lastInputMoving = false;
|
| 164 | return;
|
| 165 | }
|
| 166 | lastInputMoving = moving;
|
| 167 | } else if (packet instanceof ServerboundUseItemPacket) {
|
| 168 | lastUseItemTick = packetTick;
|
| 169 | } else if (packet instanceof ServerboundSwingPacket) {
|
| 170 | // MultiActionsE checks animation while using item; the safety gate uses player.isUsingItem().
|
| 171 | } else if (packet instanceof ServerboundInteractPacket) {
|
| 172 | lastEntityActionTick = packetTick;
|
| 173 | } else if (packet instanceof ServerboundUseItemOnPacket) {
|
| 174 | lastBlockActionTick = packetTick;
|
| 175 | } else if (packet instanceof ServerboundPlayerActionPacket action
|
| 176 | && (action.getAction() == ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK
|
| 177 | || action.getAction() == ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK)) {
|
| 178 | lastBlockActionTick = packetTick;
|
| 179 | } else if (packet instanceof ServerboundContainerClickPacket || packet instanceof ServerboundContainerClosePacket) {
|
| 180 | // Present for explicit packet classification. Grim C/D safety is evaluated before sending.
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @EventHandler
|
| 185 | private void onGameTick(GameTickEvent event) {
|
| 186 | tick();
|
| 187 | if (movementLockTicks > 0) {
|
| 188 | movementLockTicks--;
|
| 189 | }
|
| 190 |
|
| 191 | if (strictQueue.isEmpty()) {
|
| 192 | return;
|
| 193 | }
|
| 194 |
|
| 195 | requestStrictMovementLock();
|
| 196 |
|
| 197 | QueuedAction next = strictQueue.peekFirst();
|
| 198 | if (next == null) return;
|
| 199 |
|
| 200 | if (next.waitTicks > 0) {
|
| 201 | next.waitTicks--;
|
| 202 | return;
|
| 203 | }
|
| 204 |
|
| 205 | if (!next.isSafe()) {
|
| 206 | next.waitTicks = 1;
|
| 207 | return;
|
| 208 | }
|
| 209 |
|
| 210 | strictQueue.removeFirst();
|
| 211 | runGuarded(next.action);
|
| 212 |
|
| 213 | if (strictQueue.isEmpty()) {
|
| 214 | requestMovementLock(1);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | @EventHandler(priority = -10000)
|
| 219 | private void onMovementInput(MovementInputEvent event) {
|
| 220 | if (!isMovementLocked()) return;
|
| 221 |
|
| 222 | event.setForward(false);
|
| 223 | event.setBackward(false);
|
| 224 | event.setLeft(false);
|
| 225 | event.setRight(false);
|
| 226 | event.setJump(false);
|
| 227 | event.setSneak(false);
|
| 228 | event.setSprint(false);
|
| 229 | lastInputMoving = false;
|
| 230 | }
|
| 231 |
|
| 232 | @EventHandler(priority = -10000)
|
| 233 | private void onSprintControl(SprintControlEvent event) {
|
| 234 | if (isMovementLocked()) {
|
| 235 | event.setSprint(false);
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | public boolean isInternalSwap() {
|
| 240 | return internalSwapDepth > 0;
|
| 241 | }
|
| 242 |
|
| 243 | public boolean hasPendingStrictActions() {
|
| 244 | return !strictQueue.isEmpty();
|
| 245 | }
|
| 246 |
|
| 247 | public void clearPendingStrictActions() {
|
| 248 | strictQueue.clear();
|
| 249 | movementLockTicks = 0;
|
| 250 | }
|
| 251 |
|
| 252 | public boolean canRunStrict(InventoryActionKind kind) {
|
| 253 | return isSafeFor(kind, InventorySwapPolicy.GRIM_STRICT);
|
| 254 | }
|
| 255 |
|
| 256 | public InventorySwapPolicy defaultPolicy() {
|
| 257 | return defaultPolicy;
|
| 258 | }
|
| 259 |
|
| 260 | public void setDefaultPolicy(InventorySwapPolicy defaultPolicy) {
|
| 261 | this.defaultPolicy = defaultPolicy != null ? defaultPolicy : InventorySwapPolicy.NONE;
|
| 262 | }
|
| 263 |
|
| 264 | public InventorySearchScope defaultScope() {
|
| 265 | return defaultScope;
|
| 266 | }
|
| 267 |
|
| 268 | public void setDefaultScope(InventorySearchScope defaultScope) {
|
| 269 | this.defaultScope = defaultScope != null ? defaultScope : InventorySearchScope.FULL;
|
| 270 | }
|
| 271 |
|
| 272 | public InventorySwapVisibility defaultVisibility() {
|
| 273 | return defaultVisibility;
|
| 274 | }
|
| 275 |
|
| 276 | public void setDefaultVisibility(InventorySwapVisibility defaultVisibility) {
|
| 277 | this.defaultVisibility = defaultVisibility != null ? defaultVisibility : InventorySwapVisibility.SILENT;
|
| 278 | }
|
| 279 |
|
| 280 | public boolean restoreByDefault() {
|
| 281 | return restoreByDefault;
|
| 282 | }
|
| 283 |
|
| 284 | public void setRestoreByDefault(boolean restoreByDefault) {
|
| 285 | this.restoreByDefault = restoreByDefault;
|
| 286 | }
|
| 287 |
|
| 288 | public boolean preferHotbar() {
|
| 289 | return preferHotbar;
|
| 290 | }
|
| 291 |
|
| 292 | public void setPreferHotbar(boolean preferHotbar) {
|
| 293 | this.preferHotbar = preferHotbar;
|
| 294 | }
|
| 295 |
|
| 296 | public int legitWaitTicks() {
|
| 297 | return legitWaitTicks;
|
| 298 | }
|
| 299 |
|
| 300 | public void setLegitWaitTicks(int legitWaitTicks) {
|
| 301 | this.legitWaitTicks = Math.max(0, legitWaitTicks);
|
| 302 | }
|
| 303 |
|
| 304 | public int strictInventoryWaitTicks() {
|
| 305 | return strictInventoryWaitTicks;
|
| 306 | }
|
| 307 |
|
| 308 | public void setStrictInventoryWaitTicks(int strictInventoryWaitTicks) {
|
| 309 | this.strictInventoryWaitTicks = Math.max(0, strictInventoryWaitTicks);
|
| 310 | }
|
| 311 |
|
| 312 | public int strictMovementLockTicks() {
|
| 313 | return strictMovementLockTicks;
|
| 314 | }
|
| 315 |
|
| 316 | public void setStrictMovementLockTicks(int strictMovementLockTicks) {
|
| 317 | this.strictMovementLockTicks = Math.max(0, strictMovementLockTicks);
|
| 318 | }
|
| 319 |
|
| 320 | public void resetDefaults() {
|
| 321 | defaultPolicy = InventorySwapPolicy.NONE;
|
| 322 | defaultScope = InventorySearchScope.FULL;
|
| 323 | defaultVisibility = InventorySwapVisibility.SILENT;
|
| 324 | restoreByDefault = true;
|
| 325 | preferHotbar = true;
|
| 326 | legitWaitTicks = 2;
|
| 327 | strictInventoryWaitTicks = 1;
|
| 328 | strictMovementLockTicks = 2;
|
| 329 | }
|
| 330 |
|
| 331 | public int serverSelectedSlot() {
|
| 332 | HotbarLease active = activeLease();
|
| 333 | if (active != null) return active.enforcedSlot;
|
| 334 | if (lastServerSelectedSlot >= 0 && lastServerSelectedSlot <= 8) return lastServerSelectedSlot;
|
| 335 | return clientSelectedSlot();
|
| 336 | }
|
| 337 |
|
| 338 | public int effectiveSelectedSlot() {
|
| 339 | HotbarLease active = activeLease();
|
| 340 | return active != null ? active.enforcedSlot : clientSelectedSlot();
|
| 341 | }
|
| 342 |
|
| 343 | public int clientSelectedSlot() {
|
| 344 | LocalPlayer player = mc.player;
|
| 345 | if (player == null) return 0;
|
| 346 | return ((PlayerInventoryAccessor) player.getInventory()).combatant$getSelectedSlot();
|
| 347 | }
|
| 348 |
|
| 349 | public boolean isHotbarLeased() {
|
| 350 | return activeLease() != null;
|
| 351 | }
|
| 352 |
|
| 353 | public boolean isHotbarLeasedBy(Object owner) {
|
| 354 | HotbarLease active = activeLease();
|
| 355 | return active != null && active.owner == owner;
|
| 356 | }
|
| 357 |
|
| 358 | public boolean leaseHotbar(Object owner, int slot, int ticksUntilReset) {
|
| 359 | LocalPlayer player = mc.player;
|
| 360 | if (player == null || !isHotbarSlot(slot)) return false;
|
| 361 |
|
| 362 | int resetAfterAge = player.tickCount + Math.max(0, ticksUntilReset) + 1;
|
| 363 | hotbarLease = new HotbarLease(player.getUUID(), owner, slot, clientSelectedSlot(), resetAfterAge);
|
| 364 | if (!sendSelectedSlotIfNeeded(slot)) {
|
| 365 | hotbarLease = null;
|
| 366 | return false;
|
| 367 | }
|
| 368 | return true;
|
| 369 | }
|
| 370 |
|
| 371 | public void releaseHotbar(Object owner) {
|
| 372 | HotbarLease active = activeLease();
|
| 373 | if (active != null && active.owner == owner) {
|
| 374 | clearLease(active, true);
|
| 375 | }
|
| 376 | }
|
| 377 |
|
| 378 | public void tick() {
|
| 379 | activeLease();
|
| 380 | }
|
| 381 |
|
| 382 | public void saveSelectedSlot() {
|
| 383 | savedSelectedSlot = clientSelectedSlot();
|
| 384 | }
|
| 385 |
|
| 386 | public void restoreSavedSelectedSlot() {
|
| 387 | if (isHotbarSlot(savedSelectedSlot)) {
|
| 388 | selectHotbar(savedSelectedSlot);
|
| 389 | }
|
| 390 | savedSelectedSlot = -1;
|
| 391 | }
|
| 392 |
|
| 393 | public boolean selectHotbar(int slot) {
|
| 394 | LocalPlayer player = mc.player;
|
| 395 | if (player == null || mc.getConnection() == null || !isHotbarSlot(slot)) return false;
|
| 396 |
|
| 397 | int current = clientSelectedSlot();
|
| 398 | if (current != slot) {
|
| 399 | ((PlayerInventoryAccessor) player.getInventory()).combatant$setSelectedSlot(slot);
|
| 400 | }
|
| 401 |
|
| 402 | syncInteractionManagerSlot(slot);
|
| 403 | if (lastServerSelectedSlot != slot) {
|
| 404 | mc.getConnection().send(new ServerboundSetCarriedItemPacket(slot));
|
| 405 | lastServerSelectedSlot = slot;
|
| 406 | }
|
| 407 | return true;
|
| 408 | }
|
| 409 |
|
| 410 | public boolean sendSelectedSlotIfNeeded(int slot) {
|
| 411 | if (mc.player == null || mc.getConnection() == null || !isHotbarSlot(slot)) return false;
|
| 412 | if (lastServerSelectedSlot != slot) {
|
| 413 | mc.getConnection().send(new ServerboundSetCarriedItemPacket(slot));
|
| 414 | lastServerSelectedSlot = slot;
|
| 415 | }
|
| 416 | syncInteractionManagerSlot(slot);
|
| 417 | return true;
|
| 418 | }
|
| 419 |
|
| 420 | public boolean clickSwap(int containerSlot) {
|
| 421 | return clickSwap(containerSlot, clientSelectedSlot());
|
| 422 | }
|
| 423 |
|
| 424 | public boolean clickSwap(int containerSlot, int hotbarButton) {
|
| 425 | LocalPlayer player = mc.player;
|
| 426 | MultiPlayerGameMode interaction = mc.gameMode;
|
| 427 | AbstractContainerMenu handler = player != null ? player.containerMenu : null;
|
| 428 | if (player == null || interaction == null || handler == null) return false;
|
| 429 | if (!isSwapButton(hotbarButton) || containerSlot < 0) return false;
|
| 430 |
|
| 431 | beginInternalSwap();
|
| 432 | try {
|
| 433 | interaction.handleContainerInput(handler.containerId, containerSlot, hotbarButton, ContainerInput.SWAP, player);
|
| 434 | return true;
|
| 435 | } finally {
|
| 436 | endInternalSwap();
|
| 437 | }
|
| 438 | }
|
| 439 |
|
| 440 | public boolean pickupSwap(int slotA, int slotB) {
|
| 441 | LocalPlayer player = mc.player;
|
| 442 | MultiPlayerGameMode interaction = mc.gameMode;
|
| 443 | AbstractContainerMenu handler = player != null ? player.containerMenu : null;
|
| 444 | if (player == null || interaction == null || handler == null) return false;
|
| 445 | if (slotA < 0 || slotB < 0 || slotA == slotB) return false;
|
| 446 |
|
| 447 | beginInternalSwap();
|
| 448 | try {
|
| 449 | interaction.handleContainerInput(handler.containerId, slotA, 0, ContainerInput.PICKUP, player);
|
| 450 | interaction.handleContainerInput(handler.containerId, slotB, 0, ContainerInput.PICKUP, player);
|
| 451 | interaction.handleContainerInput(handler.containerId, slotA, 0, ContainerInput.PICKUP, player);
|
| 452 | return true;
|
| 453 | } finally {
|
| 454 | endInternalSwap();
|
| 455 | }
|
| 456 | }
|
| 457 |
|
| 458 | public boolean swapScreenSlots(int slotA, int slotB) {
|
| 459 | return command(InventoryActionKind.INVENTORY_CLICK, () -> pickupSwap(slotA, slotB));
|
| 460 | }
|
| 461 |
|
| 462 | public boolean swapInventoryToHotbar(int inventorySlot, int hotbarSlot) {
|
| 463 | if (!isHotbarSlot(hotbarSlot) || inventorySlot < 0 || inventorySlot >= 36) return false;
|
| 464 | if (inventorySlot == hotbarSlot) return selectHotbar(hotbarSlot);
|
| 465 |
|
| 466 | int sourceScreenSlot = mapInventoryToScreenSlot(inventorySlot);
|
| 467 | int targetScreenSlot = mapHotbarToScreenSlot(hotbarSlot);
|
| 468 | return command(InventoryActionKind.INVENTORY_CLICK, () -> {
|
| 469 | pickupSwap(sourceScreenSlot, targetScreenSlot);
|
| 470 | selectHotbar(hotbarSlot);
|
| 471 | });
|
| 472 | }
|
| 473 |
|
| 474 | public boolean swapInventoryToOffhand(int inventorySlot) {
|
| 475 | return swapInventoryToOffhand(inventorySlot, null);
|
| 476 | }
|
| 477 |
|
| 478 | public boolean swapInventoryToOffhand(int inventorySlot, Runnable afterSwap) {
|
| 479 | if (inventorySlot < 0 || inventorySlot >= 36) return false;
|
| 480 | int sourceScreenSlot = mapInventoryToScreenSlot(inventorySlot);
|
| 481 | return command(InventoryActionKind.INVENTORY_CLICK, () -> {
|
| 482 | clickSwap(sourceScreenSlot, OFFHAND_SWAP_BUTTON);
|
| 483 | if (afterSwap != null) {
|
| 484 | afterSwap.run();
|
| 485 | }
|
| 486 | });
|
| 487 | }
|
| 488 |
|
| 489 | public boolean dropStack(Slot slot) {
|
| 490 | LocalPlayer player = mc.player;
|
| 491 | MultiPlayerGameMode interaction = mc.gameMode;
|
| 492 | AbstractContainerMenu handler = player != null ? player.containerMenu : null;
|
| 493 | if (player == null || interaction == null || handler == null || slot == null) return false;
|
| 494 | int slotId = slot.index;
|
| 495 | if (slotId < 0 || slotId >= handler.slots.size()) return false;
|
| 496 |
|
| 497 | return command(InventoryActionKind.INVENTORY_CLICK, () -> {
|
| 498 | beginInternalSwap();
|
| 499 | try {
|
| 500 | interaction.handleContainerInput(handler.containerId, slotId, 1, ContainerInput.THROW, player);
|
| 501 | } finally {
|
| 502 | endInternalSwap();
|
| 503 | }
|
| 504 | });
|
| 505 | }
|
| 506 |
|
| 507 | public int quickMoveMatchingFromContainer(Set<String> itemIds) {
|
| 508 | LocalPlayer player = mc.player;
|
| 509 | MultiPlayerGameMode interaction = mc.gameMode;
|
| 510 | AbstractContainerMenu handler = player != null ? player.containerMenu : null;
|
| 511 | if (player == null || interaction == null || handler == null || itemIds == null || itemIds.isEmpty()) return 0;
|
| 512 |
|
| 513 | int moved = 0;
|
| 514 | int playerStart = playerInventoryStart(handler);
|
| 515 | for (int slotId = 0; slotId < playerStart; slotId++) {
|
| 516 | Slot slot = handler.slots.get(slotId);
|
| 517 | if (slot == null || !slot.hasItem()) continue;
|
| 518 |
|
| 519 | String itemId = BuiltInRegistries.ITEM.getKey(slot.getItem().getItem()).toString().toLowerCase();
|
| 520 | if (!itemIds.contains(itemId)) continue;
|
| 521 |
|
| 522 | int finalSlotId = slotId;
|
| 523 | if (command(InventoryActionKind.INVENTORY_CLICK, () -> {
|
| 524 | beginInternalSwap();
|
| 525 | try {
|
| 526 | interaction.handleContainerInput(handler.containerId, finalSlotId, 0, ContainerInput.QUICK_MOVE, player);
|
| 527 | } finally {
|
| 528 | endInternalSwap();
|
| 529 | }
|
| 530 | })) {
|
| 531 | moved++;
|
| 532 | }
|
| 533 | }
|
| 534 | return moved;
|
| 535 | }
|
| 536 |
|
| 537 | public boolean withSwap(SlotResult result, InventorySwapMode mode, Runnable action) {
|
| 538 | if (result == null || !result.found()) return false;
|
| 539 | return withSwap(result.slot(), mode, action);
|
| 540 | }
|
| 541 |
|
| 542 | public boolean withSwap(SlotResult result, InventorySwapMode mode, InventorySwapPolicy policy,
|
| 543 | InventoryActionKind kind, Runnable action) {
|
| 544 | if (result == null || !result.found()) return false;
|
| 545 | return withSwap(result.slot(), mode, policy, kind, action);
|
| 546 | }
|
| 547 |
|
| 548 | public boolean withSwap(int slot, InventorySwapMode mode, Runnable action) {
|
| 549 | if (action == null || mode == null) return false;
|
| 550 |
|
| 551 | beginInternalSwap();
|
| 552 | try {
|
| 553 | return switch (mode) {
|
| 554 | case NORMAL -> runNormal(slot, action);
|
| 555 | case SILENT -> runSilent(slot, action);
|
| 556 | case INVENTORY_NORMAL -> runInventoryNormal(slot, action);
|
| 557 | case INVENTORY_SILENT -> runInventorySilent(slot, action);
|
| 558 | case SILENT_FULL ->
|
| 559 | withSwap(slot, isHotbarSlot(slot) ? InventorySwapMode.SILENT : InventorySwapMode.INVENTORY_SILENT, action);
|
| 560 | case NORMAL_FULL ->
|
| 561 | withSwap(slot, isHotbarSlot(slot) ? InventorySwapMode.NORMAL : InventorySwapMode.INVENTORY_NORMAL, action);
|
| 562 | };
|
| 563 | } finally {
|
| 564 | endInternalSwap();
|
| 565 | }
|
| 566 | }
|
| 567 |
|
| 568 | public boolean withSwap(int slot, InventorySwapMode mode, InventorySwapPolicy policy,
|
| 569 | InventoryActionKind kind, Runnable action) {
|
| 570 | if (policy == null || policy == InventorySwapPolicy.NONE) {
|
| 571 | return withSwap(slot, mode, action);
|
| 572 | }
|
| 573 | if (action == null || mode == null) return false;
|
| 574 |
|
| 575 | Runnable wrapped = () -> withSwap(slot, mode, action);
|
| 576 | BooleanSupplier safety = () -> isSafeForSwap(slot, mode, kind, policy);
|
| 577 | if (safety.getAsBoolean()) {
|
| 578 | runGuarded(wrapped);
|
| 579 | return true;
|
| 580 | }
|
| 581 |
|
| 582 | strictQueue.addLast(new QueuedAction(kind, policy, wrapped, initialWaitTicks(policy, kind), safety));
|
| 583 | requestStrictMovementLock();
|
| 584 | return true;
|
| 585 | }
|
| 586 |
|
| 587 | public SlotResult findSlotForMode(Predicate<ItemStack> predicate, InventorySwapMode mode) {
|
| 588 | if (predicate == null || mode == null) return SlotResult.empty();
|
| 589 | return switch (mode) {
|
| 590 | case NORMAL, SILENT -> findHotbar(predicate);
|
| 591 | case INVENTORY_NORMAL, INVENTORY_SILENT -> findInventory(predicate);
|
| 592 | case SILENT_FULL, NORMAL_FULL -> {
|
| 593 | SlotResult hotbar = findHotbar(predicate);
|
| 594 | yield hotbar.found() ? hotbar : findInventory(predicate);
|
| 595 | }
|
| 596 | };
|
| 597 | }
|
| 598 |
|
| 599 | public boolean withFoundSlot(Predicate<ItemStack> predicate, InventorySwapMode mode, Runnable action) {
|
| 600 | SlotResult result = findSlotForMode(predicate, mode);
|
| 601 | return result.found() && withSwap(result, mode, action);
|
| 602 | }
|
| 603 |
|
| 604 | public boolean withFoundSlot(Predicate<ItemStack> predicate, InventorySwapMode mode, InventorySwapPolicy policy,
|
| 605 | InventoryActionKind kind, Runnable action) {
|
| 606 | SlotResult result = findSlotForMode(predicate, mode);
|
| 607 | return result.found() && withSwap(result, mode, policy, kind, action);
|
| 608 | }
|
| 609 |
|
| 610 | public boolean execute(InventorySwapRequest request) {
|
| 611 | if (request == null || request.predicate() == null || request.action() == null) return false;
|
| 612 |
|
| 613 | InventorySearchScope scope = resolveScope(request);
|
| 614 | InventorySwapMode mode = resolveMode(request, scope);
|
| 615 | SlotResult result = findSlotForRequest(request, mode, scope);
|
| 616 | if (!result.found()) return false;
|
| 617 |
|
| 618 | InventorySwapPolicy policy = request.policy(defaultPolicy);
|
| 619 | return withSwap(result, mode, policy, request.actionKind(), request.action());
|
| 620 | }
|
| 621 |
|
| 622 | public boolean command(InventorySwapRequest request) {
|
| 623 | return execute(request);
|
| 624 | }
|
| 625 |
|
| 626 | public boolean command(InventoryActionKind actionKind, Runnable action) {
|
| 627 | return command(actionKind, defaultPolicy, action);
|
| 628 | }
|
| 629 |
|
| 630 | public boolean command(InventoryActionKind actionKind, InventorySwapPolicy policy, Runnable action) {
|
| 631 | if (action == null) return false;
|
| 632 | InventoryActionKind kind = actionKind != null ? actionKind : InventoryActionKind.GENERIC;
|
| 633 | InventorySwapPolicy resolvedPolicy = policy != null ? policy : defaultPolicy;
|
| 634 |
|
| 635 | if (resolvedPolicy == InventorySwapPolicy.NONE || isSafeFor(kind, resolvedPolicy)) {
|
| 636 | runGuarded(action);
|
| 637 | return true;
|
| 638 | }
|
| 639 |
|
| 640 | strictQueue.addLast(new QueuedAction(
|
| 641 | kind,
|
| 642 | resolvedPolicy,
|
| 643 | action,
|
| 644 | initialWaitTicks(resolvedPolicy, kind),
|
| 645 | () -> isSafeFor(kind, resolvedPolicy)
|
| 646 | ));
|
| 647 | requestStrictMovementLock();
|
| 648 | return true;
|
| 649 | }
|
| 650 |
|
| 651 | public boolean command(Predicate<ItemStack> predicate, InventoryActionKind actionKind, Runnable action) {
|
| 652 | return execute(predicate, actionKind, action);
|
| 653 | }
|
| 654 |
|
| 655 | public boolean command(Predicate<ItemStack> predicate, Runnable action) {
|
| 656 | return execute(InventorySwapRequest.builder(predicate, action).build());
|
| 657 | }
|
| 658 |
|
| 659 | public boolean execute(Predicate<ItemStack> predicate, InventoryActionKind actionKind, Runnable action) {
|
| 660 | return execute(InventorySwapRequest.builder(predicate, action)
|
| 661 | .actionKind(actionKind)
|
| 662 | .build());
|
| 663 | }
|
| 664 |
|
| 665 | public boolean executeSilent(Predicate<ItemStack> predicate, InventoryActionKind actionKind, Runnable action) {
|
| 666 | return execute(InventorySwapRequest.builder(predicate, action)
|
| 667 | .scope(InventorySearchScope.FULL)
|
| 668 | .visibility(InventorySwapVisibility.SILENT)
|
| 669 | .restore(true)
|
| 670 | .actionKind(actionKind)
|
| 671 | .build());
|
| 672 | }
|
| 673 |
|
| 674 | public boolean executeNormal(Predicate<ItemStack> predicate, InventoryActionKind actionKind, Runnable action) {
|
| 675 | return execute(InventorySwapRequest.builder(predicate, action)
|
| 676 | .scope(InventorySearchScope.FULL)
|
| 677 | .visibility(InventorySwapVisibility.NORMAL)
|
| 678 | .restore(false)
|
| 679 | .actionKind(actionKind)
|
| 680 | .build());
|
| 681 | }
|
| 682 |
|
| 683 | public boolean closeHandledScreen(InventorySwapPolicy policy) {
|
| 684 | LocalPlayer player = mc.player;
|
| 685 | if (player == null || mc.getConnection() == null || player.containerMenu == null) return false;
|
| 686 |
|
| 687 | Runnable close = () -> mc.getConnection().send(
|
| 688 | new ServerboundContainerClosePacket(player.containerMenu.containerId)
|
| 689 | );
|
| 690 |
|
| 691 | if (policy == null || policy == InventorySwapPolicy.NONE
|
| 692 | || isSafeFor(InventoryActionKind.INVENTORY_CLOSE, policy)) {
|
| 693 | runGuarded(close);
|
| 694 | return true;
|
| 695 | }
|
| 696 |
|
| 697 | strictQueue.addLast(new QueuedAction(
|
| 698 | InventoryActionKind.INVENTORY_CLOSE,
|
| 699 | policy,
|
| 700 | close,
|
| 701 | initialWaitTicks(policy, InventoryActionKind.INVENTORY_CLOSE),
|
| 702 | () -> isSafeFor(InventoryActionKind.INVENTORY_CLOSE, policy)
|
| 703 | ));
|
| 704 | requestStrictMovementLock();
|
| 705 | return true;
|
| 706 | }
|
| 707 |
|
| 708 | public InteractionResult useItem(InteractionHand hand) {
|
| 709 | LocalPlayer player = mc.player;
|
| 710 | MultiPlayerGameMode interaction = mc.gameMode;
|
| 711 | if (player == null || interaction == null || hand == null) return InteractionResult.FAIL;
|
| 712 | return interaction.useItem(player, hand);
|
| 713 | }
|
| 714 |
|
| 715 | public boolean useItemWith(Predicate<ItemStack> predicate, InventorySwapMode mode, InteractionHand hand, boolean swing) {
|
| 716 | return withFoundSlot(predicate, mode, () -> {
|
| 717 | InteractionResult result = useItem(hand);
|
| 718 | if (swing && result.consumesAction() && mc.player != null) {
|
| 719 | mc.player.swing(hand);
|
| 720 | }
|
| 721 | });
|
| 722 | }
|
| 723 |
|
| 724 | public boolean useItemWith(Predicate<ItemStack> predicate, InventorySwapMode mode, InventorySwapPolicy policy,
|
| 725 | InteractionHand hand, boolean swing) {
|
| 726 | return withFoundSlot(predicate, mode, policy, InventoryActionKind.USE_ITEM, () -> {
|
| 727 | InteractionResult result = useItem(hand);
|
| 728 | if (swing && result.consumesAction() && mc.player != null) {
|
| 729 | swingHand(hand, policy);
|
| 730 | }
|
| 731 | });
|
| 732 | }
|
| 733 |
|
| 734 | public boolean swingHand(InteractionHand hand, InventorySwapPolicy policy) {
|
| 735 | if (mc.player == null || hand == null) return false;
|
| 736 | Runnable swing = () -> mc.player.swing(hand);
|
| 737 | if (policy == null || policy == InventorySwapPolicy.NONE || isSafeFor(InventoryActionKind.SWING, policy)) {
|
| 738 | runGuarded(swing);
|
| 739 | return true;
|
| 740 | }
|
| 741 |
|
| 742 | strictQueue.addLast(new QueuedAction(
|
| 743 | InventoryActionKind.SWING,
|
| 744 | policy,
|
| 745 | swing,
|
| 746 | initialWaitTicks(policy, InventoryActionKind.SWING),
|
| 747 | () -> isSafeFor(InventoryActionKind.SWING, policy)
|
| 748 | ));
|
| 749 | requestStrictMovementLock();
|
| 750 | return true;
|
| 751 | }
|
| 752 |
|
| 753 | public SlotResult findHotbar(Predicate<ItemStack> predicate) {
|
| 754 | LocalPlayer player = mc.player;
|
| 755 | if (player == null || predicate == null) return SlotResult.empty();
|
| 756 |
|
| 757 | for (int slot = 0; slot < 9; slot++) {
|
| 758 | ItemStack stack = player.getInventory().getItem(slot);
|
| 759 | if (!stack.isEmpty() && predicate.test(stack)) {
|
| 760 | return new SlotResult(slot, true, stack);
|
| 761 | }
|
| 762 | }
|
| 763 |
|
| 764 | return SlotResult.empty();
|
| 765 | }
|
| 766 |
|
| 767 | public SlotResult findInventory(Predicate<ItemStack> predicate) {
|
| 768 | LocalPlayer player = mc.player;
|
| 769 | if (player == null || predicate == null) return SlotResult.empty();
|
| 770 |
|
| 771 | for (int slot = 9; slot < 36; slot++) {
|
| 772 | ItemStack stack = player.getInventory().getItem(slot);
|
| 773 | if (!stack.isEmpty() && predicate.test(stack)) {
|
| 774 | return new SlotResult(mapInventoryToScreenSlot(slot), true, stack);
|
| 775 | }
|
| 776 | }
|
| 777 |
|
| 778 | return SlotResult.empty();
|
| 779 | }
|
| 780 |
|
| 781 | private boolean runNormal(int slot, Runnable action) {
|
| 782 | if (!isHotbarSlot(slot)) return false;
|
| 783 | if (!selectHotbar(slot)) return false;
|
| 784 | action.run();
|
| 785 | return true;
|
| 786 | }
|
| 787 |
|
| 788 | private boolean runSilent(int slot, Runnable action) {
|
| 789 | if (!isHotbarSlot(slot)) return false;
|
| 790 | int previous = clientSelectedSlot();
|
| 791 |
|
| 792 | if (lastServerSelectedSlot != slot) {
|
| 793 | if (!selectHotbar(slot)) return false;
|
| 794 | }
|
| 795 |
|
| 796 | try {
|
| 797 | action.run();
|
| 798 | return true;
|
| 799 | } finally {
|
| 800 | if (lastServerSelectedSlot != previous) {
|
| 801 | selectHotbar(previous);
|
| 802 | }
|
| 803 | }
|
| 804 | }
|
| 805 |
|
| 806 | private boolean runInventoryNormal(int slot, Runnable action) {
|
| 807 | if (isHotbarSlot(slot)) return runNormal(slot, action);
|
| 808 | if (!clickSwap(slot)) return false;
|
| 809 | action.run();
|
| 810 | return true;
|
| 811 | }
|
| 812 |
|
| 813 | private boolean runInventorySilent(int slot, Runnable action) {
|
| 814 | if (isHotbarSlot(slot)) return runSilent(slot, action);
|
| 815 | int hotbarButton = clientSelectedSlot();
|
| 816 | if (!clickSwap(slot, hotbarButton)) return false;
|
| 817 |
|
| 818 | try {
|
| 819 | action.run();
|
| 820 | return true;
|
| 821 | } finally {
|
| 822 | clickSwap(slot, hotbarButton);
|
| 823 | }
|
| 824 | }
|
| 825 |
|
| 826 | private InventorySearchScope resolveScope(InventorySwapRequest request) {
|
| 827 | return request.scope() != null ? request.scope() : defaultScope;
|
| 828 | }
|
| 829 |
|
| 830 | private InventorySwapVisibility resolveVisibility(InventorySwapRequest request) {
|
| 831 | return request.visibility() != null ? request.visibility() : defaultVisibility;
|
| 832 | }
|
| 833 |
|
| 834 | private InventorySwapMode resolveMode(InventorySwapRequest request, InventorySearchScope scope) {
|
| 835 | InventorySearchScope safeScope = scope != null ? scope : defaultScope;
|
| 836 | boolean restore = request.restore(restoreByDefault);
|
| 837 | boolean silent = resolveVisibility(request) == InventorySwapVisibility.SILENT || restore;
|
| 838 |
|
| 839 | return switch (safeScope) {
|
| 840 | case HOTBAR -> silent ? InventorySwapMode.SILENT : InventorySwapMode.NORMAL;
|
| 841 | case INVENTORY -> silent ? InventorySwapMode.INVENTORY_SILENT : InventorySwapMode.INVENTORY_NORMAL;
|
| 842 | case FULL -> silent ? InventorySwapMode.SILENT_FULL : InventorySwapMode.NORMAL_FULL;
|
| 843 | };
|
| 844 | }
|
| 845 |
|
| 846 | private SlotResult findSlotForRequest(InventorySwapRequest request, InventorySwapMode mode, InventorySearchScope scope) {
|
| 847 | if (scope != InventorySearchScope.FULL || preferHotbar) {
|
| 848 | return findSlotForMode(request.predicate(), mode);
|
| 849 | }
|
| 850 |
|
| 851 | SlotResult inventory = findInventory(request.predicate());
|
| 852 | return inventory.found() ? inventory : findHotbar(request.predicate());
|
| 853 | }
|
| 854 |
|
| 855 | private boolean isSafeFor(InventoryActionKind kind, InventorySwapPolicy policy) {
|
| 856 | if (policy == null || policy == InventorySwapPolicy.NONE) return true;
|
| 857 | LocalPlayer player = mc.player;
|
| 858 | if (player == null) return false;
|
| 859 |
|
| 860 | InventoryActionKind safeKind = kind != null ? kind : InventoryActionKind.GENERIC;
|
| 861 |
|
| 862 | if ((safeKind == InventoryActionKind.INVENTORY_CLICK || safeKind == InventoryActionKind.INVENTORY_CLOSE)
|
| 863 | && isGrimMovingForInventory(player)) {
|
| 864 | return false;
|
| 865 | }
|
| 866 |
|
| 867 | if ((safeKind == InventoryActionKind.ATTACK_ENTITY || safeKind == InventoryActionKind.SWING)
|
| 868 | && player.isUsingItem()) {
|
| 869 | return false;
|
| 870 | }
|
| 871 |
|
| 872 | if ((safeKind == InventoryActionKind.ATTACK_ENTITY || safeKind == InventoryActionKind.ENTITY_INTERACT)
|
| 873 | && lastBlockActionTick == packetTick) {
|
| 874 | return false;
|
| 875 | }
|
| 876 |
|
| 877 | if ((safeKind == InventoryActionKind.BLOCK_INTERACT || safeKind == InventoryActionKind.BLOCK_BREAK)
|
| 878 | && lastEntityActionTick == packetTick) {
|
| 879 | return false;
|
| 880 | }
|
| 881 |
|
| 882 | return safeKind != InventoryActionKind.SWING || lastUseItemTick != packetTick;
|
| 883 | }
|
| 884 |
|
| 885 | private boolean isSafeForSwap(int slot, InventorySwapMode mode, InventoryActionKind kind, InventorySwapPolicy policy) {
|
| 886 | if (!isSafeFor(kind, policy)) return false;
|
| 887 | if (requiresInventoryClick(slot, mode)) {
|
| 888 | return isSafeFor(InventoryActionKind.INVENTORY_CLICK, policy);
|
| 889 | }
|
| 890 | return true;
|
| 891 | }
|
| 892 |
|
| 893 | private boolean requiresInventoryClick(int slot, InventorySwapMode mode) {
|
| 894 | if (mode == null) return false;
|
| 895 | return switch (mode) {
|
| 896 | case INVENTORY_NORMAL, INVENTORY_SILENT -> !isHotbarSlot(slot);
|
| 897 | case SILENT_FULL, NORMAL_FULL -> !isHotbarSlot(slot);
|
| 898 | case NORMAL, SILENT -> false;
|
| 899 | };
|
| 900 | }
|
| 901 |
|
| 902 | private int initialWaitTicks(InventorySwapPolicy policy, InventoryActionKind kind) {
|
| 903 | if (policy == InventorySwapPolicy.LEGIT) return legitWaitTicks;
|
| 904 | if (kind == InventoryActionKind.INVENTORY_CLICK || kind == InventoryActionKind.INVENTORY_CLOSE) {
|
| 905 | return strictInventoryWaitTicks;
|
| 906 | }
|
| 907 | return 0;
|
| 908 | }
|
| 909 |
|
| 910 | private boolean isGrimMovingForInventory(LocalPlayer player) {
|
| 911 | if (isMovementLocked()) return false;
|
| 912 | if (player.isSprinting() && (!player.isSwimming() || player.onGround())) return true;
|
| 913 | if (lastInputMoving) return true;
|
| 914 | if (player.input == null) return false;
|
| 915 | Input input = player.input.keyPresses;
|
| 916 | if (input == null) return false;
|
| 917 | return input.forward() || input.backward() || input.left() || input.right();
|
| 918 | }
|
| 919 |
|
| 920 | private void runGuarded(Runnable action) {
|
| 921 | beginInternalSwap();
|
| 922 | try {
|
| 923 | action.run();
|
| 924 | } finally {
|
| 925 | endInternalSwap();
|
| 926 | }
|
| 927 | }
|
| 928 |
|
| 929 | private void requestStrictMovementLock() {
|
| 930 | requestMovementLock(strictMovementLockTicks);
|
| 931 | }
|
| 932 |
|
| 933 | private void requestMovementLock(int ticks) {
|
| 934 | movementLockTicks = Math.max(movementLockTicks, Math.max(0, ticks));
|
| 935 | if (mc.player != null && mc.player.isSprinting()) {
|
| 936 | mc.player.setSprinting(false);
|
| 937 | }
|
| 938 | }
|
| 939 |
|
| 940 | private boolean isMovementLocked() {
|
| 941 | return movementLockTicks > 0 || !strictQueue.isEmpty();
|
| 942 | }
|
| 943 |
|
| 944 | private HotbarLease activeLease() {
|
| 945 | HotbarLease current = hotbarLease;
|
| 946 | if (current == null) return null;
|
| 947 |
|
| 948 | LocalPlayer player = mc.player;
|
| 949 | if (player == null) {
|
| 950 | hotbarLease = null;
|
| 951 | return null;
|
| 952 | }
|
| 953 |
|
| 954 | if (!player.getUUID().equals(current.playerUuid) || player.tickCount >= current.resetAfterAge) {
|
| 955 | clearLease(current, player.getUUID().equals(current.playerUuid));
|
| 956 | return null;
|
| 957 | }
|
| 958 |
|
| 959 | return current;
|
| 960 | }
|
| 961 |
|
| 962 | private void clearLease(HotbarLease lease, boolean restoreServerSlot) {
|
| 963 | hotbarLease = null;
|
| 964 | if (!restoreServerSlot) return;
|
| 965 |
|
| 966 | int currentClientSlot = clientSelectedSlot();
|
| 967 | if (currentClientSlot != lease.enforcedSlot) {
|
| 968 | sendSelectedSlotIfNeeded(currentClientSlot);
|
| 969 | }
|
| 970 | }
|
| 971 |
|
| 972 | private void beginInternalSwap() {
|
| 973 | internalSwapDepth++;
|
| 974 | }
|
| 975 |
|
| 976 | private void endInternalSwap() {
|
| 977 | if (internalSwapDepth > 0) internalSwapDepth--;
|
| 978 | }
|
| 979 |
|
| 980 | private void syncInteractionManagerSlot(int slot) {
|
| 981 | if (mc.gameMode instanceof MultiPlayerGameModeAccessor accessor) {
|
| 982 | accessor.combatant$setLastSelectedSlot(slot);
|
| 983 | }
|
| 984 | }
|
| 985 |
|
| 986 | private record HotbarLease(UUID playerUuid, Object owner, int enforcedSlot, int clientSlot, int resetAfterAge) {
|
| 987 | }
|
| 988 |
|
| 989 | private static final class QueuedAction {
|
| 990 | private final InventoryActionKind kind;
|
| 991 | private final InventorySwapPolicy policy;
|
| 992 | private final Runnable action;
|
| 993 | private final BooleanSupplier safety;
|
| 994 | private int waitTicks;
|
| 995 |
|
| 996 | private QueuedAction(InventoryActionKind kind, InventorySwapPolicy policy, Runnable action, int waitTicks,
|
| 997 | BooleanSupplier safety) {
|
| 998 | this.kind = kind != null ? kind : InventoryActionKind.GENERIC;
|
| 999 | this.policy = policy != null ? policy : InventorySwapPolicy.NONE;
|
| 1000 | this.action = action;
|
| 1001 | this.safety = safety != null ? safety : () -> true;
|
| 1002 | this.waitTicks = Math.max(0, waitTicks);
|
| 1003 | }
|
| 1004 |
|
| 1005 | private boolean isSafe() {
|
| 1006 | return safety.getAsBoolean();
|
| 1007 | }
|
| 1008 | }
|
| 1009 | }
|
| 1010 |
|