diff options
author | Lorenz <lo.scherf@gmail.com> | 2022-07-30 22:15:45 +0200 |
---|---|---|
committer | Lorenz <lo.scherf@gmail.com> | 2022-07-30 22:15:45 +0200 |
commit | ec6056f6c69349c6a298bd999db0b88edc79ccab (patch) | |
tree | a14f580006eb043804d665c9f20557db5a98357c /src/main/java/io | |
parent | 00186ba42516ddfdceeadf7bbc01690507df63a8 (diff) | |
download | NotEnoughUpdates-ec6056f6c69349c6a298bd999db0b88edc79ccab.tar.gz NotEnoughUpdates-ec6056f6c69349c6a298bd999db0b88edc79ccab.tar.bz2 NotEnoughUpdates-ec6056f6c69349c6a298bd999db0b88edc79ccab.zip |
moved price calculation logic into single class
Diffstat (limited to 'src/main/java/io')
4 files changed, 180 insertions, 133 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperManager.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperManager.java index e0edef17..72987d54 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperManager.java @@ -19,8 +19,6 @@ package io.github.moulberry.notenoughupdates.miscgui.minionhelper; -import com.google.common.collect.ArrayListMultimap; -import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.loaders.MinionHelperApiLoader; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.loaders.MinionHelperRepoLoader; @@ -29,19 +27,13 @@ import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.Cu import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.MinionRequirement; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.ReputationRequirement; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.SlayerRequirement; -import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.CraftingSource; -import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.CustomSource; -import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.MinionSource; -import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.NpcSource; import io.github.moulberry.notenoughupdates.util.Utils; import net.minecraft.client.Minecraft; import net.minecraft.inventory.Container; import net.minecraft.inventory.ContainerChest; -import net.minecraftforge.client.event.GuiOpenEvent; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.common.MinecraftForge; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -50,20 +42,10 @@ import java.util.Map; public class MinionHelperManager { private static MinionHelperManager instance = null; private final Map<String, Minion> minions = new HashMap<>(); - private final Map<String, String> upgradeCostFormatCache = new HashMap<>(); - private final Map<String, String> fullCostFormatCache = new HashMap<>(); private ApiData apiData = null; private boolean shouldNotifyNoCollectionApi = false; - //TODO maybe change logic with 0 coins later or stuff - private final List<String> cheapItems = Arrays.asList( - "WOOD_SWORD", - "WOOD_HOE", - "WOOD_AXE", - "WOOD_PICKAXE", - "WOOD_SPADE", - "FISHING_ROD", - "SKYBLOCK_PELT" - ); + + private final MinionHelperPriceCalculation priceCalculation = new MinionHelperPriceCalculation(this); public static MinionHelperManager getInstance() { if (instance == null) { @@ -72,10 +54,8 @@ public class MinionHelperManager { return instance; } - @SubscribeEvent - public void onGuiOpen(GuiOpenEvent event) { - upgradeCostFormatCache.clear(); - fullCostFormatCache.clear(); + private MinionHelperManager() { + MinecraftForge.EVENT_BUS.register(priceCalculation); } public boolean inCraftedMinionsInventory() { @@ -117,98 +97,6 @@ public class MinionHelperManager { return null; } - public String calculateUpgradeCostsFormat(MinionSource source, boolean upgradeOnly) { - if (source == null) return "§c?"; - String internalName = source.getMinion().getInternalName(); - if (upgradeOnly) { - if (upgradeCostFormatCache.containsKey(internalName)) { - upgradeCostFormatCache.get(internalName); - } - } else { - if (fullCostFormatCache.containsKey(internalName)) { - fullCostFormatCache.get(internalName); - } - } - - if (source instanceof CustomSource) { - return "§f" + ((CustomSource) source).getCustomSource(); - } - - long costs = calculateUpgradeCosts(source, upgradeOnly); - String result = formatCoins(costs, !upgradeOnly ? "§o" : ""); - - if (source instanceof NpcSource) { - ArrayListMultimap<String, Integer> items = ((NpcSource) source).getItems(); - if (items.containsKey("SKYBLOCK_PELT")) { - int amount = items.get("SKYBLOCK_PELT").get(0); - result += " §7+ §5" + amount + " Pelts"; - } - } - - if (upgradeOnly) { - upgradeCostFormatCache.put(internalName, result); - } else { - fullCostFormatCache.put(internalName, result); - } - - return result; - } - - public long calculateUpgradeCosts(MinionSource source, boolean upgradeOnly) { - if (source instanceof CraftingSource) { - CraftingSource craftingSource = (CraftingSource) source; - return getCosts(source, upgradeOnly, craftingSource.getItems()); - - } else if (source instanceof NpcSource) { - NpcSource npcSource = (NpcSource) source; - long upgradeCost = getCosts(source, upgradeOnly, npcSource.getItems()); - long coins = npcSource.getCoins(); - upgradeCost += coins; - - return upgradeCost; - } - - return 0; - } - - private long getCosts(MinionSource source, boolean upgradeOnly, ArrayListMultimap<String, Integer> items) { - long upgradeCost = 0; - for (Map.Entry<String, Integer> entry : items.entries()) { - String internalName = entry.getKey(); - long price = getPrice(internalName); - int amount = entry.getValue(); - upgradeCost += price * amount; - } - if (!upgradeOnly) { - Minion parent = source.getMinion().getParent(); - if (parent != null) { - upgradeCost += calculateUpgradeCosts(parent.getMinionSource(), false); - } - } - return upgradeCost; - } - - public long getPrice(String internalName) { - JsonObject bazaarInfo = NotEnoughUpdates.INSTANCE.manager.auctionManager.getBazaarInfo(internalName); - if (bazaarInfo == null) { - if (internalName.contains("_GENERATOR_")) { - return calculateUpgradeCosts(getMinionById(internalName).getMinionSource(), false); - } else { - if (!cheapItems.contains(internalName)) { - return (long) NotEnoughUpdates.INSTANCE.manager.auctionManager.getItemAvgBin(internalName); - } - } - return 0; - } - - //TODO use average bazaar price? - if (!bazaarInfo.has("curr_sell")) { - System.err.println("curr_sell does not exist for '" + internalName + "'"); - return 0; - } - return (long) bazaarInfo.get("curr_sell").getAsDouble(); - } - public void createMinion(String internalName, int tier) { minions.put(internalName, new Minion(internalName, tier)); } @@ -301,15 +189,6 @@ public class MinionHelperManager { } } - public String formatCoins(long coins) { - return formatCoins(coins, ""); - } - - public String formatCoins(long coins, String extraFormat) { - String format = Utils.shortNumberFormat(coins, 0); - return "§6" + extraFormat + format + " coins"; - } - public boolean isCollectionApiDisabled() { return apiData != null && apiData.isCollectionApiDisabled(); } @@ -387,4 +266,8 @@ public class MinionHelperManager { } } } + + public MinionHelperPriceCalculation getPriceCalculation() { + return priceCalculation; + } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperOverlay.java index e455e61c..a9a54d10 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperOverlay.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperOverlay.java @@ -209,7 +209,7 @@ public class MinionHelperOverlay { if (minionSource instanceof CraftingSource) { CraftingSource craftingSource = (CraftingSource) minionSource; lines.add(""); - String format = manager.calculateUpgradeCostsFormat(craftingSource, true); + String format = manager.getPriceCalculation().calculateUpgradeCostsFormat(craftingSource, true); if (minion.getTier() == 1) { lines.add("§7Full crafting costs: " + format); } else { @@ -223,8 +223,8 @@ public class MinionHelperOverlay { lines.add(""); lines.add("§7Buy from: §9" + npcName + " (NPC)"); lines.add(""); - lines.add("§7Buy costs: " + manager.calculateUpgradeCostsFormat(npcSource, true)); - lines.add(" §8- " + manager.formatCoins(npcSource.getCoins())); + lines.add("§7Buy costs: " + manager.getPriceCalculation().calculateUpgradeCostsFormat(npcSource, true)); + lines.add(" §8- " + manager.getPriceCalculation().formatCoins(npcSource.getCoins())); formatItems(lines, grabAllItems(npcSource.getItems())); } @@ -241,7 +241,7 @@ public class MinionHelperOverlay { int amount = entry.getValue(); String amountText = amount != 1 ? amount + "§7x " : ""; - String price = manager.formatCoins(manager.getPrice(internalName) * amount); + String price = manager.getPriceCalculation().formatCoins(manager.getPriceCalculation().getPrice(internalName) * amount); lines.add(" §8- §a" + amountText + "§f" + name + " " + price); } } @@ -263,7 +263,7 @@ public class MinionHelperOverlay { if (!minion.doesMeetRequirements() && !shift) continue; if (!minion.isCrafted()) { - long price = manager.calculateUpgradeCosts(minion.getMinionSource(), true); + long price = manager.getPriceCalculation().calculateUpgradeCosts(minion.getMinionSource(), true); prices.put(minion, price); } } @@ -334,7 +334,7 @@ public class MinionHelperOverlay { continue; } displayName = displayName.replace(" Minion", ""); - String format = manager.calculateUpgradeCostsFormat(minion.getMinionSource(), true); + String format = manager.getPriceCalculation().calculateUpgradeCostsFormat(minion.getMinionSource(), true); String requirementFormat = !minion.doesMeetRequirements() ? "§7§o" : ""; renderMap.put( requirementFormat + displayName + "§r " + requirementFormat + minion.getTier() + " §r§8- " + format, diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperPriceCalculation.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperPriceCalculation.java new file mode 100644 index 00000000..8505ab0b --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperPriceCalculation.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + +package io.github.moulberry.notenoughupdates.miscgui.minionhelper; + +import com.google.common.collect.ArrayListMultimap; +import com.google.gson.JsonObject; +import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.CraftingSource; +import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.CustomSource; +import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.MinionSource; +import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.NpcSource; +import io.github.moulberry.notenoughupdates.util.Utils; +import net.minecraftforge.client.event.GuiOpenEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MinionHelperPriceCalculation { + + private final MinionHelperManager manager; + private final Map<String, String> upgradeCostFormatCache = new HashMap<>(); + private final Map<String, String> fullCostFormatCache = new HashMap<>(); + //TODO maybe change logic with 0 coins later or stuff + private final List<String> cheapItems = Arrays.asList( + "WOOD_SWORD", + "WOOD_HOE", + "WOOD_AXE", + "WOOD_PICKAXE", + "WOOD_SPADE", + "FISHING_ROD", + "SKYBLOCK_PELT" + ); + + public MinionHelperPriceCalculation(MinionHelperManager manager) { + this.manager = manager; + } + + @SubscribeEvent + public void onGuiOpen(GuiOpenEvent event) { + upgradeCostFormatCache.clear(); + fullCostFormatCache.clear(); + } + + public String calculateUpgradeCostsFormat(MinionSource source, boolean upgradeOnly) { + if (source == null) return "§c?"; + String internalName = source.getMinion().getInternalName(); + if (upgradeOnly) { + if (upgradeCostFormatCache.containsKey(internalName)) { + upgradeCostFormatCache.get(internalName); + } + } else { + if (fullCostFormatCache.containsKey(internalName)) { + fullCostFormatCache.get(internalName); + } + } + + if (source instanceof CustomSource) { + return "§f" + ((CustomSource) source).getCustomSource(); + } + + long costs = calculateUpgradeCosts(source, upgradeOnly); + String result = formatCoins(costs, !upgradeOnly ? "§o" : ""); + + if (source instanceof NpcSource) { + ArrayListMultimap<String, Integer> items = ((NpcSource) source).getItems(); + if (items.containsKey("SKYBLOCK_PELT")) { + int amount = items.get("SKYBLOCK_PELT").get(0); + result += " §7+ §5" + amount + " Pelts"; + } + } + + if (upgradeOnly) { + upgradeCostFormatCache.put(internalName, result); + } else { + fullCostFormatCache.put(internalName, result); + } + + return result; + } + + public long calculateUpgradeCosts(MinionSource source, boolean upgradeOnly) { + if (source instanceof CraftingSource) { + CraftingSource craftingSource = (CraftingSource) source; + return getCosts(source, upgradeOnly, craftingSource.getItems()); + + } else if (source instanceof NpcSource) { + NpcSource npcSource = (NpcSource) source; + long upgradeCost = getCosts(source, upgradeOnly, npcSource.getItems()); + long coins = npcSource.getCoins(); + upgradeCost += coins; + + return upgradeCost; + } + + return 0; + } + + private long getCosts(MinionSource source, boolean upgradeOnly, ArrayListMultimap<String, Integer> items) { + long upgradeCost = 0; + for (Map.Entry<String, Integer> entry : items.entries()) { + String internalName = entry.getKey(); + long price = getPrice(internalName); + int amount = entry.getValue(); + upgradeCost += price * amount; + } + if (!upgradeOnly) { + Minion parent = source.getMinion().getParent(); + if (parent != null) { + upgradeCost += calculateUpgradeCosts(parent.getMinionSource(), false); + } + } + return upgradeCost; + } + + public long getPrice(String internalName) { + JsonObject bazaarInfo = NotEnoughUpdates.INSTANCE.manager.auctionManager.getBazaarInfo(internalName); + if (bazaarInfo == null) { + if (internalName.contains("_GENERATOR_")) { + return calculateUpgradeCosts(manager.getMinionById(internalName).getMinionSource(), false); + } else { + if (!cheapItems.contains(internalName)) { + return (long) NotEnoughUpdates.INSTANCE.manager.auctionManager.getItemAvgBin(internalName); + } + } + return 0; + } + + //TODO use average bazaar price? + if (!bazaarInfo.has("curr_sell")) { + System.err.println("curr_sell does not exist for '" + internalName + "'"); + return 0; + } + return (long) bazaarInfo.get("curr_sell").getAsDouble(); + } + + public String formatCoins(long coins) { + return formatCoins(coins, ""); + } + + public String formatCoins(long coins, String extraFormat) { + String format = Utils.shortNumberFormat(coins, 0); + return "§6" + extraFormat + format + " coins"; + } +} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperTooltips.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperTooltips.java index 135a8b64..b92ee768 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperTooltips.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/MinionHelperTooltips.java @@ -79,7 +79,7 @@ public class MinionHelperTooltips { System.err.println("minionSource is null for " + minion.getInternalName()); continue; } - String format = manager.calculateUpgradeCostsFormat(minionSource, !showFullCost); + String format = manager.getPriceCalculation().calculateUpgradeCostsFormat(minionSource, !showFullCost); event.toolTip.set(index, line + " §8- " + format); } |