From 658837814a43b4c76ff83adf3c84338d955c9b6b Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Sun, 21 Aug 2022 17:36:01 +0200 Subject: [PATCH] Refactor ShopTransactions --- .../com/alttd/playershops/PlayerShops.java | 9 +- .../playershops/shop/ShopTransaction.java | 124 ------------------ 2 files changed, 6 insertions(+), 127 deletions(-) delete mode 100755 src/main/java/com/alttd/playershops/shop/ShopTransaction.java diff --git a/src/main/java/com/alttd/playershops/PlayerShops.java b/src/main/java/com/alttd/playershops/PlayerShops.java index 64bb53b..2da4e08 100644 --- a/src/main/java/com/alttd/playershops/PlayerShops.java +++ b/src/main/java/com/alttd/playershops/PlayerShops.java @@ -1,8 +1,10 @@ package com.alttd.playershops; +import com.alttd.playershops.commands.ShopCommand; import com.alttd.playershops.config.Config; import com.alttd.playershops.config.DatabaseConfig; import com.alttd.playershops.config.MessageConfig; +import com.alttd.playershops.gui.GuiIcon; import com.alttd.playershops.handler.ShopHandler; import com.alttd.playershops.listener.BlockListener; import com.alttd.playershops.listener.PlayerListener; @@ -84,9 +86,7 @@ public class PlayerShops extends JavaPlugin { } private void registerCommands() { -// for(String command : this.getDescription().getCommands().keySet()) { -// getCommand(command).setExecutor(); -// } + getCommand("playershop").setExecutor(new ShopCommand()); } public void reloadConfigs() { @@ -97,6 +97,9 @@ public class PlayerShops extends JavaPlugin { for (ShopType shopType : ShopType.values()) { // preload ShopType to get the configs active } + for (GuiIcon guiIcon : GuiIcon.values()) { + // preload to get config values generated + } } } diff --git a/src/main/java/com/alttd/playershops/shop/ShopTransaction.java b/src/main/java/com/alttd/playershops/shop/ShopTransaction.java deleted file mode 100755 index 37cfb31..0000000 --- a/src/main/java/com/alttd/playershops/shop/ShopTransaction.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.alttd.playershops.shop; - -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 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; - } - -}