2012-08-10 21:44:32 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners.PostTransaction;
|
2012-08-10 18:56:16 +02:00
|
|
|
|
2021-03-03 18:16:14 +01:00
|
|
|
import com.Acrobot.Breeze.Utils.ImplementationAdapter;
|
2012-08-10 18:56:16 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
2021-03-03 18:16:14 +01:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2013-02-07 14:17:23 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2012-08-10 18:56:16 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.TransactionEvent;
|
2019-04-23 19:00:21 +02:00
|
|
|
import org.bukkit.block.BlockState;
|
2021-03-03 18:16:14 +01:00
|
|
|
import org.bukkit.block.DoubleChest;
|
2019-04-23 19:00:21 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2012-08-10 18:56:16 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
2018-08-22 01:21:34 +02:00
|
|
|
import org.bukkit.event.EventPriority;
|
2012-08-10 18:56:16 +02:00
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
2019-04-23 19:00:21 +02:00
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
2012-08-10 18:56:16 +02:00
|
|
|
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 {
|
2018-08-22 01:21:34 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2012-08-10 18:56:16 +02:00
|
|
|
public static void shopItemRemover(TransactionEvent event) {
|
|
|
|
if (event.getTransactionType() != BUY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 15:49:27 +02:00
|
|
|
transferItems(event.getOwnerInventory(), event.getClientInventory(), event.getStock());
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 01:21:34 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2012-08-10 18:56:16 +02:00
|
|
|
public static void inventoryItemRemover(TransactionEvent event) {
|
|
|
|
if (event.getTransactionType() != SELL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 15:49:27 +02:00
|
|
|
transferItems(event.getClientInventory(), event.getOwnerInventory(), event.getStock());
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
|
2019-04-05 15:49:27 +02:00
|
|
|
private static void transferItems(Inventory sourceInventory, Inventory targetInventory, ItemStack[] items) {
|
2013-02-07 14:17:23 +01:00
|
|
|
if (Properties.STACK_TO_64) {
|
|
|
|
for (ItemStack item : items) {
|
2019-04-05 15:49:27 +02:00
|
|
|
InventoryUtil.transfer(item, sourceInventory, targetInventory, 64);
|
2013-02-07 14:17:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (ItemStack item : items) {
|
2019-04-05 15:49:27 +02:00
|
|
|
InventoryUtil.transfer(item, sourceInventory, targetInventory);
|
2013-02-07 14:17:23 +01:00
|
|
|
}
|
2012-08-10 22:25:08 +02:00
|
|
|
}
|
2019-04-23 19:00:21 +02:00
|
|
|
update(sourceInventory.getHolder());
|
|
|
|
update(targetInventory.getHolder());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void update(InventoryHolder holder) {
|
|
|
|
if (holder instanceof Player) {
|
|
|
|
((Player) holder).updateInventory();
|
2021-03-03 18:16:14 +01:00
|
|
|
} else if (holder instanceof BlockState) {
|
2019-04-23 19:00:21 +02:00
|
|
|
((BlockState) holder).update();
|
2021-03-03 18:16:14 +01:00
|
|
|
} else if (holder instanceof DoubleChest) {
|
|
|
|
update(ImplementationAdapter.getLeftSide((DoubleChest) holder, false));
|
|
|
|
update(ImplementationAdapter.getRightSide((DoubleChest) holder, false));
|
2019-04-23 19:00:21 +02:00
|
|
|
}
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
}
|