| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.OptionalInt;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import net.minecraft.ChatFormatting;
|
| 13 | import net.minecraft.core.NonNullList;
|
| 14 | import net.minecraft.core.component.DataComponentGetter;
|
| 15 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 18 | import net.minecraft.network.codec.StreamCodec;
|
| 19 | import net.minecraft.world.item.Item;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.item.ItemStackTemplate;
|
| 22 | import net.minecraft.world.item.TooltipFlag;
|
| 23 |
|
| 24 | public final class ItemContainerContents implements ContainerComponent<ItemContainerContents>, TooltipProvider {
|
| 25 | private static final int NO_SLOT = -1;
|
| 26 | public static final int MAX_SIZE = 256;
|
| 27 | public static final ItemContainerContents EMPTY = new ItemContainerContents(List.of());
|
| 28 | public static final Codec<ItemContainerContents> CODEC = ItemContainerContents.Slot.CODEC
|
| 29 | .sizeLimitedListOf(256)
|
| 30 | .xmap(ItemContainerContents::fromSlots, ItemContainerContents::asSlots);
|
| 31 | public static final StreamCodec<RegistryFriendlyByteBuf, ItemContainerContents> STREAM_CODEC = ItemStackTemplate.STREAM_CODEC
|
| 32 | .apply(ByteBufCodecs::optional)
|
| 33 | .<List<Optional<ItemStackTemplate>>>apply(ByteBufCodecs.list(256))
|
| 34 | .map(ItemContainerContents::new, c -> c.items);
|
| 35 | private final List<Optional<ItemStackTemplate>> items;
|
| 36 | private final int hashCode;
|
| 37 |
|
| 38 | private ItemContainerContents(final List<Optional<ItemStackTemplate>> items) {
|
| 39 | if (items.size() > 256) {
|
| 40 | throw new IllegalArgumentException("Got " + items.size() + " items, but maximum is 256");
|
| 41 | }
|
| 42 |
|
| 43 | this.items = items;
|
| 44 | this.hashCode = items.hashCode();
|
| 45 | }
|
| 46 |
|
| 47 | private static List<Optional<ItemStackTemplate>> emptyContents(final int size) {
|
| 48 | return new ArrayList<>(Collections.nCopies(size, Optional.empty()));
|
| 49 | }
|
| 50 |
|
| 51 | private static ItemContainerContents fromSlots(final List<ItemContainerContents.Slot> slots) {
|
| 52 | OptionalInt maxSlotIndex = slots.stream().mapToInt(ItemContainerContents.Slot::index).max();
|
| 53 | if (maxSlotIndex.isEmpty()) {
|
| 54 | return EMPTY;
|
| 55 | }
|
| 56 |
|
| 57 | List<Optional<ItemStackTemplate>> items = emptyContents(maxSlotIndex.getAsInt() + 1);
|
| 58 |
|
| 59 | for (ItemContainerContents.Slot slot : slots) {
|
| 60 | items.set(slot.index(), Optional.of(slot.item()));
|
| 61 | }
|
| 62 |
|
| 63 | return new ItemContainerContents(items);
|
| 64 | }
|
| 65 |
|
| 66 | public static ItemContainerContents fromItems(final List<ItemStack> itemStacks) {
|
| 67 | int lastNonEmptySlot = findLastNonEmptySlot(itemStacks);
|
| 68 | if (lastNonEmptySlot == -1) {
|
| 69 | return EMPTY;
|
| 70 | }
|
| 71 |
|
| 72 | List<Optional<ItemStackTemplate>> items = emptyContents(lastNonEmptySlot + 1);
|
| 73 |
|
| 74 | for (int i = 0; i <= lastNonEmptySlot; i++) {
|
| 75 | ItemStack sourceStack = itemStacks.get(i);
|
| 76 | if (!sourceStack.isEmpty()) {
|
| 77 | items.set(i, Optional.of(ItemStackTemplate.fromNonEmptyStack(sourceStack)));
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | return new ItemContainerContents(items);
|
| 82 | }
|
| 83 |
|
| 84 | private static int findLastNonEmptySlot(final List<ItemStack> itemStacks) {
|
| 85 | for (int i = itemStacks.size() - 1; i >= 0; i--) {
|
| 86 | if (!itemStacks.get(i).isEmpty()) {
|
| 87 | return i;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | return -1;
|
| 92 | }
|
| 93 |
|
| 94 | private List<ItemContainerContents.Slot> asSlots() {
|
| 95 | List<ItemContainerContents.Slot> slots = new ArrayList<>();
|
| 96 |
|
| 97 | for (int i = 0; i < this.items.size(); i++) {
|
| 98 | Optional<ItemStackTemplate> item = this.items.get(i);
|
| 99 | if (item.isPresent()) {
|
| 100 | slots.add(new ItemContainerContents.Slot(i, item.get()));
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | return slots;
|
| 105 | }
|
| 106 |
|
| 107 | private ItemStack createStackFromSlot(final int slot) {
|
| 108 | if (slot < this.items.size()) {
|
| 109 | Optional<ItemStackTemplate> slotContents = this.items.get(slot);
|
| 110 | if (slotContents.isPresent()) {
|
| 111 | return slotContents.get().create();
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | return ItemStack.EMPTY;
|
| 116 | }
|
| 117 |
|
| 118 | public void copyInto(final NonNullList<ItemStack> destination) {
|
| 119 | for (int i = 0; i < destination.size(); i++) {
|
| 120 | destination.set(i, this.createStackFromSlot(i));
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public ItemStack copyOne() {
|
| 125 | return this.createStackFromSlot(0);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public Stream<ItemStack> itemCopies() {
|
| 130 | return this.items.stream().map(i -> i.map(ItemStackTemplate::create).orElse(ItemStack.EMPTY));
|
| 131 | }
|
| 132 |
|
| 133 | private Stream<ItemStackTemplate> nonEmptyItemsStream() {
|
| 134 | return this.items.stream().flatMap(Optional::stream);
|
| 135 | }
|
| 136 |
|
| 137 | public Stream<ItemStack> nonEmptyItemCopyStream() {
|
| 138 | return this.nonEmptyItemsStream().map(ItemStackTemplate::create);
|
| 139 | }
|
| 140 |
|
| 141 | public Iterable<ItemStackTemplate> nonEmptyItems() {
|
| 142 | return () -> this.nonEmptyItemsStream().iterator();
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public int size() {
|
| 147 | return this.items.size();
|
| 148 | }
|
| 149 |
|
| 150 | public ItemContainerContents copyWithContents(final Stream<ItemStack> newContents) {
|
| 151 | return fromItems(newContents.toList());
|
| 152 | }
|
| 153 |
|
| 154 | public ItemContainerContents.Mutable asMutable() {
|
| 155 | List<ItemStack> itemsList = new ArrayList<>(this.items.size());
|
| 156 |
|
| 157 | for (Optional<ItemStackTemplate> item : this.items) {
|
| 158 | itemsList.add(item.map(ItemStackTemplate::create).orElse(ItemStack.EMPTY));
|
| 159 | }
|
| 160 |
|
| 161 | return new ItemContainerContents.Mutable(itemsList);
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public boolean equals(final Object obj) {
|
| 166 | return this == obj ? true : obj instanceof ItemContainerContents contents && this.items.equals(contents.items);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public int hashCode() {
|
| 171 | return this.hashCode;
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 176 | int lineCount = 0;
|
| 177 | int itemCount = 0;
|
| 178 |
|
| 179 | for (Optional<ItemStackTemplate> item : this.items) {
|
| 180 | if (!item.isEmpty()) {
|
| 181 | itemCount++;
|
| 182 | if (lineCount <= 4) {
|
| 183 | lineCount++;
|
| 184 | ItemStack itemStack = item.get().create();
|
| 185 | consumer.accept(Component.translatable("item.container.item_count", itemStack.getCount(), itemStack.getHoverName()));
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | if (itemCount - lineCount > 0) {
|
| 191 | consumer.accept(Component.translatable("item.container.more_items", itemCount - lineCount).withStyle(ChatFormatting.ITALIC));
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | public static class Mutable extends GrowableMutableContainer<ItemContainerContents> {
|
| 196 | private Mutable(final List<ItemStack> items) {
|
| 197 | super(items);
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public boolean canInsertNewSlots() {
|
| 202 | return this.items.size() < 256;
|
| 203 | }
|
| 204 |
|
| 205 | public ItemContainerContents toImmutable() {
|
| 206 | return ItemContainerContents.fromItems(this.items);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | private record Slot(int index, ItemStackTemplate item) {
|
| 211 | public static final Codec<ItemContainerContents.Slot> CODEC = RecordCodecBuilder.create(
|
| 212 | i -> i.group(
|
| 213 | Codec.intRange(0, 255).fieldOf("slot").forGetter(ItemContainerContents.Slot::index),
|
| 214 | ItemStackTemplate.CODEC.fieldOf("item").forGetter(ItemContainerContents.Slot::item)
|
| 215 | )
|
| 216 | .apply(i, ItemContainerContents.Slot::new)
|
| 217 | );
|
| 218 | }
|
| 219 | }
|
| 220 |
|