Skip to content

Commit

Permalink
Backend: Renderable in GardenCropMilestoneDisplay (#1534)
Browse files Browse the repository at this point in the history
Co-authored-by: HiZe <superhize@hotmail.com>
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Co-authored-by: alexia <me@alexia.lol>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
5 people authored May 3, 2024
1 parent 04f23d3 commit 9cb0e36
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 48 deletions.
11 changes: 11 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactor
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateShopPrice
import at.hannibal2.skyhanni.utils.BlockUtils.isBabyCrop
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.CollectionUtils.addItemStack
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
Expand All @@ -41,6 +42,7 @@ import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.RenderUtils.addItemIcon
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getCultivatingCounter
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getHoeCounter
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraft.network.play.client.C09PacketHeldItemChange
Expand Down Expand Up @@ -169,6 +171,15 @@ object GardenAPI {
) =
addItemIcon(crop.icon.copy(), highlight, scale = scale)

// TODO rename to addCropIcon
fun MutableList<Renderable>.addCropIconRenderable(
crop: CropType,
scale: Double = NEUItems.itemFontSize,
highlight: Boolean = false,
) {
addItemStack(crop.icon.copy(), highlight = highlight, scale = scale)
}

fun hideExtraGuis() = ComposterOverlay.inInventory || AnitaMedalProfit.inInventory ||
SkyMartCopperPrice.inInventory || FarmingContestAPI.inInventory || VisitorAPI.inInventory ||
FFGuideGUI.isInGui() || ChocolateShopPrice.inInventory || ChocolateFactoryAPI.inChocolateFactory ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@ import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.FarmingFortuneDisplay
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.features.garden.GardenAPI.addCropIcon
import at.hannibal2.skyhanni.features.garden.GardenAPI.addCropIconRenderable
import at.hannibal2.skyhanni.features.garden.GardenAPI.getCropType
import at.hannibal2.skyhanni.features.garden.farming.GardenCropSpeed.setSpeed
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.CollectionUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.CollectionUtils.addString
import at.hannibal2.skyhanni.utils.ConditionalUtils
import at.hannibal2.skyhanni.utils.ConfigUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems
import at.hannibal2.skyhanni.utils.SoundUtils
import at.hannibal2.skyhanni.utils.TimeUnit
import at.hannibal2.skyhanni.utils.TimeUtils
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.Collections
import kotlin.time.Duration.Companion.seconds

object GardenCropMilestoneDisplay {

private var progressDisplay = emptyList<List<Any>>()
private var mushroomCowPerkDisplay = emptyList<List<Any>>()
private var progressDisplay = emptyList<Renderable>()
private var mushroomCowPerkDisplay = emptyList<Renderable>()
private val cultivatingData = mutableMapOf<CropType, Long>()
private val config get() = GardenAPI.config.cropMilestones
private val overflowConfig get() = config.overflow
Expand Down Expand Up @@ -71,12 +72,12 @@ object GardenCropMilestoneDisplay {
if (!isEnabled()) return
if (GardenAPI.hideExtraGuis()) return

config.progressDisplayPos.renderStringsAndItems(
config.progressDisplayPos.renderRenderables(
progressDisplay, posLabel = "Crop Milestone Progress"
)

if (config.mushroomPetPerk.enabled) {
config.mushroomPetPerk.pos.renderStringsAndItems(
config.mushroomPetPerk.pos.renderRenderables(
mushroomCowPerkDisplay, posLabel = "Mushroom Cow Perk"
)
}
Expand Down Expand Up @@ -144,10 +145,10 @@ object GardenCropMilestoneDisplay {
}
}

private fun drawProgressDisplay(crop: CropType): MutableList<List<Any>> {
private fun drawProgressDisplay(crop: CropType): List<Renderable> {
val counter = crop.getCounter()
val lineMap = HashMap<Int, List<Any>>()
lineMap[0] = Collections.singletonList("§6Crop Milestones")
val lineMap = mutableMapOf<Int, Renderable>()
lineMap[0] = Renderable.string("§6Crop Milestones")

val customTargetLevel = storage?.get(crop) ?: 0
val overflowDisplay = overflowConfig.cropMilestoneDisplay
Expand All @@ -158,14 +159,14 @@ object GardenCropMilestoneDisplay {
val useCustomGoal = customTargetLevel != 0 && customTargetLevel > currentTier
nextTier = if (useCustomGoal) customTargetLevel else nextTier

val list = mutableListOf<Any>()
list.addCropIcon(crop)
if (crop.isMaxed(overflowDisplay)) {
list.add("§7" + crop.cropName + " §eMAXED")
} else {
list.add("§7" + crop.cropName + " §8$currentTier➜§3$nextTier")
}
lineMap[1] = list
lineMap[1] = Renderable.horizontalContainer(buildList {
addCropIconRenderable(crop)
if (crop.isMaxed(overflowDisplay) && !overflowDisplay) {
addString("§7" + crop.cropName + " §eMAXED")
} else {
addString("§7" + crop.cropName + " §8$currentTier➜§3$nextTier")
}
})

val allowOverflowOrCustom = overflowDisplay || useCustomGoal
val cropsForNextTier = GardenCropMilestones.getCropsForTier(nextTier, crop, allowOverflowOrCustom)
Expand All @@ -178,13 +179,13 @@ object GardenCropMilestoneDisplay {
Pair(have, need)
}

lineMap[2] = if (crop.isMaxed(overflowDisplay)) {
lineMap[2] = if (crop.isMaxed(overflowDisplay) && !overflowDisplay) {
val haveFormat = counter.addSeparators()
Collections.singletonList("§7Counter: §e$haveFormat")
Renderable.string("§7Counter: §e$haveFormat")
} else {
val haveFormat = have.addSeparators()
val needFormat = need.addSeparators()
Collections.singletonList("§e$haveFormat§8/§e$needFormat")
Renderable.string("§e$haveFormat§8/§e$needFormat")
}

val farmingFortune = FarmingFortuneDisplay.getCurrentFarmingFortune()
Expand All @@ -193,7 +194,7 @@ object GardenCropMilestoneDisplay {

if (farmingFortuneSpeed > 0) {
crop.setSpeed(farmingFortuneSpeed)
if (!crop.isMaxed(overflowDisplay)) {
if (!crop.isMaxed(overflowDisplay) || overflowDisplay) {
val missing = need - have
val missingTimeSeconds = missing / farmingFortuneSpeed
val millis = missingTimeSeconds * 1000
Expand All @@ -204,30 +205,38 @@ object GardenCropMilestoneDisplay {
tryWarn(millis, "§b${crop.cropName} $nextTier in $duration")

val speedText = "§7In §b$duration"
lineMap[3] = Collections.singletonList(speedText)
lineMap[3] = Renderable.string(speedText)
GardenAPI.itemInHand?.let {
if (GardenAPI.readCounter(it) == -1L) {
lineMap[3] = listOf(speedText, " §7Inaccurate!")
lineMap[3] = Renderable.string("$speedText §7Inaccurate!")
}
}
}

val format = (farmingFortuneSpeed * 60).addSeparators()
lineMap[4] = Collections.singletonList("§7Crops/Minute§8: §e$format")
lineMap[4] = Renderable.string("§7Crops/Minute§8: §e$format")
val formatBps = speed.round(config.blocksBrokenPrecision).addSeparators()
lineMap[5] = Collections.singletonList("§7Blocks/Second§8: §e$formatBps")
lineMap[5] = Renderable.string("§7Blocks/Second§8: §e$formatBps")
}

val percentageFormat = LorenzUtils.formatPercentage(have.toDouble() / need.toDouble())
lineMap[6] = if (crop.isMaxed(overflowDisplay)) {
Collections.singletonList("§7Percentage: §e100%")
lineMap[6] = if (crop.isMaxed(overflowDisplay) && !overflowDisplay) {
Renderable.string("§7Percentage: §e100%")
} else {
Collections.singletonList("§7Percentage: §e$percentageFormat")
Renderable.string("§7Percentage: §e$percentageFormat")
}

if (overflowConfig.chat) {
if (currentTier >= 46 && currentTier == previousNext &&
nextRealTier == currentTier + 1 && lastWarnedLevel != currentTier) {
GardenCropMilestones.onOverflowLevelUp(crop, currentTier - 1, nextRealTier - 1)
lastWarnedLevel = currentTier
}
}

if (overflowConfig.chat) {
if (currentTier >= 46 && currentTier == previousNext && nextRealTier == currentTier + 1 && lastWarnedLevel != currentTier) {
if (currentTier >= 46 && currentTier == previousNext &&
nextRealTier == currentTier + 1 && lastWarnedLevel != currentTier) {
GardenCropMilestones.onOverflowLevelUp(crop, currentTier - 1, nextRealTier - 1)
lastWarnedLevel = currentTier
}
Expand Down Expand Up @@ -256,8 +265,8 @@ object GardenCropMilestoneDisplay {
}
}

private fun formatDisplay(lineMap: HashMap<Int, List<Any>>): MutableList<List<Any>> {
val newList = mutableListOf<List<Any>>()
private fun formatDisplay(lineMap: MutableMap<Int, Renderable>): List<Renderable> {
val newList = mutableListOf<Renderable>()
for (index in config.text) {
// TODO, change functionality to use enum rather than ordinals
lineMap[index.ordinal]?.let {
Expand All @@ -266,7 +275,7 @@ object GardenCropMilestoneDisplay {
}

if (needsInventory) {
newList.addAsSingletonList("§cOpen §e/cropmilestones §cto update!")
newList.addString("§cOpen §e/cropmilestones §cto update!")
}

return newList
Expand All @@ -277,13 +286,13 @@ object GardenCropMilestoneDisplay {
val allowOverflow = overflowConfig.cropMilestoneDisplay
if (mushroom.isMaxed(allowOverflow)) {
mushroomCowPerkDisplay = listOf(
listOf("§6Mooshroom Cow Perk"),
listOf("§eMushroom crop is maxed!"),
Renderable.string("§6Mooshroom Cow Perk"),
Renderable.string("§eMushroom crop is maxed!"),
)
return
}

val lineMap = HashMap<Int, List<Any>>()
val lineMap = HashMap<Int, Renderable>()
val counter = mushroom.getCounter()

val currentTier = GardenCropMilestones.getTierForCropCount(counter, mushroom, allowOverflow)
Expand All @@ -300,14 +309,15 @@ object GardenCropMilestoneDisplay {

val missing = need - have

lineMap[0] = Collections.singletonList("§6Mooshroom Cow Perk")

val list = mutableListOf<Any>()
list.addCropIcon(mushroom)
list.add("§7Mushroom Tier $nextTier")
lineMap[1] = list
lineMap[0] = Renderable.string("§6Mooshroom Cow Perk")
lineMap[1] = Renderable.horizontalContainer(
buildList {
addCropIconRenderable(mushroom)
addString("§7Mushroom Tier $nextTier")
}
)

lineMap[2] = Collections.singletonList("§e$haveFormat§8/§e$needFormat")
lineMap[2] = Renderable.string("§e$haveFormat§8/§e$needFormat")

val speed = GardenCropSpeed.averageBlocksPerSecond
if (speed != 0.0) {
Expand All @@ -318,18 +328,18 @@ object GardenCropMilestoneDisplay {
// TODO, change functionality to use enum rather than ordinals
val biggestUnit = TimeUnit.entries[config.highestTimeFormat.get().ordinal]
val duration = TimeUtils.formatDuration(millis.toLong(), biggestUnit)
lineMap[3] = Collections.singletonList("§7In §b$duration")
lineMap[3] = Renderable.string("§7In §b$duration")
}

val percentageFormat = LorenzUtils.formatPercentage(have.toDouble() / need.toDouble())
lineMap[4] = Collections.singletonList("§7Percentage: §e$percentageFormat")
lineMap[4] = Renderable.string("§7Percentage: §e$percentageFormat")

if (currentTier >= 46 && currentTier == previousMushNext && nextTier == currentTier + 1 && lastMushWarnedLevel != currentTier) {
GardenCropMilestones.onOverflowLevelUp(mushroom, currentTier - 1, nextTier - 1)
lastMushWarnedLevel = currentTier
}

val newList = mutableListOf<List<Any>>()
val newList = mutableListOf<Renderable>()
for (index in config.mushroomPetPerk.text) {
// TODO, change functionality to use enum rather than ordinals
lineMap[index.ordinal]?.let {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.renderables.Renderable
import net.minecraft.enchantment.Enchantment
import net.minecraft.item.ItemStack
import java.util.Collections
import java.util.Queue
Expand Down Expand Up @@ -202,8 +203,16 @@ object CollectionUtils {
}

// TODO add internal name support, and caching
fun MutableList<Renderable>.addItemStack(itemStack: ItemStack) {
add(Renderable.itemStack(itemStack))
fun MutableList<Renderable>.addItemStack(
itemStack: ItemStack,
highlight: Boolean = false,
scale: Double = NEUItems.itemFontSize,
) {
if (highlight) {
// Hack to add enchant glint, like Hypixel does it
itemStack.addEnchantment(Enchantment.protection, 0)
}
add(Renderable.itemStack(itemStack, scale = scale))
}

fun MutableList<Renderable>.addItemStack(internalName: NEUInternalName) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ object RenderUtils {
return offsetX
}

@Deprecated("use renderable item list", ReplaceWith(""))
fun MutableList<Any>.addItemIcon(
item: ItemStack,
highlight: Boolean = false,
Expand Down

0 comments on commit 9cb0e36

Please sign in to comment.