Search in sources :

Example 1 with SlotAdapter

use of org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.

the class MixinContainer method beforeSlotChangedCraftingGrid.

@Redirect(method = "slotChangedCraftingGrid", at = @At(value = "INVOKE", target = "Lnet/minecraft/inventory/InventoryCraftResult;setInventorySlotContents(ILnet/minecraft/item/ItemStack;)V"))
private void beforeSlotChangedCraftingGrid(InventoryCraftResult output, int index, ItemStack itemstack) {
    if (!this.captureInventory) {
        // Capture Inventory is true when caused by a vanilla inventory packet
        // This is to prevent infinite loops when a client mod re-requests the recipe result after we modified/cancelled it
        output.setInventorySlotContents(index, itemstack);
        return;
    }
    this.spongeInit();
    this.capturedCraftPreviewTransactions.clear();
    ItemStackSnapshot orig = ItemStackUtil.snapshotOf(output.getStackInSlot(index));
    output.setInventorySlotContents(index, itemstack);
    ItemStackSnapshot repl = ItemStackUtil.snapshotOf(output.getStackInSlot(index));
    SlotAdapter slot = this.adapters.get(index);
    this.capturedCraftPreviewTransactions.add(new SlotTransaction(slot, orig, repl));
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with SlotAdapter

use of org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method handleSlotRestore.

public static void handleSlotRestore(EntityPlayer player, @Nullable Container openContainer, List<SlotTransaction> slotTransactions, boolean eventCancelled) {
    for (SlotTransaction slotTransaction : slotTransactions) {
        if ((!slotTransaction.getCustom().isPresent() && slotTransaction.isValid()) && !eventCancelled) {
            continue;
        }
        final SlotAdapter slot = (SlotAdapter) slotTransaction.getSlot();
        ItemStackSnapshot snapshot = eventCancelled || !slotTransaction.isValid() ? slotTransaction.getOriginal() : slotTransaction.getCustom().get();
        final ItemStack originalStack = ItemStackUtil.fromSnapshotToNative(snapshot);
        if (openContainer == null) {
            slot.set(((org.spongepowered.api.item.inventory.ItemStack) originalStack));
        } else {
            final int slotNumber = slot.slotNumber;
            final Slot nmsSlot = openContainer.getSlot(slotNumber);
            if (nmsSlot != null) {
                nmsSlot.putStack(originalStack);
            }
        }
    }
    if (openContainer != null) {
        openContainer.detectAndSendChanges();
        // we must also validate the player still has the same container open after the event has been processed
        if (eventCancelled && player.openContainer == openContainer && player instanceof EntityPlayerMP) {
            ((EntityPlayerMP) player).sendContainerToPlayer(openContainer);
        }
    }
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Slot(net.minecraft.inventory.Slot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) SlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(net.minecraft.item.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Aggregations

ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)2 SlotAdapter (org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 Slot (net.minecraft.inventory.Slot)1 ItemStack (net.minecraft.item.ItemStack)1 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1