aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt61
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt110
2 files changed, 171 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt
new file mode 100644
index 000000000..750e23794
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/data/PurseAPI.kt
@@ -0,0 +1,61 @@
+package at.hannibal2.skyhanni.data
+
+import at.hannibal2.skyhanni.events.InventoryCloseEvent
+import at.hannibal2.skyhanni.events.PurseChangeCause
+import at.hannibal2.skyhanni.events.PurseChangeEvent
+import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
+import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
+import net.minecraft.client.Minecraft
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent
+
+class PurseAPI {
+ private val pattern = "(Piggy|Purse): §6(?<coins>[\\d,]*).*".toPattern()
+ private var currentPurse = 0.0
+ private var inventoryCloseTime = 0L
+
+ @SubscribeEvent
+ fun onInventoryClose(event: InventoryCloseEvent) {
+ inventoryCloseTime = System.currentTimeMillis()
+ }
+
+ @SubscribeEvent
+ fun onTick(event: TickEvent.ClientTickEvent) {
+ if (event.phase != TickEvent.Phase.START) return
+
+ for (line in ScoreboardData.sidebarLinesFormatted) {
+ val newPurse = pattern.matchMatcher(line) {
+ group("coins").formatNumber().toDouble()
+ } ?: continue
+ val diff = newPurse - currentPurse
+ if (diff == 0.0) continue
+ currentPurse = newPurse
+
+ PurseChangeEvent(diff, getCause(diff)).postAndCatch()
+ }
+ }
+
+ // TODO add more causes in the future (e.g. ah/bz/bank)
+ private fun getCause(diff: Double): PurseChangeCause {
+ if (diff > 0) {
+ if (diff == 1.0) {
+ return PurseChangeCause.GAIN_TALISMAN_OF_COINS
+ }
+ if (Minecraft.getMinecraft().currentScreen == null) {
+ val timeDiff = System.currentTimeMillis() - inventoryCloseTime
+ if (timeDiff > 2_000) {
+ return PurseChangeCause.GAIN_MOB_KILL
+ }
+
+ }
+ return PurseChangeCause.GAIN_UNKNOWN
+ } else {
+ val timeDiff = System.currentTimeMillis() - SlayerAPI.questStartTime
+ if (timeDiff < 1500) {
+ return PurseChangeCause.LOSE_SLAYER_QUEST_STARTED
+ }
+
+ return PurseChangeCause.LOSE_UNKNOWN
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
new file mode 100644
index 000000000..6b4adc54c
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
@@ -0,0 +1,110 @@
+package at.hannibal2.skyhanni.data
+
+import at.hannibal2.skyhanni.events.LorenzChatEvent
+import at.hannibal2.skyhanni.events.SlayerChangeEvent
+import at.hannibal2.skyhanni.features.bazaar.BazaarApi
+import at.hannibal2.skyhanni.features.slayer.SlayerType
+import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
+import at.hannibal2.skyhanni.utils.ItemUtils.nameWithEnchantment
+import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.LorenzUtils.nextAfter
+import at.hannibal2.skyhanni.utils.NEUItems
+import at.hannibal2.skyhanni.utils.NumberUtil
+import at.hannibal2.skyhanni.utils.StringUtils.removeColor
+import com.google.common.cache.CacheBuilder
+import net.minecraft.item.ItemStack
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent
+import java.util.concurrent.TimeUnit
+
+object SlayerAPI {
+
+ var tick = 0
+
+ private var nameCache =
+ CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build<Pair<String, Int>, Pair<String, Double>>()
+
+ var questStartTime = 0L
+ var isInSlayerArea = false
+ private var latestSlayerCategory = ""
+ private var latestProgressChangeTime = 0L
+ private var latestSlayerProgress = ""
+
+ fun hasActiveSlayerQuest() = latestSlayerCategory != ""
+
+ fun getLatestProgressChangeTime() = if (latestSlayerProgress == "§eSlay the boss!") {
+ System.currentTimeMillis()
+ } else latestProgressChangeTime
+
+
+ // TODO use repo
+ fun ignoreSlayerDrop(name: String) = when (name.removeColor()) {
+ // maybe everywhere?
+ "Stone" -> true
+ "Head" -> true
+
+ // Spider
+ "Cobweb" -> true
+ "String" -> true
+ "Spider Eye" -> true
+ "Bone" -> true
+
+ // Blaze
+ "Water Bottle" -> true
+
+ else -> false
+ }
+
+ fun getItemNameAndPrice(stack: ItemStack): Pair<String, Double> {
+ val internalName = stack.getInternalName()
+ val amount = stack.stackSize
+ val key = internalName to amount
+ nameCache.getIfPresent(key)?.let {
+ return it
+ }
+
+ val amountFormat = if (amount != 1) "§7${amount}x §r" else ""
+ val displayName = NEUItems.getItemStack(internalName).nameWithEnchantment
+
+ val price = NEUItems.getPrice(internalName)
+ val npcPrice = BazaarApi.getBazaarDataByInternalName(internalName)?.npcPrice ?: 0.0
+ val maxPrice = npcPrice.coerceAtLeast(price)
+ val totalPrice = maxPrice * amount
+
+ val format = NumberUtil.format(totalPrice)
+ val priceFormat = " §7(§6$format coins§7)"
+
+ val result = "$amountFormat$displayName$priceFormat" to totalPrice
+ nameCache.put(key, result)
+ return result
+ }
+
+ @SubscribeEvent
+ fun onChat(event: LorenzChatEvent) {
+ if (event.message.contains("§r§5§lSLAYER QUEST STARTED!")) {
+ questStartTime = System.currentTimeMillis()
+ }
+ }
+
+ @SubscribeEvent
+ fun onTick(event: TickEvent.ClientTickEvent) {
+ if (event.phase != TickEvent.Phase.START) return
+ if (!LorenzUtils.inSkyBlock) return
+
+ val slayerQuest = ScoreboardData.sidebarLinesFormatted.nextAfter("Slayer Quest") ?: ""
+ if (slayerQuest != latestSlayerCategory) {
+ SlayerChangeEvent(latestSlayerCategory, slayerQuest).postAndCatch()
+ latestSlayerCategory = slayerQuest
+ }
+
+ val slayerProgress = ScoreboardData.sidebarLinesFormatted.nextAfter("Slayer Quest", 2) ?: ""
+ if (latestSlayerProgress != slayerProgress) {
+ latestSlayerProgress = slayerProgress
+ latestProgressChangeTime = System.currentTimeMillis()
+ }
+
+ if (tick++ % 5 == 0) {
+ isInSlayerArea = SlayerType.getByArea(LorenzUtils.skyBlockArea) != null
+ }
+ }
+} \ No newline at end of file