From 4fb4ea4a7940cd06a61d4f86fc3f695e83217683 Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Sun, 21 Aug 2022 00:19:42 +0200 Subject: [PATCH] Work on ShopTransactions --- .../playershops/shop/ShopTransaction.java | 127 ++++++++++++++++-- .../playershops/shop/TransactionError.java | 11 ++ .../com/alttd/playershops/utils/ShopUtil.java | 39 +----- 3 files changed, 135 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/alttd/playershops/shop/TransactionError.java diff --git a/src/main/java/com/alttd/playershops/shop/ShopTransaction.java b/src/main/java/com/alttd/playershops/shop/ShopTransaction.java index cfa5ab9..37cfb31 100755 --- a/src/main/java/com/alttd/playershops/shop/ShopTransaction.java +++ b/src/main/java/com/alttd/playershops/shop/ShopTransaction.java @@ -1,13 +1,124 @@ package com.alttd.playershops.shop; -public class ShopTransaction { +import com.alttd.playershops.events.PlayerExchangeShopEvent; +import com.alttd.playershops.utils.EconomyUtils; +import com.alttd.playershops.utils.InventoryUtils; +import com.alttd.playershops.utils.Util; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; - public enum ShopTransactionError { - CANCELLED, - INSUFFICIENT_FUNDS_SHOP, - INSUFFICIENT_FUNDS_PLAYER, - INVENTORY_FULL_SHOP, - INVENTORY_FULL_PLAYER, - NONE; +public record ShopTransaction(int orders, Player player, PlayerShop shop) { + + public TransactionError executeTransaction(boolean isCheck) { + return switch (shop.getType()) { + case SELL -> executeSellTransaction(); + case BUY -> executeBuyTransaction(); + case GAMBLE -> executeGambleTransaction(); + default -> TransactionError.NONE; // This should not happen + }; } + + public TransactionError executeSellTransaction() { + ItemStack itemStack = shop.getItemStack().clone(); + double price = shop.getPrice(); + if (orders == itemStack.getMaxStackSize()) { + itemStack.setAmount(orders); + price = shop.getPricePerItem() * orders; + } + + int shopItems = InventoryUtils.countItems(shop.getInventory(), itemStack); + if (shopItems < itemStack.getAmount()) + return TransactionError.INSUFFICIENT_FUNDS_SHOP; + + boolean hasFunds = EconomyUtils.hasSufficientFunds(player, price); + if (!hasFunds) + return TransactionError.INSUFFICIENT_FUNDS_PLAYER; + + boolean hasRoom = EconomyUtils.canAcceptFunds(shop, price); + if (!hasRoom) + return TransactionError.INVENTORY_FULL_SHOP; + + boolean playerHasRoom = InventoryUtils.hasRoom(player.getInventory(), itemStack); + if (!playerHasRoom) + return TransactionError.INVENTORY_FULL_PLAYER; + + PlayerExchangeShopEvent playerExchangeShopEvent = new PlayerExchangeShopEvent(player, shop); + if (Util.fireCancellableEvent(playerExchangeShopEvent)) { + return TransactionError.CANCELLED; + } + + InventoryUtils.removeItem(shop.getInventory(), itemStack); + EconomyUtils.removeFunds(player, price); + shop.AddBalance(price); + InventoryUtils.addItem(player.getInventory(), itemStack); + + player.updateInventory(); + return TransactionError.NONE; + } + + public TransactionError executeBuyTransaction() { +// ItemStack is = shop.getItemStack(); +// double price = shop.getPrice(); +// if(orders == is.getMaxStackSize()) { +// is.setAmount(orders); +// price = shop.getPricePerItem() * orders; +// } +// +// //check if player has enough items +// if(isCheck) { +// int playerItems = InventoryUtils.countItems(player.getInventory(), is); +// if (playerItems < is.getAmount()) +// return TransactionError.INSUFFICIENT_FUNDS_PLAYER; +// } else { +// //remove items from player +// InventoryUtils.removeItem(player.getInventory(), is, player); +// } +// +// //check if shop has enough currency +// if(isCheck) { +// if (!(shop.getBalance() >= price)) +// return TransactionError.INSUFFICIENT_FUNDS_SHOP; +// } else { +// shop.RemoveBalance(price); +// } +// +// if(isCheck) { +// if (!player.isOnline()) +// return TransactionError.PLAYER_OFFLINE; +// } else { +// //add currency to player +// EconomyUtils.addFunds(player, player.getInventory(), price); +// } +// +// //check if shop has enough room to accept items +// if(isCheck) { +// boolean shopHasRoom = InventoryUtils.hasRoom(this.getInventory(), is, this.getOwner()); +// if (!shopHasRoom) +// return TransactionError.INVENTORY_FULL_SHOP; +// } else{ +// //add items to shop's inventory +// InventoryUtils.addItem(this.getInventory(), is, this.getOwner()); +// } +// +// player.updateInventory(); +// +// //if there are no issues with the test/check transaction +// if(isCheck){ +// +// PlayerExchangeShopEvent e = new PlayerExchangeShopEvent(player, this); +// Bukkit.getPluginManager().callEvent(e); +// +// if(e.isCancelled()) +// return TransactionError.CANCELLED; +// +// //run the transaction again without the check clause +// return executeTransaction(false); +// } + return TransactionError.NONE; + } + + public TransactionError executeGambleTransaction() { + return TransactionError.NONE; + } + } diff --git a/src/main/java/com/alttd/playershops/shop/TransactionError.java b/src/main/java/com/alttd/playershops/shop/TransactionError.java new file mode 100644 index 0000000..235258a --- /dev/null +++ b/src/main/java/com/alttd/playershops/shop/TransactionError.java @@ -0,0 +1,11 @@ +package com.alttd.playershops.shop; + +public enum TransactionError { + CANCELLED, + INSUFFICIENT_FUNDS_SHOP, + INSUFFICIENT_FUNDS_PLAYER, + INVENTORY_FULL_SHOP, + INVENTORY_FULL_PLAYER, + PLAYER_OFFLINE, + NONE; +} diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index c085887..8600428 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -2,6 +2,7 @@ package com.alttd.playershops.utils; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.enchantments.Enchantment; @@ -13,28 +14,6 @@ import java.util.Map; public class ShopUtil { - /** - * Counts the number of items in the given inventory where - * Util.matches(inventory item, item) is true. - * - * @param inv - * The inventory to search - * @param item - * The ItemStack to search for - * @return The number of items that match in this inventory. - */ - public static int countItems(Inventory inv, ItemStack item) { - int items = 0; - for (ItemStack iStack : inv.getContents()) { - if (iStack == null) - continue; - if (ShopUtil.matches(item, iStack)) { - items += iStack.getAmount(); - } - } - return items; - } - /** * Returns true if the given location is loaded or not. * @@ -43,22 +22,12 @@ public class ShopUtil { * @return true if the given location is loaded or not. */ public static boolean isLoaded(Location loc) { - // System.out.println("Checking isLoaded(Location loc)"); - if (loc.getWorld() == null) { - // System.out.println("Is not loaded. (No world)"); + if (!loc.isWorldLoaded()) { return false; } - // Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT - // location rounded to the nearest 16. int x = (int) Math.floor((loc.getBlockX()) / 16.0); int z = (int) Math.floor((loc.getBlockZ()) / 16.0); - if (loc.getWorld().isChunkLoaded(x, z)) { - // System.out.println("Chunk is loaded " + x + ", " + z); - return true; - } else { - // System.out.println("Chunk is NOT loaded " + x + ", " + z); - return false; - } + return (loc.getWorld().isChunkLoaded(x, z)); } /** @@ -149,4 +118,6 @@ public class ShopUtil { ItemStack stack = cfg.getItemStack("item"); return stack; } + + }