2012-08-10 21:44:32 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners.PreTransaction;
|
2012-08-10 18:56:16 +02:00
|
|
|
|
|
|
|
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
|
|
|
import com.Acrobot.ChestShop.Economy.Economy;
|
|
|
|
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.*;
|
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
|
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class AmountAndPriceChecker implements Listener {
|
|
|
|
@EventHandler
|
|
|
|
public static void onItemCheck(PreTransactionEvent event) {
|
|
|
|
if (event.isCancelled() || event.getTransactionType() != BUY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack[] stock = event.getStock();
|
|
|
|
Inventory ownerInventory = event.getOwnerInventory();
|
|
|
|
|
|
|
|
if (!Economy.hasEnough(event.getClient().getName(), event.getPrice())) {
|
|
|
|
event.setCancelled(CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:03:45 +02:00
|
|
|
if (!InventoryUtil.hasItems(stock, ownerInventory)) {
|
2012-08-10 18:56:16 +02:00
|
|
|
event.setCancelled(NOT_ENOUGH_STOCK_IN_CHEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public static void onSellItemCheck(PreTransactionEvent event) {
|
|
|
|
if (event.isCancelled() || event.getTransactionType() != SELL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack[] stock = event.getStock();
|
|
|
|
Inventory clientInventory = event.getClientInventory();
|
|
|
|
|
2012-09-15 20:32:22 +02:00
|
|
|
if (Economy.isOwnerEconomicallyActive(event.getOwnerInventory()) && !Economy.hasEnough(event.getOwner().getName(), event.getPrice())) {
|
2012-08-10 18:56:16 +02:00
|
|
|
event.setCancelled(SHOP_DOES_NOT_HAVE_ENOUGH_MONEY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:03:45 +02:00
|
|
|
if (!InventoryUtil.hasItems(stock, clientInventory)) {
|
2012-08-10 18:56:16 +02:00
|
|
|
event.setCancelled(NOT_ENOUGH_STOCK_IN_INVENTORY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|