Skip to content

Commit

Permalink
Feature: Add overflow crop milestones (#997)
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: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
4 people authored May 3, 2024
1 parent 23a5bc7 commit 37a9fd7
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 67 deletions.
2 changes: 2 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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.GardenInventoryTooltipOverflow
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
Expand Down Expand Up @@ -900,6 +901,7 @@ class SkyHanniMod {
loadModule(VerminTracker)
loadModule(VerminHighlighter())
loadModule(SkillProgress)
loadModule(GardenInventoryTooltipOverflow())
loadModule(SkillTooltip())
loadModule(MaxPurseItems())
loadModule(SuperCraftFeatures())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.config.ConfigGuiManager
import at.hannibal2.skyhanni.config.features.About.UpdateStream
import at.hannibal2.skyhanni.data.ChatClickActionManager
import at.hannibal2.skyhanni.data.ChatManager
import at.hannibal2.skyhanni.data.GardenCropMilestones
import at.hannibal2.skyhanni.data.GardenCropMilestonesCommunityFix
import at.hannibal2.skyhanni.data.GuiEditManager
import at.hannibal2.skyhanni.data.PartyAPI
Expand Down Expand Up @@ -291,6 +292,14 @@ object Commands {
},
FarmingMilestoneCommand::onComplete
)
registerCommand0(
"shcropgoal",
"Define a custom milestone goal for a crop.",
{
FarmingMilestoneCommand.setGoal(it.getOrNull(0), it.getOrNull(1))
},
FarmingMilestoneCommand::onComplete
)
registerCommand0(
"shskills",
"Skills XP/Level related command",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class CropMilestonesConfig {
@FeatureToggle
public boolean progress = true;

@Expose
@ConfigOption(name = "Overflow", desc = "")
@Accordion
public CropMilestonesOverflowConfig overflow = new CropMilestonesOverflowConfig();

@Expose
@ConfigOption(
name = "Warn When Close",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package at.hannibal2.skyhanni.config.features.garden.cropmilestones;

import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;

public class CropMilestonesOverflowConfig {

@Expose
@ConfigOption(name = "Crop Milestone Display", desc = "Show overflow levels in Crop Milestone display.")
@ConfigEditorBoolean
public boolean cropMilestoneDisplay = false;

@Expose
@ConfigOption(name = "Best Crop Time", desc = "Show overflow levels in Best Crop Time Display.")
@ConfigEditorBoolean
public boolean bestCropTime = false;

@Expose
@ConfigOption(name = "Inventory", desc = "Show overflow levels as stack size in the Crop Milestones inventory (will also change milestone average).")
@ConfigEditorBoolean
public boolean inventoryStackSize = false;

@Expose
@ConfigOption(name = "Tooltip", desc = "Show overflow level progress in the item tooltip in the Crop Milestones inventory.")
@ConfigEditorBoolean
public boolean inventoryTooltip = false;

@Expose
@ConfigOption(name = "Discord RPC", desc = "Show overflow levels in the Discord RPC milestone display.")
@ConfigEditorBoolean
public boolean discordRPC = false;

@Expose
@ConfigOption(name = "Chat", desc = "Send a chat message when gaining an overflow milestone level.")
@ConfigEditorBoolean
public boolean chat = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ public static class FarmingWeightConfig {
@Expose
public Map<String, LorenzVec> npcVisitorLocations = new HashMap<>();

@Expose
public Map<CropType, Integer> customGoalMilestone = new HashMap<>();

@Expose
public PestProfitTracker.Data pestProfitTracker = new PestProfitTracker.Data();

Expand Down
99 changes: 86 additions & 13 deletions src/main/java/at/hannibal2/skyhanni/data/GardenCropMilestones.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.utils.ChatUtils.chat
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.SoundUtils
import at.hannibal2.skyhanni.utils.SoundUtils.playSound
import at.hannibal2.skyhanni.utils.StringUtils.matchFirst
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object GardenCropMilestones {

private val patternGroup = RepoPattern.group("data.garden.milestone")
private val cropPattern by patternGroup.pattern(
"crop",
Expand All @@ -24,6 +28,8 @@ object GardenCropMilestones {
"§7Total: §a(?<name>.*)"
)

private val config get() = GardenAPI.config.cropMilestones

fun getCropTypeByLore(itemStack: ItemStack): CropType? {
itemStack.getLore().matchFirst(cropPattern) {
val name = group("name")
Expand All @@ -47,6 +53,42 @@ object GardenCropMilestones {
GardenCropMilestonesCommunityFix.openInventory(event.inventoryItems)
}

fun onOverflowLevelUp(crop: CropType, oldLevel: Int, newLevel: Int) {
val customGoalLevel = ProfileStorageData.profileSpecific?.garden?.customGoalMilestone?.get(crop) ?: 0
val goalReached = newLevel == customGoalLevel

// TODO utils function that is shared with Garden Level Display
val rewards = buildList {
add(" §r§8+§aRespect from Elite Farmers and SkyHanni members :)")
add(" §r§8+§b1 Flexing Point")
if (newLevel % 5 == 0)
add(" §r§7§8+§d2 SkyHanni User Luck")
}

val messages = listOf(
"§r§3§l▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬§r",
" §r§b§lGARDEN MILESTONE §3${crop.cropName} §8$oldLevel➜§3$newLevel§r",
if (goalReached)
listOf(
"",
" §r§d§lGOAL REACHED!",
"",
).joinToString("\n")
else
"",
" §r§a§lREWARDS§r",
rewards.joinToString("\n"),
"§r§3§l▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬§r"
)

chat(messages.joinToString("\n"), false)

if (goalReached)
chat("§e§lYou have reached your milestone goal of §b§l${customGoalLevel} §e§lin the §b§l${crop.cropName} §e§lcrop!", false)

SoundUtils.createSound("random.levelup", 1f, 1f).playSound()
}

var cropMilestoneData: Map<CropType, List<Int>> = emptyMap()

val cropCounter: MutableMap<CropType, Long>? get() = GardenAPI.storage?.cropCounter
Expand All @@ -58,50 +100,81 @@ object GardenCropMilestones {
cropCounter?.set(this, counter)
}

fun CropType.isMaxed(): Boolean {
fun CropType.isMaxed(useOverflow: Boolean): Boolean {
if (useOverflow) return false

// TODO change 1b
val maxValue = cropMilestoneData[this]?.sum() ?: 1_000_000_000 // 1 bil for now
return getCounter() >= maxValue
}

fun getTierForCropCount(count: Long, crop: CropType): Int {
fun getTierForCropCount(count: Long, crop: CropType, allowOverflow: Boolean = false): Int {
var tier = 0
var totalCrops = 0L
val cropMilestone = cropMilestoneData[crop] ?: return 0
val last = cropMilestone.last()

for (tierCrops in cropMilestone) {
totalCrops += tierCrops
if (totalCrops > count) {
if (totalCrops >= count) {
return tier
}
tier++
}

if (allowOverflow) {
while (totalCrops < count) {
totalCrops += last
if (totalCrops >= count) {
return tier
}
tier++
}
}
return tier
}

fun getMaxTier() = cropMilestoneData.values.firstOrNull()?.size ?: 0

fun getCropsForTier(requestedTier: Int, crop: CropType): Long {
fun getCropsForTier(requestedTier: Int, crop: CropType, allowOverflow: Boolean = false): Long {
var totalCrops = 0L
var tier = 0
val cropMilestone = cropMilestoneData[crop] ?: return 0
val definedTiers = cropMilestone.size

if (requestedTier <= definedTiers || !allowOverflow) {
for (tierCrops in cropMilestone) {
totalCrops += tierCrops
tier++
if (tier == requestedTier) {
return totalCrops
}
}

return if (!allowOverflow) 0 else totalCrops
}


for (tierCrops in cropMilestone) {
totalCrops += tierCrops
tier++
if (tier == requestedTier) {
return totalCrops
}
}

return 0
val additionalTiers = requestedTier - definedTiers

val lastIncrement = cropMilestone.last().toLong()

totalCrops += lastIncrement * additionalTiers

return totalCrops
}

fun CropType.progressToNextLevel(): Double {
fun CropType.progressToNextLevel(allowOverflow: Boolean = false): Double {
val progress = getCounter()
val startTier = getTierForCropCount(progress, this)
val startCrops = getCropsForTier(startTier, this)
val end = getCropsForTier(startTier + 1, this).toDouble()
return (progress - startCrops) / (end - startCrops)
val startTier = getTierForCropCount(progress, this, allowOverflow)
val startCrops = getCropsForTier(startTier, this, allowOverflow)
val end = getCropsForTier(startTier + 1, this, allowOverflow)
return (progress - startCrops).toDouble() / (end - startCrops)
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ object GardenCropMilestonesCommunityFix {
// debug("crop: $crop")
// debug("realTier: $realTier")

val guessNextMax = GardenCropMilestones.getCropsForTier(
realTier + 1,
crop
) - GardenCropMilestones.getCropsForTier(realTier, crop)
val guessNextMax = GardenCropMilestones.getCropsForTier(realTier + 1, crop) - GardenCropMilestones.getCropsForTier(realTier, crop)
// debug("guessNextMax: ${guessNextMax.addSeparators()}")
val nextMax = amountPattern.matchMatcher(next) {
group("max").formatLong()
Expand All @@ -105,7 +102,7 @@ object GardenCropMilestonesCommunityFix {
wrongData.add("$crop:$realTier:${nextMax.addSeparators()}")
}

val guessTotalMax = GardenCropMilestones.getCropsForTier(46, crop)
val guessTotalMax = GardenCropMilestones.getCropsForTier(46, crop) // no need to overflow here
// println("guessTotalMax: ${guessTotalMax.addSeparators()}")
val totalMax = amountPattern.matchMatcher(total) {
group("max").formatLong()
Expand Down Expand Up @@ -163,13 +160,8 @@ object GardenCropMilestonesCommunityFix {
}

private fun tryFix(crop: CropType, tier: Int, amount: Int): Boolean {
val guessNextMax = GardenCropMilestones.getCropsForTier(tier + 1, crop) - GardenCropMilestones.getCropsForTier(
tier,
crop
)
if (guessNextMax.toInt() == amount) {
return false
}
val guessNextMax = GardenCropMilestones.getCropsForTier(tier + 1, crop) - GardenCropMilestones.getCropsForTier(tier, crop)
if (guessNextMax.toInt() == amount) return false
GardenCropMilestones.cropMilestoneData = GardenCropMilestones.cropMilestoneData.editCopy {
fix(crop, this, tier, amount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ enum class CropType(
fun getByNameOrNull(itemName: String): CropType? {
if (itemName == "Red Mushroom" || itemName == "Brown Mushroom") return MUSHROOM
if (itemName == "Seeds") return WHEAT
return entries.firstOrNull { it.cropName == itemName }
return entries.firstOrNull { it.cropName.equals(itemName, ignoreCase = true) }
}

fun getByName(name: String) = getByNameOrNull(name) ?: error("No valid crop type '$name'")
Expand Down
Loading

0 comments on commit 37a9fd7

Please sign in to comment.