use of net.kyori.adventure.inventory.Book in project SpongeCommon by SpongePowered.
the class BookUtil method fakeBookView.
public static void fakeBookView(final Book book, final Collection<? extends Player> players) {
// First we need to send a fake a Book ItemStack with the BookView's
// contents to the player's hand
// These values are localized since sending item NBT doesn't trigger translation
final ItemStack item = ItemStack.of(ItemTypes.WRITTEN_BOOK, 1);
Locale lastLocale = BookUtil.STARTER_LOCALE;
for (final Player player : players) {
if (!Objects.equals(player.locale(), lastLocale)) {
lastLocale = player.locale();
item.offer(Keys.CUSTOM_NAME, GlobalTranslator.render(book.title(), lastLocale));
item.offer(Keys.AUTHOR, GlobalTranslator.render(book.author(), lastLocale));
final Locale finalLastLocale = lastLocale;
item.offer(Keys.PAGES, Lists.transform(book.pages(), page -> GlobalTranslator.render(page, finalLastLocale)));
}
final ServerPlayer mcPlayer = (ServerPlayer) player;
final ServerGamePacketListenerImpl receiver = mcPlayer.connection;
final Inventory inventory = mcPlayer.inventory;
final int bookSlot = inventory.items.size() + inventory.selected;
receiver.send(new ClientboundContainerSetSlotPacket(BookUtil.WINDOW_PLAYER_INVENTORY, bookSlot, ItemStackUtil.toNative(item)));
// Next we tell the client to open the Book GUI
receiver.send(new ClientboundOpenBookPacket(InteractionHand.MAIN_HAND));
// Now we can remove the fake Book since it's contents will have already
// been transferred to the GUI
receiver.send(new ClientboundContainerSetSlotPacket(BookUtil.WINDOW_PLAYER_INVENTORY, bookSlot, inventory.getSelected()));
}
}
Aggregations