|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC and contributors |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | + |
| 6 | +package net.p3pp3rf1y.sophisticatedcore.inventory; |
| 7 | + |
| 8 | +import com.google.common.base.Preconditions; |
| 9 | +import net.minecraft.core.NonNullList; |
| 10 | +import net.minecraft.core.component.DataComponentType; |
| 11 | +import net.minecraft.world.item.Item; |
| 12 | +import net.minecraft.world.item.ItemStack; |
| 13 | +import net.minecraft.world.item.component.ItemContainerContents; |
| 14 | +import net.neoforged.neoforge.common.MutableDataComponentHolder; |
| 15 | +import net.neoforged.neoforge.items.IItemHandlerModifiable; |
| 16 | + |
| 17 | +public class StatefulComponentItemHandler implements IItemHandlerModifiable, ISlotChangeListener { |
| 18 | + protected NonNullList<ItemStack> stacks; |
| 19 | + protected final MutableDataComponentHolder parent; |
| 20 | + protected final DataComponentType<ItemContainerContents> component; |
| 21 | + protected final int size; |
| 22 | + |
| 23 | + public StatefulComponentItemHandler(MutableDataComponentHolder parent, DataComponentType<ItemContainerContents> component, int size) { |
| 24 | + this.parent = parent; |
| 25 | + this.component = component; |
| 26 | + this.size = size; |
| 27 | + Preconditions.checkArgument(size <= 256, "The max size of ItemContainerContents is 256 slots."); |
| 28 | + fillStacks(); |
| 29 | + } |
| 30 | + |
| 31 | + private void fillStacks() { |
| 32 | + ItemContainerContents contents = getContents(); |
| 33 | + stacks = NonNullList.withSize(size, ItemStack.EMPTY); |
| 34 | + contents.copyInto(stacks); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public int getSlots() { |
| 39 | + return size; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public ItemStack getStackInSlot(int slot) { |
| 44 | + validateSlotIndex(slot); |
| 45 | + return stacks.get(slot); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void setStackInSlot(int slot, ItemStack stack) { |
| 50 | + validateSlotIndex(slot); |
| 51 | + if (!isItemValid(slot, stack)) { |
| 52 | + throw new RuntimeException("Invalid stack " + stack + " for slot " + slot + ")"); |
| 53 | + } |
| 54 | + ItemStack existing = stacks.get(slot); |
| 55 | + if (!ItemStack.matches(stack, existing)) { |
| 56 | + updateContents(stack, slot); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public ItemStack insertItem(int slot, ItemStack toInsert, boolean simulate) { |
| 62 | + validateSlotIndex(slot); |
| 63 | + |
| 64 | + if (toInsert.isEmpty()) { |
| 65 | + return ItemStack.EMPTY; |
| 66 | + } |
| 67 | + |
| 68 | + if (!isItemValid(slot, toInsert)) { |
| 69 | + return toInsert; |
| 70 | + } |
| 71 | + |
| 72 | + ItemStack existing = stacks.get(slot); |
| 73 | + |
| 74 | + int insertLimit = Math.min(getSlotLimit(slot), toInsert.getMaxStackSize()); |
| 75 | + |
| 76 | + if (!existing.isEmpty()) { |
| 77 | + if (!ItemStack.isSameItemSameComponents(toInsert, existing)) { |
| 78 | + return toInsert; |
| 79 | + } |
| 80 | + |
| 81 | + insertLimit -= existing.getCount(); |
| 82 | + } |
| 83 | + |
| 84 | + if (insertLimit <= 0) { |
| 85 | + return toInsert; |
| 86 | + } |
| 87 | + |
| 88 | + int inserted = Math.min(insertLimit, toInsert.getCount()); |
| 89 | + |
| 90 | + if (!simulate) { |
| 91 | + updateContents(toInsert.copyWithCount(existing.getCount() + inserted), slot); |
| 92 | + } |
| 93 | + |
| 94 | + return toInsert.copyWithCount(toInsert.getCount() - inserted); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public ItemStack extractItem(int slot, int amount, boolean simulate) { |
| 99 | + validateSlotIndex(slot); |
| 100 | + |
| 101 | + if (amount == 0) { |
| 102 | + return ItemStack.EMPTY; |
| 103 | + } |
| 104 | + |
| 105 | + ItemStack existing = stacks.get(slot); |
| 106 | + |
| 107 | + if (existing.isEmpty()) { |
| 108 | + return ItemStack.EMPTY; |
| 109 | + } |
| 110 | + |
| 111 | + int toExtract = Math.min(amount, existing.getMaxStackSize()); |
| 112 | + |
| 113 | + if (!simulate) { |
| 114 | + updateContents(existing.copyWithCount(existing.getCount() - toExtract), slot); |
| 115 | + } |
| 116 | + |
| 117 | + return existing.copyWithCount(toExtract); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int getSlotLimit(int slot) { |
| 122 | + return Item.ABSOLUTE_MAX_STACK_SIZE; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean isItemValid(int slot, ItemStack stack) { |
| 127 | + return stack.getItem().canFitInsideContainerItems(); |
| 128 | + } |
| 129 | + |
| 130 | + protected void onContentsChanged(int slot, ItemStack oldStack, ItemStack newStack) { |
| 131 | + } |
| 132 | + |
| 133 | + protected ItemContainerContents getContents() { |
| 134 | + return parent.getOrDefault(component, ItemContainerContents.EMPTY); |
| 135 | + } |
| 136 | + |
| 137 | + protected void updateContents(ItemStack stack, int slot) { |
| 138 | + validateSlotIndex(slot); |
| 139 | + ItemStack oldStack = stacks.get(slot); |
| 140 | + stacks.set(slot, stack); |
| 141 | + parent.set(component, ItemContainerContents.fromItems(stacks)); |
| 142 | + onContentsChanged(slot, oldStack, stack); |
| 143 | + } |
| 144 | + |
| 145 | + protected final void validateSlotIndex(int slot) { |
| 146 | + if (slot < 0 || slot >= getSlots()) { |
| 147 | + throw new RuntimeException("Slot " + slot + " not in valid range - [0," + getSlots() + ")"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void onSlotChanged(int slot) { |
| 153 | + updateContents(getStackInSlot(slot), slot); |
| 154 | + } |
| 155 | +} |
0 commit comments