Search in sources :

Example 1 with InventoryQueueEntry

use of com.karanumcoding.adamantineshield.db.queue.InventoryQueueEntry 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)

Aggregations

InventoryQueueEntry (com.karanumcoding.adamantineshield.db.queue.InventoryQueueEntry)1 Date (java.util.Date)1 Chest (org.spongepowered.api.block.tileentity.carrier.Chest)1 Listener (org.spongepowered.api.event.Listener)1 BlockCarrier (org.spongepowered.api.item.inventory.BlockCarrier)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)1 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)1