| 1 | package net.minecraft.core.component.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.advancements.predicates.CollectionPredicate;
|
| 8 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 9 | import net.minecraft.advancements.predicates.SingleComponentItemPredicate;
|
| 10 | import net.minecraft.core.component.DataComponentType;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.chat.ComponentSerialization;
|
| 14 | import net.minecraft.server.network.Filterable;
|
| 15 | import net.minecraft.world.item.component.WrittenBookContent;
|
| 16 |
|
| 17 | public record WrittenBookPredicate(
|
| 18 | Optional<CollectionPredicate<Filterable<Component>, WrittenBookPredicate.PagePredicate>> pages,
|
| 19 | Optional<String> author,
|
| 20 | Optional<String> title,
|
| 21 | MinMaxBounds.Ints generation,
|
| 22 | Optional<Boolean> resolved
|
| 23 | ) implements SingleComponentItemPredicate<WrittenBookContent> {
|
| 24 | public static final Codec<WrittenBookPredicate> CODEC = RecordCodecBuilder.create(
|
| 25 | i -> i.group(
|
| 26 | CollectionPredicate.<Filterable<Component>, WrittenBookPredicate.PagePredicate>codec(WrittenBookPredicate.PagePredicate.CODEC)
|
| 27 | .optionalFieldOf("pages")
|
| 28 | .forGetter(WrittenBookPredicate::pages),
|
| 29 | Codec.STRING.optionalFieldOf("author").forGetter(WrittenBookPredicate::author),
|
| 30 | Codec.STRING.optionalFieldOf("title").forGetter(WrittenBookPredicate::title),
|
| 31 | MinMaxBounds.Ints.CODEC.optionalFieldOf("generation", MinMaxBounds.Ints.ANY).forGetter(WrittenBookPredicate::generation),
|
| 32 | Codec.BOOL.optionalFieldOf("resolved").forGetter(WrittenBookPredicate::resolved)
|
| 33 | )
|
| 34 | .apply(i, WrittenBookPredicate::new)
|
| 35 | );
|
| 36 |
|
| 37 | @Override
|
| 38 | public DataComponentType<WrittenBookContent> componentType() {
|
| 39 | return DataComponents.WRITTEN_BOOK_CONTENT;
|
| 40 | }
|
| 41 |
|
| 42 | public boolean matches(final WrittenBookContent value) {
|
| 43 | if (this.author.isPresent() && !this.author.get().equals(value.author())) {
|
| 44 | return false;
|
| 45 | } else if (this.title.isPresent() && !this.title.get().equals(value.title().raw())) {
|
| 46 | return false;
|
| 47 | } else if (!this.generation.matches(value.generation())) {
|
| 48 | return false;
|
| 49 | } else {
|
| 50 | return this.resolved.isPresent() && this.resolved.get() != value.resolved() ? false : !this.pages.isPresent() || this.pages.get().test(value.pages());
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | public record PagePredicate(Component contents) implements Predicate<Filterable<Component>> {
|
| 55 | public static final Codec<WrittenBookPredicate.PagePredicate> CODEC = ComponentSerialization.CODEC
|
| 56 | .xmap(WrittenBookPredicate.PagePredicate::new, WrittenBookPredicate.PagePredicate::contents);
|
| 57 |
|
| 58 | public boolean test(final Filterable<Component> value) {
|
| 59 | return value.raw().equals(this.contents);
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 |
|