diff options
author | Empa <42304516+ItsEmpa@users.noreply.github.com> | 2024-06-22 09:00:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-22 09:00:20 +0200 |
commit | 6b7c9d2455be319321d237083cbe2648246ee572 (patch) | |
tree | fd6e60b5d8a0d33b744a464b635953012ef7ea04 /src/main/java/at/hannibal2 | |
parent | 4c1f34ff39c9ab59991648cc1a7c771ef7b1d8a2 (diff) | |
download | skyhanni-6b7c9d2455be319321d237083cbe2648246ee572.tar.gz skyhanni-6b7c9d2455be319321d237083cbe2648246ee572.tar.bz2 skyhanni-6b7c9d2455be319321d237083cbe2648246ee572.zip |
Fix Fishing Bait Warning (#2135)
Diffstat (limited to 'src/main/java/at/hannibal2')
4 files changed, 50 insertions, 38 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingAPI.kt index f48309bb0..59b14b66b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingAPI.kt @@ -106,7 +106,7 @@ object FishingAPI { fun NEUInternalName.isWaterRod() = this in waterRods - fun ItemStack.isBait(): Boolean = stackSize == 1 && getItemCategoryOrNull() == ItemCategory.FISHING_BAIT + fun ItemStack.isBait(): Boolean = stackSize == 1 && getItemCategoryOrNull() == ItemCategory.BAIT @SubscribeEvent fun onItemInHandChange(event: ItemInHandChangeEvent) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt index 8f59a5eed..6773f1341 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt @@ -1,18 +1,21 @@ package at.hannibal2.skyhanni.features.fishing import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.events.FishingBobberCastEvent +import at.hannibal2.skyhanni.api.event.HandleEvent import at.hannibal2.skyhanni.events.FishingBobberInWaterEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent +import at.hannibal2.skyhanni.events.entity.EntityEnterWorldEvent import at.hannibal2.skyhanni.features.fishing.FishingAPI.isBait +import at.hannibal2.skyhanni.features.nether.kuudra.KuudraAPI import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.DelayedRun -import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.SoundUtils +import at.hannibal2.skyhanni.utils.TimeLimitedSet import at.hannibal2.skyhanni.utils.getLorenzVec import net.minecraft.entity.item.EntityItem import net.minecraft.entity.projectile.EntityFishHook @@ -25,13 +28,19 @@ object FishingBaitWarnings { private val config get() = SkyHanniMod.feature.fishing.fishingBaitWarnings - @SubscribeEvent - fun onBobberThrow(event: FishingBobberCastEvent) { + private data class Bait( + private val entity: EntityItem, + val name: String = entity.entityItem.name, + val location: LorenzVec = entity.getLorenzVec(), + ) { + fun distanceTo(bobber: EntityFishHook) = location.distance(bobber.getLorenzVec()) } private var lastBait: String? = null private var wasUsingBait = true + private val baitEntities = TimeLimitedSet<Bait>(750.seconds) + @SubscribeEvent fun onWorldChange(event: LorenzWorldChangeEvent) { lastBait = null @@ -40,48 +49,45 @@ object FishingBaitWarnings { @SubscribeEvent fun onBobberInWater(event: FishingBobberInWaterEvent) { + if (!isEnabled()) return DelayedRun.runDelayed(500.milliseconds) { - checkBobber() + checkBait() } } - private fun checkBobber() { + @HandleEvent(onlyOnSkyblock = true) + fun onEntityEnterWorld(event: EntityEnterWorldEvent<EntityItem>) { + if (!isEnabled() || !FishingAPI.isFishing()) return + if (event.entity.distanceToPlayer() > 10) return + DelayedRun.runNextTick { + val isBait = event.entity.entityItem.isBait() + if (isBait) { + baitEntities += Bait(event.entity) + } + } + } + + private fun checkBait() { val bobber = FishingAPI.bobber ?: return - val bait = detectBait(bobber) + val bait = baitEntities.filter { it.distanceTo(bobber) < 8 }.minByOrNull { it.distanceTo(bobber) }?.name + baitEntities.clear() + if (bait == null) { - if (config.noBaitWarning) { - if (!wasUsingBait) { - showNoBaitWarning() - } + if (config.noBaitWarning && !wasUsingBait) { + showNoBaitWarning() } - } else { - if (config.baitChangeWarning) { - lastBait?.let { - if (it != bait) { - showBaitChangeWarning(it, bait) - } + } else if (config.baitChangeWarning) { + lastBait?.let { + if (it != bait) { + showBaitChangeWarning(it, bait) } } } + wasUsingBait = bait != null lastBait = bait } - private fun detectBait(bobber: EntityFishHook): String? { - for (entity in EntityUtils.getEntitiesNearby<EntityItem>(bobber.getLorenzVec(), 6.0)) { - val itemStack = entity.entityItem ?: continue - if (!itemStack.isBait()) continue - val ticksExisted = entity.ticksExisted - if (ticksExisted in 6..15) { - return itemStack.name - } - - val distance = "distance: ${entity.getDistanceToEntity(bobber).addSeparators()}" - ChatUtils.debug("fishing bait: ticksExisted: $ticksExisted, $distance") - } - return null - } - private fun showBaitChangeWarning(before: String, after: String) { SoundUtils.playClickSound() LorenzUtils.sendTitle("§eBait changed!", 2.seconds) @@ -94,5 +100,5 @@ object FishingBaitWarnings { ChatUtils.chat("You're not using any fishing baits!") } - private fun isEnabled() = LorenzUtils.inSkyBlock && FishingAPI.isFishing() && !LorenzUtils.inKuudraFight + private fun isEnabled() = LorenzUtils.inSkyBlock && !KuudraAPI.inKuudra() } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt index e17389c52..018c858fa 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt @@ -4,10 +4,10 @@ import com.google.common.cache.CacheBuilder import java.util.concurrent.TimeUnit import kotlin.time.Duration -class TimeLimitedCache<K: Any, V: Any>( +class TimeLimitedCache<K : Any, V : Any>( expireAfterWrite: Duration, private val removalListener: (K?, V?) -> Unit = { _, _ -> }, -): Iterable<Map.Entry<K, V>> { +) : Iterable<Map.Entry<K, V>> { private val cache = CacheBuilder.newBuilder() .expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt index c0626fccb..4c4a35151 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt @@ -5,7 +5,7 @@ import kotlin.time.Duration class TimeLimitedSet<T : Any>( expireAfterWrite: Duration, private val removalListener: (T) -> Unit = {}, -) { +) : Iterable<T> { private val cache = TimeLimitedCache<T, Unit>(expireAfterWrite) { key, _ -> key?.let { removalListener(it) } } @@ -13,12 +13,16 @@ class TimeLimitedSet<T : Any>( cache[element] = Unit } + operator fun plusAssign(element: T) = add(element) + fun addIfAbsent(element: T) { if (!contains(element)) add(element) } fun remove(element: T) = cache.remove(element) + operator fun minusAssign(element: T) = remove(element) + operator fun contains(element: T): Boolean = cache.containsKey(element) fun clear() = cache.clear() @@ -26,4 +30,6 @@ class TimeLimitedSet<T : Any>( fun toSet(): Set<T> = cache.keys().let { keys -> if (keys.isEmpty()) emptySet() else keys.toSet() } + + override fun iterator(): Iterator<T> = toSet().iterator() } |