Search in sources :

Example 6 with SlotTransaction

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

the class PlaceRecipePacketState method unwind.

@Override
public void unwind(InventoryPacketContext context) {
    CPacketPlaceRecipe packet = context.getPacket();
    boolean shift = packet.func_194319_c();
    IRecipe recipe = packet.func_194317_b();
    final EntityPlayerMP player = context.getPacketPlayer();
    ((IMixinContainer) player.openContainer).detectAndSendChanges(true);
    ((IMixinContainer) player.openContainer).setCaptureInventory(false);
    ((IMixinContainer) player.openContainer).setFirePreview(true);
    Inventory craftInv = ((Inventory) player.openContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
    if (!(craftInv instanceof CraftingInventory)) {
        SpongeImpl.getLogger().warn("Detected crafting without a InventoryCrafting!? Crafting Event will not fire.");
        return;
    }
    List<SlotTransaction> previewTransactions = ((IMixinContainer) player.openContainer).getPreviewTransactions();
    if (previewTransactions.isEmpty()) {
        CraftingOutput slot = ((CraftingInventory) craftInv).getResult();
        SlotTransaction st = new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(slot.peek().orElse(ItemStack.empty())));
        previewTransactions.add(st);
    }
    SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) craftInv), previewTransactions.get(0), ((CraftingRecipe) recipe), player.openContainer, previewTransactions);
    previewTransactions.clear();
    final Entity spongePlayer = EntityUtil.fromNative(player);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(spongePlayer);
        frame.pushCause(player.openContainer);
        List<SlotTransaction> transactions = ((IMixinContainer) player.openContainer).getCapturedTransactions();
        ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(player.inventory.getItemStack());
        Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(cursor, cursor);
        ClickInventoryEvent event;
        if (shift) {
            event = SpongeEventFactory.createClickInventoryEventShiftPrimary(frame.getCurrentCause(), cursorTransaction, ((Container) player.openContainer), transactions);
        } else {
            event = SpongeEventFactory.createClickInventoryEventPrimary(frame.getCurrentCause(), cursorTransaction, ((Container) player.openContainer), transactions);
        }
        SpongeImpl.postEvent(event);
        if (event.isCancelled() || !event.getCursorTransaction().isValid()) {
            PacketPhaseUtil.handleCustomCursor(player, event.getCursorTransaction().getOriginal());
        } else {
            PacketPhaseUtil.handleCustomCursor(player, event.getCursorTransaction().getFinal());
        }
        PacketPhaseUtil.handleSlotRestore(player, player.openContainer, event.getTransactions(), event.isCancelled());
        event.getTransactions().clear();
    }
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) Entity(org.spongepowered.api.entity.Entity) IRecipe(net.minecraft.item.crafting.IRecipe) CraftingOutput(org.spongepowered.api.item.inventory.crafting.CraftingOutput) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) 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) CauseStackManager(org.spongepowered.api.event.CauseStackManager) CPacketPlaceRecipe(net.minecraft.network.play.client.CPacketPlaceRecipe) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory)

Example 7 with SlotTransaction

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

the class MixinEntityPlayerMP method dropItem.

/**
 * @author gabizou, April 7th, 2016
 *
 * Technically an overwrite of {@link EntityPlayer#dropItem(boolean)}
 * @param dropAll
 * @return
 */
@Override
@Nullable
public EntityItem dropItem(boolean dropAll) {
    final ItemStack currentItem = this.inventory.getCurrentItem();
    if (currentItem.isEmpty()) {
        return null;
    }
    // Add SlotTransaction to PlayerContainer
    org.spongepowered.api.item.inventory.Slot slot = ((Inventory) this.inventoryContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class)).query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(this.inventory.currentItem)));
    final ItemStackSnapshot originalItem = ItemStackUtil.snapshotOf(currentItem);
    ItemStack itemToDrop = this.inventory.decrStackSize(this.inventory.currentItem, dropAll && !currentItem.isEmpty() ? currentItem.getCount() : 1);
    ((IMixinContainer) this.inventoryContainer).getCapturedTransactions().add(new SlotTransaction(slot, originalItem, ItemStackUtil.snapshotOf(this.inventory.getCurrentItem())));
    return this.dropItem(itemToDrop, false, true);
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.item.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Nullable(javax.annotation.Nullable)

Example 8 with SlotTransaction

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

the class MixinInventoryPlayer method onAdd.

@Inject(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/NonNullList;set(ILjava/lang/Object;)Ljava/lang/Object;", ordinal = 0))
public void onAdd(int index, ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
    if (this.doCapture) {
        // Capture "damaged" items picked up
        Slot slot = getSpongeSlot(index);
        this.capturedTransactions.add(new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(stack)));
    }
}
Also used : Slot(org.spongepowered.api.item.inventory.Slot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 9 with SlotTransaction

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

the class MixinContainer method onPutStackInSlot.

@Inject(method = "putStackInSlot", at = @At(value = "HEAD"))
public void onPutStackInSlot(int slotId, ItemStack itemstack, CallbackInfo ci) {
    if (this.captureInventory) {
        this.spongeInit();
        final Slot slot = getSlot(slotId);
        if (slot != null) {
            ItemStackSnapshot originalItem = slot.getStack().isEmpty() ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
            ItemStackSnapshot newItem = itemstack.isEmpty() ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) itemstack).createSnapshot();
            org.spongepowered.api.item.inventory.Slot adapter = this.getContainerSlot(slotId);
            this.capturedSlotTransactions.add(new SlotTransaction(adapter, originalItem, newItem));
        }
    }
}
Also used : SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 10 with SlotTransaction

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

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