Search in sources :

Example 1 with BlockCarrier

use of org.spongepowered.api.item.inventory.BlockCarrier 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 2 with BlockCarrier

use of org.spongepowered.api.item.inventory.BlockCarrier 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 3 with BlockCarrier

use of org.spongepowered.api.item.inventory.BlockCarrier in project LanternServer by LanternPowered.

the class LanternMultiBlockCarrier method getInventory.

@Override
public Optional<Inventory> getInventory(Location<World> at, Direction from) {
    checkNotNull(at, "at");
    checkNotNull(from, "from");
    final BlockCarrier carrier = this.carriers.get(at);
    return carrier == null ? Optional.empty() : Optional.of(carrier.getInventory(from));
}
Also used : MultiBlockCarrier(org.spongepowered.api.item.inventory.MultiBlockCarrier) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier)

Example 4 with BlockCarrier

use of org.spongepowered.api.item.inventory.BlockCarrier in project LanternServer by LanternPowered.

the class LanternMultiBlockCarrier method getInventory.

@Override
public Optional<Inventory> getInventory(Location<World> at) {
    checkNotNull(at, "at");
    final BlockCarrier carrier = this.carriers.get(at);
    return carrier == null ? Optional.empty() : Optional.of(carrier.getInventory());
}
Also used : MultiBlockCarrier(org.spongepowered.api.item.inventory.MultiBlockCarrier) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier)

Aggregations

BlockCarrier (org.spongepowered.api.item.inventory.BlockCarrier)4 Listener (org.spongepowered.api.event.Listener)2 MultiBlockCarrier (org.spongepowered.api.item.inventory.MultiBlockCarrier)2 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)2 InventoryQueueEntry (com.karanumcoding.adamantineshield.db.queue.InventoryQueueEntry)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Action (org.cubeengine.module.vigil.report.Action)1 Chest (org.spongepowered.api.block.tileentity.carrier.Chest)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)1