Create a written book?

Hi everyone,

I have a 6.1.0 sponge vanilla server and I’m compiling with sponge api 6.0.0 (gradle).

I’m trying to create a written book.
Here’s my current code:

if(src instanceof Player) {
	Player p = (Player) src;
	
	List<Text> lpages = new LinkedList<>();
	
	for(Report r : ReporterLab.getReports()) {
		Text head = r.toText(null);
		lpages.add(head);
	}
	
	ItemStack book = ItemStack.builder()
		.itemType(ItemTypes.WRITTEN_BOOK)
		.build();
	
	if(book.supports(Keys.BOOK_PAGES)) {
		book.tryOffer(Keys.BOOK_PAGES, lpages);
	} else {
		logger.error("BOOK DOES NOT SUPPORT PAGES; WRONG ITEM USED");
	}
	
	book.offer(Keys.BOOK_AUTHOR,
		Text.of(TextColors.DARK_PURPLE, TextStyles.BOLD, "The Server"));
	
	//NOW:
	LocalDateTime datetime = LocalDateTime.now();
	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm, dd-MM-yyyy");
	
	book.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD,
		"Reports, as of ",
		datetime.format(formatter)));
	
	p.getInventory().set(book);
	}

Where Report#toText(null); returns a Text made of several strings and numbers (no colors, no styles).

Whenever I run this command to get the book, the pages of the book are filled with:

“* Invalid book tag *”

However, the name (title?) and author are set correctly. book.tryOffer(...) (line 16) also doesn’t cause an error.

What am I doing wrong?

Do us a favor.

Once you’ve finished writing the book, print out the result of DataFormats.JSON.write(book.toContainer()). It will be informative.

A written book must have

  • Pages (Keys.BOOK_PAGES), each page must serialise to JSON with less than or equal to 32767 characters
  • Title (Keys.DISPLAY_NAME) less than or equal to 32 characters
  • Author (Keys.BOOK_AUTHOR)

Make sure your code meets those conditions

I did this today. It returned:

{"ContentVersion":1,
  "ItemType":"minecraft:written_book",
  "Count":1,
  "UnsafeDamage":0,
  "UnsafeData": {
    "pages":[],
    "author":"The Server",
    "title":"Book, as of 20:21, 27-06-2017"
    ,"resolved":1
  }
}

As you can see, there are no pages, even though I’m adding a List<Text>.
Also, the data is listed as “UnsafeData”, does this mean anything?


what do you mean by that?


##UPDATE

Okay, it seems I did something wrong. I now filled the pages with Text.of("TEXT") and pages look like

"pages":["{\"text\":\"simpleton\"}","{\"text\":\"simpleton\"}","{\"text\":\"simpleton\"}","{\"text\":\"simpleton\"}"]

I can now see what you said about texts and JSON.

Still, the issue is not solved

The Minecraft protocol serialises Text objects as JSON (as shown here: http://wiki.vg/Chat). You can serialise a Text object with TextSerializers.JSON.serialize(text) and see what the resulting length of the string is.

As for your issue, I’m not sure why it’s not working, there could be an issue with Keys.BOOK_PAGES

I wrote a book manually (in Minecraft) and wrote that to JSON. Here are the results:


{
  "ContentVersion":1,
  "ItemType":"minecraft:written_book",
  "Count":1,
  "UnsafeDamage":0,
  "UnsafeData" : {
    "pages":
      [
        "{\"text\":\"I LIKE TO WRITE TEXT HERE.\\n\\nAND ALSO HERE\"}",
        "{\"text\":\"BUT NOT HERE\"}",
        "{\"text\":\"\"}",
        "{\"text\":\"BLEUHG\"}"
      ],
    "author":"MisterCavespider",
    "title":"ez",
    "resolved":1
  }
}