| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.Predicate;
|
| 7 | import net.minecraft.core.HolderGetter;
|
| 8 | import net.minecraft.core.HolderSet;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 11 | import net.minecraft.tags.TagKey;
|
| 12 | import net.minecraft.world.item.Item;
|
| 13 | import net.minecraft.world.item.ItemInstance;
|
| 14 | import net.minecraft.world.level.ItemLike;
|
| 15 |
|
| 16 | public record ItemPredicate(Optional<HolderSet<Item>> items, MinMaxBounds.Ints count, DataComponentMatchers components) implements Predicate<ItemInstance> {
|
| 17 | public static final Codec<ItemPredicate> CODEC = RecordCodecBuilder.create(
|
| 18 | i -> i.group(
|
| 19 | RegistryCodecs.holderSet(Registries.ITEM).optionalFieldOf("items").forGetter(ItemPredicate::items),
|
| 20 | MinMaxBounds.Ints.CODEC.optionalFieldOf("count", MinMaxBounds.Ints.ANY).forGetter(ItemPredicate::count),
|
| 21 | DataComponentMatchers.CODEC.forGetter(ItemPredicate::components)
|
| 22 | )
|
| 23 | .apply(i, ItemPredicate::new)
|
| 24 | );
|
| 25 |
|
| 26 | public boolean test(final ItemInstance itemStack) {
|
| 27 | if (this.items.isPresent() && !itemStack.is(this.items.get())) {
|
| 28 | return false;
|
| 29 | } else {
|
| 30 | return !this.count.matches(itemStack.count()) ? false : this.components.test(itemStack);
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | public static class Builder {
|
| 35 | private Optional<HolderSet<Item>> items = Optional.empty();
|
| 36 | private MinMaxBounds.Ints count = MinMaxBounds.Ints.ANY;
|
| 37 | private DataComponentMatchers components = DataComponentMatchers.ANY;
|
| 38 |
|
| 39 | public static ItemPredicate.Builder item() {
|
| 40 | return new ItemPredicate.Builder();
|
| 41 | }
|
| 42 |
|
| 43 | public ItemPredicate.Builder of(final HolderGetter<Item> lookup, final ItemLike... items) {
|
| 44 | this.items = Optional.of(HolderSet.direct(i -> i.asItem().builtInRegistryHolder(), items));
|
| 45 | return this;
|
| 46 | }
|
| 47 |
|
| 48 | public ItemPredicate.Builder of(final HolderGetter<Item> lookup, final TagKey<Item> tag) {
|
| 49 | this.items = Optional.of(lookup.getOrThrow(tag));
|
| 50 | return this;
|
| 51 | }
|
| 52 |
|
| 53 | public ItemPredicate.Builder withCount(final MinMaxBounds.Ints count) {
|
| 54 | this.count = count;
|
| 55 | return this;
|
| 56 | }
|
| 57 |
|
| 58 | public ItemPredicate.Builder withComponents(final DataComponentMatchers components) {
|
| 59 | this.components = components;
|
| 60 | return this;
|
| 61 | }
|
| 62 |
|
| 63 | public ItemPredicate build() {
|
| 64 | return new ItemPredicate(this.items, this.count, this.components);
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|