|
| 1 | +package com.hanprogramer.androids.entities.android.util; |
| 2 | + |
| 3 | +import net.minecraft.block.entity.BlockEntity; |
| 4 | +import net.minecraft.entity.Entity; |
| 5 | +import net.minecraft.entity.player.PlayerEntity; |
| 6 | +import net.minecraft.inventory.Inventory; |
| 7 | +import net.minecraft.item.Item; |
| 8 | +import net.minecraft.item.ItemStack; |
| 9 | +import net.minecraft.item.Items; |
| 10 | +import net.minecraft.registry.Registries; |
| 11 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 12 | +import net.minecraft.util.Identifier; |
| 13 | +import net.minecraft.util.math.BlockPos; |
| 14 | +import net.minecraft.world.World; |
| 15 | + |
| 16 | +public class ContainerExtractor { |
| 17 | + |
| 18 | + /** |
| 19 | + * Try to extract up to `wanted` items of type `itemIdString` from the container at `containerPos` |
| 20 | + * and put them into the entity's inventory (fills entity inventory slots when possible). |
| 21 | + * <p> |
| 22 | + * Returns the number of items actually moved. |
| 23 | + * <p> |
| 24 | + * Behavior: |
| 25 | + * - If itemIdString is null/blank or cannot be resolved to a valid Item, no items are moved. |
| 26 | + * - Operates only server-side (returns 0 on client). |
| 27 | + * - If the entity does not provide an Inventory interface, returns 0. |
| 28 | + */ |
| 29 | + public static int takeFromContainerToEntity(Entity entity, World world, BlockPos containerPos, String itemIdString, int wanted) { |
| 30 | + if (world == null || world.isClient() || entity == null || containerPos == null) return 0; |
| 31 | + if (wanted <= 0) return 0; |
| 32 | + |
| 33 | + // Resolve item id |
| 34 | + if (itemIdString == null || itemIdString.isBlank()) return 0; |
| 35 | + Identifier id = Identifier.tryParse(itemIdString); |
| 36 | + if (id == null) return 0; |
| 37 | + Item targetItem = Registries.ITEM.get(id); |
| 38 | + if (targetItem == Items.AIR) return 0; |
| 39 | + |
| 40 | + // Ensure container is an Inventory |
| 41 | + BlockEntity be = world.getBlockEntity(containerPos); |
| 42 | + if (!(be instanceof Inventory container)) return 0; |
| 43 | + |
| 44 | + // Ensure entity has inventory to receive items |
| 45 | + if (!(entity instanceof Inventory receiver)) return 0; |
| 46 | + |
| 47 | + int remainingToTake = wanted; |
| 48 | + // Iterate container slots, taking from stacks that match the item |
| 49 | + for (int i = 0; i < container.size() && remainingToTake > 0; i++) { |
| 50 | + ItemStack slotStack = container.getStack(i); |
| 51 | + if (slotStack == null || slotStack.isEmpty()) continue; |
| 52 | + if (!slotStack.isOf(targetItem)) continue; |
| 53 | + |
| 54 | + // How many we can take from this slot |
| 55 | + int takeFromSlot = Math.min(remainingToTake, slotStack.getCount()); |
| 56 | + |
| 57 | + // Create a stack to move (respect maxCount of item) |
| 58 | + ItemStack moving = slotStack.copy(); |
| 59 | + moving.setCount(takeFromSlot); |
| 60 | + |
| 61 | + // Try to insert into receiver inventory (merge into existing stacks first, then empty slots) |
| 62 | + int stillRemaining = insertStackIntoInventory(receiver, moving); |
| 63 | + |
| 64 | + int actuallyMoved = takeFromSlot - stillRemaining; |
| 65 | + if (actuallyMoved > 0) { |
| 66 | + // decrement container slot accordingly |
| 67 | + ItemStack newSlot = slotStack.copy(); |
| 68 | + newSlot.decrement(actuallyMoved); |
| 69 | + if (newSlot.isEmpty()) container.setStack(i, ItemStack.EMPTY); |
| 70 | + else container.setStack(i, newSlot); |
| 71 | + |
| 72 | + remainingToTake -= actuallyMoved; |
| 73 | + } |
| 74 | + |
| 75 | + // If receiver couldn't accept any of this slot, try next slot |
| 76 | + } |
| 77 | + |
| 78 | + // mark inventories dirty / sync players |
| 79 | + container.markDirty(); |
| 80 | + |
| 81 | + // If receiver is a custom Inventory object with markDirty, call it (we already modified it via setStack) |
| 82 | + try { |
| 83 | + receiver.markDirty(); |
| 84 | + } catch (Throwable ignored) { } |
| 85 | + |
| 86 | + return wanted - remainingToTake; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Insert as much of `toInsert` as possible into `inv`. |
| 91 | + * Mutates `toInsert` to reflect remaining items; returns remaining count. |
| 92 | + * <p> |
| 93 | + * Insertion policy: |
| 94 | + * - First merge into existing compatible stacks (same item and ItemStack.areItemsAndComponentsEqual) |
| 95 | + * - Then place into empty slots up to item max stack size |
| 96 | + */ |
| 97 | + private static int insertStackIntoInventory(Inventory inv, ItemStack toInsert) { |
| 98 | + if (toInsert == null || toInsert.isEmpty()) return 0; |
| 99 | + int remaining = toInsert.getCount(); |
| 100 | + |
| 101 | + // Merge into existing stacks |
| 102 | + for (int i = 0; i < inv.size() && remaining > 0; i++) { |
| 103 | + ItemStack slot = inv.getStack(i); |
| 104 | + if (slot == null || slot.isEmpty()) continue; |
| 105 | + if (!slot.isOf(toInsert.getItem())) continue; |
| 106 | + if (!ItemStack.areItemsAndComponentsEqual(slot, toInsert)) continue; |
| 107 | + |
| 108 | + int maxStack = Math.min(slot.getMaxCount(), toInsert.getMaxCount()); |
| 109 | + int transferable = Math.min(remaining, maxStack - slot.getCount()); |
| 110 | + if (transferable <= 0) continue; |
| 111 | + |
| 112 | + ItemStack newSlot = slot.copy(); |
| 113 | + newSlot.increment(transferable); |
| 114 | + inv.setStack(i, newSlot); |
| 115 | + remaining -= transferable; |
| 116 | + } |
| 117 | + |
| 118 | + // Put into empty slots |
| 119 | + for (int i = 0; i < inv.size() && remaining > 0; i++) { |
| 120 | + ItemStack slot = inv.getStack(i); |
| 121 | + if (slot != null && !slot.isEmpty()) continue; |
| 122 | + |
| 123 | + int putAmount = Math.min(remaining, toInsert.getMaxCount()); |
| 124 | + ItemStack toPut = toInsert.copy(); |
| 125 | + toPut.setCount(putAmount); |
| 126 | + inv.setStack(i, toPut); |
| 127 | + remaining -= putAmount; |
| 128 | + } |
| 129 | + |
| 130 | + return remaining; |
| 131 | + } |
| 132 | +} |
0 commit comments