Make book metas use components for pages

This commit is contained in:
Kieran Wallbanks 2021-03-24 17:18:45 +00:00
parent 9f87912d02
commit eeedd97e8b
2 changed files with 19 additions and 17 deletions

View File

@ -1,5 +1,8 @@
package net.minestom.server.item.metadata;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.MinecraftServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -14,7 +17,7 @@ public class WritableBookMeta extends ItemMeta {
private String title;
private String author;
private List<String> pages = new ArrayList<>();
private List<Component> pages = new ArrayList<>();
@Nullable
public String getTitle() {
@ -42,7 +45,7 @@ public class WritableBookMeta extends ItemMeta {
* @return a modifiable {@link ArrayList} containing the book pages
*/
@NotNull
public List<String> getPages() {
public List<Component> getPages() {
return pages;
}
@ -51,7 +54,7 @@ public class WritableBookMeta extends ItemMeta {
*
* @param pages the pages list
*/
public void setPages(@NotNull List<String> pages) {
public void setPages(@NotNull List<Component> pages) {
this.pages = pages;
}
@ -82,7 +85,7 @@ public class WritableBookMeta extends ItemMeta {
if (compound.containsKey("pages")) {
final NBTList<NBTString> list = compound.getList("pages");
for (NBTString page : list) {
this.pages.add(page.getValue());
this.pages.add(GsonComponentSerializer.gson().deserialize(page.getValue()));
}
}
}
@ -100,8 +103,8 @@ public class WritableBookMeta extends ItemMeta {
if (!pages.isEmpty()) {
NBTList<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
for (String page : pages) {
list.add(new NBTString(page));
for (Component page : pages) {
list.add(new NBTString(MinecraftServer.getSerializationManager().serialize(page)));
}
compound.set("pages", list);
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.item.metadata;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.Localizable;
import org.jetbrains.annotations.NotNull;
@ -19,7 +20,7 @@ public class WrittenBookMeta extends ItemMeta {
private WrittenBookGeneration generation;
private String author;
private String title;
private List<String> pages = new ArrayList<>();
private List<Component> pages = new ArrayList<>();
/**
* Gets if the book is resolved.
@ -97,11 +98,12 @@ public class WrittenBookMeta extends ItemMeta {
/**
* Gets an {@link ArrayList} containing all the pages.
* <p>
* The list is modifiable.
* The list is not modifiable as it is .
*
* @return a modifiable {@link ArrayList} with the pages of the book
*/
public List<String> getPages() {
@Deprecated
public List<Component> getPagesJson() {
return pages;
}
@ -110,7 +112,7 @@ public class WrittenBookMeta extends ItemMeta {
*
* @param pages the array list containing the book pages
*/
public void setPages(List<String> pages) {
public void setPages(List<Component> pages) {
this.pages = pages;
}
@ -150,7 +152,7 @@ public class WrittenBookMeta extends ItemMeta {
if (compound.containsKey("pages")) {
final NBTList<NBTString> list = compound.getList("pages");
for (NBTString page : list) {
this.pages.add(page.getValue());
this.pages.add(GsonComponentSerializer.gson().deserialize(page.getValue()));
}
}
}
@ -171,8 +173,8 @@ public class WrittenBookMeta extends ItemMeta {
}
if (!pages.isEmpty()) {
NBTList<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
for (String page : pages) {
list.add(new NBTString(page));
for (Component page : pages) {
list.add(new NBTString(MinecraftServer.getSerializationManager().serialize(page)));
}
compound.set("pages", list);
}
@ -212,10 +214,7 @@ public class WrittenBookMeta extends ItemMeta {
meta.author = MinecraftServer.getSerializationManager().translateAndSerialize(book.author(), localizable);
meta.title = MinecraftServer.getSerializationManager().translateAndSerialize(book.title(), localizable);
meta.pages = new ArrayList<>();
for (Component page : book.pages()) {
meta.pages.add(MinecraftServer.getSerializationManager().serialize(page));
}
meta.pages.addAll(book.pages());
return meta;
}