2012-08-10 21:44:32 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners.PostTransaction;
|
2012-08-10 18:56:16 +02:00
|
|
|
|
2012-09-12 12:35:48 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
2013-10-27 16:50:19 +01:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2013-10-27 16:50:19 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
|
2012-08-10 18:56:16 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.TransactionEvent;
|
2012-08-24 10:24:20 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-08-10 18:56:16 +02:00
|
|
|
import org.bukkit.Material;
|
2018-10-23 20:04:18 +02:00
|
|
|
import org.bukkit.block.Container;
|
2012-09-12 12:35:48 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2012-08-10 18:56:16 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
2012-09-12 12:35:48 +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;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class EmptyShopDeleter implements Listener {
|
2013-10-27 16:50:19 +01:00
|
|
|
|
2018-08-22 01:21:34 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
2012-08-10 18:56:16 +02:00
|
|
|
public static void onTransaction(TransactionEvent event) {
|
|
|
|
if (event.getTransactionType() != TransactionEvent.TransactionType.BUY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-27 16:50:19 +01:00
|
|
|
Sign sign = event.getSign();
|
2021-01-24 23:33:36 +01:00
|
|
|
|
|
|
|
if (ChestShopSign.isAdminShop(sign)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Inventory ownerInventory = event.getOwnerInventory();
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2012-09-12 12:35:48 +02:00
|
|
|
if (!shopShouldBeRemoved(ownerInventory, event.getStock())) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-05-05 17:23:30 +02:00
|
|
|
if (!isInRemoveWorld(sign)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-12 12:35:48 +02:00
|
|
|
|
2021-01-24 23:33:36 +01:00
|
|
|
Container connectedContainer = uBlock.findConnectedContainer(sign);
|
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedContainer);
|
2013-10-27 16:50:19 +01:00
|
|
|
ChestShop.callEvent(destroyedEvent);
|
|
|
|
|
2020-05-20 01:42:28 +02:00
|
|
|
Material signType = sign.getType();
|
2012-09-12 12:35:48 +02:00
|
|
|
sign.getBlock().setType(Material.AIR);
|
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
if (Properties.REMOVE_EMPTY_CHESTS && !ChestShopSign.isAdminShop(ownerInventory) && InventoryUtil.isEmpty(ownerInventory)) {
|
2023-03-01 18:14:18 +01:00
|
|
|
if (connectedContainer != null) {
|
|
|
|
connectedContainer.getBlock().setType(Material.AIR);
|
|
|
|
}
|
2012-09-12 12:35:48 +02:00
|
|
|
} else {
|
2020-05-20 01:42:28 +02:00
|
|
|
if (!signType.isItem()) {
|
|
|
|
try {
|
|
|
|
signType = Material.valueOf(signType.name().replace("WALL_", ""));
|
|
|
|
} catch (IllegalArgumentException ignored) {}
|
|
|
|
}
|
|
|
|
if (signType.isItem()) {
|
|
|
|
ownerInventory.addItem(new ItemStack(signType, 1));
|
|
|
|
} else {
|
|
|
|
ChestShop.getBukkitLogger().warning("Unable to get item for sign " + signType + " to add to removed shop's container!");
|
|
|
|
}
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2012-09-02 22:41:24 +02:00
|
|
|
private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) {
|
2021-01-24 23:33:36 +01:00
|
|
|
if (Properties.REMOVE_EMPTY_SHOPS) {
|
2020-05-06 21:53:59 +02:00
|
|
|
if (Properties.ALLOW_PARTIAL_TRANSACTIONS) {
|
|
|
|
for (ItemStack itemStack : stock) {
|
|
|
|
if (inventory.containsAtLeast(itemStack, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else if (!InventoryUtil.hasItems(stock, inventory)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-09-02 22:41:24 +02:00
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-05-05 17:23:30 +02:00
|
|
|
private static boolean isInRemoveWorld(Sign sign) {
|
|
|
|
return Properties.REMOVE_EMPTY_WORLDS.isEmpty() || Properties.REMOVE_EMPTY_WORLDS.contains(sign.getWorld().getName());
|
|
|
|
}
|
2012-08-10 18:56:16 +02:00
|
|
|
}
|