diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-09-30 11:50:19 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-09-30 11:50:19 +0200 |
commit | 6a353ffab19c25eed64d36eee32be5bfa1fd4aa9 (patch) | |
tree | d841592fce26eaa9f97ca4ce8a670d777ece5c01 /src/main/java | |
parent | 044e8574a0f32cb739f94eb2abaf92782c860305 (diff) | |
download | skyhanni-6a353ffab19c25eed64d36eee32be5bfa1fd4aa9.tar.gz skyhanni-6a353ffab19c25eed64d36eee32be5bfa1fd4aa9.tar.bz2 skyhanni-6a353ffab19c25eed64d36eee32be5bfa1fd4aa9.zip |
made visitor rewards use internal name logic of GardenVisitorFeatures, fetching the item rewards off the item lore at opening the inventory once, and caching the item lore for performance.
Diffstat (limited to 'src/main/java')
4 files changed, 101 insertions, 65 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/events/VisitorAcceptEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/VisitorAcceptEvent.kt new file mode 100644 index 000000000..620694a22 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/VisitorAcceptEvent.kt @@ -0,0 +1,5 @@ +package at.hannibal2.skyhanni.events + +import at.hannibal2.skyhanni.features.garden.visitor.GardenVisitorFeatures + +class VisitorAcceptEvent(val visitor: GardenVisitorFeatures.Visitor) : LorenzEvent()
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorDropStatistics.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorDropStatistics.kt index fa828003d..0cb9588e3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorDropStatistics.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorDropStatistics.kt @@ -7,11 +7,12 @@ import at.hannibal2.skyhanni.events.ConfigLoadEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzChatEvent import at.hannibal2.skyhanni.events.PreProfileSwitchEvent +import at.hannibal2.skyhanni.events.VisitorAcceptEvent import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.test.command.CopyErrorCommand import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList +import at.hannibal2.skyhanni.utils.LorenzUtils.addOrPut import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy -import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NumberUtil import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber @@ -55,6 +56,18 @@ object GardenVisitorDropStatistics { } @SubscribeEvent + fun onVisitorAccept(event: VisitorAcceptEvent) { + if (!GardenAPI.onBarnPlot) return + if (!ProfileStorageData.loaded) return + + for (internalName in event.visitor.allRewards) { + val reward = VisitorReward.getByInternalName(internalName) ?: continue + rewardsCount = rewardsCount.editCopy { addOrPut(reward, 1) } + saveAndUpdate() + } + } + + @SubscribeEvent fun onChat(event: LorenzChatEvent) { if (!GardenAPI.onBarnPlot) return if (!ProfileStorageData.loaded) return @@ -97,14 +110,6 @@ object GardenVisitorDropStatistics { setRarities(group("rarity")) saveAndUpdate() } - - for (reward in VisitorReward.entries) { - reward.pattern.matchMatcher(message) { - val old = rewardsCount[reward] ?: 0 - rewardsCount = rewardsCount.editCopy { this[reward] = old + 1 } - saveAndUpdate() - } - } } } @@ -131,7 +136,10 @@ object GardenVisitorDropStatistics { ) } else { addAsSingletonList("§c?") - CopyErrorCommand.logError(RuntimeException("visitorRarities is empty, maybe visitor refusing was the cause?"), "Error rendering visitor drop statistics") + CopyErrorCommand.logError( + RuntimeException("visitorRarities is empty, maybe visitor refusing was the cause?"), + "Error rendering visitor drop statistics" + ) } //3 addAsSingletonList(format(acceptedVisitors, "Accepted", "§2", "")) @@ -150,7 +158,7 @@ object GardenVisitorDropStatistics { for (reward in VisitorReward.entries) { val count = rewardsCount[reward] ?: 0 if (config.displayIcons) {// Icons - val stack = NEUItems.getItemStack(reward.internalName, true) + val stack = reward.itemStack if (config.displayNumbersFirst) add(listOf("§b${count.addSeparators()} ", stack)) else add(listOf(stack, " §b${count.addSeparators()}")) diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt index b618c1937..1b139190e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt @@ -14,6 +14,7 @@ import at.hannibal2.skyhanni.events.PacketEvent import at.hannibal2.skyhanni.events.PreProfileSwitchEvent import at.hannibal2.skyhanni.events.TabListLineRenderEvent import at.hannibal2.skyhanni.events.TabListUpdateEvent +import at.hannibal2.skyhanni.events.VisitorAcceptEvent import at.hannibal2.skyhanni.events.VisitorArrivalEvent import at.hannibal2.skyhanni.events.withAlpha import at.hannibal2.skyhanni.features.bazaar.BazaarApi @@ -84,7 +85,6 @@ class GardenVisitorFeatures { private val logger = LorenzLogger("garden/visitors") private var lastFullPrice = 0.0 - private val offerCache = mutableListOf<String>() companion object { var inVisitorInventory = false @@ -138,16 +138,7 @@ class GardenVisitorFeatures { val internalName = NEUInternalName.fromItemName(itemName) visitor.items[internalName] = amount } - - readReward(offerItem)?.let { reward -> - if (visitor.reward == reward) return@let - visitor.reward = reward - visitor.hasReward()?.let { - if (config.visitorRewardWarning.notifyInChat) { - LorenzUtils.chat("§e[SkyHanni] Found Visitor Reward ${it.displayName}§e!") - } - } - } + readToolTip(visitor, event.inventoryItems[29]) if (visitor.status == VisitorStatus.NEW) { val alreadyReady = event.inventoryItems[29]?.getLore()?.any { it == "§eClick to give!" } == true @@ -340,6 +331,7 @@ class GardenVisitorFeatures { } if (event.slotId == 29 && event.slot.stack?.getLore()?.any { it == "§eClick to give!" } == true) { changeStatus(visitor, VisitorStatus.ACCEPTED, "accepted") + acceptVisitor(visitor) update() GardenVisitorDropStatistics.coinsSpent += round(lastFullPrice).toLong() GardenVisitorDropStatistics.lastAccept = System.currentTimeMillis() @@ -347,6 +339,10 @@ class GardenVisitorFeatures { } } + private fun acceptVisitor(visitor: Visitor) { + VisitorAcceptEvent(visitor).postAndCatch() + } + private fun getVisitor(id: Int) = visitors.map { it.value }.find { it.entityId == id } @SubscribeEvent @@ -391,32 +387,24 @@ class GardenVisitorFeatures { if (!inVisitorInventory) return if (event.itemStack.name != "§aAccept Offer") return - if (offerCache.isEmpty()) { - drawToolTip(event.toolTip) - val temp = event.toolTip.listIterator() - for (line in temp) { - offerCache.add(line) - } - } else { - val iterator = event.toolTip.listIterator() - for (i in iterator) { - iterator.remove() - } - for (line in offerCache) { - iterator.add(line) - } + val visitor = getVisitor(lastClickedNpc) ?: return + + event.toolTip.let { + it.clear() + it.addAll(visitor.lastLore) } } - private fun drawToolTip(list: MutableList<String>) { - + private fun readToolTip(visitor: Visitor, itemStack: ItemStack?) { + println("readToolTip") + val stack = itemStack ?: error("Accept offer item not found for visitor ${visitor.visitorName}") var totalPrice = 0.0 var timeRequired = -1L var readingItemsNeeded = true lastFullPrice = 0.0 + val foundRewards = mutableListOf<NEUInternalName>() - for (line in list) { - val formattedLine = line.substring(4) + for (formattedLine in stack.getLore()) { if (formattedLine.contains("Rewards")) { readingItemsNeeded = false } @@ -429,6 +417,7 @@ class GardenVisitorFeatures { totalPrice += price lastFullPrice += price } else { + foundRewards.add(internalName) totalPrice -= price } } @@ -436,16 +425,28 @@ class GardenVisitorFeatures { totalPrice = 0.0 } - readingItemsNeeded = true - val iterator = list.listIterator() - for (line in iterator) { - val formattedLine = line.substring(4) + if (foundRewards.isNotEmpty()) { + val wasEmpty = visitor.allRewards.isEmpty() + visitor.allRewards = foundRewards + if (wasEmpty) { + visitor.hasReward()?.let { reward -> + if (config.visitorRewardWarning.notifyInChat) { + LorenzUtils.chat("§e[SkyHanni] Found Visitor Reward ${reward.displayName}§e!") + } + } + } + } + readingItemsNeeded = true + val finalList = stack.getLore().toMutableList() + var offset = 0 + for ((i, formattedLine) in finalList.toMutableList().withIndex()) { + val index = i + offset if (config.visitorExperiencePrice) { gardenExperiencePattern.matchMatcher(formattedLine) { val gardenExp = group("amount").replace(",", "").toInt() val pricePerCopper = NumberUtil.format((totalPrice / gardenExp).toInt()) - iterator.set("$formattedLine §7(§6$pricePerCopper §7per)") + finalList.set(index, "$formattedLine §7(§6$pricePerCopper §7per)") } } @@ -458,7 +459,7 @@ class GardenVisitorFeatures { if (config.visitorCopperTime) { copperLine += if (timeRequired != -1L) " §7(§b$timePerCopper §7per)" else " §7(§cno speed data!§7)" } - iterator.set(copperLine) + finalList.set(index, copperLine) } if (formattedLine.contains("Rewards")) { @@ -471,7 +472,7 @@ class GardenVisitorFeatures { if (config.visitorShowPrice) { val format = NumberUtil.format(price) - iterator.set("$formattedLine §7(§6$format§7)") + finalList[index] = "$formattedLine §7(§6$format§7)" } if (!readingItemsNeeded) continue val multiplier = NEUItems.getMultiplier(internalName) @@ -488,9 +489,11 @@ class GardenVisitorFeatures { "in §b$duration" } ?: "§cno speed data!" if (config.visitorExactAmountAndTime) { - iterator.add("§7- $formattedName($formattedSpeed§7)") + finalList.add(index + 1, "§7- $formattedName($formattedSpeed§7)") + offset++ } } + visitor.lastLore = finalList } @SubscribeEvent @@ -507,7 +510,6 @@ class GardenVisitorFeatures { @SubscribeEvent fun onInventoryClose(event: InventoryCloseEvent) { inVisitorInventory = false - offerCache.clear() } @SubscribeEvent @@ -624,7 +626,7 @@ class GardenVisitorFeatures { } if (GardenAPI.inGarden() && config.visitorHideChat && hideVisitorMessage(event.message)) { - event.blockedReason = "garden_visitor_message" + event.blockedReason = "garden_visitor_message" } } @@ -794,14 +796,26 @@ class GardenVisitorFeatures { var nameTagEntityId: Int = -1, var status: VisitorStatus, var inSacks: Boolean = false, - var reward: VisitorReward? = null, val items: MutableMap<NEUInternalName, Int> = mutableMapOf(), ) { + + var allRewards = listOf<NEUInternalName>() + var lastLore = listOf<String>() fun getEntity(): Entity? = Minecraft.getMinecraft().theWorld.getEntityByID(entityId) fun getNameTagEntity(): Entity? = Minecraft.getMinecraft().theWorld.getEntityByID(nameTagEntityId) - fun hasReward() = reward?.let { if (config.visitorRewardWarning.drops.contains(it.ordinal)) it else null } + fun hasReward(): VisitorReward? { + for (internalName in allRewards) { + val reward = VisitorReward.getByInternalName(internalName) ?: continue + + if (config.visitorRewardWarning.drops.contains(reward.ordinal)) { + return reward + } + } + + return null + } } enum class VisitorStatus(val displayName: String, val color: Int) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorReward.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorReward.kt index 81419939b..d21ffebe7 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorReward.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorReward.kt @@ -1,17 +1,26 @@ package at.hannibal2.skyhanni.features.garden.visitor -import java.util.regex.Pattern +import at.hannibal2.skyhanni.utils.ItemUtils.nameWithEnchantment +import at.hannibal2.skyhanni.utils.NEUInternalName +import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName +import at.hannibal2.skyhanni.utils.NEUItems.getItemStack -enum class VisitorReward(val displayName: String, val internalName: String, val pattern: Pattern) { - FLOWERING_BOUQUET("§9Flowering Bouquet", "FLOWERING_BOUQUET", "Flowering Bouquet".toPattern()), - OVERGROWN_GRASS("§9Overgrown Grass", "OVERGROWN_GRASS", "Overgrown Grass".toPattern()), - GREEN_BANDANA("§9Green Bandana", "GREEN_BANDANA", "Green Bandana".toPattern()), - DEDICATION("§9Dedication IV", "DEDICATION;4", "Dedication (IV|4) Book".toPattern()), - MUSIC_RUNE("§9Music Rune", "MUSIC_RUNE;1", "◆ Music Rune [1I]".toPattern()), - SPACE_HELMET("§cSpace Helmet", "DCTR_SPACE_HELM", "Space Helmet".toPattern()), - CULTIVATING( - "§9Cultivating I", "CULTIVATING;1", - "Cultivating ([I1]) Book".toPattern() - ), - REPLENISH("§9Replenish I", "REPLENISH;1", "Replenish ([I1]) Book".toPattern()), +enum class VisitorReward(private val rawInternalName: String) { + FLOWERING_BOUQUET("FLOWERING_BOUQUET"), + OVERGROWN_GRASS("OVERGROWN_GRASS"), + GREEN_BANDANA("GREEN_BANDANA"), + DEDICATION("DEDICATION;4"), + MUSIC_RUNE("MUSIC_RUNE;1"), + SPACE_HELMET("DCTR_SPACE_HELM"), + CULTIVATING("CULTIVATING;1"), + REPLENISH("REPLENISH;1"), + ; + + private val internalName by lazy { rawInternalName.asInternalName() } + val itemStack by lazy { internalName.getItemStack() } + val displayName by lazy { itemStack.nameWithEnchantment ?: internalName.toString() } + + companion object { + fun getByInternalName(internalName: NEUInternalName) = entries.firstOrNull { it.internalName == internalName } + } }
\ No newline at end of file |