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();
}
}
}
Aggregations