| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.ImmutableList.Builder;
|
| 6 | import com.google.gson.JsonElement;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.DataResult;
|
| 9 | import com.mojang.serialization.JsonOps;
|
| 10 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.Consumer;
|
| 14 | import net.minecraft.ChatFormatting;
|
| 15 | import net.minecraft.core.HolderLookup;
|
| 16 | import net.minecraft.core.component.DataComponentGetter;
|
| 17 | import net.minecraft.core.component.DataComponents;
|
| 18 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.network.chat.ComponentSerialization;
|
| 21 | import net.minecraft.network.chat.ComponentUtils;
|
| 22 | import net.minecraft.network.chat.ResolutionContext;
|
| 23 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 24 | import net.minecraft.network.codec.StreamCodec;
|
| 25 | import net.minecraft.server.network.Filterable;
|
| 26 | import net.minecraft.util.ExtraCodecs;
|
| 27 | import net.minecraft.util.GsonHelper;
|
| 28 | import net.minecraft.util.StringUtil;
|
| 29 | import net.minecraft.world.item.Item;
|
| 30 | import net.minecraft.world.item.ItemStack;
|
| 31 | import net.minecraft.world.item.TooltipFlag;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 |
|
| 34 | public record WrittenBookContent(Filterable<String> title, String author, int generation, List<Filterable<Component>> pages, boolean resolved)
|
| 35 | implements BookContent<Component, WrittenBookContent>,
|
| 36 | TooltipProvider {
|
| 37 | public static final WrittenBookContent EMPTY = new WrittenBookContent(Filterable.passThrough(""), "", 0, List.of(), true);
|
| 38 | public static final int PAGE_LENGTH = 32767;
|
| 39 | public static final int TITLE_LENGTH = 16;
|
| 40 | public static final int TITLE_MAX_LENGTH = 32;
|
| 41 | public static final int MAX_GENERATION = 3;
|
| 42 | public static final Codec<Component> CONTENT_CODEC = ComponentSerialization.flatRestrictedCodec(32767);
|
| 43 | public static final Codec<List<Filterable<Component>>> PAGES_CODEC = pagesCodec(CONTENT_CODEC);
|
| 44 | public static final Codec<WrittenBookContent> CODEC = RecordCodecBuilder.create(
|
| 45 | i -> i.group(
|
| 46 | Filterable.codec(Codec.string(0, 32)).fieldOf("title").forGetter(WrittenBookContent::title),
|
| 47 | Codec.STRING.fieldOf("author").forGetter(WrittenBookContent::author),
|
| 48 | ExtraCodecs.intRange(0, 3).optionalFieldOf("generation", 0).forGetter(WrittenBookContent::generation),
|
| 49 | PAGES_CODEC.optionalFieldOf("pages", List.of()).forGetter(WrittenBookContent::pages),
|
| 50 | Codec.BOOL.optionalFieldOf("resolved", false).forGetter(WrittenBookContent::resolved)
|
| 51 | )
|
| 52 | .apply(i, WrittenBookContent::new)
|
| 53 | );
|
| 54 | public static final StreamCodec<RegistryFriendlyByteBuf, WrittenBookContent> STREAM_CODEC = StreamCodec.composite(
|
| 55 | Filterable.streamCodec(ByteBufCodecs.stringUtf8(32)),
|
| 56 | WrittenBookContent::title,
|
| 57 | ByteBufCodecs.STRING_UTF8,
|
| 58 | WrittenBookContent::author,
|
| 59 | ByteBufCodecs.VAR_INT,
|
| 60 | WrittenBookContent::generation,
|
| 61 | Filterable.streamCodec(ComponentSerialization.STREAM_CODEC).apply(ByteBufCodecs.list()),
|
| 62 | WrittenBookContent::pages,
|
| 63 | ByteBufCodecs.BOOL,
|
| 64 | WrittenBookContent::resolved,
|
| 65 | WrittenBookContent::new
|
| 66 | );
|
| 67 |
|
| 68 | public WrittenBookContent(Filterable<String> title, String author, int generation, List<Filterable<Component>> pages, boolean resolved) {
|
| 69 | if (generation >= 0 && generation <= 3) {
|
| 70 | this.title = title;
|
| 71 | this.author = author;
|
| 72 | this.generation = generation;
|
| 73 | this.pages = pages;
|
| 74 | this.resolved = resolved;
|
| 75 | } else {
|
| 76 | throw new IllegalArgumentException("Generation was " + generation + ", but must be between 0 and 3");
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | private static Codec<Filterable<Component>> pageCodec(final Codec<Component> contentCodec) {
|
| 81 | return Filterable.codec(contentCodec);
|
| 82 | }
|
| 83 |
|
| 84 | public static Codec<List<Filterable<Component>>> pagesCodec(final Codec<Component> contentCodec) {
|
| 85 | return pageCodec(contentCodec).listOf();
|
| 86 | }
|
| 87 |
|
| 88 | public WrittenBookContent craftCopy() {
|
| 89 | return new WrittenBookContent(this.title, this.author, this.generation + 1, this.pages, this.resolved);
|
| 90 | }
|
| 91 |
|
| 92 | public static boolean resolveForItem(final ItemStack itemStack, final ResolutionContext context, final HolderLookup.Provider registries) {
|
| 93 | WrittenBookContent content = itemStack.get(DataComponents.WRITTEN_BOOK_CONTENT);
|
| 94 | if (content != null && !content.resolved()) {
|
| 95 | WrittenBookContent resolvedContent = content.resolve(context, registries);
|
| 96 | if (resolvedContent != null) {
|
| 97 | itemStack.set(DataComponents.WRITTEN_BOOK_CONTENT, resolvedContent);
|
| 98 | return true;
|
| 99 | }
|
| 100 |
|
| 101 | itemStack.set(DataComponents.WRITTEN_BOOK_CONTENT, content.markResolved());
|
| 102 | }
|
| 103 |
|
| 104 | return false;
|
| 105 | }
|
| 106 |
|
| 107 | public @Nullable WrittenBookContent resolve(final ResolutionContext context, final HolderLookup.Provider registries) {
|
| 108 | if (this.resolved) {
|
| 109 | return null;
|
| 110 | }
|
| 111 |
|
| 112 | Builder<Filterable<Component>> newPages = ImmutableList.builderWithExpectedSize(this.pages.size());
|
| 113 |
|
| 114 | for (Filterable<Component> page : this.pages) {
|
| 115 | Optional<Filterable<Component>> resolvedPage = resolvePage(context, registries, page);
|
| 116 | if (resolvedPage.isEmpty()) {
|
| 117 | return null;
|
| 118 | }
|
| 119 |
|
| 120 | newPages.add(resolvedPage.get());
|
| 121 | }
|
| 122 |
|
| 123 | return new WrittenBookContent(this.title, this.author, this.generation, newPages.build(), true);
|
| 124 | }
|
| 125 |
|
| 126 | public WrittenBookContent markResolved() {
|
| 127 | return new WrittenBookContent(this.title, this.author, this.generation, this.pages, true);
|
| 128 | }
|
| 129 |
|
| 130 | private static Optional<Filterable<Component>> resolvePage(
|
| 131 | final ResolutionContext context, final HolderLookup.Provider registries, final Filterable<Component> page
|
| 132 | ) {
|
| 133 | return page.resolve(component -> {
|
| 134 | try {
|
| 135 | Component newComponent = ComponentUtils.resolve(context, component);
|
| 136 | return isPageTooLarge(newComponent, registries) ? Optional.empty() : Optional.of(newComponent);
|
| 137 | } catch (Exception ignored) {
|
| 138 | return Optional.of(component);
|
| 139 | }
|
| 140 | });
|
| 141 | }
|
| 142 |
|
| 143 | private static boolean isPageTooLarge(final Component page, final HolderLookup.Provider registries) {
|
| 144 | DataResult<JsonElement> json = ComponentSerialization.CODEC.encodeStart(registries.createSerializationContext(JsonOps.INSTANCE), page);
|
| 145 | return json.isSuccess() && GsonHelper.encodesLongerThan(json.getOrThrow(), 32767);
|
| 146 | }
|
| 147 |
|
| 148 | public List<Component> getPages(final boolean filterEnabled) {
|
| 149 | return Lists.transform(this.pages, page -> page.get(filterEnabled));
|
| 150 | }
|
| 151 |
|
| 152 | public WrittenBookContent withReplacedPages(final List<Filterable<Component>> newPages) {
|
| 153 | return new WrittenBookContent(this.title, this.author, this.generation, newPages, false);
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 158 | if (!StringUtil.isBlank(this.author)) {
|
| 159 | consumer.accept(Component.translatable("book.byAuthor", this.author).withStyle(ChatFormatting.GRAY));
|
| 160 | }
|
| 161 |
|
| 162 | consumer.accept(Component.translatable("book.generation." + this.generation).withStyle(ChatFormatting.GRAY));
|
| 163 | }
|
| 164 | }
|
| 165 |
|