Skip to content

Commit

Permalink
Make 'actionSelling' and 'actionBuying' methods return transaction re…
Browse files Browse the repository at this point in the history
…sult (#1831)
  • Loading branch information
maxcom1 authored Jan 4, 2025
1 parent dd49e84 commit e098e6e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public interface ShopManager {
* @param info The info of the shop
* @param shop The shop
* @param amount The amount of the item/stack
* @return If the transaction was successfull
*/
void actionBuying(
boolean actionBuying(
@NotNull Player buyer,
@NotNull InventoryWrapper buyerInventory,
@NotNull AbstractEconomy eco,
Expand All @@ -64,8 +65,9 @@ void actionBuying(
* @param info The info of the shop
* @param shop The shop
* @param amount The amount of the item/stack
* @return If the transaction was successfull
*/
void actionSelling(
boolean actionSelling(
@NotNull Player seller,
@NotNull InventoryWrapper sellerInventory,
@NotNull AbstractEconomy eco,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,40 +170,40 @@ public void init() {
}

@Override
public void actionBuying(@NotNull final Player buyer, @NotNull final InventoryWrapper buyerInventory, @NotNull final AbstractEconomy eco, @NotNull final Info info, @NotNull final Shop shop, final int amount) {
public boolean actionBuying(@NotNull final Player buyer, @NotNull final InventoryWrapper buyerInventory, @NotNull final AbstractEconomy eco, @NotNull final Info info, @NotNull final Shop shop, final int amount) {

final QUser buyerQUser = QUserImpl.createFullFilled(buyer);
if(!plugin.perm().hasPermission(buyer, "quickshop.other.use") && !shop.playerAuthorize(buyer.getUniqueId(), BuiltInShopPermission.PURCHASE)) {
plugin.text().of("no-permission").send();
return;
return false;
}

if(shop.isFrozen()) {
plugin.text().of(buyer, "shop-cannot-trade-when-freezing").send();
return;
return false;
}

if(shopIsNotValid(buyerQUser, info, shop)) {
return;
return false;
}
int space = shop.getRemainingSpace();
if(space == -1) {
space = 10000;
}
if(space < amount) {
plugin.text().of(buyer, "shop-has-no-space", Component.text(space), Util.getItemStackName(shop.getItem())).send();
return;
return false;
}
final int count = Util.countItems(buyerInventory, shop);
// Not enough items
if(amount > count) {
plugin.text().of(buyer, "you-dont-have-that-many-items", Component.text(count), Util.getItemStackName(shop.getItem())).send();
return;
return false;
}
if(amount < 1) {
// & Dumber
plugin.text().of(buyer, "negative-amount").send();
return;
return false;
}

// Money handling
Expand All @@ -213,7 +213,7 @@ public void actionBuying(@NotNull final Player buyer, @NotNull final InventoryWr
final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, buyerQUser, buyerInventory, amount, total);
if(Util.fireCancellableEvent(e)) {
plugin.text().of(buyer, "plugin-cancelled", e.getCancelReason()).send();
return; // Cancelled
return false; // Cancelled
} else {
total = e.getTotal(); // Allow addon to set it
}
Expand All @@ -237,13 +237,13 @@ public void actionBuying(@NotNull final Player buyer, @NotNull final InventoryWr
}
if(!transaction.checkBalance()) {
plugin.text().of(buyer, "the-owner-cant-afford-to-buy-from-you", format(total, shop.getLocation().getWorld(), shop.getCurrency()), format(eco.getBalance(shop.getOwner(), shop.getLocation().getWorld(), shop.getCurrency()), shop.getLocation().getWorld(), shop.getCurrency())).send();
return;
return false;
}
if(!transaction.failSafeCommit()) {
plugin.text().of(buyer, "economy-transaction-failed", transaction.getLastError()).send();
plugin.logger().error("EconomyTransaction Failed, last error: {}", transaction.getLastError());
plugin.logger().error("Tips: If you see any economy plugin name appears above, please don't ask QuickShop support. Contact with developer of economy plugin. QuickShop didn't process the transaction, we only receive the transaction result from your economy plugin.");
return;
return false;
}

try {
Expand All @@ -252,12 +252,13 @@ public void actionBuying(@NotNull final Player buyer, @NotNull final InventoryWr
plugin.logger().warn("Failed to processing purchase, rolling back...", shopError);
transaction.rollback(true);
plugin.text().of(buyer, "shop-transaction-failed", shopError.getMessage()).send();
return;
return false;
}
sendSellSuccess(buyerQUser, shop, amount, total, transaction.getTax());
new ShopSuccessPurchaseEvent(shop, buyerQUser, buyerInventory, amount, total, transaction.getTax()).callEvent();
shop.setSignText(plugin.text().findRelativeLanguages(buyer)); // Update the signs count
notifySold(buyerQUser, shop, amount, space);
return true;
}

private void notifySold(@NotNull final QUser buyerQUser, @NotNull final Shop shop, final int amount, final int space) {
Expand Down Expand Up @@ -363,22 +364,22 @@ ShopType.SELLING, new YamlConfiguration(), null, false,
}

@Override
public void actionSelling(@NotNull final Player seller, @NotNull final InventoryWrapper sellerInventory, @NotNull final AbstractEconomy eco, @NotNull final Info info, @NotNull final Shop shop, final int amount) {
public boolean actionSelling(@NotNull final Player seller, @NotNull final InventoryWrapper sellerInventory, @NotNull final AbstractEconomy eco, @NotNull final Info info, @NotNull final Shop shop, final int amount) {

Util.ensureThread(false);
final QUser sellerQUser = QUserImpl.createFullFilled(seller);

if(!plugin.perm().hasPermission(seller, "quickshop.other.use") && !shop.playerAuthorize(seller.getUniqueId(), BuiltInShopPermission.PURCHASE)) {
plugin.text().of("no-permission").send();
return;
return false;
}
if(shopIsNotValid(sellerQUser, info, shop)) {
return;
return false;
}

if(shop.isFrozen()) {
plugin.text().of(seller, "shop-cannot-trade-when-freezing").send();
return;
return false;
}

int stock = shop.getRemainingStock();
Expand All @@ -387,22 +388,22 @@ public void actionSelling(@NotNull final Player seller, @NotNull final Inventory
}
if(stock < amount) {
plugin.text().of(seller, "shop-stock-too-low", Component.text(stock), Util.getItemStackName(shop.getItem())).send();
return;
return false;
}
final int playerSpace = Util.countSpace(sellerInventory, shop);
if(playerSpace < amount) {
plugin.text().of(seller, "inventory-space-full", amount, playerSpace).send();
return;
return false;
}
if(amount < 1) {
// & Dumber
plugin.text().of(seller, "negative-amount").send();
return;
return false;
}
final int pSpace = Util.countSpace(sellerInventory, shop);
if(amount > pSpace) {
plugin.text().of(seller, "not-enough-space", Component.text(pSpace)).send();
return;
return false;
}

final double taxModifier = getTax(shop, sellerQUser);
Expand All @@ -411,7 +412,7 @@ public void actionSelling(@NotNull final Player seller, @NotNull final Inventory
final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, sellerQUser, sellerInventory, amount, total);
if(Util.fireCancellableEvent(e)) {
plugin.text().of(seller, "plugin-cancelled", e.getCancelReason()).send();
return; // Cancelled
return false; // Cancelled
} else {
total = e.getTotal(); // Allow addon to set it
}
Expand All @@ -438,12 +439,12 @@ public void actionSelling(@NotNull final Player seller, @NotNull final Inventory

if(!transaction.checkBalance()) {
plugin.text().of(seller, "you-cant-afford-to-buy", format(total, shop.getLocation().getWorld(), shop.getCurrency()), format(eco.getBalance(sellerQUser, shop.getLocation().getWorld(), shop.getCurrency()), shop.getLocation().getWorld(), shop.getCurrency())).send();
return;
return false;
}
if(!transaction.failSafeCommit()) {
plugin.text().of(seller, "economy-transaction-failed", transaction.getLastError()).send();
plugin.logger().error("EconomyTransaction Failed, last error: {}", transaction.getLastError());
return;
return false;
}

try {
Expand All @@ -452,11 +453,12 @@ public void actionSelling(@NotNull final Player seller, @NotNull final Inventory
plugin.logger().warn("Failed to processing purchase, rolling back...", shopError);
transaction.rollback(true);
plugin.text().of(seller, "shop-transaction-failed", shopError.getMessage()).send();
return;
return false;
}
sendPurchaseSuccess(sellerQUser, shop, amount, total, transaction.getTax());
new ShopSuccessPurchaseEvent(shop, sellerQUser, sellerInventory, amount, total, transaction.getTax()).callEvent();
notifyBought(sellerQUser, shop, amount, stock, transaction.getTax(), total);
return true;
}


Expand Down

0 comments on commit e098e6e

Please sign in to comment.