diff options
author | nmccullagh <narhanael64@gmail.com> | 2024-06-17 21:28:54 +0100 |
---|---|---|
committer | nmccullagh <narhanael64@gmail.com> | 2024-06-26 06:01:41 +0100 |
commit | 3187223676066b0a3b42b662ae10f2dc601a099f (patch) | |
tree | 6854210b2dccc0c31e9011d76e32e57f41aba3d9 /src/main/java/de/hysky/skyblocker/skyblock/item | |
parent | 45fa1f8859a39583d0c1edc6e1e9e13f6b00c179 (diff) | |
download | Skyblocker-3187223676066b0a3b42b662ae10f2dc601a099f.tar.gz Skyblocker-3187223676066b0a3b42b662ae10f2dc601a099f.tar.bz2 Skyblocker-3187223676066b0a3b42b662ae10f2dc601a099f.zip |
recursion hurts my head
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/item')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/adders/CraftPriceTooltip.java | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/adders/CraftPriceTooltip.java b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/adders/CraftPriceTooltip.java index 849505a8..8b06229c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/adders/CraftPriceTooltip.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/adders/CraftPriceTooltip.java @@ -16,8 +16,12 @@ import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Map; +import java.util.HashMap; public class CraftPriceTooltip extends TooltipAdder { + private final Map<String, Double> cachedCraftCosts = new HashMap<>(); + private static final int MAX_RECURSION_DEPTH = 10; + public CraftPriceTooltip(int priority) { super(priority); } @@ -30,7 +34,7 @@ public class CraftPriceTooltip extends TooltipAdder { String internalID = stack.getSkyblockId(); if (neuName == null || internalID == null) return; - if (TooltipInfoType.ONE_DAY_AVERAGE.getData() == null || TooltipInfoType.BAZAAR.getData() == null) { + if (TooltipInfoType.LOWEST_BINS.getData() == null || TooltipInfoType.BAZAAR.getData() == null) { ItemTooltip.nullWarning(); return; } @@ -41,30 +45,51 @@ public class CraftPriceTooltip extends TooltipAdder { List<NEURecipe> neuRecipes = neuItem.getRecipes(); if (neuRecipes.isEmpty()) return; - double totalCraftCost = 0.0; - - for (NEUIngredient input : neuRecipes.getFirst().getAllInputs()) { - String inputItemName = input.getItemId(); - double inputItemCount = input.getAmount(); - - double inputItemCost = getItemCost(inputItemName); - totalCraftCost += inputItemCost * inputItemCount; - } + double totalCraftCost = getItemCost(neuRecipes.getFirst(), 0); if (totalCraftCost == 0) return; lines.add(Text.literal(String.format("%-18s", "Crafting Cost:")) .formatted(Formatting.GOLD) - .append(ItemTooltip.getCoinsMessage(totalCraftCost, 1))); + .append(ItemTooltip.getCoinsMessage(totalCraftCost, stack.getCount()))); } - private double getItemCost(String itemName) { - if (TooltipInfoType.BAZAAR.getData().has(itemName)) { - return TooltipInfoType.BAZAAR.getData().getAsJsonObject(itemName).get("sellPrice").getAsDouble(); - } else if (TooltipInfoType.LOWEST_BINS.getData().has(itemName)) { - return TooltipInfoType.LOWEST_BINS.getData().get(itemName).getAsDouble(); - } else { - return 0.0; // No data available + private double getItemCost(NEURecipe recipe, int depth) { + if (depth >= MAX_RECURSION_DEPTH) return -1; + + double totalCraftCost = 0; + for (NEUIngredient input : recipe.getAllInputs()) { + String inputItemName = input.getItemId(); + double inputItemCount = input.getAmount(); + if (cachedCraftCosts.containsKey(inputItemName)) { + totalCraftCost += cachedCraftCosts.get(inputItemName) * inputItemCount; + continue; + } + + double itemCost = 0; + + if (TooltipInfoType.BAZAAR.getData().has(inputItemName)) { + itemCost = TooltipInfoType.BAZAAR.getData().getAsJsonObject(inputItemName).get("sellPrice").getAsDouble(); + } else if (TooltipInfoType.LOWEST_BINS.getData().has(inputItemName)) { + itemCost = TooltipInfoType.LOWEST_BINS.getData().get(inputItemName).getAsDouble(); + } + + if (itemCost > 0) { + cachedCraftCosts.put(inputItemName, itemCost); + } + + NEUItem neuItem = NEURepoManager.NEU_REPO.getItems().getItemBySkyblockId(inputItemName); + if (neuItem != null) { + List<NEURecipe> neuRecipes = neuItem.getRecipes(); + if (!neuRecipes.isEmpty()) { + double craftCost = getItemCost(neuRecipes.getFirst(), depth + 1); + if (craftCost != -1) itemCost = Math.min(itemCost, craftCost); + cachedCraftCosts.put(inputItemName, itemCost); + } + } + + totalCraftCost += itemCost * inputItemCount; } + return totalCraftCost; } } |