Search in sources :

Example 1 with SlotAccessor

use of org.spongepowered.common.accessor.world.inventory.SlotAccessor 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)

Aggregations

ClientboundContainerSetSlotPacket (net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket)1 Slot (net.minecraft.world.inventory.Slot)1 SlotAccessor (org.spongepowered.common.accessor.world.inventory.SlotAccessor)1