aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-11-18 14:25:05 +0100
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-11-18 14:25:05 +0100
commitc7a7f70a22f22b80fa9c12619891592e5a83e7dc (patch)
tree8a7c3c32684ea32975df5bf9e137e2b27da0c306
parentd441ad03dc9bdf29c847ee033d3cb1d185d36bc7 (diff)
downloadskyhanni-c7a7f70a22f22b80fa9c12619891592e5a83e7dc.tar.gz
skyhanni-c7a7f70a22f22b80fa9c12619891592e5a83e7dc.tar.bz2
skyhanni-c7a7f70a22f22b80fa9c12619891592e5a83e7dc.zip
Fixed dice counting as slayer drop when rolled.
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt100
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt29
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt3
3 files changed, 95 insertions, 37 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt b/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt
index da505f409..d6bd2d892 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt
@@ -1,59 +1,90 @@
package at.hannibal2.skyhanni.data
import at.hannibal2.skyhanni.events.InventoryCloseEvent
-import at.hannibal2.skyhanni.events.OwnInventoryItemUpdateEvent
+import at.hannibal2.skyhanni.events.LorenzTickEvent
+import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.events.entity.ItemAddInInventoryEvent
import at.hannibal2.skyhanni.features.bazaar.BazaarApi
+import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalNameOrNull
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy
import at.hannibal2.skyhanni.utils.NEUItems
import net.minecraft.item.ItemStack
+import net.minecraft.network.play.server.S0DPacketCollectItem
import net.minecraft.network.play.server.S2FPacketSetSlot
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
-class OwnInventoryData {
+typealias SlotNumber = Int
+typealias ItemName = String
+typealias ItemData = Pair<ItemName, Int>
- private var itemNames = mutableMapOf<Int, String>()
- private var itemAmount = mutableMapOf<Int, Int>()
+class OwnInventoryData {
+ private var items = mapOf<SlotNumber, ItemData>()
+ private var dirty = false
@SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
fun onChatPacket(event: PacketEvent.ReceiveEvent) {
if (!LorenzUtils.inSkyBlock) return
- val packet = event.packet
- if (packet is S2FPacketSetSlot) {
- val windowId = packet.func_149175_c()
- if (windowId == 0) {
- val item = packet.func_149174_e() ?: return
- OwnInventoryItemUpdateEvent(item).postAndCatch()
+ if (event.packet.let { it is S2FPacketSetSlot || it is S0DPacketCollectItem }) {
+ dirty = true
+ }
+ }
+
+ @SubscribeEvent
+ fun onTick(event: LorenzTickEvent) {
+ if (!LorenzUtils.inSkyBlock) return
+ if (items.isEmpty()) {
+ initInventory()
+ }
+
+ if (!dirty) return
+
+ dirty = false
+ for ((slot, itemStack) in InventoryUtils.getItemsInOwnInventoryWithNull().withIndex()) {
+ val old = items[slot]
+ val new = itemStack.itemToPair()
+ if (old != new) {
+ item(slot, new, itemStack)
+ }
+ }
+ }
+
+ private fun initInventory() {
+ items = items.editCopy {
+ for ((slot, itemStack) in InventoryUtils.getItemsInOwnInventoryWithNull().withIndex()) {
+ this[slot] = itemStack.itemToPair()
}
}
- if (packet is S2FPacketSetSlot) {
- val windowId = packet.func_149175_c()
- val item = packet.func_149174_e()
- val slot = packet.func_149173_d()
- if (windowId != 0) return
- val name = item?.name ?: "null"
-
- val oldItem = itemNames.getOrDefault(slot, "null")
- val oldAmount = itemAmount.getOrDefault(slot, 0)
-
- val amount = item?.stackSize ?: 0
- if (name == oldItem) {
- val diff = amount - oldAmount
- if (amount > oldAmount) {
- add(item, diff)
- }
- } else {
- if (name != "null") {
- add(item, amount)
- }
+ }
+
+ @SubscribeEvent
+ fun onWorldChange(event: LorenzWorldChangeEvent) {
+ items = emptyMap()
+ }
+
+ private fun ItemStack?.itemToPair(): ItemData = this?.let { (name ?: "null") to stackSize } ?: Pair("null", 0)
+
+ private fun item(slot: SlotNumber, new: ItemData, itemStack: ItemStack?) {
+ val (oldItem, oldAmount) = items[slot] ?: Pair("null", 0)
+ val (name, amount) = new
+
+ if (name == oldItem) {
+ val diff = amount - oldAmount
+ if (amount > oldAmount) {
+ add(itemStack, diff)
}
- itemNames[slot] = name
- itemAmount[slot] = amount
+ } else {
+ if (name != "null") {
+ add(itemStack!!, amount)
+ }
+ }
+ items = items.editCopy {
+ this[slot] = new
}
}
@@ -65,9 +96,8 @@ class OwnInventoryData {
private var lastClose = 0L
- private fun add(item: ItemStack?, add: Int) {
- if (item == null) return
-
+ private fun add(item_: ItemStack?, add: Int) {
+ val item = item_ ?: return
val diffClose = System.currentTimeMillis() - lastClose
if (diffClose < 500) return
diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt
index 7dbd4a7c3..0b31117a8 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerProfitTracker.kt
@@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.Storage
import at.hannibal2.skyhanni.data.SlayerAPI
import at.hannibal2.skyhanni.events.GuiRenderEvent
+import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.PurseChangeCause
import at.hannibal2.skyhanni.events.PurseChangeEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
@@ -20,11 +21,14 @@ import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.LorenzUtils.addSelector
import at.hannibal2.skyhanni.utils.LorenzUtils.sortedDesc
import at.hannibal2.skyhanni.utils.NEUInternalName
+import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getNpcPriceOrNull
import at.hannibal2.skyhanni.utils.NEUItems.getPriceOrNull
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
+import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils
+import at.hannibal2.skyhanni.utils.StringUtils.matches
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.jsonobjects.SlayerProfitTrackerItemsJson
import at.hannibal2.skyhanni.utils.renderables.Renderable
@@ -32,11 +36,18 @@ import at.hannibal2.skyhanni.utils.tracker.SkyHanniTracker
import at.hannibal2.skyhanni.utils.tracker.TrackerData
import com.google.gson.annotations.Expose
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
object SlayerProfitTracker {
private val config get() = SkyHanniMod.feature.slayer.itemProfitTracker
+ private val diceRollChatPattern =
+ "§eYour §r§(5|6High Class )Archfiend Dice §r§erolled a §r§.(?<number>.)§r§e! Bonus: §r§.(?<hearts>.*)❤".toPattern()
+
+ private val ARCHFIEND_DICE = "ARCHFIEND_DICE".asInternalName()
+ private val HIGH_CLASS_ARCHFIEND_DICE = "HIGH_CLASS_ARCHFIEND_DICE".asInternalName()
+
private var itemLogCategory = ""
private var baseSlayerType = ""
private val logger = LorenzLogger("slayer/profit_tracker")
@@ -183,7 +194,23 @@ object SlayerProfitTracker {
if (!SlayerAPI.isInCorrectArea) return
if (!SlayerAPI.hasActiveSlayerQuest()) return
- addItem(event.internalName, event.amount)
+ val internalName = event.internalName
+ if (internalName == ARCHFIEND_DICE || internalName == HIGH_CLASS_ARCHFIEND_DICE) {
+ if (lastDiceRoll.passedSince() < 500.milliseconds) {
+ return
+ }
+ }
+
+ addItem(internalName, event.amount)
+ }
+
+ private var lastDiceRoll = SimpleTimeMark.farPast()
+
+ @SubscribeEvent
+ fun onChat(event: LorenzChatEvent) {
+ if (diceRollChatPattern.matches(event.message)) {
+ lastDiceRoll = SimpleTimeMark.now()
+ }
}
private fun addItem(internalName: NEUInternalName, amount: Int) {
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt
index 4c60444c1..eb06c23ee 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt
@@ -40,6 +40,7 @@ object InventoryUtils {
fun ContainerChest.getInventoryName() = this.lowerChestInventory.displayName.unformattedText.trim()
fun getItemsInOwnInventory() = Minecraft.getMinecraft().thePlayer.inventory.mainInventory.filterNotNull()
+ fun getItemsInOwnInventoryWithNull() = Minecraft.getMinecraft().thePlayer.inventory.mainInventory
fun countItemsInLowerInventory(predicate: (ItemStack) -> Boolean) =
getItemsInOwnInventory().filter { predicate(it) }.sumOf { it.stackSize }
@@ -68,4 +69,4 @@ object InventoryUtils {
false
}
}
-} \ No newline at end of file
+}