Search in sources :

Example 1 with ClientboundContainerSetSlotPacket

use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.

the class ContainerBasedTransaction method handleCraftingPreview.

private void handleCraftingPreview(final Player player, final ClickContainerEvent event) {
    if (this.craftingInventory != null) {
        // TODO push event to cause?
        // TODO prevent event when there is no preview?
        final SlotTransaction previewTransaction = this.getPreviewTransaction(this.craftingInventory.result(), event.transactions());
        final Optional<CraftingRecipe> recipe = player.level.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, this.craftingContainer, player.level).map(CraftingRecipe.class::cast);
        final CraftItemEvent.Preview previewEvent = SpongeEventFactory.createCraftItemEventPreview(event.cause(), (Container) this.menu, this.craftingInventory, event.cursorTransaction(), previewTransaction, recipe, Optional.empty(), event.transactions());
        SpongeCommon.post(previewEvent);
        this.handleEventResults(player, previewEvent);
        if (player instanceof ServerPlayer && previewEvent instanceof CraftItemEvent.Preview) {
            final SlotTransaction preview = previewEvent.preview();
            // Resend modified output if needed
            if (!preview.isValid() || previewEvent.isCancelled()) {
                ((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(0, 0, ItemStackUtil.fromSnapshotToNative(previewEvent.preview().original())));
            // TODO handle preview event cancel during shift-crafting
            } else if (preview.custom().isPresent()) {
                ((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(0, 0, ItemStackUtil.fromSnapshotToNative(previewEvent.preview().finalReplacement())));
            // TODO handle preview event modification during shift-crafting
            }
        }
    }
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 2 with ClientboundContainerSetSlotPacket

use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket 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)

Example 3 with ClientboundContainerSetSlotPacket

use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method handlePlayerSlotRestore.

public static void handlePlayerSlotRestore(final net.minecraft.server.level.ServerPlayer player, final ItemStack itemStack, final InteractionHand hand) {
    if (itemStack.isEmpty()) {
        // No need to check if it's NONE, NONE is checked by isEmpty.
        return;
    }
    player.ignoreSlotUpdateHack = false;
    int slotId = 0;
    if (hand == InteractionHand.OFF_HAND) {
        player.inventory.offhand.set(0, itemStack);
        slotId = (player.inventory.items.size() + Inventory.getSelectionSize());
    } else {
        player.inventory.items.set(player.inventory.selected, itemStack);
        // TODO check if window id -2 and slotid = player.inventory.currentItem works instead of this:
        for (Slot containerSlot : player.containerMenu.slots) {
            if (containerSlot.container == player.inventory && ((SlotAccessor) containerSlot).accessor$slot() == player.inventory.selected) {
                slotId = containerSlot.index;
                break;
            }
        }
    }
    player.containerMenu.broadcastChanges();
    player.ignoreSlotUpdateHack = false;
    player.connection.send(new ClientboundContainerSetSlotPacket(player.containerMenu.containerId, slotId, itemStack));
}
Also used : ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) Slot(net.minecraft.world.inventory.Slot) SlotAccessor(org.spongepowered.common.accessor.world.inventory.SlotAccessor)

Example 4 with ClientboundContainerSetSlotPacket

use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method handleCursorRestore.

public static void handleCursorRestore(final Player player, final Transaction<ItemStackSnapshot> cursorTransaction) {
    final ItemStackSnapshot cursorSnap;
    if (!cursorTransaction.isValid()) {
        cursorSnap = cursorTransaction.original();
    } else if (cursorTransaction.custom().isPresent()) {
        cursorSnap = cursorTransaction.finalReplacement();
    } else {
        return;
    }
    final ItemStack cursor = ItemStackUtil.fromSnapshotToNative(cursorSnap);
    player.inventory.setCarried(cursor);
    if (player instanceof net.minecraft.server.level.ServerPlayer) {
        ((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(-1, -1, cursor));
    }
}
Also used : ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.world.item.ItemStack)

Example 5 with ClientboundContainerSetSlotPacket

use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method handleCustomCursor.

public static void handleCustomCursor(final Player player, final ItemStackSnapshot customCursor) {
    final ItemStack cursor = ItemStackUtil.fromSnapshotToNative(customCursor);
    player.inventory.setCarried(cursor);
    if (player instanceof net.minecraft.server.level.ServerPlayer) {
        ((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(-1, -1, cursor));
    }
}
Also used : ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

ClientboundContainerSetSlotPacket (net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket)5 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 ItemStack (net.minecraft.world.item.ItemStack)2 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)2 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 ClientboundOpenBookPacket (net.minecraft.network.protocol.game.ClientboundOpenBookPacket)1 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)1 InteractionHand (net.minecraft.world.InteractionHand)1 Inventory (net.minecraft.world.entity.player.Inventory)1 Slot (net.minecraft.world.inventory.Slot)1 Keys (org.spongepowered.api.data.Keys)1 Player (org.spongepowered.api.entity.living.player.Player)1 CraftItemEvent (org.spongepowered.api.event.item.inventory.CraftItemEvent)1 ItemTypes (org.spongepowered.api.item.ItemTypes)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1