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;
|
2013-07-13 23:14:10 +02:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
|
|
|
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
|
2012-08-10 18:56:16 +02:00
|
|
|
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;
|
|
|
|
|
2013-07-13 23:14:10 +02:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
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 {
|
2013-07-13 23:14:10 +02:00
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
@EventHandler
|
2013-07-13 23:14:10 +02:00
|
|
|
public static void onBuyItemCheck(PreTransactionEvent event) {
|
2012-08-10 18:56:16 +02:00
|
|
|
if (event.isCancelled() || event.getTransactionType() != BUY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack[] stock = event.getStock();
|
|
|
|
Inventory ownerInventory = event.getOwnerInventory();
|
|
|
|
|
2013-07-13 23:14:10 +02:00
|
|
|
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
|
|
|
|
ChestShop.callEvent(currencyCheckEvent);
|
|
|
|
|
|
|
|
if (!currencyCheckEvent.hasEnough()) {
|
2012-08-10 18:56:16 +02:00
|
|
|
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();
|
|
|
|
|
2014-04-12 13:38:11 +02:00
|
|
|
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()),
|
2017-10-29 23:50:24 +01:00
|
|
|
event.getOwnerAccount().getUuid(),
|
2014-04-12 13:38:11 +02:00
|
|
|
event.getSign().getWorld());
|
2013-07-13 23:14:10 +02:00
|
|
|
ChestShop.callEvent(currencyCheckEvent);
|
|
|
|
|
|
|
|
if (!currencyCheckEvent.hasEnough()) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|