diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-10 04:41:53 +0100 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-03-10 04:41:53 +0100 |
commit | f234a3db037e36242fcf92463abe947999d420fa (patch) | |
tree | 60243ad2c69be29d91c903cfcb54c89f0fe25fca /src/main/java/at/hannibal2/skyhanni/features | |
parent | 342d03543b9471320ad5c202d0a965aaf5c52eb0 (diff) | |
download | skyhanni-f234a3db037e36242fcf92463abe947999d420fa.tar.gz skyhanni-f234a3db037e36242fcf92463abe947999d420fa.tar.bz2 skyhanni-f234a3db037e36242fcf92463abe947999d420fa.zip |
Added Garden Level Display.
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt new file mode 100644 index 000000000..f40b4edc3 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenLevelDisplay.kt @@ -0,0 +1,146 @@ +package at.hannibal2.skyhanni.features.garden + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.InventoryOpenEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.ProfileJoinEvent +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.NumberUtil.romanToDecimalIfNeeded +import at.hannibal2.skyhanni.utils.RenderUtils.renderString +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.regex.Pattern + +class GardenLevelDisplay { + private val config get() = SkyHanniMod.feature.garden + private val overflowPattern = Pattern.compile("(?:.*) §e(.*)§6\\/(?:.*)") + private val namePattern = Pattern.compile("Garden Level (.*)") + private var gardenExp + get() = SkyHanniMod.feature.hidden.gardenExp + set(value) { + SkyHanniMod.feature.hidden.gardenExp = value + } + private var display = "" + private var visitorRewardPattern = Pattern.compile(" {4}§r§8\\+§r§2(.*) §r§7Garden Experience") + + @SubscribeEvent + fun onProfileJoin(event: ProfileJoinEvent) { + update() + } + + @SubscribeEvent(receiveCanceled = true) + fun onChatMessage(event: LorenzChatEvent) { + if (!isEnabled()) return + + val matcher = visitorRewardPattern.matcher(event.message) + if (matcher.matches()) { + val moreExp = matcher.group(1).toInt() + gardenExp += moreExp + update() + } + } + + @SubscribeEvent + fun onInventoryOpen(event: InventoryOpenEvent) { + if (!isEnabled()) return + if (event.inventoryName != "Desk") return + val item = event.inventoryItems[4]!! + + val name = item.name!!.removeColor() + val nameMatcher = namePattern.matcher(name) + if (!nameMatcher.matches()) return + val currentLevel = nameMatcher.group(1).romanToDecimalIfNeeded() + var overflow = 0 + for (line in item.getLore()) { + val matcher = overflowPattern.matcher(line) + if (matcher.matches()) { + overflow = matcher.group(1).replace(",", "").toInt() + break + } + } + val expForLevel = getExpForLevel(currentLevel).toInt() + gardenExp = expForLevel + overflow + update() + } + + private fun update() { + display = drawDisplay() + } + + private fun drawDisplay(): String { + if (gardenExp == -1) return "§aGarden Level ? §cOpen the desk!" + val currentLevel = getLevelForExp(gardenExp.toLong()) + val needForLevel = getExpForLevel(currentLevel).toInt() + val nextLevel = currentLevel + 1 + val needForNextLevel = getExpForLevel(nextLevel).toInt() + + return "§aGarden Level $currentLevel" + if (needForNextLevel != 0) { + val overflow = gardenExp - needForLevel + val needForOnlyNextLvl = needForNextLevel - needForLevel + + val need = LorenzUtils.formatInteger(overflow) + val have = LorenzUtils.formatInteger(needForOnlyNextLvl) + " §7(§e$need§7/§e$have§7)" + } else "" + } + + private fun getLevelForExp(gardenExp: Long): Int { + var tier = 0 + var totalCrops = 0L + for (tierCrops in gardenExperience) { + totalCrops += tierCrops + if (totalCrops > gardenExp) { + return tier + } + tier++ + } + + return tier + } + + // TODO make table utils method + private fun getExpForLevel(requestedLevel: Int): Long { + var totalCrops = 0L + var tier = 0 + for (tierCrops in gardenExperience) { + totalCrops += tierCrops + tier++ + if (tier == requestedLevel) { + return totalCrops + } + } + + return 0 + } + + @SubscribeEvent + fun onRenderOverlay(event: GuiRenderEvent.GameOverlayRenderEvent) { + if (!isEnabled()) return + + config.gardenLevelPos.renderString(display) + } + + private fun isEnabled() = GardenAPI.inGarden() && config.gardenLevelDisplay + + // TODO use repo + private val gardenExperience = listOf( + 0, + 70, + 100, + 140, + 240, + 600, + 1500, + 2000, + 2500, + 3000, + 10000, + 10000, + 10000, + 10000, + 10000, + ) +}
\ No newline at end of file |