| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.ImmutableList.Builder;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.List;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.function.Supplier;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 | import net.minecraft.world.entity.SlotAccess;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.inventory.Slot;
|
| 20 | import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
| 21 | import net.minecraft.world.item.ItemInstance;
|
| 22 | import net.minecraft.world.item.ItemProvider;
|
| 23 | import net.minecraft.world.item.ItemStack;
|
| 24 | import net.minecraft.world.item.ItemStackTemplate;
|
| 25 | import net.minecraft.world.item.slot.SlotSelector;
|
| 26 | import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
|
| 27 | import org.apache.commons.lang3.math.Fraction;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public final class BundleContents implements ContainerComponent<BundleContents>, TooltipComponent {
|
| 31 | public static final BundleContents EMPTY = new BundleContents(List.of());
|
| 32 | public static final Codec<BundleContents> CODEC = ItemStackTemplate.CODEC.listOf().xmap(BundleContents::new, contents -> contents.items);
|
| 33 | public static final StreamCodec<RegistryFriendlyByteBuf, BundleContents> STREAM_CODEC = ItemStackTemplate.STREAM_CODEC
|
| 34 | .apply(ByteBufCodecs.list())
|
| 35 | .map(BundleContents::new, contents -> contents.items);
|
| 36 | private static final Fraction BUNDLE_IN_BUNDLE_WEIGHT = Fraction.getFraction(1, 16);
|
| 37 | private static final int NO_STACK_INDEX = -1;
|
| 38 | public static final int NO_SELECTED_ITEM_INDEX = -1;
|
| 39 | public static final DataResult<Fraction> BEEHIVE_WEIGHT = DataResult.success(Fraction.ONE);
|
| 40 | private final List<ItemStackTemplate> items;
|
| 41 | private final int selectedItem;
|
| 42 | private final Supplier<DataResult<Fraction>> weight;
|
| 43 |
|
| 44 | private BundleContents(final List<ItemStackTemplate> items, final int selectedItem) {
|
| 45 | this.items = items;
|
| 46 | this.selectedItem = selectedItem;
|
| 47 | this.weight = Suppliers.memoize(() -> computeContentWeight(this.items));
|
| 48 | }
|
| 49 |
|
| 50 | public BundleContents(final List<ItemStackTemplate> items) {
|
| 51 | this(items, -1);
|
| 52 | }
|
| 53 |
|
| 54 | private static DataResult<Fraction> computeContentWeight(final List<? extends ItemInstance> items) {
|
| 55 | try {
|
| 56 | Fraction weight = Fraction.ZERO;
|
| 57 |
|
| 58 | for (ItemInstance stack : items) {
|
| 59 | DataResult<Fraction> itemWeight = getWeight(stack);
|
| 60 | if (itemWeight.isError()) {
|
| 61 | return itemWeight;
|
| 62 | }
|
| 63 |
|
| 64 | weight = weight.add(itemWeight.getOrThrow().multiplyBy(Fraction.getFraction(stack.count(), 1)));
|
| 65 | }
|
| 66 |
|
| 67 | return DataResult.success(weight);
|
| 68 | } catch (ArithmeticException exception) {
|
| 69 | return DataResult.error(() -> "Excessive total bundle weight");
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | private static DataResult<Fraction> getWeight(final ItemInstance item) {
|
| 74 | BundleContents bundle = item.get(DataComponents.BUNDLE_CONTENTS);
|
| 75 | if (bundle != null) {
|
| 76 | return bundle.weight().map(nestedWeight -> nestedWeight.add(BUNDLE_IN_BUNDLE_WEIGHT));
|
| 77 | }
|
| 78 |
|
| 79 | List<BeehiveBlockEntity.Occupant> bees = item.getOrDefault(DataComponents.BEES, Bees.EMPTY).bees();
|
| 80 | return !bees.isEmpty() ? BEEHIVE_WEIGHT : DataResult.success(Fraction.getFraction(1, item.getMaxStackSize()));
|
| 81 | }
|
| 82 |
|
| 83 | public static boolean canItemBeInBundle(final ItemStack itemToAdd) {
|
| 84 | return !itemToAdd.isEmpty() && itemToAdd.getItem().canFitInsideContainerItems();
|
| 85 | }
|
| 86 |
|
| 87 | public int getNumberOfItemsToShow() {
|
| 88 | int numberOfItemStacks = this.size();
|
| 89 | int availableItemsToShow = numberOfItemStacks > 12 ? 11 : 12;
|
| 90 | int itemsOnNonFullRow = numberOfItemStacks % 4;
|
| 91 | int emptySpaceOnNonFullRow = itemsOnNonFullRow == 0 ? 0 : 4 - itemsOnNonFullRow;
|
| 92 | return Math.min(numberOfItemStacks, availableItemsToShow - emptySpaceOnNonFullRow);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public Stream<ItemStack> itemCopies() {
|
| 97 | return this.items.stream().map(ItemStackTemplate::create);
|
| 98 | }
|
| 99 |
|
| 100 | public List<ItemStackTemplate> items() {
|
| 101 | return this.items;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public int size() {
|
| 106 | return this.items.size();
|
| 107 | }
|
| 108 |
|
| 109 | public DataResult<Fraction> weight() {
|
| 110 | return this.weight.get();
|
| 111 | }
|
| 112 |
|
| 113 | public boolean isEmpty() {
|
| 114 | return this.items.isEmpty();
|
| 115 | }
|
| 116 |
|
| 117 | public int getSelectedItemIndex() {
|
| 118 | return this.selectedItem;
|
| 119 | }
|
| 120 |
|
| 121 | public @Nullable ItemStackTemplate getSelectedItem() {
|
| 122 | return this.selectedItem == -1 ? null : this.items.get(this.selectedItem);
|
| 123 | }
|
| 124 |
|
| 125 | public BundleContents copyWithContents(final Stream<ItemStack> newContents) {
|
| 126 | BundleContents.Mutable builder = new BundleContents.Mutable();
|
| 127 | newContents.forEach(builder::tryInsert);
|
| 128 | return builder.toImmutable();
|
| 129 | }
|
| 130 |
|
| 131 | public BundleContents.Mutable asMutable() {
|
| 132 | DataResult<Fraction> currentWeight = this.weight.get();
|
| 133 | if (currentWeight.isError()) {
|
| 134 | return new BundleContents.Mutable();
|
| 135 | }
|
| 136 |
|
| 137 | List<ItemStack> itemsList = new ArrayList<>(this.items.size());
|
| 138 |
|
| 139 | for (ItemStackTemplate item : this.items) {
|
| 140 | itemsList.add(item.create());
|
| 141 | }
|
| 142 |
|
| 143 | return new BundleContents.Mutable(itemsList, currentWeight.getOrThrow(), this.selectedItem);
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public boolean equals(final Object obj) {
|
| 148 | if (this == obj) {
|
| 149 | return true;
|
| 150 | } else {
|
| 151 | return obj instanceof BundleContents contents ? this.items.equals(contents.items) : false;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public int hashCode() {
|
| 157 | return this.items.hashCode();
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public String toString() {
|
| 162 | return "BundleContents" + this.items;
|
| 163 | }
|
| 164 |
|
| 165 | public static class Mutable extends GrowableMutableContainer<BundleContents> {
|
| 166 | private Fraction weight;
|
| 167 | private int selectedItem;
|
| 168 | private boolean needsFlattening;
|
| 169 |
|
| 170 | private Mutable(final List<ItemStack> items, final Fraction weight, final int selectedItem) {
|
| 171 | super(items);
|
| 172 | this.weight = weight;
|
| 173 | this.selectedItem = selectedItem;
|
| 174 | }
|
| 175 |
|
| 176 | public Mutable() {
|
| 177 | this(new ArrayList<>(), Fraction.ZERO, -1);
|
| 178 | }
|
| 179 |
|
| 180 | public BundleContents.Mutable clearItems() {
|
| 181 | this.items.clear();
|
| 182 | this.weight = Fraction.ZERO;
|
| 183 | this.selectedItem = -1;
|
| 184 | return this;
|
| 185 | }
|
| 186 |
|
| 187 | private int findStackIndexWithinRange(final ItemStack itemsToAdd, final int minInclusive, final int maxExclusive) {
|
| 188 | if (!itemsToAdd.isStackable()) {
|
| 189 | return -1;
|
| 190 | }
|
| 191 |
|
| 192 | int startIndex = Math.max(minInclusive, 0);
|
| 193 | int endIndex = Math.min(maxExclusive, this.items.size());
|
| 194 |
|
| 195 | for (int i = startIndex; i < endIndex; i++) {
|
| 196 | if (ItemStack.isSameItemSameComponents(this.items.get(i), itemsToAdd)) {
|
| 197 | return i;
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | return -1;
|
| 202 | }
|
| 203 |
|
| 204 | private int findStackIndex(final ItemStack itemsToAdd) {
|
| 205 | return this.findStackIndexWithinRange(itemsToAdd, 0, this.items.size());
|
| 206 | }
|
| 207 |
|
| 208 | private int getMaxAmountToAdd(final Fraction itemWeight) {
|
| 209 | Fraction remainingWeight = Fraction.ONE.subtract(this.weight);
|
| 210 | return Math.max(remainingWeight.divideBy(itemWeight).intValue(), 0);
|
| 211 | }
|
| 212 |
|
| 213 | public int tryInsert(final ItemStack itemsToAdd) {
|
| 214 | if (!BundleContents.canItemBeInBundle(itemsToAdd)) {
|
| 215 | return 0;
|
| 216 | }
|
| 217 |
|
| 218 | DataResult<Fraction> maybeItemWeight = BundleContents.getWeight(itemsToAdd);
|
| 219 | if (maybeItemWeight.isError()) {
|
| 220 | return 0;
|
| 221 | }
|
| 222 |
|
| 223 | Fraction itemWeight = maybeItemWeight.getOrThrow();
|
| 224 | int amountToAdd = Math.min(itemsToAdd.getCount(), this.getMaxAmountToAdd(itemWeight));
|
| 225 | if (amountToAdd == 0) {
|
| 226 | return 0;
|
| 227 | }
|
| 228 |
|
| 229 | this.weight = this.weight.add(getStackedWeight(itemWeight, amountToAdd));
|
| 230 | int stackIndex = this.findStackIndex(itemsToAdd);
|
| 231 | if (stackIndex != -1) {
|
| 232 | ItemStack removedStack = this.items.remove(stackIndex);
|
| 233 | ItemStack mergedStack = removedStack.copyWithCount(removedStack.getCount() + amountToAdd);
|
| 234 | itemsToAdd.shrink(amountToAdd);
|
| 235 | this.items.addFirst(mergedStack);
|
| 236 | } else {
|
| 237 | this.items.addFirst(itemsToAdd.split(amountToAdd));
|
| 238 | }
|
| 239 |
|
| 240 | return amountToAdd;
|
| 241 | }
|
| 242 |
|
| 243 | public int tryTransfer(final Slot slot, final Player player) {
|
| 244 | ItemStack other = slot.getItem();
|
| 245 | DataResult<Fraction> itemWeight = BundleContents.getWeight(other);
|
| 246 | if (itemWeight.isError()) {
|
| 247 | return 0;
|
| 248 | }
|
| 249 |
|
| 250 | int maxAmount = this.getMaxAmountToAdd(itemWeight.getOrThrow());
|
| 251 | return BundleContents.canItemBeInBundle(other) ? this.tryInsert(slot.safeTake(other.getCount(), maxAmount, player)) : 0;
|
| 252 | }
|
| 253 |
|
| 254 | public void toggleSelectedItem(final int selectedItem) {
|
| 255 | this.selectedItem = this.selectedItem != selectedItem && !this.indexIsOutsideAllowedBounds(selectedItem) ? selectedItem : -1;
|
| 256 | }
|
| 257 |
|
| 258 | private boolean indexIsOutsideAllowedBounds(final int selectedItem) {
|
| 259 | return selectedItem < 0 || selectedItem >= this.items.size();
|
| 260 | }
|
| 261 |
|
| 262 | public @Nullable ItemStack removeOne() {
|
| 263 | if (this.items.isEmpty()) {
|
| 264 | return null;
|
| 265 | }
|
| 266 |
|
| 267 | int removeIndex = this.indexIsOutsideAllowedBounds(this.selectedItem) ? 0 : this.selectedItem;
|
| 268 | ItemStack stack = this.items.remove(removeIndex).copy();
|
| 269 | this.weight = this.weight.subtract(getStackedWeight(stack));
|
| 270 | this.toggleSelectedItem(-1);
|
| 271 | return stack;
|
| 272 | }
|
| 273 |
|
| 274 | private static Fraction getStackedWeight(final Fraction weight, final int count) {
|
| 275 | return weight.multiplyBy(Fraction.getFraction(count, 1));
|
| 276 | }
|
| 277 |
|
| 278 | private static Fraction getStackedWeight(final ItemStack stack) {
|
| 279 | return getStackedWeight(BundleContents.getWeight(stack).getOrThrow(), stack.getCount());
|
| 280 | }
|
| 281 |
|
| 282 | public Fraction weight() {
|
| 283 | return this.weight;
|
| 284 | }
|
| 285 |
|
| 286 | @Override
|
| 287 | public int replaceSlotItems(final ItemProvider newItems, final SlotSelector slotSelector) {
|
| 288 | this.mergeIdenticalStacks();
|
| 289 | return super.replaceSlotItems(newItems, slotSelector);
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public void modifySlots(final Consumer<? super SlotAccess> consumer, final SlotSelector slotSelector) {
|
| 294 | this.mergeIdenticalStacks();
|
| 295 | super.modifySlots(consumer, slotSelector);
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | protected boolean setItem(final int slot, final ItemStack itemStack) {
|
| 300 | ItemStack currentItem = this.items.get(slot);
|
| 301 | Fraction adjustedWeight = currentItem.isEmpty() ? this.weight : this.weight.subtract(getStackedWeight(currentItem));
|
| 302 | Fraction newWeight = itemStack.isEmpty() ? adjustedWeight : getWeightWithAddedItems(adjustedWeight, itemStack);
|
| 303 | if (newWeight != null && super.setItem(slot, itemStack)) {
|
| 304 | this.weight = newWeight;
|
| 305 | this.needsFlattening = true;
|
| 306 | return true;
|
| 307 | } else {
|
| 308 | return false;
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | @Override
|
| 313 | protected boolean addSlotWithItem(final ItemProvider newItems) {
|
| 314 | if (!newItems.findNextNonEmpty()) {
|
| 315 | return false;
|
| 316 | } else {
|
| 317 | Fraction newWeight = getWeightWithAddedItems(this.weight, newItems.peek());
|
| 318 | if (newWeight != null && super.addSlotWithItem(newItems)) {
|
| 319 | this.weight = newWeight;
|
| 320 | this.needsFlattening = true;
|
| 321 | return true;
|
| 322 | } else {
|
| 323 | return false;
|
| 324 | }
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | private static @Nullable Fraction getWeightWithAddedItems(final Fraction weight, final ItemStack itemsToAdd) {
|
| 329 | if (BundleContents.canItemBeInBundle(itemsToAdd)) {
|
| 330 | DataResult<Fraction> itemWeight = BundleContents.getWeight(itemsToAdd);
|
| 331 | if (itemWeight.isError()) {
|
| 332 | return null;
|
| 333 | }
|
| 334 |
|
| 335 | Fraction newWeight = weight.add(getStackedWeight(itemWeight.getOrThrow(), itemsToAdd.getCount()));
|
| 336 | if (newWeight.compareTo(Fraction.ONE) <= 0) {
|
| 337 | return newWeight;
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | return null;
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | public boolean canInsertNewSlots() {
|
| 346 | return this.weight.compareTo(Fraction.ONE) < 0;
|
| 347 | }
|
| 348 |
|
| 349 | private void mergeIdenticalStacks() {
|
| 350 | if (this.needsFlattening) {
|
| 351 | for (int index = 0; index < this.items.size(); index++) {
|
| 352 | ItemStack itemStack = this.items.get(index);
|
| 353 | if (itemStack.isEmpty()) {
|
| 354 | this.items.remove(index--);
|
| 355 | } else {
|
| 356 | int stackIndex = this.findStackIndexWithinRange(itemStack, index + 1, this.items.size());
|
| 357 | if (stackIndex != -1) {
|
| 358 | ItemStack targetStack = this.items.get(stackIndex);
|
| 359 | int mergedCount = itemStack.getCount() + targetStack.getCount();
|
| 360 | this.items.set(stackIndex, itemStack.copyWithCount(mergedCount));
|
| 361 | this.items.remove(index--);
|
| 362 | }
|
| 363 | }
|
| 364 | }
|
| 365 |
|
| 366 | this.needsFlattening = false;
|
| 367 | }
|
| 368 | }
|
| 369 |
|
| 370 | public BundleContents toImmutable() {
|
| 371 | this.mergeIdenticalStacks();
|
| 372 | Builder<ItemStackTemplate> builder = ImmutableList.builder();
|
| 373 |
|
| 374 | for (ItemStack item : this.items) {
|
| 375 | builder.add(ItemStackTemplate.fromNonEmptyStack(item));
|
| 376 | }
|
| 377 |
|
| 378 | return new BundleContents(builder.build(), this.selectedItem);
|
| 379 | }
|
| 380 | }
|
| 381 | }
|
| 382 |
|