Skip to content

Commit

Permalink
transaction refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
7sat committed Mar 4, 2023
1 parent 1baee42 commit 15a595f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 98 deletions.
7 changes: 6 additions & 1 deletion src/main/java/me/sat7/dynamicshop/guis/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/me/sat7/dynamicshop/transactions/Buy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)
Expand All @@ -225,11 +212,11 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT)
if (useLocalizedName)
{
message = message.replace("{item}", "<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);
}
Expand Down
123 changes: 46 additions & 77 deletions src/main/java/me/sat7/dynamicshop/transactions/Sell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
Expand Down Expand Up @@ -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");
Expand All @@ -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]);
Expand All @@ -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)
Expand All @@ -334,11 +303,11 @@ else if (currency == ItemTrade.CURRENCY.PLAYER_POINT)
if (useLocalizedName)
{
message = message.replace("{item}", "<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);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/me/sat7/dynamicshop/utilities/StringUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.sat7.dynamicshop.utilities;

import me.sat7.dynamicshop.guis.ItemTrade;
import org.bukkit.ChatColor;

import java.util.Arrays;
Expand Down Expand Up @@ -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";
}
}

0 comments on commit 15a595f

Please sign in to comment.