summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features
diff options
context:
space:
mode:
authorappable <enzospiacitelli@gmail.com>2023-04-10 17:44:46 -0700
committerGitHub <noreply@github.com>2023-04-11 02:44:46 +0200
commit04cfe48dbbc58da81c8390dfb20216a7258cba3e (patch)
tree09b517607af1cb71b2207981fc13115eb6696d91 /src/main/java/at/hannibal2/skyhanni/features
parent65d8a310f7880d1acfe799dbc0c90cc9cef08610 (diff)
downloadskyhanni-04cfe48dbbc58da81c8390dfb20216a7258cba3e.tar.gz
skyhanni-04cfe48dbbc58da81c8390dfb20216a7258cba3e.tar.bz2
skyhanni-04cfe48dbbc58da81c8390dfb20216a7258cba3e.zip
Farming fortune display (#34)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/CropType.kt28
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/FarmingFortuneDisplay.kt148
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt20
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/ToolTooltipTweaks.kt116
4 files changed, 295 insertions, 17 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/CropType.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/CropType.kt
index d83399c77..d8c05dff0 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/CropType.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/CropType.kt
@@ -1,5 +1,6 @@
package at.hannibal2.skyhanni.features.garden
+import net.minecraft.block.state.IBlockState
import net.minecraft.init.Blocks
import net.minecraft.init.Items
import net.minecraft.item.EnumDyeColor
@@ -33,5 +34,32 @@ enum class CropType(val cropName: String, val toolName: String, iconSupplier: ()
}
return getByName(itemName)
}
+
+ fun getByBlock(blockState: IBlockState): CropType? {
+ return when (blockState.block) {
+ Blocks.wheat -> WHEAT
+ Blocks.carrots -> CARROT
+ Blocks.potatoes -> POTATO
+ Blocks.pumpkin -> PUMPKIN
+ Blocks.reeds -> SUGAR_CANE
+ Blocks.melon_block -> MELON
+ Blocks.cactus -> CACTUS
+ Blocks.cocoa -> COCOA_BEANS
+ Blocks.red_mushroom -> MUSHROOM
+ Blocks.brown_mushroom -> MUSHROOM
+ Blocks.nether_wart -> NETHER_WART
+ else -> null
+ }
+ }
+
+ fun CropType.getTurboCrop(): String {
+ return when (this) {
+ COCOA_BEANS -> "turbo_coco"
+ SUGAR_CANE -> "turbo_cane"
+ NETHER_WART -> "turbo_warts"
+ MUSHROOM -> "turbo_mushrooms"
+ else -> "turbo_${this.cropName.lowercase()}"
+ }
+ }
}
} \ No newline at end of file
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/FarmingFortuneDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/FarmingFortuneDisplay.kt
new file mode 100644
index 000000000..781691546
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/FarmingFortuneDisplay.kt
@@ -0,0 +1,148 @@
+package at.hannibal2.skyhanni.features.garden
+
+import at.hannibal2.skyhanni.SkyHanniMod
+import at.hannibal2.skyhanni.data.GardenCropMilestones
+import at.hannibal2.skyhanni.data.GardenCropMilestones.Companion.getCounter
+import at.hannibal2.skyhanni.data.GardenCropUpgrades.Companion.getUpgradeLevel
+import at.hannibal2.skyhanni.events.*
+import at.hannibal2.skyhanni.features.garden.CropType.Companion.getTurboCrop
+import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
+import at.hannibal2.skyhanni.utils.ItemUtils.getLore
+import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.RenderUtils.renderSingleLineWithItems
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getCounter
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEnchantments
+import net.minecraft.item.ItemStack
+import net.minecraftforge.fml.common.eventhandler.EventPriority
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent
+import kotlin.math.floor
+import kotlin.math.log10
+
+class FarmingFortuneDisplay {
+ private val config get() = SkyHanniMod.feature.garden
+
+ private val tabFortunePattern = " Farming Fortune: §r§6☘(\\d+)".toRegex()
+
+ private var display = listOf<Any>()
+ private var currentCrop: CropType? = null
+
+ private var tabFortune: Double = 0.0
+ private var toolFortune: Double = 0.0
+ private val upgradeFortune: Double? get() = currentCrop?.getUpgradeLevel()?.let { it * 5.0 }
+
+ private var lastToolSwitch: Long = 0
+ private var ticks: Int = 0
+
+ @SubscribeEvent
+ fun onTabListUpdate(event: TabListUpdateEvent) {
+ if (!GardenAPI.inGarden()) return
+ tabFortune = event.tabList.firstNotNullOfOrNull {
+ tabFortunePattern.matchEntire(it)?.groups?.get(1)?.value?.toDoubleOrNull()
+ } ?: tabFortune
+ }
+
+ @SubscribeEvent(priority = EventPriority.LOW)
+ fun onInventoryUpdate(event: OwnInventorItemUpdateEvent) {
+ if (!GardenAPI.inGarden()) return
+ if (GardenAPI.getCropTypeFromItem(event.itemStack) == null) return
+ updateToolFortune(event.itemStack)
+ }
+
+ @SubscribeEvent
+ fun onBlockBreak(event: BlockClickEvent) {
+ if (!GardenAPI.inGarden()) return
+ val cropBroken = CropType.getByBlock(event.getBlockState) ?: return
+ if (cropBroken != currentCrop) {
+ currentCrop = cropBroken
+ updateToolFortune(event.itemInHand)
+ }
+ }
+
+ @SubscribeEvent
+ fun onGardenToolChange(event: GardenToolChangeEvent) {
+ lastToolSwitch = System.currentTimeMillis()
+ val heldTool = event.toolItem
+ currentCrop = event.crop ?: currentCrop
+ updateToolFortune(heldTool)
+ }
+
+ @SubscribeEvent
+ fun onRenderOverlay(event: GuiRenderEvent.GameOverlayRenderEvent) {
+ if (!isEnabled()) return
+ config.farmingFortunePos.renderSingleLineWithItems(display, posLabel = "Farming Fortune")
+ }
+
+ @SubscribeEvent
+ fun onTick(event: TickEvent.ClientTickEvent) {
+ if (event.phase != TickEvent.Phase.START || ticks++ % 5 != 0) return
+ val displayCrop = currentCrop ?: return
+ val updatedDisplay = mutableListOf<Any>()
+ GardenAPI.addGardenCropToList(displayCrop, updatedDisplay)
+ val recentlySwitchedTool = System.currentTimeMillis() < lastToolSwitch + 1000
+ val displayString = upgradeFortune?.let {
+ "§6Farming Fortune§7: §e" + if (!recentlySwitchedTool) {
+ val totalFortune = it + tabFortune + toolFortune
+ LorenzUtils.formatDouble(totalFortune, 0)
+ } else "?"
+ } ?: "§cOpen §e/cropupgrades§c to use!"
+ updatedDisplay.add(displayString)
+ display = updatedDisplay
+ }
+
+ private fun updateToolFortune(tool: ItemStack?) {
+ val cropMatchesTool = currentCrop == GardenAPI.getCropTypeFromItem(tool)
+ val toolCounterFortune = if (cropMatchesTool) {
+ getToolFortune(tool) + getCounterFortune(tool) + getCollectionFortune(tool)
+ } else 0.0
+ toolFortune = toolCounterFortune + getTurboCropFortune(tool, currentCrop) + getDedicationFortune(tool, currentCrop)
+ }
+
+ private fun isEnabled(): Boolean = GardenAPI.inGarden() && config.farmingFortuneDisplay
+
+ companion object {
+ private val collectionPattern = "§7You have §6\\+([\\d]{1,3})☘ Farming Fortune".toRegex()
+
+ fun getToolFortune(tool: ItemStack?): Double {
+ val internalName = tool?.getInternalName() ?: return 0.0
+ return if (internalName.startsWith("THEORETICAL_HOE")) {
+ listOf(10.0, 25.0, 50.0)[internalName.last().digitToInt() - 1]
+ } else when (internalName) {
+ "FUNGI_CUTTER" -> 30.0
+ "COCO_CHOPPER" -> 20.0
+ else -> 0.0
+ }
+ }
+
+ fun getTurboCropFortune(tool: ItemStack?, cropType: CropType?): Double {
+ val crop = cropType ?: return 0.0
+ return tool?.getEnchantments()?.get(crop.getTurboCrop())?.let { it * 5.0 } ?: 0.0
+ }
+
+ fun getCollectionFortune(tool: ItemStack?): Double {
+ val lore = tool?.getLore() ?: return 0.0
+ var hasCollectionAbility = false
+ return lore.firstNotNullOfOrNull {
+ if (hasCollectionAbility || it == "§6Collection Analysis") {
+ hasCollectionAbility = true
+ collectionPattern.matchEntire(it)?.groups?.get(1)?.value?.toDoubleOrNull()
+ } else null
+ } ?: 0.0
+ }
+
+ fun getCounterFortune(tool: ItemStack?): Double {
+ val counter = tool?.getCounter() ?: return 0.0
+ val digits = floor(log10(counter.toDouble()))
+ return (16 * digits - 48).takeIf { it > 0.0 } ?: 0.0
+ }
+
+ fun getDedicationFortune(tool: ItemStack?, cropType: CropType?): Double {
+ val dedicationLevel = tool?.getEnchantments()?.get("dedication") ?: 0
+ val dedicationMultiplier = listOf(0.0, 0.5, 0.75, 1.0, 2.0)[dedicationLevel]
+ val cropMilestone = GardenCropMilestones.getTierForCrops(
+ cropType?.getCounter() ?: 0
+ )
+ return dedicationMultiplier * cropMilestone
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt
index 534c88ac4..086c31d66 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt
@@ -6,6 +6,8 @@ import at.hannibal2.skyhanni.data.ScoreboardData
import at.hannibal2.skyhanni.events.*
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getCounter
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getCultivatingCount
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraft.network.play.client.C09PacketHeldItemChange
@@ -91,23 +93,7 @@ class GardenAPI {
return CropType.values().firstOrNull { internalName.startsWith(it.toolName) }
}
- fun readCounter(itemStack: ItemStack): Int {
- if (itemStack.hasTagCompound()) {
- val tag = itemStack.tagCompound
- if (tag.hasKey("ExtraAttributes", 10)) {
- val ea = tag.getCompoundTag("ExtraAttributes")
- if (ea.hasKey("mined_crops", 99)) {
- return ea.getInteger("mined_crops")
- }
-
- // only using cultivating when no crops counter is there
- if (ea.hasKey("farmed_cultivating", 99)) {
- return ea.getInteger("farmed_cultivating")
- }
- }
- }
- return -1
- }
+ fun readCounter(itemStack: ItemStack): Int = itemStack.getCounter() ?: itemStack.getCultivatingCount() ?: -1
fun CropType.getSpeed(): Int {
val speed = cropsPerSecond[this]
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/ToolTooltipTweaks.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/ToolTooltipTweaks.kt
new file mode 100644
index 000000000..4368ac0f9
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/ToolTooltipTweaks.kt
@@ -0,0 +1,116 @@
+package at.hannibal2.skyhanni.features.garden
+
+import at.hannibal2.skyhanni.SkyHanniMod
+import at.hannibal2.skyhanni.events.LorenzToolTipEvent
+import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getEnchantments
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getFarmingForDummiesCount
+import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getReforgeName
+import at.hannibal2.skyhanni.utils.StringUtils.firstLetterUppercase
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import org.lwjgl.input.Keyboard
+import java.text.DecimalFormat
+import kotlin.math.roundToInt
+
+class ToolTooltipTweaks {
+ private val config get() = SkyHanniMod.feature.garden
+ private val tooltipFortunePattern = "^§5§o§7Farming Fortune: §a\\+([\\d.]+)(?: §2\\(\\+\\d\\))?(?: §9\\(\\+(\\d+)\\))\$".toRegex()
+ private val counterStartLine = setOf("§5§o§6Logarithmic Counter", "§5§o§6Collection Analysis")
+
+ private val reforgeEndLine = setOf("§5§o", "§5§o§7chance for multiple crops.")
+
+ @SubscribeEvent
+ fun onTooltip(event: LorenzToolTipEvent) {
+ if (!LorenzUtils.inSkyBlock) return
+ val crop = GardenAPI.getCropTypeFromItem(event.itemStack) ?: return
+ val toolFortune = FarmingFortuneDisplay.getToolFortune(event.itemStack)
+ val counterFortune = FarmingFortuneDisplay.getCounterFortune(event.itemStack)
+ val collectionFortune = FarmingFortuneDisplay.getCollectionFortune(event.itemStack)
+ val turboCropFortune = FarmingFortuneDisplay.getTurboCropFortune(event.itemStack, crop)
+ val dedicationFortune = FarmingFortuneDisplay.getDedicationFortune(event.itemStack, crop)
+
+ val reforgeName = event.itemStack.getReforgeName()?.firstLetterUppercase()
+ val enchantments = event.itemStack.getEnchantments()
+ val sunderFortune = (enchantments["sunder"] ?: 0) * 12.5
+ val harvestingFortune = (enchantments["harvesting"] ?: 0) * 12.5
+ val cultivatingFortune = (enchantments["cultivating"] ?: 0).toDouble()
+
+ val ffdFortune = event.itemStack.getFarmingForDummiesCount().toDouble()
+ val cropFortune = (toolFortune + counterFortune + collectionFortune + turboCropFortune + dedicationFortune)
+ val iterator = event.toolTip.listIterator()
+
+ var removingFarmhandDescription = false
+ var removingCounterDescription = false
+ var removingReforgeDescription = false
+
+ for (line in iterator) {
+ val match = tooltipFortunePattern.matchEntire(line)?.groups
+ if (match != null) {
+ val displayedFortune = match[1]!!.value.toDouble()
+ val reforgeFortune = match[2]!!.value.toDouble()
+ val totalFortune = displayedFortune + cropFortune
+
+ val ffdString = if (ffdFortune != 0.0) " §2(+${ffdFortune.formatStat()})" else ""
+ val reforgeString = if (reforgeFortune != 0.0) " §9(+${reforgeFortune.formatStat()})" else ""
+ val cropString = if (cropFortune != 0.0) " §6[+${cropFortune.roundToInt()}]" else ""
+
+ val fortuneLine = when (config.cropTooltipFortune) {
+ 0 -> "§7Farming Fortune: §a+${displayedFortune.formatStat()}$ffdString$reforgeString"
+ 1 -> "§7Farming Fortune: §a+${displayedFortune.formatStat()}$ffdString$reforgeString$cropString"
+ else -> "§7Farming Fortune: §a+${totalFortune.formatStat()}$ffdString$reforgeString$cropString"
+ }
+ iterator.set(fortuneLine)
+
+ if (Keyboard.isKeyDown(config.fortuneTooltipKeybind)) {
+ iterator.addStat(" §7Sunder: §a+", sunderFortune)
+ iterator.addStat(" §7Harvesting: §a+", harvestingFortune)
+ iterator.addStat(" §7Cultivating: §a+", cultivatingFortune)
+ iterator.addStat(" §7Farming for Dummies: §2+", ffdFortune)
+ iterator.addStat(" §7$reforgeName: §9+", reforgeFortune)
+ iterator.addStat(" §7Tool: §6+", toolFortune)
+ iterator.addStat(" §7Counter: §6+", counterFortune)
+ iterator.addStat(" §7Collection: §6+", collectionFortune)
+ iterator.addStat(" §7Dedication: §6+", dedicationFortune)
+ iterator.addStat(" §7Turbo-Crop: §6+", turboCropFortune)
+ }
+ }
+ // Beware, dubious control flow beyond these lines
+ if (config.compactToolTooltips) {
+ if (line.startsWith("§5§o§7§8Bonus ")) removingFarmhandDescription = true
+ if (removingFarmhandDescription) {
+ iterator.remove()
+ removingFarmhandDescription = line != "§5§o"
+ }
+
+ if (removingCounterDescription && !line.startsWith("§5§o§7You have")) {
+ iterator.remove()
+ } else {
+ removingCounterDescription = false
+ }
+ if (counterStartLine.contains(line)) removingCounterDescription = true
+
+ if (line == "§5§o§9Blessed Bonus") removingReforgeDescription = true
+ if (removingReforgeDescription ) {
+ iterator.remove()
+ removingReforgeDescription = !reforgeEndLine.contains(line)
+ }
+ if (line == "§5§o§9Bountiful Bonus") removingReforgeDescription = true
+ }
+
+ }
+ }
+
+ companion object {
+ private fun Double.formatStat(): String {
+ val formatter = DecimalFormat("0.##")
+ return formatter.format(this)
+ }
+
+
+ private fun MutableListIterator<String>.addStat(description: String, value: Double) {
+ if (value != 0.0) {
+ this.add("$description${value.formatStat()}")
+ }
+ }
+ }
+} \ No newline at end of file