diff options
author | J10a1n15 <45315647+j10a1n15@users.noreply.github.com> | 2024-03-12 19:59:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-12 19:59:55 +0100 |
commit | 4352ffb08d4bfffc06adad2a068f375ab9874333 (patch) | |
tree | b8ce745fca8ab82ffa04f6ab87334139a6b7192b /src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt | |
parent | eb863d60e82b5541a9f42d2608f61cb97ada209b (diff) | |
download | skyhanni-4352ffb08d4bfffc06adad2a068f375ab9874333.tar.gz skyhanni-4352ffb08d4bfffc06adad2a068f375ab9874333.tar.bz2 skyhanni-4352ffb08d4bfffc06adad2a068f375ab9874333.zip |
Feature: CustomScoreboard (#893)
Co-authored-by: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com>
Co-authored-by: Thunderblade73 <gaidermarkus@gmail.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt new file mode 100644 index 000000000..2802399bf --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt @@ -0,0 +1,200 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.FameRanks.getFameRankByNameOrNull +import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.ScoreboardChangeEvent +import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.ChatUtils +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.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.StringUtils.matches +import at.hannibal2.skyhanni.utils.StringUtils.removeResets +import at.hannibal2.skyhanni.utils.StringUtils.trimWhiteSpace +import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +object BitsAPI { + private val profileStorage get() = ProfileStorageData.profileSpecific?.bits + private val playerStorage get() = SkyHanniMod.feature.storage + + var bits: Int + get() = profileStorage?.bits ?: 0 + private set(value) { + profileStorage?.bits = value + } + var currentFameRank: FameRank? + get() = playerStorage?.currentFameRank?.let { getFameRankByNameOrNull(it) } + private set(value) { + if (value != null) { + playerStorage?.currentFameRank = value.name + } + } + var bitsToClaim: Int + get() = profileStorage?.bitsToClaim ?: 0 + private set(value) { + profileStorage?.bitsToClaim = value + } + + private const val defaultcookiebits = 4800 + + private val bitsDataGroup = RepoPattern.group("data.bits") + + // Scoreboard patterns + val bitsScoreboardPattern by bitsDataGroup.pattern( + "scoreboard", + "^Bits: §b(?<amount>[\\d,.]+).*$" + ) + + // Chat patterns + private val bitsChatGroup = bitsDataGroup.group("chat") + + private val bitsFromFameRankUpChatPattern by bitsChatGroup.pattern( + "famerankup", + "§eYou gained §3(?<amount>.*) Bits Available §ecompounded from all your §epreviously eaten §6cookies§e! Click here to open §6cookie menu§e!" + ) + + private val boosterCookieAte by bitsChatGroup.pattern( + "boostercookieate", + "§eYou consumed a §6Booster Cookie§e!.*" + ) + + // GUI patterns + private val bitsGuiGroup = bitsDataGroup.group("gui") + + private val bitsAvailableMenuPattern by bitsGuiGroup.pattern( + "availablemenu", + "§7Bits Available: §b(?<toClaim>[\\d,]+)(§3.+)?" + ) + + private val fameRankSbMenuPattern by bitsGuiGroup.pattern( + "sbmenufamerank", + "§7Your rank: §e(?<rank>.*)" + ) + + private val fameRankCommunityShopPattern by bitsGuiGroup.pattern( + "communityshopfamerank", + "§7Fame Rank: §e(?<rank>.*)" + ) + + private val bitsGuiNamePattern by bitsGuiGroup.pattern( + "mainmenuname", + "^SkyBlock Menu$" + ) + + private val bitsGuiStackPattern by bitsGuiGroup.pattern( + "mainmenustack", + "^§6Booster Cookie$" + ) + + private val fameRankGuiNamePattern by bitsGuiGroup.pattern( + "famerankmenuname", + "^(Community Shop|Booster Cookie)$" + ) + + private val fameRankGuiStackPattern by bitsGuiGroup.pattern( + "famerankmenustack", + "^(§aCommunity Shop|§eFame Rank)$" + ) + + @SubscribeEvent + fun onScoreboardChange(event: ScoreboardChangeEvent) { + if (!isEnabled()) return + for (line in event.newList) { + val message = line.trimWhiteSpace().removeResets() + + bitsScoreboardPattern.matchMatcher(message) { + val amount = group("amount").formatInt() + + if (amount > bits) { + bitsToClaim -= amount - bits + ChatUtils.debug("You have gained §3${amount - bits} Bits §7according to the scoreboard!") + } + bits = amount + + return + } + } + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + if (!isEnabled()) return + val message = event.message.trimWhiteSpace().removeResets() + + bitsFromFameRankUpChatPattern.matchMatcher(message) { + val amount = group("amount").formatInt() + bitsToClaim += amount + + return + } + + boosterCookieAte.matchMatcher(message) { + bitsToClaim += (defaultcookiebits * (currentFameRank?.bitsMultiplier ?: return)).toInt() + + return + } + } + + @SubscribeEvent + fun onInventoryFullyLoaded(event: InventoryFullyOpenedEvent) { + if (!isEnabled()) return + + val stacks = event.inventoryItems + + if (bitsGuiNamePattern.matches(event.inventoryName)) { + val cookieStack = stacks.values.lastOrNull { bitsGuiStackPattern.matches(it.displayName) } ?: return + for (line in cookieStack.getLore()) { + bitsAvailableMenuPattern.matchMatcher(line) { + bitsToClaim = group("toClaim").formatInt() + + return + } + } + return + } + + if (fameRankGuiNamePattern.matches(event.inventoryName)) { + val fameRankStack = stacks.values.lastOrNull { fameRankGuiStackPattern.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 + } + } + } + } + + fun isEnabled() = LorenzUtils.inSkyBlock && profileStorage != null + + class FameRankNotFoundException(rank: String) : Exception("FameRank not found: $rank") +} |