Skip to content

Commit 6b618a1

Browse files
Potential optimisation for item sell
1 parent 6617a79 commit 6b618a1

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/command/CommandSellGUI.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.stream.Collectors;
2020

2121
import net.brcdev.shopgui.ShopGuiPlugin;
22+
import net.mackenziemolloy.shopguiplus.sellgui.objects.ShopItemPriceValue;
2223
import net.mackenziemolloy.shopguiplus.sellgui.utility.sirblobman.HexColorUtility;
2324
import org.apache.commons.lang3.text.WordUtils;
2425
import org.bukkit.Bukkit;
@@ -403,7 +404,10 @@ private void setItemDamage(ItemStack item, int damage) {
403404
private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> ignoredSlotSet) {
404405
int minorVersion = VersionUtility.getMinorVersion();
405406
CommentedConfiguration configuration = this.plugin.getConfiguration();
406-
407+
408+
// ItemStack is a stack size of 0, Integer is Price
409+
Map<ItemStack, ShopItemPriceValue> itemStackSellPriceCache = new HashMap<>();
410+
407411
Map<ItemStack, Map<Short, Integer>> soldMap2 = new HashMap<>();
408412
Map<EconomyType, Double> moneyMap = new EnumMap<>(EconomyType.class);
409413

@@ -415,28 +419,35 @@ private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> i
415419
Inventory inventory = event.getInventory();
416420
for (int a = 0; a < inventory.getSize(); a++) {
417421
ItemStack i = inventory.getItem(a);
422+
418423
if (i == null) continue;
419424

420425
if(ignoredSlotSet.contains(a)) {
421426
continue;
422427
}
423-
428+
429+
ItemStack singleItem = new ItemStack(i);
430+
singleItem.setAmount(1);
431+
424432
itemsPlacedInGui = true;
425-
if (ShopHandler.getItemSellPrice(i, player) > 0) {
433+
434+
if (itemStackSellPriceCache.getOrDefault(singleItem, new ShopItemPriceValue(null, 0.0)).getSellPrice() > 0 || ShopHandler.getItemSellPrice(i, player) > 0) {
426435
itemAmount += i.getAmount();
427436

428437
@Deprecated
429438
short materialDamage = i.getDurability();
430439
int amount = i.getAmount();
431440

432-
double itemSellPrice = ShopHandler.getItemSellPrice(i, player);
441+
double itemSellPrice = itemStackSellPriceCache.containsKey(singleItem) ? ( itemStackSellPriceCache.get(singleItem).getSellPrice() * amount ) : ShopHandler.getItemSellPrice(i, player);
433442

434443
totalPrice = totalPrice + itemSellPrice;
435444

436445
EconomyType itemEconomyType = ShopHandler.getEconomyType(i);
437446

438447
ItemStack SingleItemStack = new ItemStack(i);
439448
SingleItemStack.setAmount(1);
449+
450+
itemStackSellPriceCache.putIfAbsent(SingleItemStack, new ShopItemPriceValue(itemEconomyType, itemSellPrice/amount));
440451

441452
Map<Short, Integer> totalSold = soldMap2.getOrDefault(SingleItemStack, new HashMap<>());
442453
int totalSoldCount = totalSold.getOrDefault(materialDamage, 0);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.mackenziemolloy.shopguiplus.sellgui.objects;
2+
3+
import net.brcdev.shopgui.economy.EconomyType;
4+
5+
public class ShopItemPriceValue {
6+
7+
private final EconomyType economyType;
8+
private final double sellPrice;
9+
10+
public ShopItemPriceValue(EconomyType economyType, double sellPrice) {
11+
this.economyType = economyType;
12+
this.sellPrice = sellPrice;
13+
}
14+
15+
public EconomyType getEconomyType() {
16+
return economyType;
17+
}
18+
19+
public double getSellPrice() {
20+
return sellPrice;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)