Search in sources :

Example 36 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callCreativeClickInventoryEvent.

public static ClickInventoryEvent.Creative callCreativeClickInventoryEvent(EntityPlayerMP player, CPacketCreativeInventoryAction packetIn) {
    Sponge.getCauseStackManager().pushCause(player);
    // Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
    // Due to this, we can only send the original item that was clicked in slot
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
    if (((IMixinContainer) player.openContainer).getCapturedTransactions().size() == 0 && packetIn.getSlotId() >= 0 && packetIn.getSlotId() < player.openContainer.inventorySlots.size()) {
        Slot slot = player.openContainer.getSlot(packetIn.getSlotId());
        if (slot != null) {
            ItemStackSnapshot clickedItem = slot.getStack() == null ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
            SlotTransaction slotTransaction = new SlotTransaction(((org.spongepowered.api.item.inventory.Slot) slot), clickedItem, ItemStackSnapshot.NONE);
            ((IMixinContainer) player.openContainer).getCapturedTransactions().add(slotTransaction);
        }
    }
    ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer, ((IMixinContainer) player.openContainer).getCapturedTransactions());
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    return event;
}
Also used : ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot)

Example 37 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method captureTransaction.

/**
 * Captures a transaction
 *
 * @param captureIn the {@link IMixinInventory} to capture the transaction in
 * @param inv the Inventory
 * @param index the affected SlotIndex
 * @param transaction the transaction to execute
 * @return the result if the transaction
 */
public static ItemStack captureTransaction(IMixinInventory captureIn, Inventory inv, int index, Supplier<ItemStack> transaction) {
    // TODO make sure we never got null
    if (captureIn == null || inv == null) {
        return transaction.get();
    }
    Inventory ordered = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(OrderedInventory.class));
    if (ordered instanceof OrderedInventory) {
        Optional<org.spongepowered.api.item.inventory.Slot> slot = ((OrderedInventory) ordered).getSlot(SlotIndex.of(index));
        if (slot.isPresent()) {
            ItemStackSnapshot original = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
            ItemStack remaining = transaction.get();
            if (remaining.isEmpty()) {
                ItemStackSnapshot replacement = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
                captureIn.getCapturedTransactions().add(new SlotTransaction(slot.get(), original, replacement));
            }
            return remaining;
        }
    }
    // else inventory was missing the slot for some reason
    return transaction.get();
}
Also used : SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) ItemStack(net.minecraft.item.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory) IInventory(net.minecraft.inventory.IInventory)

Example 38 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.

the class SwapHandItemsState method unwind.

@Override
public void unwind(InventoryPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    final Entity spongePlayer = EntityUtil.fromNative(player);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(spongePlayer);
        final IMixinInventoryPlayer mixinInventory = ((IMixinInventoryPlayer) player.inventory);
        List<SlotTransaction> trans = mixinInventory.getCapturedTransactions();
        ChangeInventoryEvent.SwapHand swapItemEvent = this.createInventoryEvent(((Inventory) player.inventory), trans);
        SpongeImpl.postEvent(swapItemEvent);
        PacketPhaseUtil.handleSlotRestore(player, null, swapItemEvent.getTransactions(), swapItemEvent.isCancelled());
        mixinInventory.setCapture(false);
        mixinInventory.getCapturedTransactions().clear();
    }
}
Also used : IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) Entity(org.spongepowered.api.entity.Entity) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 39 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction 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)

Example 40 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.

the class DragInventoryStopState method unwindCraftPreview.

public static void unwindCraftPreview(InventoryPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    ((IMixinContainer) player.openContainer).setFirePreview(true);
    Inventory craftInv = ((Inventory) player.openContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
    if (craftInv instanceof CraftingInventory) {
        List<SlotTransaction> previewTransactions = ((IMixinContainer) player.openContainer).getPreviewTransactions();
        if (!previewTransactions.isEmpty()) {
            CraftingRecipe recipe = SpongeCraftingRecipeRegistry.getInstance().findMatchingRecipe(((CraftingInventory) craftInv).getCraftingGrid(), ((World) player.world)).orElse(null);
            SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) craftInv), previewTransactions.get(0), recipe, player.openContainer, previewTransactions);
            previewTransactions.clear();
        }
    }
}
Also used : IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory)

Aggregations

SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)50 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)30 ArrayList (java.util.ArrayList)20 ItemStack (org.spongepowered.api.item.inventory.ItemStack)16 Inventory (org.spongepowered.api.item.inventory.Inventory)14 CraftingInventory (org.spongepowered.api.item.inventory.crafting.CraftingInventory)14 Entity (org.spongepowered.api.entity.Entity)13 Transaction (org.spongepowered.api.data.Transaction)11 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)11 ClickInventoryEvent (org.spongepowered.api.event.item.inventory.ClickInventoryEvent)11 Slot (org.spongepowered.api.item.inventory.Slot)11 Slot (net.minecraft.inventory.Slot)9 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)9 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)9 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)9 IMixinContainer (org.spongepowered.common.interfaces.IMixinContainer)9 Nullable (javax.annotation.Nullable)8 ItemStack (net.minecraft.item.ItemStack)8 CauseStack (org.lanternpowered.server.event.CauseStack)8 List (java.util.List)7