Search in sources :

Example 1 with IMixinInventory

use of org.spongepowered.common.interfaces.IMixinInventory in project SpongeCommon by SpongePowered.

the class MixinBlockDropper method afterDispense.

@Inject(method = "dispense", cancellable = true, locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "INVOKE", target = "Lnet/minecraft/tileentity/TileEntityDispenser;setInventorySlotContents(ILnet/minecraft/item/ItemStack;)V"))
private void afterDispense(World worldIn, BlockPos pos, CallbackInfo callbackInfo, BlockSourceImpl blocksourceimpl, TileEntityDispenser tileentitydispenser, int i, ItemStack itemstack, EnumFacing enumfacing, BlockPos blockpos, IInventory iinventory, ItemStack itemstack1) {
    // after setInventorySlotContents
    tileentitydispenser.setInventorySlotContents(i, itemstack1);
    // Transfer worked if remainder is one less than the original stack
    if (itemstack1.getCount() == itemstack.getCount() - 1) {
        IMixinInventory capture = forCapture(tileentitydispenser);
        Inventory sourceInv = toInventory(tileentitydispenser);
        SpongeCommonEventFactory.captureTransaction(capture, sourceInv, i, itemstack);
        SpongeCommonEventFactory.callTransferPost(capture, sourceInv, toInventory(iinventory));
    }
    callbackInfo.cancel();
}
Also used : IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with IMixinInventory

use of org.spongepowered.common.interfaces.IMixinInventory 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 originalStack the original Stack
 */
public static void captureTransaction(IMixinInventory captureIn, Inventory inv, int index, ItemStack originalStack) {
    // TODO make sure we never got null
    if (captureIn == null || inv == null) {
        return;
    }
    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()) {
            SlotTransaction trans = new SlotTransaction(slot.get(), ItemStackUtil.snapshotOf(originalStack), ItemStackUtil.snapshotOf(slot.get().peek().orElse(org.spongepowered.api.item.inventory.ItemStack.empty())));
            captureIn.getCapturedTransactions().add(trans);
        }
    }
}
Also used : SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) 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 3 with IMixinInventory

use of org.spongepowered.common.interfaces.IMixinInventory in project SpongeCommon by SpongePowered.

the class MixinTileEntityHopper method afterPutStackInSlots.

// Post Captured Transactions
@Inject(method = "transferItemsOut", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", ordinal = 1))
private void afterPutStackInSlots(CallbackInfoReturnable<Boolean> cir, IInventory iInventory, EnumFacing enumFacing, int i, ItemStack itemStack, ItemStack itemStack1) {
    // after putStackInInventoryAllSlots if the transfer worked
    if (ShouldFire.CHANGE_INVENTORY_EVENT_TRANSFER_POST && itemStack1.isEmpty()) {
        // Capture Insert in Origin
        IMixinInventory capture = forCapture(this);
        Inventory source = toInventory(this);
        SpongeCommonEventFactory.captureTransaction(capture, source, i, itemStack);
        // Call event
        if (SpongeCommonEventFactory.callTransferPost(capture, source, toInventory(iInventory))) {
            // Set remainder when cancelled
            itemStack1 = itemStack;
        }
    }
}
Also used : IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) SpongeCommonEventFactory.toInventory(org.spongepowered.common.event.SpongeCommonEventFactory.toInventory) IInventory(net.minecraft.inventory.IInventory) IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with IMixinInventory

use of org.spongepowered.common.interfaces.IMixinInventory in project SpongeCommon by SpongePowered.

the class MixinTileEntityHopper method afterPullItemFromSlot.

@Inject(method = "pullItemFromSlot", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;isEmpty()Z", ordinal = 1))
private static void afterPullItemFromSlot(IHopper hopper, IInventory iInventory, int index, EnumFacing direction, CallbackInfoReturnable<Boolean> cir, ItemStack itemStack, ItemStack itemStack1, ItemStack itemStack2) {
    // after putStackInInventoryAllSlots if the transfer worked
    if (ShouldFire.CHANGE_INVENTORY_EVENT_TRANSFER_POST && itemStack2.isEmpty()) {
        // Capture Insert in Origin
        IMixinInventory capture = forCapture(hopper);
        Inventory source = toInventory(iInventory);
        SpongeCommonEventFactory.captureTransaction(capture, source, index, itemStack1);
        // Call event
        if (SpongeCommonEventFactory.callTransferPost(capture, source, toInventory(hopper))) {
            // Set remainder when cancelled
            itemStack1 = itemStack;
        }
    }
}
Also used : IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) SpongeCommonEventFactory.toInventory(org.spongepowered.common.event.SpongeCommonEventFactory.toInventory) IInventory(net.minecraft.inventory.IInventory) IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 5 with IMixinInventory

use of org.spongepowered.common.interfaces.IMixinInventory 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)

Aggregations

Inventory (org.spongepowered.api.item.inventory.Inventory)8 IMixinInventory (org.spongepowered.common.interfaces.IMixinInventory)8 IInventory (net.minecraft.inventory.IInventory)6 Inject (org.spongepowered.asm.mixin.injection.Inject)5 Slot (net.minecraft.inventory.Slot)2 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)2 CraftingInventory (org.spongepowered.api.item.inventory.crafting.CraftingInventory)2 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)2 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)2 OrderedInventory (org.spongepowered.api.item.inventory.type.OrderedInventory)2 SpongeCommonEventFactory.toInventory (org.spongepowered.common.event.SpongeCommonEventFactory.toInventory)2 CustomInventory (org.spongepowered.common.item.inventory.custom.CustomInventory)2 ItemStack (net.minecraft.item.ItemStack)1 EnumFacing (net.minecraft.util.EnumFacing)1 BlockPos (net.minecraft.util.math.BlockPos)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 Surrogate (org.spongepowered.asm.mixin.injection.Surrogate)1