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