Skip to content

Commit

Permalink
Allow null for player parameter of Quick sell API.
Browse files Browse the repository at this point in the history
  • Loading branch information
7sat committed Apr 1, 2022
1 parent e171cbd commit 09fd114
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.sat7</groupId>
<artifactId>DynamicShop</artifactId>
<version>3.6.4</version>
<version>3.6.5</version>
<packaging>jar</packaging>

<name>DynamicShop</name>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/sat7/dynamicshop/DynaShopAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ public static String[] FindTheBestShopToSell(Player player, ItemStack itemStack,
return ret;
}

/**
* Quick Sell
*
* @param player seller. This can be null. If null, permission and time are not checked.
* @param itemStack Item to sell)
* @return price sum.
*/
public static double QuickSell(Player player, ItemStack itemStack)
{
String[] ret = ShopUtil.FindTheBestShopToSell(player, itemStack);
Expand Down
137 changes: 80 additions & 57 deletions src/main/java/me/sat7/dynamicshop/transactions/Sell.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,72 @@ public static double quickSellItem(Player player, ItemStack tempIS, String shopN

// 실제 판매 가능량 확인
int tradeAmount;
if (isShiftClick)
if (player != null)
{
int amount = 0;
for (ItemStack item : player.getInventory().getStorageContents())
if (isShiftClick)
{
if (item == null)
continue;

if (item.isSimilar(tempIS))
int amount = 0;
for (ItemStack item : player.getInventory().getStorageContents())
{
if (maxStock == -1)
{
amount += item.getAmount();
player.getInventory().removeItem(item);
} else
if (item == null)
continue;

if (item.isSimilar(tempIS))
{
int tempAmount = Clamp(tempIS.getAmount(), 0, maxStock - stockOld);
int itemLeft = item.getAmount() - tempAmount;
if (itemLeft <= 0)
if (maxStock == -1)
{
amount += item.getAmount();
player.getInventory().removeItem(item);
} else
{
item.setAmount(itemLeft);
int tempAmount = Clamp(tempIS.getAmount(), 0, maxStock - stockOld);
int itemLeft = item.getAmount() - tempAmount;
if (itemLeft <= 0)
{
player.getInventory().removeItem(item);
} else
{
item.setAmount(itemLeft);
}
amount += tempAmount;
}
amount += tempAmount;
}
}

if (maxStock != -1 && amount + stockOld <= maxStock)
break;
}
tradeAmount = amount;
} else
{
if (maxStock == -1)
{
tradeAmount = player.getInventory().getItem(slot).getAmount();
player.getInventory().setItem(slot, null);
if (maxStock != -1 && amount + stockOld <= maxStock)
break;
}
tradeAmount = amount;
} else
{
tradeAmount = Clamp(tempIS.getAmount(), 0, maxStock - stockOld);
int itemAmountOld = player.getInventory().getItem(slot).getAmount();
int itemLeft = itemAmountOld - tradeAmount;

if (itemLeft <= 0)
if (maxStock == -1)
{
tradeAmount = player.getInventory().getItem(slot).getAmount();
player.getInventory().setItem(slot, null);
else
player.getInventory().getItem(slot).setAmount(itemLeft);
} else
{
tradeAmount = Clamp(tempIS.getAmount(), 0, maxStock - stockOld);
int itemAmountOld = player.getInventory().getItem(slot).getAmount();
int itemLeft = itemAmountOld - tradeAmount;

if (itemLeft <= 0)
player.getInventory().setItem(slot, null);
else
player.getInventory().getItem(slot).setAmount(itemLeft);
}
}
player.updateInventory();
}
else
{
tradeAmount = tempIS.getAmount();
}
player.updateInventory();

// 판매할 아이탬이 없음
if (tradeAmount == 0)
{
player.sendMessage(DynamicShop.dsPrefix(player) + t(player, "MESSAGE.NO_ITEM_TO_SELL"));
if(player != null)
player.sendMessage(DynamicShop.dsPrefix(player) + t(player, "MESSAGE.NO_ITEM_TO_SELL"));

return 0;
}

Expand All @@ -110,40 +119,54 @@ public static double quickSellItem(Player player, ItemStack tempIS, String shopN

// 실제 거래부----------
Economy econ = DynamicShop.getEconomy();
EconomyResponse r = DynamicShop.getEconomy().depositPlayer(player, priceSum);
EconomyResponse r = null;
if(player != null)
r = DynamicShop.getEconomy().depositPlayer(player, priceSum);

if (r.transactionSuccess())
if (player == null || r.transactionSuccess())
{
data.save();

//로그 기록
LogUtil.addLog(shopName, tempIS.getType().toString(), -tradeAmount, priceSum, "vault", player.getName());

boolean useLocalizedName = DynamicShop.plugin.getConfig().getBoolean("UI.LocalizedItemName");
String message = DynamicShop.dsPrefix(player) + t(player, "MESSAGE.SELL_SUCCESS", !useLocalizedName)
.replace("{amount}", Integer.toString(tradeAmount))
.replace("{price}", n(r.amount))
.replace("{bal}", n(econ.getBalance((player))));
LogUtil.addLog(shopName, tempIS.getType().toString(), -tradeAmount, priceSum, "vault", player != null ? player.getName() : shopName);

if (useLocalizedName)
{
message = message.replace("{item}", "<item>");
LangUtil.sendMessageWithLocalizedItemName(player, message, tempIS.getType());
} else
if (player != null)
{
message = message.replace("{item}", ItemsUtil.getBeautifiedName(tempIS.getType()));
player.sendMessage(message);
}
boolean useLocalizedName = DynamicShop.plugin.getConfig().getBoolean("UI.LocalizedItemName");
String message = DynamicShop.dsPrefix(player) + t(player, "MESSAGE.SELL_SUCCESS", !useLocalizedName)
.replace("{amount}", Integer.toString(tradeAmount))
.replace("{price}", n(r.amount))
.replace("{bal}", n(econ.getBalance((player))));

if (useLocalizedName)
{
message = message.replace("{item}", "<item>");
LangUtil.sendMessageWithLocalizedItemName(player, message, tempIS.getType());
} else
{
message = message.replace("{item}", ItemsUtil.getBeautifiedName(tempIS.getType()));
player.sendMessage(message);
}

player.playSound(player.getLocation(), Sound.valueOf("ENTITY_EXPERIENCE_ORB_PICKUP"), 1, 1);
player.playSound(player.getLocation(), Sound.valueOf("ENTITY_EXPERIENCE_ORB_PICKUP"), 1, 1);
}

if (data.get().contains("Options.Balance"))
{
ShopUtil.addShopBalance(shopName, priceSum * -1);
}

ShopBuySellEvent event = new ShopBuySellEvent(false, priceBuyOld, Calc.getCurrentPrice(shopName, String.valueOf(tradeIdx), true), priceSellOld, DynaShopAPI.getSellPrice(shopName, tempIS), stockOld, DynaShopAPI.getStock(shopName, tempIS), DynaShopAPI.getMedian(shopName, tempIS), shopName, tempIS, player);
Bukkit.getPluginManager().callEvent(event);
if (player != null)
{
ShopBuySellEvent event = new ShopBuySellEvent(false, priceBuyOld, Calc.getCurrentPrice(shopName, String.valueOf(tradeIdx), true),
priceSellOld,
DynaShopAPI.getSellPrice(shopName, tempIS),
stockOld,
DynaShopAPI.getStock(shopName, tempIS),
DynaShopAPI.getMedian(shopName, tempIS),
shopName, tempIS, player);
Bukkit.getPluginManager().callEvent(event);
}
} else
{
player.sendMessage(String.format("[Vault] An error occured: %s", r.errorMessage));
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/me/sat7/dynamicshop/utilities/ShopUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,13 @@ public static String[] FindTheBestShopToSell(Player player, ItemStack itemStack)
CustomConfig data = entry.getValue();

// 권한 없는 상점
String permission = data.get().getString("Options.permission");
if (permission != null && permission.length() > 0 && !player.hasPermission(permission) && !player.hasPermission(permission + ".sell"))
if(player != null)
{
continue;
String permission = data.get().getString("Options.permission");
if (permission != null && permission.length() > 0 && !player.hasPermission(permission) && !player.hasPermission(permission + ".sell"))
{
continue;
}
}

// 비활성화된 상점
Expand All @@ -557,7 +560,7 @@ public static String[] FindTheBestShopToSell(Player player, ItemStack itemStack)
continue;

// 영업시간 확인
if (!CheckShopHour(entry.getKey(), player))
if (player != null && !CheckShopHour(entry.getKey(), player))
continue;

int sameItemIdx = ShopUtil.findItemFromShop(entry.getKey(), itemStack);
Expand Down

0 comments on commit 09fd114

Please sign in to comment.