From 15a595f5263b5114eb6c69a624cf29f7e8436e18 Mon Sep 17 00:00:00 2001 From: 7sat <49030779+7sat@users.noreply.github.com> Date: Sat, 4 Mar 2023 17:08:02 +0900 Subject: [PATCH] transaction refactoring --- .../java/me/sat7/dynamicshop/guis/Shop.java | 7 +- .../me/sat7/dynamicshop/transactions/Buy.java | 27 +--- .../sat7/dynamicshop/transactions/Sell.java | 123 +++++++----------- .../dynamicshop/utilities/StringUtil.java | 15 +++ 4 files changed, 74 insertions(+), 98 deletions(-) diff --git a/src/main/java/me/sat7/dynamicshop/guis/Shop.java b/src/main/java/me/sat7/dynamicshop/guis/Shop.java index 6b845d4..f2f982b 100644 --- a/src/main/java/me/sat7/dynamicshop/guis/Shop.java +++ b/src/main/java/me/sat7/dynamicshop/guis/Shop.java @@ -5,6 +5,7 @@ import java.util.regex.Pattern; import me.sat7.dynamicshop.DynaShopAPI; +import me.sat7.dynamicshop.economyhook.PlayerpointHook; import me.sat7.dynamicshop.utilities.ConfigUtil; import me.sat7.dynamicshop.utilities.SoundUtil; import org.bukkit.Bukkit; @@ -53,12 +54,16 @@ public Inventory getGui(Player player, String shopName, int page) { shopData = ShopUtil.shopConfigFiles.get(shopName).get(); - // jobreborn 플러그인 있는지 확인. if (!JobsHook.jobsRebornActive && shopData.contains("Options.flag.jobpoint")) { player.sendMessage(DynamicShop.dsPrefix(player) + t(player, "ERR.JOBS_REBORN_NOT_FOUND")); return null; } + if (!PlayerpointHook.isPPActive && shopData.contains("Options.flag.playerpoint")) + { + player.sendMessage(DynamicShop.dsPrefix(player) + t(player, "ERR.PLAYER_POINT_NOT_FOUND")); + return null; + } this.player = player; this.shopName = shopName; diff --git a/src/main/java/me/sat7/dynamicshop/transactions/Buy.java b/src/main/java/me/sat7/dynamicshop/transactions/Buy.java index 38a38c8..1874563 100644 --- a/src/main/java/me/sat7/dynamicshop/transactions/Buy.java +++ b/src/main/java/me/sat7/dynamicshop/transactions/Buy.java @@ -157,23 +157,10 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) } //로그 기록 - String currencyString = ""; - if (currency == ItemTrade.CURRENCY.VAULT) - { - currencyString = "vault"; - } - else if (currency == ItemTrade.CURRENCY.JOB_POINT) - { - currencyString = "jobPoint"; - } - else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) - { - currencyString = "playerPoint"; - } - LogUtil.addLog(shopName, itemStack.getType().toString(), actualAmount, priceSum, currencyString, player.getName()); + LogUtil.addLog(shopName, itemStack.getType().toString(), actualAmount, priceSum, StringUtil.GetCurrencyString(currency), player.getName()); // 메시지 출력 - SendBuyMessage(currency, econ, r, player, actualAmount, priceSum, itemStack); + SendBuyMessage(currency, econ, player, actualAmount, priceSum, itemStack); // 플레이어에게 소리 재생 SoundUtil.playerSoundEffect(player, "buy"); @@ -195,16 +182,16 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) Bukkit.getPluginManager().callEvent(event); } - private static void SendBuyMessage(ItemTrade.CURRENCY currency, Economy econ, EconomyResponse r, Player player, int actualAmount, double priceSum, ItemStack tempIS) + private static void SendBuyMessage(ItemTrade.CURRENCY currency, Economy econ, Player player, int actualAmount, double priceSum, ItemStack itemStack) { String message = ""; - boolean itemHasCustomName = tempIS.getItemMeta() != null && tempIS.getItemMeta().hasDisplayName(); + boolean itemHasCustomName = itemStack.getItemMeta() != null && itemStack.getItemMeta().hasDisplayName(); boolean useLocalizedName = !itemHasCustomName && ConfigUtil.GetLocalizedItemName(); if (currency == ItemTrade.CURRENCY.VAULT) { message = DynamicShop.dsPrefix(player) + t(player, "MESSAGE.BUY_SUCCESS", !useLocalizedName) .replace("{amount}", Integer.toString(actualAmount)) - .replace("{price}", n(r.amount)) + .replace("{price}", n(priceSum)) .replace("{bal}", n(econ.getBalance(player))); } else if (currency == ItemTrade.CURRENCY.JOB_POINT) @@ -225,11 +212,11 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) if (useLocalizedName) { message = message.replace("{item}", ""); - LangUtil.sendMessageWithLocalizedItemName(player, message, tempIS.getType()); + LangUtil.sendMessageWithLocalizedItemName(player, message, itemStack.getType()); } else { - String itemNameFinal = itemHasCustomName ? tempIS.getItemMeta().getDisplayName() : ItemsUtil.getBeautifiedName(tempIS.getType()); + String itemNameFinal = itemHasCustomName ? itemStack.getItemMeta().getDisplayName() : ItemsUtil.getBeautifiedName(itemStack.getType()); message = message.replace("{item}", itemNameFinal); player.sendMessage(message); } diff --git a/src/main/java/me/sat7/dynamicshop/transactions/Sell.java b/src/main/java/me/sat7/dynamicshop/transactions/Sell.java index 4bec26d..9e121e5 100644 --- a/src/main/java/me/sat7/dynamicshop/transactions/Sell.java +++ b/src/main/java/me/sat7/dynamicshop/transactions/Sell.java @@ -35,21 +35,17 @@ public static double quickSellItem(Player player, ItemStack itemStack, String sh CustomConfig data = ShopUtil.shopConfigFiles.get(shopName); ItemTrade.CURRENCY currencyType; - String currencyString; if (data.get().contains("Options.flag.jobpoint")) { currencyType = ItemTrade.CURRENCY.JOB_POINT; - currencyString = "jobPoint"; } else if (data.get().contains("Options.flag.playerpoint")) { currencyType = ItemTrade.CURRENCY.PLAYER_POINT; - currencyString = "playerPoint"; } else { currencyType = ItemTrade.CURRENCY.VAULT; - currencyString = "vault"; } double priceSellOld = DynaShopAPI.getSellPrice(shopName, itemStack); @@ -138,42 +134,18 @@ else if (data.get().contains("Options.flag.playerpoint")) double[] calcResult = Calc.calcTotalCost(shopName, String.valueOf(tradeIdx), -tradeAmount); priceSum += calcResult[0]; - // 재고 증가 - if (stockOld > 0) - { - data.get().set(tradeIdx + ".stock", MathUtil.SafeAdd(stockOld, tradeAmount)); - } - - // 실제 거래부---------- - Economy econ = null; + Economy econ = DynamicShop.getEconomy(); EconomyResponse r = null; - if (currencyType == ItemTrade.CURRENCY.VAULT) - { - econ = DynamicShop.getEconomy(); - if (player != null) - r = DynamicShop.getEconomy().depositPlayer(player, priceSum); - - if (r != null && !r.transactionSuccess()) - return 0; - } - else if (currencyType == ItemTrade.CURRENCY.JOB_POINT) - { - if (!JobsHook.addJobsPoint(player, priceSum)) - return 0; - } - else if (currencyType == ItemTrade.CURRENCY.PLAYER_POINT) - { - if (!PlayerpointHook.addPP(player, priceSum)) - return 0; - } + if (!CheckTransactionSuccess(currencyType, r, player, priceSum)) + return 0; //로그 기록 - LogUtil.addLog(shopName, itemStack.getType().toString(), -tradeAmount, priceSum, currencyString, player != null ? player.getName() : shopName); + LogUtil.addLog(shopName, itemStack.getType().toString(), -tradeAmount, priceSum, StringUtil.GetCurrencyString(currencyType), player != null ? player.getName() : shopName); if (player != null) { // 플레이어에게 메시지 출력 - SendSellMessage(currencyType, econ, r, player, tradeAmount, priceSum, itemStack); + SendSellMessage(currencyType, econ, player, tradeAmount, priceSum, itemStack); // 플레이어에게 소리 재생 player.playSound(player.getLocation(), Sound.valueOf("ENTITY_EXPERIENCE_ORB_PICKUP"), 1, 1); @@ -184,6 +156,11 @@ else if (currencyType == ItemTrade.CURRENCY.PLAYER_POINT) { ShopUtil.addShopBalance(shopName, priceSum * -1); } + // 상점 재고 증가 + if (stockOld > 0) + { + data.get().set(tradeIdx + ".stock", MathUtil.SafeAdd(stockOld, tradeAmount)); + } // 커맨드 실행 RunSellCommand(data, player, shopName, itemStack, tradeAmount, priceSum, calcResult[1]); @@ -239,50 +216,16 @@ public static void sell(ItemTrade.CURRENCY currency, Player player, String shopN double[] calcResult = Calc.calcTotalCost(shopName, tradeIdx, -actualAmount); priceSum += calcResult[0]; - // 재고 증가 - if (!infiniteStock) - { - data.get().set(tradeIdx + ".stock", MathUtil.SafeAdd(stockOld, actualAmount)); - } - - Economy econ = null; + Economy econ = DynamicShop.getEconomy(); EconomyResponse r = null; - if (currency == ItemTrade.CURRENCY.VAULT) - { - econ = DynamicShop.getEconomy(); - r = DynamicShop.getEconomy().depositPlayer(player, priceSum); - if (!r.transactionSuccess()) - return; - } - else if (currency == ItemTrade.CURRENCY.JOB_POINT) - { - if (!JobsHook.addJobsPoint(player, priceSum)) - return; - } - else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) - { - if (!PlayerpointHook.addPP(player, priceSum)) - return; - } + if (!CheckTransactionSuccess(currency, r, player, priceSum)) + return; //로그 기록 - String currencyString = ""; - if (currency == ItemTrade.CURRENCY.VAULT) - { - currencyString = "vault"; - } - else if (currency == ItemTrade.CURRENCY.JOB_POINT) - { - currencyString = "jobPoint"; - } - else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) - { - currencyString = "playerPoint"; - } - LogUtil.addLog(shopName, itemStack.getType().toString(), -actualAmount, priceSum, currencyString, player.getName()); + LogUtil.addLog(shopName, itemStack.getType().toString(), -actualAmount, priceSum, StringUtil.GetCurrencyString(currency), player.getName()); // 메시지 출력 - SendSellMessage(currency, econ, r, player, actualAmount, priceSum, itemStack); + SendSellMessage(currency, econ, player, actualAmount, priceSum, itemStack); // 플레이어에게 소리 재생 SoundUtil.playerSoundEffect(player, "sell"); @@ -292,6 +235,11 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) { ShopUtil.addShopBalance(shopName, priceSum * -1); } + // 상점 재고 증가 + if (!infiniteStock) + { + data.get().set(tradeIdx + ".stock", MathUtil.SafeAdd(stockOld, actualAmount)); + } // 커맨드 실행 RunSellCommand(data, player, shopName, itemStack, actualAmount, priceSum, calcResult[1]); @@ -304,16 +252,37 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) Bukkit.getPluginManager().callEvent(event); } - private static void SendSellMessage(ItemTrade.CURRENCY currency, Economy econ, EconomyResponse r, Player player, int actualAmount, double priceSum, ItemStack tempIS) + private static boolean CheckTransactionSuccess(ItemTrade.CURRENCY currencyType, EconomyResponse r, Player player, double priceSum) { - boolean itemHasCustomName = tempIS.getItemMeta() != null && tempIS.getItemMeta().hasDisplayName(); + if (currencyType == ItemTrade.CURRENCY.VAULT) + { + if (player != null) + r = DynamicShop.getEconomy().depositPlayer(player, priceSum); + + return r == null || r.transactionSuccess(); + } + else if (currencyType == ItemTrade.CURRENCY.JOB_POINT) + { + return JobsHook.addJobsPoint(player, priceSum); + } + else if (currencyType == ItemTrade.CURRENCY.PLAYER_POINT) + { + return PlayerpointHook.addPP(player, priceSum); + } + + return false; + } + + private static void SendSellMessage(ItemTrade.CURRENCY currency, Economy econ, Player player, int actualAmount, double priceSum, ItemStack itemStack) + { + boolean itemHasCustomName = itemStack.getItemMeta() != null && itemStack.getItemMeta().hasDisplayName(); boolean useLocalizedName = !itemHasCustomName && ConfigUtil.GetLocalizedItemName(); String message = ""; if (currency == ItemTrade.CURRENCY.VAULT) { message = DynamicShop.dsPrefix(player) + t(player, "MESSAGE.SELL_SUCCESS", !useLocalizedName) .replace("{amount}", Integer.toString(actualAmount)) - .replace("{price}", n(r.amount)) + .replace("{price}", n(priceSum)) .replace("{bal}", n(econ.getBalance(player))); } else if (currency == ItemTrade.CURRENCY.JOB_POINT) @@ -334,11 +303,11 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) if (useLocalizedName) { message = message.replace("{item}", ""); - LangUtil.sendMessageWithLocalizedItemName(player, message, tempIS.getType()); + LangUtil.sendMessageWithLocalizedItemName(player, message, itemStack.getType()); } else { - String itemNameFinal = itemHasCustomName ? tempIS.getItemMeta().getDisplayName() : ItemsUtil.getBeautifiedName(tempIS.getType()); + String itemNameFinal = itemHasCustomName ? itemStack.getItemMeta().getDisplayName() : ItemsUtil.getBeautifiedName(itemStack.getType()); message = message.replace("{item}", itemNameFinal); player.sendMessage(message); } diff --git a/src/main/java/me/sat7/dynamicshop/utilities/StringUtil.java b/src/main/java/me/sat7/dynamicshop/utilities/StringUtil.java index ec4576e..ad35713 100644 --- a/src/main/java/me/sat7/dynamicshop/utilities/StringUtil.java +++ b/src/main/java/me/sat7/dynamicshop/utilities/StringUtil.java @@ -1,5 +1,6 @@ package me.sat7.dynamicshop.utilities; +import me.sat7.dynamicshop.guis.ItemTrade; import org.bukkit.ChatColor; import java.util.Arrays; @@ -145,4 +146,18 @@ public static String getShortenedNameSign(String itemName) { } return String.join("", itemParts); } + + public static String GetCurrencyString (ItemTrade.CURRENCY currency) + { + if (currency == ItemTrade.CURRENCY.JOB_POINT) + { + return "jobPoint"; + } + else if (currency == ItemTrade.CURRENCY.PLAYER_POINT) + { + return "playerPoint"; + } + + return "vault"; + } }