From 9742360a3c1c9435692fb36febb565130e75e65e Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 5 Nov 2023 11:16:59 +0100 Subject: code cleanup and created ItemStack.isBait() --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 4 +- .../skyhanni/config/features/FishingConfig.java | 2 +- .../skyhanni/features/fishing/BaitChangeWarning.kt | 89 ---------------------- .../skyhanni/features/fishing/FishingAPI.kt | 8 ++ .../features/fishing/FishingBaitWarnings.kt | 85 +++++++++++++++++++++ .../features/fishing/ShowFishingItemName.kt | 5 +- 6 files changed, 98 insertions(+), 95 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/features/fishing/BaitChangeWarning.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt (limited to 'src/main/java/at') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index 02d196cc3..78828fe8e 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -105,8 +105,8 @@ import at.hannibal2.skyhanni.features.event.jerry.frozentreasure.FrozenTreasureT import at.hannibal2.skyhanni.features.event.spook.TheGreatSpook import at.hannibal2.skyhanni.features.fame.AccountUpgradeReminder import at.hannibal2.skyhanni.features.fame.CityProjectFeatures -import at.hannibal2.skyhanni.features.fishing.BaitChangeWarning import at.hannibal2.skyhanni.features.fishing.ChumBucketHider +import at.hannibal2.skyhanni.features.fishing.FishingBaitWarnings import at.hannibal2.skyhanni.features.fishing.FishingHookDisplay import at.hannibal2.skyhanni.features.fishing.FishingTimer import at.hannibal2.skyhanni.features.fishing.SeaCreatureFeatures @@ -622,7 +622,7 @@ class SkyHanniMod { loadModule(LockMouseLook) loadModule(DungeonFinderFeatures()) loadModule(PabloHelper()) - loadModule(BaitChangeWarning()); + loadModule(FishingBaitWarnings()); init() diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/FishingConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/FishingConfig.java index 235b606f8..11a21d083 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/FishingConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/FishingConfig.java @@ -236,7 +236,7 @@ public class FishingConfig { @Expose @ConfigOption(name = "Bait Warnings", desc = "") @Accordion - public FishingBaitWarningsConfig fishingBaitWarning = new FishingBaitWarningsConfig(); + public FishingBaitWarningsConfig fishingBaitWarnings = new FishingBaitWarningsConfig(); public static class FishingBaitWarningsConfig { @Expose diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/BaitChangeWarning.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/BaitChangeWarning.kt deleted file mode 100644 index 3f0487639..000000000 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/BaitChangeWarning.kt +++ /dev/null @@ -1,89 +0,0 @@ -package at.hannibal2.skyhanni.features.fishing - -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent -import at.hannibal2.skyhanni.events.LorenzTickEvent -import at.hannibal2.skyhanni.utils.EntityUtils -import at.hannibal2.skyhanni.utils.ItemUtils.name -import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.SoundUtils -import at.hannibal2.skyhanni.utils.StringUtils.removeColor -import at.hannibal2.skyhanni.utils.getLorenzVec -import at.hannibal2.skyhanni.utils.toLorenzVec -import net.minecraft.client.Minecraft -import net.minecraft.entity.item.EntityItem -import net.minecraft.entity.projectile.EntityFishHook -import net.minecraftforge.event.entity.EntityJoinWorldEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import kotlin.time.Duration.Companion.seconds - -class BaitChangeWarning { - private val config get() = SkyHanniMod.feature.fishing.fishingBaitWarning - private var bobber: EntityFishHook? = null - private var lastBait: String? = null - private var timeLastCast: Long = 0L - private var isUsingBait: Boolean = false - - @SubscribeEvent - fun onJoinWorld(event: EntityJoinWorldEvent){ - if(!isEnabled()) return - val entity = event.entity ?: return - if(entity !is EntityFishHook) return - if(entity.angler != Minecraft.getMinecraft().thePlayer) return - - bobber = entity; - timeLastCast = System.currentTimeMillis() - isUsingBait = false - } - - @SubscribeEvent - fun onTick(event: LorenzTickEvent){ - if(!isEnabled() || bobber == null) return - //Is there a way to get event sent time to be more accurate? - if(System.currentTimeMillis() - timeLastCast < 1000L) return - - if(!isUsingBait && config.noBaitWarning) showNoBaitWarning() - reset() - } - - fun reset(){ - bobber = null - isUsingBait = false - } - - @SubscribeEvent - fun onRenderWorld(event: LorenzRenderWorldEvent){ - if(!isEnabled() || !config.baitChangeWarning) return - if(bobber == null) return - for(entityItem in EntityUtils.getEntitiesNearby(bobber!!.getLorenzVec(), 1.5)){ - val itemStack = entityItem.entityItem - var name = itemStack.name ?: continue - name = name.removeColor() - - if((!name.endsWith(" Bait") && !name.startsWith("Obfuscated")) - || itemStack.stackSize != 1) continue - - isUsingBait = true - if(lastBait == null){ - lastBait = name.removeColor() - continue - } - if(name.removeColor() == lastBait) continue - showBaitChangeWarning(lastBait!!, name.removeColor()) - lastBait = name.removeColor() - } - } - - fun showBaitChangeWarning(before: String, after: String){ - SoundUtils.playClickSound() - LorenzUtils.sendTitle("§eBait changed!", 2.seconds) - LorenzUtils.chat("§e" + before + " -> " + after) - } - - fun showNoBaitWarning(){ - SoundUtils.playErrorSound() - LorenzUtils.sendTitle("§cNo bait is used!", 2.seconds) - } - - private fun isEnabled() = LorenzUtils.inSkyBlock && FishingAPI.hasFishingRodInHand() -} 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 bfe3cb5bb..ccadaec94 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingAPI.kt @@ -1,7 +1,15 @@ package at.hannibal2.skyhanni.features.fishing import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import net.minecraft.item.ItemStack object FishingAPI { fun hasFishingRodInHand() = InventoryUtils.itemInHandId.asString().contains("ROD") + + fun ItemStack.isBait(): Boolean { + val name = name ?: return false + return stackSize == 1 && (name.removeColor().startsWith("Obfuscated") || name.endsWith(" Bait")) + } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt new file mode 100644 index 000000000..3be74ea8a --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/FishingBaitWarnings.kt @@ -0,0 +1,85 @@ +package at.hannibal2.skyhanni.features.fishing + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent +import at.hannibal2.skyhanni.events.LorenzTickEvent +import at.hannibal2.skyhanni.features.fishing.FishingAPI.isBait +import at.hannibal2.skyhanni.utils.EntityUtils +import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.SoundUtils +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.getLorenzVec +import net.minecraft.client.Minecraft +import net.minecraft.entity.item.EntityItem +import net.minecraft.entity.projectile.EntityFishHook +import net.minecraftforge.event.entity.EntityJoinWorldEvent +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.seconds + +class FishingBaitWarnings { + private val config get() = SkyHanniMod.feature.fishing.fishingBaitWarnings + private var bobber: EntityFishHook? = null + private var lastBait: String? = null + private var timeLastCast = SimpleTimeMark.farPast() + private var isUsingBait: Boolean = false + + @SubscribeEvent + fun onJoinWorld(event: EntityJoinWorldEvent) { + if (!isEnabled()) return + val entity = event.entity ?: return + if (entity !is EntityFishHook) return + if (entity.angler != Minecraft.getMinecraft().thePlayer) return + + bobber = entity + timeLastCast = SimpleTimeMark.now() + isUsingBait = false + } + + @SubscribeEvent + fun onTick(event: LorenzTickEvent) { + if (!isEnabled() || bobber == null) return + //Is there a way to get event sent time to be more accurate? + if (timeLastCast.passedSince() < 1.seconds) return + + if (!isUsingBait && config.noBaitWarning) showNoBaitWarning() + reset() + } + + fun reset() { + bobber = null + isUsingBait = false + } + + @SubscribeEvent + fun onRenderWorld(event: LorenzRenderWorldEvent) { + if (!isEnabled() || !config.baitChangeWarning) return + val bobber = bobber ?: return + for (entityItem in EntityUtils.getEntitiesNearby(bobber.getLorenzVec(), 1.5)) { + val itemStack = entityItem.entityItem + if (!itemStack.isBait()) continue + val name = itemStack.name?.removeColor() ?: continue + + isUsingBait = true + lastBait?.let { + if (name == it) continue + showBaitChangeWarning(it, name) + } + lastBait = name + } + } + + private fun showBaitChangeWarning(before: String, after: String) { + SoundUtils.playClickSound() + LorenzUtils.sendTitle("§eBait changed!", 2.seconds) + LorenzUtils.chat("§e[SkyHanni] Fishing Bait change detected: $before -> $after") + } + + private fun showNoBaitWarning() { + SoundUtils.playErrorSound() + LorenzUtils.sendTitle("§cNo bait is used!", 2.seconds) + } + + private fun isEnabled() = LorenzUtils.inSkyBlock && FishingAPI.hasFishingRodInHand() +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt b/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt index 99fcca5e4..a64d3da77 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/fishing/ShowFishingItemName.kt @@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.features.fishing import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent import at.hannibal2.skyhanni.events.LorenzTickEvent +import at.hannibal2.skyhanni.features.fishing.FishingAPI.isBait import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.InventoryUtils import at.hannibal2.skyhanni.utils.ItemUtils.name @@ -55,9 +56,7 @@ class ShowFishingItemName { if (name.removeColor() == "Stone") continue val size = itemStack.stackSize - val isBait = (name.removeColor().startsWith("Obfuscated") - || name.endsWith(" Bait")) && size == 1 - val prefix = if (!isBait) { + val prefix = if (!itemStack.isBait()) { "§a§l+" } else { if (!config.showBaits) continue -- cgit