Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Visitor's Logbook stats #1287

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ import at.hannibal2.skyhanni.features.garden.fortuneguide.CaptureFarmingGear
import at.hannibal2.skyhanni.features.garden.inventory.AnitaExtraFarmingFortune
import at.hannibal2.skyhanni.features.garden.inventory.GardenCropMilestoneInventory
import at.hannibal2.skyhanni.features.garden.inventory.GardenInventoryNumbers
import at.hannibal2.skyhanni.features.garden.inventory.LogBookStats
import at.hannibal2.skyhanni.features.garden.inventory.SkyMartCopperPrice
import at.hannibal2.skyhanni.features.garden.inventory.plots.GardenNextPlotPrice
import at.hannibal2.skyhanni.features.garden.inventory.plots.GardenPlotIcon
Expand Down Expand Up @@ -709,6 +710,8 @@ class SkyHanniMod {
loadModule(ServerRestartTitle())
loadModule(CityProjectFeatures())
loadModule(GardenPlotIcon)
loadModule(GardenPlotBorders)
loadModule(LogBookStats())
loadModule(PocketSackInASackDisplay())
loadModule(ShowFishingItemName())
loadModule(WarpTabComplete)
Expand Down Expand Up @@ -766,7 +769,6 @@ class SkyHanniMod {
loadModule(AccountUpgradeReminder())
loadModule(PetExpTooltip())
loadModule(Translator())
loadModule(GardenPlotBorders)
loadModule(CosmeticFollowingLine())
loadModule(SuperpairsClicksAlert())
loadModule(PowderTracker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,13 @@ public class GardenConfig {
@ConfigEditorBoolean
@FeatureToggle
public boolean copyMilestoneData = true;

@Expose
@ConfigOption(name = "Log Book Stats", desc = "Show total visited/accepted/denied visitors stats.")
@ConfigEditorBoolean
@FeatureToggle
public boolean showLogBookStats = true;

@Expose
public Position logBookStatsPos = new Position(427, 92, false, true);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package at.hannibal2.skyhanni.features.garden.inventory

import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.features.garden.visitor.VisitorAPI
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.init.Items
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class LogBookStats {

private val groupPattern = RepoPattern.group("garden.inventory.logbook")
private val visitedPattern by groupPattern.pattern(
"visited",
"§7Times Visited: §a(?<timesVisited>[0-9,.]+)"
)
private val acceptedPattern by groupPattern.pattern(
"accepted",
"§7Offers Accepted: §a(?<timesAccepted>[0-9,.]+)"
)
private val pagePattern by groupPattern.pattern(
"page.current",
"§ePage (?<page>\\d)"
)

private val config get() = GardenAPI.config
private var display = emptyList<Renderable>()
private val loggedVisitors = mutableMapOf<Int, List<VisitorInfo>>()
private var inInventory = false
private var currentPage = 0

@SubscribeEvent
fun onInventoryOpen(event: InventoryFullyOpenedEvent) {
val inventoryName = event.inventoryName
if (inventoryName != "Visitor's Logbook") return

inInventory = true
checkPages(event)
val list = mutableListOf<VisitorInfo>()

for ((index, item) in event.inventoryItems) {
val visitorName = item.displayName ?: continue
var timesVisited = 0L
var timesAccepted = 0L
val lore = item.getLore()
lore.matchFirst(visitedPattern) {
timesVisited += group("timesVisited").formatLong()
}
lore.matchFirst(acceptedPattern) {
timesAccepted += group("timesAccepted").formatLong()
}

val visitor = VisitorInfo(index, visitorName, timesVisited, timesAccepted)
list.add(visitor)
}
loggedVisitors[currentPage] = list
display = buildList {
val visited = loggedVisitors.values.sumOf { it.sumOf { visitor -> visitor.timesVisited } }
val accepted = loggedVisitors.values.sumOf { it.sumOf { visitor -> visitor.timesAccepted } }
val visitingNow = VisitorAPI.getVisitors().size
val denied = visited - accepted - visitingNow
add(Renderable.string("§6Times Visited: §b${visited.addSeparators()}"))
add(Renderable.string("§6Times Accepted: §a${accepted.addSeparators()}"))
add(Renderable.string("§6Times Denied: §c${denied.addSeparators()}"))
}
}

@SubscribeEvent
fun onBackgroundDraw(event: GuiRenderEvent.ChestGuiOverlayRenderEvent) {
if (inInventory && config.showLogBookStats) {
config.logBookStatsPos.renderRenderables(
display,
extraSpace = 5,
posLabel = "Visitor's LogBook Stats"
)
}
}

@SubscribeEvent
fun onProfileChange(event: ProfileJoinEvent) {
display = emptyList()
loggedVisitors.clear()
currentPage = 0
inInventory = false
}

@SubscribeEvent
fun onInventoryClosed(event: InventoryCloseEvent) {
inInventory = false
}

private fun checkPages(event: InventoryFullyOpenedEvent) {
val next = event.inventoryItems[53]
if (next?.item != Items.arrow) {
currentPage++
return
}
for (item in event.inventoryItems.values) {
if (item.displayName != "§aNext Page") continue
item.getLore().matchFirst(pagePattern) {
currentPage = group("page").toInt() - 1
}
}
}

data class VisitorInfo(
var index: Int = -1,
var displayName: String = "",
var timesVisited: Long = 0,
var timesAccepted: Long = 0,
)
}
Loading