net.minestom.server.item.component.WritableBookContent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.item.component;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.ListBinaryTag;
import net.minestom.server.item.book.FilteredText;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public record WritableBookContent(@NotNull List> pages) {
public static final WritableBookContent EMPTY = new WritableBookContent(List.of());
public static final NetworkBuffer.Type NETWORK_TYPE = new NetworkBuffer.Type<>() {
@Override
public void write(@NotNull NetworkBuffer buffer, WritableBookContent value) {
buffer.writeCollection(FilteredText.STRING_NETWORK_TYPE, value.pages);
}
@Override
public WritableBookContent read(@NotNull NetworkBuffer buffer) {
return new WritableBookContent(buffer.readCollection(FilteredText.STRING_NETWORK_TYPE, 100));
}
};
public static final BinaryTagSerializer NBT_TYPE = new BinaryTagSerializer<>() {
@Override
public @NotNull BinaryTag write(@NotNull WritableBookContent value) {
ListBinaryTag.Builder pages = ListBinaryTag.builder();
for (FilteredText page : value.pages) {
pages.add(FilteredText.STRING_NBT_TYPE.write(page));
}
return CompoundBinaryTag.builder().put("pages", pages.build()).build();
}
@Override
public @NotNull WritableBookContent read(@NotNull BinaryTag tag) {
if (!(tag instanceof CompoundBinaryTag compound)) return EMPTY;
ListBinaryTag pagesTag = compound.getList("pages");
if (pagesTag.size() == 0) return EMPTY;
List> pages = new ArrayList<>(pagesTag.size());
for (BinaryTag pageTag : pagesTag) {
pages.add(FilteredText.STRING_NBT_TYPE.read(pageTag));
}
return new WritableBookContent(pages);
}
};
public WritableBookContent {
pages = List.copyOf(pages);
}
}