| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.minecraft.ChatFormatting;
|
| 10 | import net.minecraft.core.component.DataComponentGetter;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 15 | import net.minecraft.network.codec.StreamCodec;
|
| 16 | import net.minecraft.world.item.Item;
|
| 17 | import net.minecraft.world.item.ItemProvider;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.item.ItemStackTemplate;
|
| 20 | import net.minecraft.world.item.TooltipFlag;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public record ChargedProjectiles(List<ItemStackTemplate> items) implements ContainerComponent<ChargedProjectiles>, TooltipProvider {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private static final int MAX_SIZE = 1024;
|
| 26 | public static final ChargedProjectiles EMPTY = new ChargedProjectiles(List.of());
|
| 27 | public static final Codec<ChargedProjectiles> CODEC = ItemStackTemplate.CODEC
|
| 28 | .sizeLimitedListOf(1024)
|
| 29 | .xmap(ChargedProjectiles::new, projectiles -> projectiles.items);
|
| 30 | public static final StreamCodec<RegistryFriendlyByteBuf, ChargedProjectiles> STREAM_CODEC = ItemStackTemplate.STREAM_CODEC
|
| 31 | .apply(ByteBufCodecs.list(1024))
|
| 32 | .map(ChargedProjectiles::new, projectiles -> projectiles.items);
|
| 33 |
|
| 34 | public ChargedProjectiles {
|
| 35 | if (items.size() > 1024) {
|
| 36 | throw new IllegalArgumentException("Got " + items.size() + " items, but maximum is 1024");
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public static ChargedProjectiles of(final ItemStackTemplate stack) {
|
| 41 | return new ChargedProjectiles(List.of(stack));
|
| 42 | }
|
| 43 |
|
| 44 | public static ChargedProjectiles ofNonEmpty(final List<ItemStack> items) {
|
| 45 | List<ItemStackTemplate> list = items.stream().filter(i -> !i.isEmpty()).map(ItemStackTemplate::fromStack).limit(1024L).toList();
|
| 46 | if (list.size() != items.size()) {
|
| 47 | LOGGER.warn("Tried to load invalid items as charged projectiles");
|
| 48 | }
|
| 49 |
|
| 50 | return new ChargedProjectiles(list);
|
| 51 | }
|
| 52 |
|
| 53 | public boolean contains(final Item item) {
|
| 54 | for (ItemStackTemplate projectile : this.items) {
|
| 55 | if (projectile.is(item)) {
|
| 56 | return true;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public Stream<ItemStack> itemCopies() {
|
| 65 | return this.items.stream().map(ItemStackTemplate::create);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public int size() {
|
| 70 | return this.items.size();
|
| 71 | }
|
| 72 |
|
| 73 | public boolean isEmpty() {
|
| 74 | return this.items.isEmpty();
|
| 75 | }
|
| 76 |
|
| 77 | public ChargedProjectiles copyWithContents(final Stream<ItemStack> newContents) {
|
| 78 | return new ChargedProjectiles(newContents.filter(s -> !s.isEmpty()).map(ItemStackTemplate::fromNonEmptyStack).toList());
|
| 79 | }
|
| 80 |
|
| 81 | public ChargedProjectiles.Mutable asMutable() {
|
| 82 | List<ItemStack> itemsList = new ArrayList<>(this.items.size());
|
| 83 |
|
| 84 | for (ItemStackTemplate item : this.items) {
|
| 85 | itemsList.add(item.create());
|
| 86 | }
|
| 87 |
|
| 88 | return new ChargedProjectiles.Mutable(itemsList);
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 93 | ItemStack current = null;
|
| 94 | int count = 0;
|
| 95 |
|
| 96 | for (ItemStackTemplate projectileTemplate : this.items) {
|
| 97 | ItemStack projectile = projectileTemplate.create();
|
| 98 | if (current == null) {
|
| 99 | current = projectile;
|
| 100 | count = 1;
|
| 101 | } else if (ItemStack.matches(current, projectile)) {
|
| 102 | count++;
|
| 103 | } else {
|
| 104 | addProjectileTooltip(context, consumer, current, count);
|
| 105 | current = projectile;
|
| 106 | count = 1;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | if (current != null) {
|
| 111 | addProjectileTooltip(context, consumer, current, count);
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | private static void addProjectileTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final ItemStack projectile, final int count) {
|
| 116 | if (count == 1) {
|
| 117 | consumer.accept(Component.translatable("item.minecraft.crossbow.projectile.single", projectile.getDisplayName()));
|
| 118 | } else {
|
| 119 | consumer.accept(Component.translatable("item.minecraft.crossbow.projectile.multiple", count, projectile.getHoverName()));
|
| 120 | }
|
| 121 |
|
| 122 | TooltipDisplay projectileDisplay = projectile.getOrDefault(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT);
|
| 123 | projectile.addDetailsToTooltip(
|
| 124 | context, projectileDisplay, null, TooltipFlag.NORMAL, line -> consumer.accept(Component.literal(" ").append(line).withStyle(ChatFormatting.GRAY))
|
| 125 | );
|
| 126 | }
|
| 127 |
|
| 128 | public static class Mutable extends GrowableMutableContainer<ChargedProjectiles> {
|
| 129 | private Mutable(final List<ItemStack> items) {
|
| 130 | super(items);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected boolean addSlotWithItem(final ItemProvider newItems) {
|
| 135 | return newItems.findNextNonEmpty() && super.addSlotWithItem(newItems);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public boolean canInsertNewSlots() {
|
| 140 | return this.items.size() < 1024;
|
| 141 | }
|
| 142 |
|
| 143 | public ChargedProjectiles toImmutable() {
|
| 144 | List<ItemStackTemplate> nonEmptyItems = new ArrayList<>(this.items.size());
|
| 145 |
|
| 146 | for (ItemStack item : this.items) {
|
| 147 | if (!item.isEmpty()) {
|
| 148 | nonEmptyItems.add(ItemStackTemplate.fromNonEmptyStack(item));
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | return new ChargedProjectiles(nonEmptyItems);
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|