diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/garden')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/GardenPlotAPI.kt | 36 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestFinder.kt | 146 |
2 files changed, 179 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenPlotAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenPlotAPI.kt index 6ab6775e9..0d6905edf 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenPlotAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenPlotAPI.kt @@ -4,7 +4,9 @@ import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LocationUtils.isInside +import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import com.google.gson.annotations.Expose import net.minecraft.util.AxisAlignedBB import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -21,10 +23,37 @@ object GardenPlotAPI { class Plot(val id: Int, var inventorySlot: Int, val box: AxisAlignedBB) - val Plot.name get() = GardenAPI.storage?.plotNames?.get(id) ?: "$id" + class PlotData( + @Expose + val id: Int, + + @Expose + var name: String, + + @Expose + var pests: Int + ) + + private fun Plot.getData() = GardenAPI.storage?.plotData?.getOrPut(id) { PlotData(id, "$id", 0) } + + var Plot.name: String + get() = getData()?.name ?: "$id" + set(value) { + getData()?.name = value + } + + var Plot.pests: Int + get() = getData()?.pests ?: 0 + set(value) { + getData()?.pests = value + } fun Plot.isBarn() = id == -1 + fun Plot.sendTeleportTo() { + LorenzUtils.sendCommandToServer("tptoplot $name") + } + init { val plotMap = listOf( listOf(21, 13, 9, 14, 22), @@ -57,12 +86,13 @@ object GardenPlotAPI { if (!GardenAPI.inGarden()) return if (event.inventoryName != "Configure Plots") return - val names = GardenAPI.storage?.plotNames ?: return for (plot in plots) { val itemName = event.inventoryItems[plot.inventorySlot]?.name ?: continue pestNamePattern.matchMatcher(itemName) { - names[plot.id] = group("name") + plot.name = group("name") } } } + + fun getPlotByName(plotName: String) = plots.firstOrNull { it.name == plotName } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestFinder.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestFinder.kt new file mode 100644 index 000000000..df056bfc9 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestFinder.kt @@ -0,0 +1,146 @@ +package at.hannibal2.skyhanni.features.garden.pests + +import at.hannibal2.skyhanni.events.DamageIndicatorDeathEvent +import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent +import at.hannibal2.skyhanni.events.IslandChangeEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.garden.pests.PestSpawnEvent +import at.hannibal2.skyhanni.features.garden.GardenAPI +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.name +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.pests +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.sendTeleportTo +import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber +import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.StringUtils +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.renderables.Renderable +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class PestFinder { + + private val config get() = PestAPI.config.pestFinder + + private var display = emptyList<Renderable>() + + @SubscribeEvent + fun onPestSpawn(event: PestSpawnEvent) { + if (!isEnabled()) return + PestSpawnTimer.lastSpawnTime = SimpleTimeMark.now() + val plot = GardenPlotAPI.getPlotByName(event.plotName) + if (plot == null) { + LorenzUtils.userError("Open Desk to load plot names and pest locations!") + return + } + plot.pests += event.amountPests + update() + } + + @SubscribeEvent + fun onInventoryOpen(event: InventoryFullyOpenedEvent) { + if (!isEnabled()) return + if (event.inventoryName != "Configure Plots") return + + val pestInventoryPattern = "§4§lൠ §cThis plot has §6(?<amount>\\d) Pests?§c!".toPattern() + + for (plot in GardenPlotAPI.plots) { + plot.pests = 0 + val item = event.inventoryItems[plot.inventorySlot] ?: continue + for (line in item.getLore()) { + pestInventoryPattern.matchMatcher(line) { + plot.pests = group("amount").formatNumber().toInt() + } + } + } + update() + + } + + private fun update() { + display = drawDisplay() + } + + private fun drawDisplay() = buildList { + val totalAmount = totalAmount() + if (totalAmount == 0) { + add(Renderable.string("§cNo pests detected.")) + add(Renderable.string("§7Open §eConfigure Plots Menu")) + add(Renderable.string("§7when incorrect to reload.")) + return@buildList + } + + add(Renderable.string("§eTotal pests on garden: §c${totalAmount()}§7/§c8")) + + for (plot in GardenPlotAPI.plots) { + val pests = plot.pests + if (pests == 0) continue + + val name = plot.name + val pestsName = StringUtils.optionalPlural(pests, "pest", "pests") + val renderable = Renderable.clickAndHover( + "§c$pestsName §7in §b$name", + listOf( + "§7Pests Found: §e$pests", + "§7In plot §b$name", + "", + "§eClick here to warp!" + ), + onClick = { + plot.sendTeleportTo() + } + ) + add(renderable) + } + } + + @SubscribeEvent + fun onIslandChange(event: IslandChangeEvent) { + update() + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!isEnabled()) return + if (event.message == "§cThere are not any Pests on your Garden right now! Keep farming!") { + GardenPlotAPI.plots.forEach { + it.pests = 0 + } + update() + } + } + + @SubscribeEvent + fun onDamageIndicatorDeath(event: DamageIndicatorDeathEvent) { + if (!isEnabled()) return + + // Check if an unknown damage indiactor mob dies in garden + val type = event.data.bossType + if (!PestType.entries.any { it.damageIndicatorBoss == type }) return + + val plot = GardenPlotAPI.getCurrentPlot()?.takeIf { it.pests > 0 } ?: run { + LorenzUtils.userError("Could not detect the plot of the killed pest. Please Open the Configure Plots menu again.") + return + } + + plot.pests-- + update() + } + + private fun totalAmount() = GardenPlotAPI.plots.sumOf { it.pests } + + @SubscribeEvent + fun onRenderOverlay(event: GuiRenderEvent) { + if (!isEnabled()) return + if (config.onlyWithVacuum && !PestAPI.hasVacuumInHand()) return + + if (GardenAPI.inGarden() && config.showDisplay) { + config.position.renderRenderables(display, posLabel = "Pest Finder") + } + } + + fun isEnabled() = GardenAPI.inGarden() && config.showDisplay +} |