Search in sources :

Example 1 with ClientboundOpenBookPacket

use of net.minecraft.network.protocol.game.ClientboundOpenBookPacket 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()));
    }
}
Also used : Locale(java.util.Locale) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) Collection(java.util.Collection) ItemTypes(org.spongepowered.api.item.ItemTypes) Book(net.kyori.adventure.inventory.Book) Inventory(net.minecraft.world.entity.player.Inventory) ClientboundOpenBookPacket(net.minecraft.network.protocol.game.ClientboundOpenBookPacket) Objects(java.util.Objects) ServerPlayer(net.minecraft.server.level.ServerPlayer) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) Keys(org.spongepowered.api.data.Keys) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lists(com.google.common.collect.Lists) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) Locale(java.util.Locale) InteractionHand(net.minecraft.world.InteractionHand) Player(org.spongepowered.api.entity.living.player.Player) GlobalTranslator(net.kyori.adventure.translation.GlobalTranslator) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(org.spongepowered.api.entity.living.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) ClientboundOpenBookPacket(net.minecraft.network.protocol.game.ClientboundOpenBookPacket) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(net.minecraft.world.entity.player.Inventory) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl)

Aggregations

Lists (com.google.common.collect.Lists)1 Collection (java.util.Collection)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 Book (net.kyori.adventure.inventory.Book)1 GlobalTranslator (net.kyori.adventure.translation.GlobalTranslator)1 ClientboundContainerSetSlotPacket (net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket)1 ClientboundOpenBookPacket (net.minecraft.network.protocol.game.ClientboundOpenBookPacket)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)1 InteractionHand (net.minecraft.world.InteractionHand)1 Inventory (net.minecraft.world.entity.player.Inventory)1 Keys (org.spongepowered.api.data.Keys)1 Player (org.spongepowered.api.entity.living.player.Player)1 ItemTypes (org.spongepowered.api.item.ItemTypes)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 ItemStackUtil (org.spongepowered.common.item.util.ItemStackUtil)1