Search in sources :

Example 11 with SlotTransaction

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

the class MixinContainer method afterSlotChangedCraftingGrid.

@Inject(method = "slotChangedCraftingGrid", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetHandlerPlayServer;sendPacket(Lnet/minecraft/network/Packet;)V"))
private void afterSlotChangedCraftingGrid(World world, EntityPlayer player, InventoryCrafting craftingInventory, InventoryCraftResult output, CallbackInfo ci) {
    if (this.firePreview && !this.capturedCraftPreviewTransactions.isEmpty()) {
        Inventory inv = this.query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
        if (!(inv instanceof CraftingInventory)) {
            SpongeImpl.getLogger().warn("Detected crafting but Sponge could not get a CraftingInventory for " + this.getClass().getName());
            return;
        }
        SlotTransaction previewTransaction = this.capturedCraftPreviewTransactions.get(this.capturedCraftPreviewTransactions.size() - 1);
        IRecipe recipe = CraftingManager.findMatchingRecipe(craftingInventory, world);
        SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) inv), previewTransaction, ((CraftingRecipe) recipe), ((Container) (Object) this), this.capturedCraftPreviewTransactions);
        this.capturedCraftPreviewTransactions.clear();
    }
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) Container(net.minecraft.inventory.Container) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) IRecipe(net.minecraft.item.crafting.IRecipe) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 12 with SlotTransaction

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

the class InventorySetOpsTest method onMidas.

@Listener
public void onMidas(ChangeInventoryEvent.Held event, @Root Player player) {
    // Checks if Slots are contained in the hotbar then may transform iron to gold
    Inventory hotbar = event.getTargetInventory().query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class));
    boolean nugget = false;
    for (SlotTransaction transaction : event.getTransactions()) {
        if (hotbar.containsInventory(transaction.getSlot())) {
            if (ItemTypes.GOLD_NUGGET.equals(transaction.getOriginal().getType())) {
                nugget = true;
            }
            if (nugget && ItemTypes.IRON_INGOT.equals(transaction.getOriginal().getType())) {
                transaction.setCustom(ItemStack.of(ItemTypes.GOLD_INGOT, transaction.getOriginal().getQuantity()));
            }
        }
    }
}
Also used : Hotbar(org.spongepowered.api.item.inventory.entity.Hotbar) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) Listener(org.spongepowered.api.event.Listener)

Example 13 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project modules-extra by CubeEngine.

the class ChangeInventoryReport method listen.

@Listener
public void listen(ClickInventoryEvent event) {
    List<SlotTransaction> upperTransactions = new ArrayList<>();
    int upperSize = event.getTargetInventory().iterator().next().capacity();
    for (SlotTransaction transaction : event.getTransactions()) {
        Integer affectedSlot = transaction.getSlot().getInventoryProperty(SlotIndex.class).map(SlotIndex::getValue).orElse(-1);
        boolean upper = affectedSlot != -1 && affectedSlot < upperSize;
        if (upper) {
            upperTransactions.add(transaction);
        }
    }
    Inventory te = event.getTargetInventory().query(QueryOperationTypes.TYPE.of(BlockCarrier.class));
    if (!(te instanceof BlockCarrier)) {
        te = te.first();
    }
    if (te instanceof BlockCarrier) {
        Action action = this.observe(event);
        action.addData(INVENTORY_CHANGES, Observe.transactions(upperTransactions));
        action.addData(Report.LOCATION, Observe.location(((BlockCarrier) te).getLocation()));
        this.report(action);
    }
}
Also used : Action(org.cubeengine.module.vigil.report.Action) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) ArrayList(java.util.ArrayList) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) Listener(org.spongepowered.api.event.Listener)

Example 14 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project AdamantineShield by Karanum.

the class InventoryChangeListener method onInventoryTransfer.

