diff options
Diffstat (limited to 'src/main/java/at')
3 files changed, 86 insertions, 20 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateAmount.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateAmount.kt index e52fcdacb..45fbd65de 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateAmount.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateAmount.kt @@ -68,6 +68,13 @@ enum class ChocolateAmount(val chocolate: () -> Long) { } } + fun addToCurrent(amount: Long) { + profileStorage?.let { + it.currentChocolate += amount + updateBestUpgrade() + } + } + private fun updateBestUpgrade() { profileStorage?.let { if (it.bestUpgradeAvailableAt == 0L || it.bestUpgradeCost == 0L) return diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateShopPrice.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateShopPrice.kt index dc942cb0a..ab55e5fb2 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateShopPrice.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/chocolatefactory/ChocolateShopPrice.kt @@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.features.inventory.chocolatefactory import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.InventoryCloseEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.events.SecondPassedEvent import at.hannibal2.skyhanni.utils.DisplayTableEntry import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName @@ -10,12 +11,17 @@ import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.ItemUtils.itemName import at.hannibal2.skyhanni.utils.ItemUtils.loreCosts import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.groupOrNull +import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUItems.getPrice import at.hannibal2.skyhanni.utils.NEUItems.getPriceOrNull import at.hannibal2.skyhanni.utils.NumberUtil import at.hannibal2.skyhanni.utils.NumberUtil.million import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.matches +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.UtilsPatterns import at.hannibal2.skyhanni.utils.renderables.Renderable import net.minecraft.item.ItemStack import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -24,13 +30,19 @@ object ChocolateShopPrice { private val config get() = ChocolateFactoryAPI.config.chocolateShopPrice private var display = emptyList<Renderable>() + private var products = emptyList<Product>() private val menuNamePattern by ChocolateFactoryAPI.patternGroup.pattern( "shop.title", "Chocolate Shop" ) + private val itemBoughtPattern by ChocolateFactoryAPI.patternGroup.pattern( + "shop.bought", + "§aYou bought §r§.(?<item>[\\w ]+)§r(?:§8 x(?<amount>\\d+)§r)?§a!" + ) var inInventory = false + private var callUpdate = false var inventoryItems = emptyMap<Int, ItemStack>() @SubscribeEvent @@ -43,56 +55,74 @@ object ChocolateShopPrice { @SubscribeEvent fun onInventoryOpen(event: InventoryFullyOpenedEvent) { if (!isEnabled()) return - if (!menuNamePattern.matches(event.inventoryName)) return + val isInShop = menuNamePattern.matches(event.inventoryName) + val isInShopOptions = UtilsPatterns.shopOptionsPattern.matches(event.inventoryName) + + if (!isInShop && !isInShopOptions) return + if (event.inventoryItems[48]?.getLore()?.first() != "§7To Chocolate Shop" && isInShopOptions) return inInventory = true + callUpdate = isInShop + inventoryItems = event.inventoryItems + if (!callUpdate) { + products.forEach { it.slot = null } + } update() } - private fun update() { - val multiplier = 1.million - // TODO merge core with SkyMartCopperPrice into a utils - val table = mutableListOf<DisplayTableEntry>() - val inventoryItems = inventoryItems + private fun updateProducts() { + val newProducts = mutableListOf<Product>() for ((slot, item) in inventoryItems) { - val lore = item.getLore() - val otherItemsPrice = item.loreCosts().sumOf { it.getPrice() }.takeIf { it != 0.0 } + val lore = item.getLore() val chocolate = ChocolateFactoryAPI.getChocolateBuyCost(lore) ?: continue val internalName = item.getInternalName() val itemPrice = internalName.getPriceOrNull() ?: continue - val profit = itemPrice - (otherItemsPrice ?: 0.0) + val otherItemsPrice = item.loreCosts().sumOf { it.getPrice() }.takeIf { it != 0.0 } + + newProducts.add(Product(slot, item.itemName, internalName, chocolate, itemPrice, otherItemsPrice)) + } + products = newProducts + } - val factor = (profit / chocolate) * multiplier + private fun update() { + if (callUpdate) updateProducts() + + val multiplier = 1.million + // TODO merge core with SkyMartCopperPrice into a utils + val table = mutableListOf<DisplayTableEntry>() + + for (product in products) { + + val profit = product.itemPrice - (product.otherItemPrice ?: 0.0) + val factor = (profit / product.chocolate) * multiplier val perFormat = NumberUtil.format(factor) - val itemName = item.itemName val hover = buildList { - add(itemName) + add(product.name) add("") - add("§7Item price: §6${NumberUtil.format(itemPrice)} ") - otherItemsPrice?.let { + add("§7Item price: §6${NumberUtil.format(product.itemPrice)} ") + product.otherItemPrice?.let { add("§7Additional cost: §6${NumberUtil.format(it)} ") } add("§7Profit per purchase: §6${NumberUtil.format(profit)} ") add("") - add("§7Chocolate amount: §c${NumberUtil.format(chocolate)} ") + add("§7Chocolate amount: §c${NumberUtil.format(product.chocolate)} ") add("§7Profit per million chocolate: §6${perFormat} ") add("") - val formattedTimeUntilGoal = ChocolateAmount.CURRENT.formattedTimeUntilGoal(chocolate) + val formattedTimeUntilGoal = ChocolateAmount.CURRENT.formattedTimeUntilGoal(product.chocolate) add("§7Time until affordable: §6$formattedTimeUntilGoal ") - } table.add( DisplayTableEntry( - "$itemName§f:", + "${product.name}§f:", "§6§l$perFormat", factor, - internalName, + product.item, hover, - highlightsOnHoverSlots = listOf(slot) + highlightsOnHoverSlots = product.slot?.let { listOf(it) } ?: emptyList() ) ) } @@ -109,6 +139,7 @@ object ChocolateShopPrice { @SubscribeEvent fun onInventoryClose(event: InventoryCloseEvent) { inInventory = false + callUpdate = false } @SubscribeEvent @@ -122,5 +153,28 @@ object ChocolateShopPrice { } } + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!LorenzUtils.inSkyBlock) return + if (!inInventory) return + itemBoughtPattern.matchMatcher(event.message) { + val item = group("item") + val amount = groupOrNull("amount")?.toIntOrNull() ?: 1 + val product = products.find { it.name.removeColor() == item } ?: return + + ChocolateAmount.addToCurrent(product.chocolate * -amount) + } + + } + private fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled + + private data class Product( + var slot: Int?, + val name: String, + val item: NEUInternalName, + val chocolate: Long, + val itemPrice: Double, + val otherItemPrice: Double?, + ) } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt b/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt index fbe2b90ab..075bf9502 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/UtilsPatterns.kt @@ -96,4 +96,9 @@ object UtilsPatterns { "tablist.profile", "(?:§.)+Profile: §r§a(?<profile>[\\w\\s]+[^ §]).*" ) + + val shopOptionsPattern by patternGroup.pattern( + "inventory.shopoptions", + "Shop Trading Options" + ) } |