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