@Listener
public void onInventoryTransfer(AffectSlotEvent e, @First Player p) {
    if (e.getTransactions().isEmpty())
        return;
    if (!(e.getTransactions().get(0).getSlot().parent() instanceof CarriedInventory))
        return;
    BlockCarrier carrier = null;
    CarriedInventory<?> c = (CarriedInventory<?>) e.getTransactions().get(0).getSlot().parent();
    if (c.getCarrier().get() instanceof BlockCarrier) {
        carrier = (BlockCarrier) c.getCarrier().get();
    }
    if (carrier == null)
        return;
    if (!logContainers && !(carrier instanceof Chest))
        return;
    long timestamp = new Date().getTime();
    int containerSize = c.iterator().next().capacity();
    for (SlotTransaction transaction : e.getTransactions()) {
        int slotId = transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(SlotIndex::getValue).orElse(-1);
        if (slotId >= containerSize)
            continue;
        ItemStackSnapshot origItem = transaction.getOriginal();
        ItemStackSnapshot finalItem = transaction.getFinal();
        if (origItem == finalItem)
            continue;
        if (origItem.createGameDictionaryEntry().matches(finalItem.createStack()) && ItemStackComparators.ITEM_DATA.compare(origItem.createStack(), finalItem.createStack()) == 0) {
            if (origItem.getQuantity() > finalItem.getQuantity()) {
                ItemStackSnapshot stack = ItemStack.builder().itemType(origItem.getType()).quantity(origItem.getQuantity() - finalItem.getQuantity()).build().createSnapshot();
                db.addToQueue(new InventoryQueueEntry(carrier, slotId, stack, ActionType.CONTAINER_REMOVE, p, timestamp));
            } else if (origItem.getQuantity() < finalItem.getQuantity()) {
                ItemStackSnapshot stack = ItemStack.builder().itemType(origItem.getType()).quantity(finalItem.getQuantity() - origItem.getQuantity()).build().createSnapshot();
                db.addToQueue(new InventoryQueueEntry(carrier, slotId, stack, ActionType.CONTAINER_ADD, p, timestamp));
            }
        } else {
            if (origItem.getType() != ItemTypes.NONE) {
                db.addToQueue(new InventoryQueueEntry(carrier, slotId, origItem, ActionType.CONTAINER_REMOVE, p, timestamp));
            }
            if (finalItem.getType() != ItemTypes.NONE) {
                db.addToQueue(new InventoryQueueEntry(carrier, slotId, finalItem, ActionType.CONTAINER_ADD, p, timestamp));
            }
        }
    }
}
Also used : Chest(org.spongepowered.api.block.tileentity.carrier.Chest) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) InventoryQueueEntry(com.karanumcoding.adamantineshield.db.queue.InventoryQueueEntry) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Date(java.util.Date) Listener(org.spongepowered.api.event.Listener)

Example 15 with SlotTransaction

use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project core by CubeEngine.

the class InventoryGuard method onInventoryInteract.

@Listener
public void onInventoryInteract(ClickInventoryEvent event) {
    if (!event.getTargetInventory().equals(this.container) && !((Slot) event.getTargetInventory().slots().iterator().next()).transform().parent().equals(this.inventory)) {
        return;
    }
    if (!blockAllIn && !blockAllOut && blockIn.isEmpty() && blockOut.isEmpty()) {
        return;
    }
    // System.out.print("Event:\n");
    boolean cancel = false;
    int upperSize = event.getTargetInventory().iterator().next().capacity();
    for (SlotTransaction transaction : event.getTransactions()) {
        ItemStack origStack = transaction.getOriginal().createStack();
        ItemStack finalStack = transaction.getFinal().createStack();
        String origString = origStack.getType().equals(ItemTypes.NONE) ? origStack.getType().getId() : origStack.getType().getId() + " " + origStack.getQuantity();
        String finalString = finalStack.getType().equals(ItemTypes.NONE) ? finalStack.getType().getId() : finalStack.getType().getId() + " " + finalStack.getQuantity();
        // System.out.print(origString + "->" + finalString + "\n");
        // System.out.println("SI: " + transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(si -> si.getValue()).orElse(-1) + " " + transaction.getSlot().parent().capacity());
        Integer affectedSlot = transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(SlotIndex::getValue).orElse(-1);
        boolean upper = affectedSlot != -1 && affectedSlot < upperSize;
        if (upper) {
            if (checkTransaction(event, transaction, origStack, finalStack)) {
                cancel = true;
            } else {
                runOnChange();
            }
        }
    // else lower inventory was affected ; actually we don't care (yet)
    }
    if (cancel) {
    // System.out.print("Cancelled\n");
    }
}
Also used : Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Listener(org.spongepowered.api.event.Listener)

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