package com.Acrobot.ChestShop.Listeners.PostTransaction;

import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;

/**
 * @author Acrobot
 */
public class ItemManager implements Listener {
    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public static void shopItemRemover(TransactionEvent event) {
        if (event.getTransactionType() != BUY) {
            return;
        }

        transferItems(event.getOwnerInventory(), event.getClientInventory(), event.getStock());

        event.getClient().updateInventory();
    }

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public static void inventoryItemRemover(TransactionEvent event) {
        if (event.getTransactionType() != SELL) {
            return;
        }

        transferItems(event.getClientInventory(), event.getOwnerInventory(), event.getStock());

        event.getClient().updateInventory();
    }

    private static void transferItems(Inventory sourceInventory, Inventory targetInventory, ItemStack[] items) {
        if (Properties.STACK_TO_64) {
            for (ItemStack item : items) {
                InventoryUtil.transfer(item, sourceInventory, targetInventory, 64);
            }
        } else {
            for (ItemStack item : items) {
                InventoryUtil.transfer(item, sourceInventory, targetInventory);
            }
        }
    }
}

@@ -207,16 +207,85 @@
 nEvent;%0A
+import org.bukkit.block.BlockState;%0Aimport org.bukkit.entity.Player;%0A
 import o
@@ -310,24 +310,24 @@
 entHandler;%0A
-
 import org.b
@@ -426,16 +426,61 @@
 entory;%0A
+import org.bukkit.inventory.InventoryHolder;%0A
 import o
@@ -1066,54 +1066,8 @@
 ());
-%0A%0A        event.getClient().updateInventory();
 %0A   
@@ -1393,54 +1393,8 @@
 ());
-%0A%0A        event.getClient().updateInventory();
 %0A   
@@ -1829,16 +1829,16 @@
 ntory);%0A
-
         
@@ -1849,20 +1849,371 @@
 %7D%0A        %7D%0A
+        update(sourceInventory.getHolder());%0A        update(targetInventory.getHolder());%0A    %7D%0A%0A    private static void update(InventoryHolder holder) %7B%0A        if (holder instanceof Player) %7B%0A            ((Player) holder).updateInventory();%0A        %7D%0A        if (holder instanceof BlockState) %7B%0A            ((BlockState) holder).update();%0A        %7D%0A
     %7D%0A%7D%0A

Update the container on item transfers (Fixes #217)