ChestShop-3/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/AmountAndPriceChecker.java
Phoenix616 2d45dcdac0 Get rid of expensive calls to Bukkit#getOfflinePlayer with a username
This changes events to store the database Account instead of an OfflinePlayer and deprecates any event method that uses/returns OfflinePlayer. This is necessary as Bukkit#getOfflinePlayer(String) queries Mojang for the UUID when the user was not found in the local cache. As we already store this information (name to UUID mapping) in our database we should not have no need to rely on querying Mojang. (This might make transactions fail for shop owners that haven't played before but that shouldn't really be an issue in most cases)
2017-10-29 23:50:24 +01:00

69 lines
2.4 KiB
Java

package com.Acrobot.ChestShop.Listeners.PreTransaction;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
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 java.math.BigDecimal;
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 onBuyItemCheck(PreTransactionEvent event) {
if (event.isCancelled() || event.getTransactionType() != BUY) {
return;
}
ItemStack[] stock = event.getStock();
Inventory ownerInventory = event.getOwnerInventory();
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
event.setCancelled(CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY);
return;
}
if (!InventoryUtil.hasItems(stock, ownerInventory)) {
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();
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()),
event.getOwnerAccount().getUuid(),
event.getSign().getWorld());
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
event.setCancelled(SHOP_DOES_NOT_HAVE_ENOUGH_MONEY);
return;
}
if (!InventoryUtil.hasItems(stock, clientInventory)) {
event.setCancelled(NOT_ENOUGH_STOCK_IN_INVENTORY);
}
}
}