Search in sources :

Example 1 with CPacketHeldItemChange

use of net.minecraft.network.play.client.CPacketHeldItemChange in project SpongeCommon by SpongePowered.

the class SwitchHotbarScrollState method unwind.

@Override
public void unwind(InventoryPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    final CPacketHeldItemChange itemChange = context.getPacket();
    final int previousSlot = context.getHighlightedSlotId();
    final net.minecraft.inventory.Container inventoryContainer = player.inventoryContainer;
    final InventoryPlayer inventory = player.inventory;
    // Crafting Grid & Result
    int preHotbarSize = inventory.mainInventory.size() - InventoryPlayer.getHotbarSize() + inventory.armorInventory.size() + 4 + 1;
    final Slot sourceSlot = inventoryContainer.getSlot(previousSlot + preHotbarSize);
    final Slot targetSlot = inventoryContainer.getSlot(itemChange.getSlotId() + preHotbarSize);
    ItemStackSnapshot sourceSnapshot = ItemStackUtil.snapshotOf(sourceSlot.getStack());
    ItemStackSnapshot targetSnapshot = ItemStackUtil.snapshotOf(targetSlot.getStack());
    SlotTransaction sourceTransaction = new SlotTransaction(ContainerUtil.getSlot(inventoryContainer, previousSlot + preHotbarSize), sourceSnapshot, sourceSnapshot);
    SlotTransaction targetTransaction = new SlotTransaction(ContainerUtil.getSlot(inventoryContainer, itemChange.getSlotId() + preHotbarSize), targetSnapshot, targetSnapshot);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(player);
        ImmutableList<SlotTransaction> transactions = new ImmutableList.Builder<SlotTransaction>().add(sourceTransaction).add(targetTransaction).build();
        final ChangeInventoryEvent.Held changeInventoryEventHeld = SpongeEventFactory.createChangeInventoryEventHeld(Sponge.getCauseStackManager().getCurrentCause(), (Inventory) inventoryContainer, transactions);
        net.minecraft.inventory.Container openContainer = player.openContainer;
        SpongeImpl.postEvent(changeInventoryEventHeld);
        if (changeInventoryEventHeld.isCancelled() || PacketPhaseUtil.allTransactionsInvalid(changeInventoryEventHeld.getTransactions())) {
            player.connection.sendPacket(new SPacketHeldItemChange(previousSlot));
            inventory.currentItem = previousSlot;
        } else {
            PacketPhaseUtil.handleSlotRestore(player, openContainer, changeInventoryEventHeld.getTransactions(), false);
            inventory.currentItem = itemChange.getSlotId();
            player.markPlayerActive();
        }
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) SPacketHeldItemChange(net.minecraft.network.play.server.SPacketHeldItemChange) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) CPacketHeldItemChange(net.minecraft.network.play.client.CPacketHeldItemChange) Slot(net.minecraft.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 InventoryPlayer (net.minecraft.entity.player.InventoryPlayer)1 Slot (net.minecraft.inventory.Slot)1 CPacketHeldItemChange (net.minecraft.network.play.client.CPacketHeldItemChange)1 SPacketHeldItemChange (net.minecraft.network.play.server.SPacketHeldItemChange)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)1