From 591c5a1ea9faa014cc4ae6f3f4c152ebc7d3ec8a Mon Sep 17 00:00:00 2001 From: Empa Date: Fri, 25 Oct 2024 16:38:58 +0200 Subject: [PATCH 1/3] add difference to BitsSpentEvent, make BitsAvailableUpdateEvent separated from BitsUpdateEvent and --- .../config/storage/PlayerSpecificStorage.java | 5 ++ .../skyhanni/config/storage/Storage.java | 4 - .../at/hannibal2/skyhanni/data/BitsAPI.kt | 89 ++++++++----------- .../at/hannibal2/skyhanni/data/FameRanks.kt | 14 +-- .../skyhanni/events/BitsUpdateEvent.kt | 9 +- .../skyhanni/features/misc/NoBitsWarning.kt | 10 +-- 6 files changed, 62 insertions(+), 69 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.java b/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.java index c3b949c79235..416a0197f153 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.java +++ b/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.java @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.utils.SimpleTimeMark; import com.google.gson.annotations.Expose; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -34,6 +35,10 @@ public class PlayerSpecificStorage { @Expose public UpgradeReminder.CommunityShopUpgrade communityShopAccountUpgrade = null; + @Nullable + @Expose + public String fameRank = null; + @Expose public List guildMembers = new ArrayList<>(); diff --git a/src/main/java/at/hannibal2/skyhanni/config/storage/Storage.java b/src/main/java/at/hannibal2/skyhanni/config/storage/Storage.java index 3b75a64eebbf..766f9e9efdab 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/storage/Storage.java +++ b/src/main/java/at/hannibal2/skyhanni/config/storage/Storage.java @@ -46,10 +46,6 @@ public class Storage { @Expose public Map players = new HashMap<>(); - // TODO this should get moved into player specific - @Expose - public String currentFameRank = "New player"; - @Expose public List blacklistedUsers = new ArrayList<>(); diff --git a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt index 022a18d926e3..f9aff09796ad 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt @@ -1,8 +1,7 @@ package at.hannibal2.skyhanni.data -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator -import at.hannibal2.skyhanni.data.FameRanks.getFameRankByNameOrNull +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.events.BitsAvailableUpdateEvent import at.hannibal2.skyhanni.events.BitsUpdateEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.events.LorenzChatEvent @@ -13,7 +12,7 @@ import at.hannibal2.skyhanni.utils.CollectionUtils.nextAfter import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NumberUtil.formatInt -import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst +import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.SimpleTimeMark @@ -27,18 +26,18 @@ import kotlin.time.Duration.Companion.days @SkyHanniModule object BitsAPI { private val profileStorage get() = ProfileStorageData.profileSpecific?.bits - private val playerStorage get() = SkyHanniMod.feature.storage + private val playerStorage get() = ProfileStorageData.playerSpecific var bits: Int get() = profileStorage?.bits ?: 0 private set(value) { profileStorage?.bits = value } - var currentFameRank: FameRank? - get() = playerStorage?.currentFameRank?.let { getFameRankByNameOrNull(it) } + var fameRank: FameRank? + get() = playerStorage?.fameRank?.let(FameRanks::getByInternalName) private set(value) { if (value != null) { - playerStorage?.currentFameRank = value.name + playerStorage?.fameRank = value.internalName } } var bitsAvailable: Int @@ -53,7 +52,7 @@ object BitsAPI { profileStorage?.boosterCookieExpiryTime = value } - private const val defaultCookieBits = 4800 + private const val DEFAULT_COOKIE_BITS = 4800 private val bitsDataGroup = RepoPattern.group("data.bits") @@ -151,16 +150,16 @@ object BitsAPI { bitsScoreboardPattern.matchMatcher(message) { val amount = group("amount").formatInt() - if (amount == bits) return + val diff = amount - bits + if (diff == 0) return - if (amount > bits) { - val difference = amount - bits - bitsAvailable -= difference + if (diff > 0) { + bitsAvailable -= diff bits = amount - sendBitsGainEvent(difference) + sendBitsGainEvent(diff) } else { bits = amount - sendBitsSpentEvent() + sendBitsSpentEvent(diff) } } } @@ -182,7 +181,7 @@ object BitsAPI { fameRankUpPattern.matchMatcher(message) { val rank = group("rank") - currentFameRank = getFameRankByNameOrNull(rank) + fameRank = FameRanks.getByName(rank) ?: return ErrorManager.logErrorWithData( FameRankNotFoundException(rank), "FameRank $rank not found", @@ -204,7 +203,7 @@ object BitsAPI { } } - fun bitsPerCookie(): Int = (defaultCookieBits * (currentFameRank?.bitsMultiplier ?: 1.0)).toInt() + fun bitsPerCookie(): Int = (DEFAULT_COOKIE_BITS * (fameRank?.bitsMultiplier ?: 1.0)).toInt() @SubscribeEvent fun onInventoryOpen(event: InventoryFullyOpenedEvent) { @@ -223,7 +222,7 @@ object BitsAPI { } val lore = cookieStack.getLore() - lore.matchFirst(bitsAvailableMenuPattern) { + bitsAvailableMenuPattern.firstMatcher(lore) { val amount = group("toClaim").formatInt() if (bitsAvailable != amount) { bitsAvailable = amount @@ -235,11 +234,11 @@ object BitsAPI { } } } - lore.matchFirst(cookieDurationPattern) { + cookieDurationPattern.firstMatcher(lore) { val duration = TimeUtils.getDuration(group("time")) cookieBuffTime = SimpleTimeMark.now() + duration } - lore.matchFirst(noCookieActiveSBMenuPattern) { + noCookieActiveSBMenuPattern.firstMatcher(lore) { val cookieTime = cookieBuffTime if (cookieTime == null || cookieTime.isInFuture()) cookieBuffTime = SimpleTimeMark.farPast() } @@ -252,34 +251,20 @@ object BitsAPI { val cookieStack = stacks.values.lastOrNull { cookieGuiStackPattern.matches(it.displayName) } ?: return line@ for (line in fameRankStack.getLore()) { - fameRankCommunityShopPattern.matchMatcher(line) { - val rank = group("rank") - - currentFameRank = getFameRankByNameOrNull(rank) - ?: return ErrorManager.logErrorWithData( - FameRankNotFoundException(rank), - "FameRank $rank not found", - "Rank" to rank, - "Lore" to fameRankStack.getLore(), - "FameRanks" to FameRanks.fameRanks, - ) - - continue@line - } - - fameRankSbMenuPattern.matchMatcher(line) { - val rank = group("rank") - - currentFameRank = getFameRankByNameOrNull(rank) - ?: return ErrorManager.logErrorWithData( - FameRankNotFoundException(rank), - "FameRank $rank not found", - "Rank" to rank, - "Lore" to fameRankStack.getLore(), - "FameRanks" to FameRanks.fameRanks, - ) - - continue@line + for (pattern in listOf(fameRankCommunityShopPattern, fameRankSbMenuPattern)) { + pattern.matchMatcher(line) { + val rank = group("rank") + + fameRank = FameRanks.getByName(rank) + ?: return ErrorManager.logErrorWithData( + FameRankNotFoundException(rank), + "FameRank $rank not found", + "Rank" to rank, + "Lore" to fameRankStack.getLore(), + "FameRanks" to FameRanks.fameRanks, + ) + continue@line + } } } @@ -311,10 +296,12 @@ object BitsAPI { fun hasCookieBuff() = cookieBuffTime?.isInFuture() ?: false private fun sendBitsGainEvent(difference: Int) = - BitsUpdateEvent.BitsGain(bits, bitsAvailable, difference).postAndCatch() + BitsUpdateEvent.BitsGain(bits, bitsAvailable, difference).post() + + private fun sendBitsSpentEvent(difference: Int) = + BitsUpdateEvent.BitsSpent(bits, bitsAvailable, difference).post() - private fun sendBitsSpentEvent() = BitsUpdateEvent.BitsSpent(bits, bitsAvailable).postAndCatch() - private fun sendBitsAvailableGainedEvent() = BitsUpdateEvent.BitsAvailableGained(bits, bitsAvailable).postAndCatch() + private fun sendBitsAvailableGainedEvent() = BitsAvailableUpdateEvent(bitsAvailable).post() fun isEnabled() = LorenzUtils.inSkyBlock && profileStorage != null diff --git a/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt b/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt index 750f1294390e..fec63494ee6e 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt @@ -7,16 +7,19 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @SkyHanniModule object FameRanks { - var fameRanks = emptyMap() + var fameRanks = listOf() private set - fun getFameRankByNameOrNull(name: String) = fameRanks[name] + fun getByName(name: String) = fameRanks.find { it.name.equals(name, true) } + + fun getByInternalName(internalName: String) = fameRanks.find { it.internalName == internalName } @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { val ranks = event.getConstant("FameRank") - fameRanks = ranks.fameRank.values.map { FameRank(it.name, it.fameRequired, it.bitsMultiplier, it.votes) } - .associateBy { it.name } + fameRanks = ranks.fameRank.map { (internalName, rank) -> + FameRank(rank.name, rank.fameRequired, rank.bitsMultiplier, rank.votes, internalName) + } } } @@ -24,5 +27,6 @@ data class FameRank( val name: String, val fameRequired: Int, val bitsMultiplier: Double, - val electionVotes: Int + val electionVotes: Int, + val internalName: String, ) diff --git a/src/main/java/at/hannibal2/skyhanni/events/BitsUpdateEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/BitsUpdateEvent.kt index 78ff15baee49..d7b692c4b554 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/BitsUpdateEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/BitsUpdateEvent.kt @@ -1,7 +1,10 @@ package at.hannibal2.skyhanni.events -open class BitsUpdateEvent(val bits: Int, val bitsAvailable: Int, val difference: Int = 0) : LorenzEvent() { +import at.hannibal2.skyhanni.api.event.SkyHanniEvent + +open class BitsUpdateEvent(val bits: Int, val bitsAvailable: Int, val difference: Int) : SkyHanniEvent() { class BitsGain(bits: Int, bitsAvailable: Int, difference: Int) : BitsUpdateEvent(bits, bitsAvailable, difference) - class BitsSpent(bits: Int, bitsAvailable: Int) : BitsUpdateEvent(bits, bitsAvailable) - class BitsAvailableGained(bits: Int, bitsAvailable: Int) : BitsUpdateEvent(bits, bitsAvailable) + class BitsSpent(bits: Int, bitsAvailable: Int, difference: Int) : BitsUpdateEvent(bits, bitsAvailable, difference) } + +class BitsAvailableUpdateEvent(val bitsAvailable: Int) : SkyHanniEvent() diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/NoBitsWarning.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/NoBitsWarning.kt index 375a2e57baaa..b6a4c3e6fb51 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/NoBitsWarning.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/NoBitsWarning.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.features.misc import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.api.event.HandleEvent import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.events.BitsUpdateEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -18,9 +19,9 @@ object NoBitsWarning { private val config get() = SkyHanniMod.feature.misc.bits - @SubscribeEvent + @HandleEvent fun onBitsGain(event: BitsUpdateEvent.BitsGain) { - if (isWarningEnabled() && event.bitsAvailable == 0) { + if (config.bitsGainChatMessage && event.bitsAvailable == 0) { ChatUtils.clickableChat( "§bNo Bits Available! §eClick to buy booster cookies on the bazaar.", @@ -32,7 +33,7 @@ object NoBitsWarning { if (config.notificationSound) SoundUtils.repeatSound(100, 10, createSound("note.pling", 0.6f)) } - if (isChatMessageEnabled()) { + if (config.enableWarning) { if (event.bits < config.threshold) return ChatUtils.chat("You have gained §b${event.difference.addSeparators()} §eBits.") } @@ -44,7 +45,4 @@ object NoBitsWarning { event.move(40, "misc.noBitsWarning.enabled", "misc.bits.enableWarning") event.move(40, "misc.noBitsWarning.notificationSound", "misc.bits.notificationSound") } - - private fun isChatMessageEnabled() = LorenzUtils.inSkyBlock && config.bitsGainChatMessage - private fun isWarningEnabled() = LorenzUtils.inSkyBlock && config.enableWarning } From 36f2d1d5aa2d2919d290025c17f8cfb2a7550d31 Mon Sep 17 00:00:00 2001 From: Empa Date: Sat, 26 Oct 2024 11:37:29 +0200 Subject: [PATCH 2/3] fix detekt and change to use map again --- src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt | 6 +++--- src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt index f9aff09796ad..196e3f5c14c8 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt @@ -1,6 +1,6 @@ package at.hannibal2.skyhanni.data -import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.events.BitsAvailableUpdateEvent import at.hannibal2.skyhanni.events.BitsUpdateEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent @@ -187,7 +187,7 @@ object BitsAPI { "FameRank $rank not found", "Rank" to rank, "Message" to message, - "FameRanks" to FameRanks.fameRanks, + "FameRanks" to FameRanks.fameRanksMap, ) return @@ -261,7 +261,7 @@ object BitsAPI { "FameRank $rank not found", "Rank" to rank, "Lore" to fameRankStack.getLore(), - "FameRanks" to FameRanks.fameRanks, + "FameRanks" to FameRanks.fameRanksMap, ) continue@line } diff --git a/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt b/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt index fec63494ee6e..2099c4c9f876 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt @@ -7,19 +7,19 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @SkyHanniModule object FameRanks { - var fameRanks = listOf() + var fameRanksMap = mapOf() private set - fun getByName(name: String) = fameRanks.find { it.name.equals(name, true) } + fun getByName(name: String) = fameRanksMap.values.find { it.name.equals(name, true) } - fun getByInternalName(internalName: String) = fameRanks.find { it.internalName == internalName } + fun getByInternalName(internalName: String) = fameRanksMap[internalName] @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { val ranks = event.getConstant("FameRank") - fameRanks = ranks.fameRank.map { (internalName, rank) -> + fameRanksMap = ranks.fameRank.map { (internalName, rank) -> FameRank(rank.name, rank.fameRequired, rank.bitsMultiplier, rank.votes, internalName) - } + }.associateBy(FameRank::internalName) } } From 328262a48fb1ec63567b96c9ebc2c77d2819fa6a Mon Sep 17 00:00:00 2001 From: Empa Date: Sun, 5 Jan 2025 15:34:14 +0100 Subject: [PATCH 3/3] fix merge Signed-off-by: Empa --- .../config/storage/PlayerSpecificStorage.kt | 3 + .../at/hannibal2/skyhanni/data/BitsAPI.kt | 62 ++++++++++--------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.kt b/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.kt index 3bcb47421253..23867b5ff3fe 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/storage/PlayerSpecificStorage.kt @@ -21,6 +21,9 @@ class PlayerSpecificStorage { @Expose var gardenCommunityUpgrade: Int = -1 + @Expose + var fameRank: String = "New Player" + @Expose var nextCityProjectParticipationTime: SimpleTimeMark = getSimpleTimeMark(farPast()).it diff --git a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt index 40d4db6cce0b..0da264db8ff3 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt @@ -1,5 +1,6 @@ package at.hannibal2.skyhanni.data +import at.hannibal2.skyhanni.api.event.HandleEvent import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator import at.hannibal2.skyhanni.events.BitsAvailableUpdateEvent import at.hannibal2.skyhanni.events.BitsUpdateEvent @@ -12,6 +13,7 @@ import at.hannibal2.skyhanni.utils.CollectionUtils.nextAfter import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NumberUtil.formatInt +import at.hannibal2.skyhanni.utils.RegexUtils.findMatcher import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matches @@ -198,7 +200,7 @@ object BitsAPI { } } - private fun updateBits(amount: Int, modifyAvailable: Boolean = true) { + private fun updateBits(amount: Int) { val diff = amount - bits if (diff == 0) return @@ -295,43 +297,44 @@ object BitsAPI { var foundFameRankStack = false var foundBitsStack = false var foundCookieStack = false - items@ for (item in stacks.values.reversed()) { + items@ for (item in stacks.values.toList().asReversed()) { if (foundFameRankStack && foundBitsStack && foundCookieStack) return if (!foundFameRankStack && fameRankGuiStackPattern.matches(item.displayName)) { foundFameRankStack = true lore@ for (line in item.getLore()) { for (pattern in listOf(fameRankCommunityShopPattern, fameRankSbMenuPattern)) { - pattern.matchMatcher(line) { - val rank = group("rank") - - currentFameRank = getFameRankByNameOrNull(rank) - ?: return ErrorManager.logErrorWithData( - FameRankNotFoundException(rank), - "FameRank $rank not found", - "Rank" to rank, - "Lore" to item.getLore(), - "FameRanks" to FameRanks.fameRanks, - ) - - continue@lore - } + pattern.matchMatcher(line) { + val rank = group("rank") + + fameRank = FameRanks.getByName(rank) + ?: return ErrorManager.logErrorWithData( + FameRankNotFoundException(rank), + "FameRank $rank not found", + "Rank" to rank, + "Lore" to item.getLore(), + "FameRanks" to FameRanks.fameRanksMap, + ) + + continue@lore + } - fameRankSbMenuPattern.matchMatcher(line) { - val rank = group("rank") + fameRankSbMenuPattern.matchMatcher(line) { + val rank = group("rank") - currentFameRank = getFameRankByNameOrNull(rank) - ?: return ErrorManager.logErrorWithData( - FameRankNotFoundException(rank), - "FameRank $rank not found", - "Rank" to rank, - "Lore" to item.getLore(), - "FameRanks" to FameRanks.fameRanks, - ) + fameRank = FameRanks.getByName(rank) + ?: return ErrorManager.logErrorWithData( + FameRankNotFoundException(rank), + "FameRank $rank not found", + "Rank" to rank, + "Lore" to item.getLore(), + "FameRanks" to FameRanks.fameRanksMap, + ) - continue@lore + continue@lore + } } + continue@items } - continue@items } if (!foundBitsStack && bitsStackPattern.matches(item.displayName)) { foundBitsStack = true @@ -342,7 +345,7 @@ object BitsAPI { if (!foundBits) bitsPurseMenuPattern.findMatcher(line) { foundBits = true val amount = group("amount").formatInt() - updateBits(amount, false) + updateBits(amount) continue@lore } @@ -388,7 +391,6 @@ object BitsAPI { private fun sendBitsSpentEvent(difference: Int) = BitsUpdateEvent.BitsSpent(bits, bitsAvailable, difference).post() - private fun sendBitsAvailableGainedEvent() = BitsAvailableUpdateEvent(bitsAvailable).post() fun isEnabled() = LorenzUtils.inSkyBlock && !LorenzUtils.isOnAlphaServer && profileStorage != null