Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2959,22 +2959,25 @@ public void openBook(ItemStack book) {
Preconditions.checkArgument(book != null, "ItemStack cannot be null");
Preconditions.checkArgument(book.hasData(DataComponentTypes.WRITTEN_BOOK_CONTENT), "ItemStack must have a 'written_book_content' component");

final ItemStack previousItem = this.getInventory().getItemInMainHand();
this.getInventory().setItemInMainHand(book);
this.getHandle().openItemGui(CraftItemStack.asNMSCopy(book), InteractionHand.MAIN_HAND);
this.getInventory().setItemInMainHand(previousItem);
net.minecraft.world.item.ItemStack bookItem = CraftItemStack.asNMSCopy(book);
ServerPlayer serverPlayer = this.getHandle();
net.minecraft.world.item.component.WrittenBookContent.resolveForItem(bookItem, serverPlayer.createCommandSourceStack(), serverPlayer);
sendBookOpen(bookItem);
}

@Override
public void openBook(final Book book) {
final ItemStack mutatedItem = ItemType.WRITTEN_BOOK.createItemStack(); // dummy item
mutatedItem.setData(DataComponentTypes.WRITTEN_BOOK_CONTENT, WrittenBookContent.writtenBookContent("", "").addPages(book.pages()));
sendBookOpen(CraftItemStack.unwrap(mutatedItem));
}

private void sendBookOpen(net.minecraft.world.item.ItemStack bookItem) {
final net.minecraft.world.item.ItemStack selectedItem = this.getHandle().getInventory().getSelectedItem();
final int slot = this.getHandle().getInventory().getSelectedSlot();
this.getHandle().connection.send(new ClientboundBundlePacket(
List.of(
new ClientboundSetPlayerInventoryPacket(slot, CraftItemStack.unwrap(mutatedItem)),
new ClientboundSetPlayerInventoryPacket(slot, bookItem),
new ClientboundOpenBookPacket(InteractionHand.MAIN_HAND),
new ClientboundSetPlayerInventoryPacket(slot, selectedItem)
)
Expand Down