diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-29 19:37:52 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-29 19:37:52 +0200 |
commit | 0245386a8301cc5f45f4a4c8be8c66786016080e (patch) | |
tree | 204294698ea7ca889b1d116a4306899f3a3fee8f /src/main/java/at/hannibal2/skyhanni/features/garden | |
parent | 84f177d3911119df066d0b89e67fdb70c503f7ee (diff) | |
download | skyhanni-0245386a8301cc5f45f4a4c8be8c66786016080e.tar.gz skyhanni-0245386a8301cc5f45f4a4c8be8c66786016080e.tar.bz2 skyhanni-0245386a8301cc5f45f4a4c8be8c66786016080e.zip |
Added Anita Medal Profit - Helps to identify profitable items to buy at the Anita item shop and potential profit from selling the item at the auction house.
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/garden')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/AnitaMedalProfit.kt | 141 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextPlotPrice.kt | 7 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/GardenVisitorFeatures.kt | 7 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/SkyMartCopperPrice.kt (renamed from src/main/java/at/hannibal2/skyhanni/features/garden/SkyMartBestProfit.kt) | 65 |
4 files changed, 171 insertions, 49 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/AnitaMedalProfit.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/AnitaMedalProfit.kt new file mode 100644 index 000000000..fa6500bcb --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/AnitaMedalProfit.kt @@ -0,0 +1,141 @@ +package at.hannibal2.skyhanni.features.garden + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.InventoryCloseEvent +import at.hannibal2.skyhanni.events.InventoryOpenEvent +import at.hannibal2.skyhanni.utils.ItemUtils +import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName +import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList +import at.hannibal2.skyhanni.utils.NEUItems +import at.hannibal2.skyhanni.utils.NumberUtil +import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class AnitaMedalProfit { + private val config get() = SkyHanniMod.feature.garden + private var display = listOf<List<Any>>() + + companion object { + var inInventory = false + } + + enum class MedalType(val displayName: String, val factorBronze: Int) { + GOLD("§6Gold medal", 8), + SILVER("§fSilver medal", 2), + BRONZE("§cBronze medal", 1), + ; + } + + private fun getMedal(name: String) = MedalType.values().firstOrNull { it.displayName == name } + + @SubscribeEvent + fun onInventoryClose(event: InventoryCloseEvent) { + inInventory = false + } + + @SubscribeEvent + fun onInventoryOpen(event: InventoryOpenEvent) { + if (!config.anitaMedalProfitEnabled) return + if (event.inventoryName != "Anita") return + + inInventory = true + + val table = mutableMapOf<Pair<String, String>, Pair<Double, String>>() + for ((_, item) in event.inventoryItems) { + readItem(item, table) + } + + val newList = mutableListOf<List<Any>>() + newList.addAsSingletonList("§eMedal Profit") + LorenzUtils.fillTable(newList, table) + display = newList + } + + private fun readItem(item: ItemStack, table: MutableMap<Pair<String, String>, Pair<Double, String>>) { + var itemName = item.name ?: return + if (itemName == " ") return + if (itemName.endsWith("Enchanted Book")) { + itemName = item.getLore()[0] + } + + val fullCost = getFullCost(getRequiredItems(item)) + if (fullCost < 0) return + + val (name, amount) = ItemUtils.readItemAmount(itemName) + if (name == null) return + + val internalName = try { + NEUItems.getInternalName(name) + } catch (e: Exception) { + // TODO make a better alternative + item.getInternalName() + } + + val itemPrice = NEUItems.getPrice(internalName) * amount + if (itemPrice < 0) return + + val profit = itemPrice - fullCost + val format = NumberUtil.format(profit) + val color = if (profit > 0) "§6" else "§c" + table[Pair(itemName, "$color$format")] = Pair(profit, internalName) + } + + private fun getFullCost(requiredItems: MutableList<String>): Double { + val jacobTicketPrice = NEUItems.getPrice("JACOBS_TICKET") + var otherItemsPrice = 0.0 + for (rawItemName in requiredItems) { + val (name, amount) = ItemUtils.readItemAmount(rawItemName) + if (name == null) { + LorenzUtils.error("§c[SkyHanni] Could not read item '$rawItemName'") + continue + } + + val medal = getMedal(name) + otherItemsPrice += if (medal != null) { + val bronze = medal.factorBronze * amount + bronze * jacobTicketPrice + } else { + val internalName = NEUItems.getInternalName(name) + NEUItems.getPrice(internalName) * amount + } + } + return otherItemsPrice + } + + private fun getRequiredItems(item: ItemStack): MutableList<String> { + val items = mutableListOf<String>() + var next = false + for (line in item.getLore()) { + if (line == "§7Cost") { + next = true + continue + } + if (next) { + if (line == "") { + next = false + continue + } + + items.add(line.replace("§8 ", " §8")) + } + } + return items + } + + @SubscribeEvent + fun onBackgroundDraw(event: GuiRenderEvent.ChestBackgroundRenderEvent) { + if (inInventory) { + config.anitaMedalProfitPos.renderStringsAndItems( + display, + extraSpace = 5, + itemScale = 1.7, + posLabel = "Anita Medal Profit" + ) + } + } +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextPlotPrice.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextPlotPrice.kt index abea33203..85b61db60 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextPlotPrice.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenNextPlotPrice.kt @@ -1,11 +1,8 @@ package at.hannibal2.skyhanni.features.garden import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.utils.InventoryUtils -import at.hannibal2.skyhanni.utils.ItemUtils +import at.hannibal2.skyhanni.utils.* import at.hannibal2.skyhanni.utils.ItemUtils.name -import at.hannibal2.skyhanni.utils.NEUItems -import at.hannibal2.skyhanni.utils.NumberUtil import net.minecraftforge.event.entity.player.ItemTooltipEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -39,6 +36,8 @@ class GardenNextPlotPrice { val price = lowestBin * amount val format = NumberUtil.format(price) list[i] = list[i] + " §7(§6$format§7)" + } else { + LorenzUtils.error("§c[SkyHanni] Could not read item '$line'") } break } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenVisitorFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenVisitorFeatures.kt index df71986ed..917b40b85 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenVisitorFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenVisitorFeatures.kt @@ -77,7 +77,10 @@ class GardenVisitorFeatures { if (line.isEmpty()) break val (itemName, amount) = ItemUtils.readItemAmount(line) - if (itemName == null) continue + if (itemName == null) { + LorenzUtils.error("§c[SkyHanni] Could not read item '$line'") + continue + } val internalName = NEUItems.getInternalName(itemName) visitor.items[internalName] = amount } @@ -287,6 +290,8 @@ class GardenVisitorFeatures { list.add(i + itemsWithSpeedCounter, " §7- $formatName($formatSpeed§7)") } } + } else { + LorenzUtils.error("§c[SkyHanni] Could not read item '$line'") } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/SkyMartBestProfit.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/SkyMartCopperPrice.kt index a4d1e5bd1..7457224e6 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/SkyMartBestProfit.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/SkyMartCopperPrice.kt @@ -7,32 +7,30 @@ import at.hannibal2.skyhanni.events.InventoryOpenEvent import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList -import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NumberUtil import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems -import at.hannibal2.skyhanni.utils.StringUtils.removeColor -import net.minecraft.client.Minecraft -import net.minecraft.item.ItemStack import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.regex.Pattern -class SkyMartBestProfit { - +class SkyMartCopperPrice { private val pattern = Pattern.compile("§c(.*) Copper") private var display = listOf<List<Any>>() private val config get() = SkyHanniMod.feature.garden + companion object { + var inInventory = false + } + @SubscribeEvent fun onInventoryOpen(event: InventoryOpenEvent) { if (!isEnabled()) return - if (event.inventoryName != "SkyMart") return - val priceMap = mutableMapOf<Pair<String, String>, Double>() - val iconMap = mutableMapOf<String, ItemStack>() - + inInventory = true + val table = mutableMapOf<Pair<String, String>, Pair<Double, String>>() for (stack in event.inventoryItems.values) { for (line in stack.getLore()) { val matcher = pattern.matcher(line) @@ -53,55 +51,34 @@ class SkyMartBestProfit { name = stack.getLore()[0] } - NEUItems.getItemStackOrNull(internalName)?.let { - iconMap[name] = it - } - val advancedStats = if (config.skyMartCopperPriceAdvancedStats) { " §7(§6$priceFormat §7/ §c$amountFormat Copper§7)" } else "" - val pair = Pair(name, "§6§l$perFormat$advancedStats") - priceMap[pair] = factor + val pair = Pair("$name§f:", "§6§l$perFormat$advancedStats") + table[pair] = Pair(factor, internalName) } } - display = drawDisplay(priceMap, iconMap) - } - - private fun drawDisplay( - priceMap: MutableMap<Pair<String, String>, Double>, - iconMap: MutableMap<String, ItemStack>, - ): MutableList<List<Any>> { val newList = mutableListOf<List<Any>>() - - newList.addAsSingletonList("Coins per §cCopper§f:") - newList.addAsSingletonList("") - - val keys = priceMap.sortedDesc().keys - val renderer = Minecraft.getMinecraft().fontRendererObj - val longest = keys.map { it.first }.maxOfOrNull { renderer.getStringWidth(it.removeColor()) } ?: 0 - - for ((name, second) in keys) { - var displayName = "$name§f:" - while (renderer.getStringWidth(displayName.removeColor()) < longest) { - displayName += " " - } - iconMap[name]?.let { - newList.add(listOf(it, "$displayName $second")) - } - } - return newList + newList.addAsSingletonList("§eCoins per Copper§f:") + LorenzUtils.fillTable(newList, table) + display = newList } @SubscribeEvent fun onInventoryClose(event: InventoryCloseEvent) { - display = emptyList() + inInventory = false } @SubscribeEvent fun onBackgroundDraw(event: GuiRenderEvent.ChestBackgroundRenderEvent) { - if (isEnabled()) { - config.skyMartCopperPricePos.renderStringsAndItems(display, posLabel = "Sky Mart Copper Price") + if (inInventory) { + config.skyMartCopperPricePos.renderStringsAndItems( + display, + extraSpace = 5, + itemScale = 1.7, + posLabel = "Sky Mart Copper Price" + ) } } |