From d9478299376f302773b59bb0961f2282ab1a03dc Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:59:00 +0100 Subject: Fixed the detection of Anita and Jacob visitors. --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 2 + .../java/at/hannibal2/skyhanni/config/Storage.java | 3 + .../garden/visitor/GardenVisitorFeatures.kt | 29 +------ .../features/garden/visitor/NPCVisitorFix.kt | 98 ++++++++++++++++++++++ 4 files changed, 104 insertions(+), 28 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/visitor/NPCVisitorFix.kt (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index 8e10ad1aa..bdca2b391 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -199,6 +199,7 @@ import at.hannibal2.skyhanni.features.garden.visitor.GardenVisitorDropStatistics import at.hannibal2.skyhanni.features.garden.visitor.GardenVisitorFeatures import at.hannibal2.skyhanni.features.garden.visitor.GardenVisitorTimer import at.hannibal2.skyhanni.features.garden.visitor.HighlightVisitorsOutsideOfGarden +import at.hannibal2.skyhanni.features.garden.visitor.NPCVisitorFix import at.hannibal2.skyhanni.features.garden.visitor.VisitorListener import at.hannibal2.skyhanni.features.inventory.AuctionsHighlighter import at.hannibal2.skyhanni.features.inventory.ChestValue @@ -568,6 +569,7 @@ class SkyHanniMod { loadModule(MiscFeatures()) loadModule(SkyMartCopperPrice()) loadModule(GardenVisitorFeatures()) + loadModule(NPCVisitorFix) loadModule(GardenInventoryNumbers()) loadModule(GardenVisitorTimer()) loadModule(MinionXp()) diff --git a/src/main/java/at/hannibal2/skyhanni/config/Storage.java b/src/main/java/at/hannibal2/skyhanni/config/Storage.java index 6e899692c..486ec5369 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/Storage.java +++ b/src/main/java/at/hannibal2/skyhanni/config/Storage.java @@ -314,6 +314,9 @@ public class Storage { @Expose public int lastFarmingWeightLeaderboard = -1; } + + @Expose + public Map npcVisitorLocations = new HashMap<>(); } @Expose diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt index 1a958a7ae..1ab640b2d 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt @@ -489,7 +489,7 @@ class GardenVisitorFeatures { val visitorName = visitor.visitorName val entity = visitor.getEntity() if (entity == null) { - findNametag(visitorName.removeColor())?.let { + NPCVisitorFix.findNametag(visitorName.removeColor())?.let { findEntity(it, visitor) } } @@ -527,33 +527,6 @@ class GardenVisitorFeatures { } } - private fun findNametag(visitorName: String): EntityArmorStand? { - val foundVisitorNameTags = mutableListOf() - for (entity in EntityUtils.getEntities()) { - if (entity.name.removeColor() == visitorName) { - foundVisitorNameTags.add(entity) - } - } - - if (visitorName in listOf("Jacob", "Anita")) { - // Only detect jacob/anita npc if the "wrong" npc got found as well - if (foundVisitorNameTags.size != 2) return null - - for (tag in foundVisitorNameTags.toMutableList()) { - for (entity in EntityUtils.getEntities()) { - if (entity in foundVisitorNameTags) continue - val distance = entity.getLorenzVec().distance(tag.getLorenzVec()) - if (distance < 1.5 && entity.name == "§bSam") { - foundVisitorNameTags.remove(tag) - } - } - } - } - - if (foundVisitorNameTags.size != 1) return null - return foundVisitorNameTags[0] - } - private fun hasItemsInInventory(visitor: VisitorAPI.Visitor): Boolean { var ready = true for ((internalName, required) in visitor.shoppingList) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/NPCVisitorFix.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/NPCVisitorFix.kt new file mode 100644 index 000000000..f42560100 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/NPCVisitorFix.kt @@ -0,0 +1,98 @@ +package at.hannibal2.skyhanni.features.garden.visitor + +import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.events.InventoryOpenEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.garden.visitor.VisitorOpenEvent +import at.hannibal2.skyhanni.utils.DelayedRun +import at.hannibal2.skyhanni.utils.EntityUtils +import at.hannibal2.skyhanni.utils.LocationUtils.distanceTo +import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.getLorenzVec +import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern +import net.minecraft.entity.item.EntityArmorStand +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds + +/** + * Fixing the visitor detection problem with Anita and Jacob, as those two are on the garden twice when visiting. + */ +object NPCVisitorFix { + private val storage get() = ProfileStorageData.profileSpecific?.garden + private val staticVisitors = listOf("Jacob", "Anita") + private val barnSkinChangePattern by RepoPattern.pattern("garden.barn.skin.change", "§aChanging Barn skin to §r.*") + + @SubscribeEvent + fun onInventoryOpen(event: InventoryOpenEvent) { + val name = staticVisitors.firstOrNull { event.inventoryName.contains(it) } ?: return + val nearest = findNametags(name).firstOrNull { it.distanceToPlayer() < 3 } ?: return + DelayedRun.runDelayed(200.milliseconds) { + saveStaticVisitor(name, nearest) + } + } + + private fun saveStaticVisitor(name: String, entity: EntityArmorStand) { + // clicked on the real visitor, ignoring + if (lastVisitorOpen.passedSince() < 1.seconds) return + + val storage = storage ?: return + + val location = entity.getLorenzVec() + storage.npcVisitorLocations[name]?.let { + // alrady stored + if (it.distance(location) < 1) return + } + + storage.npcVisitorLocations[name] = location + LorenzUtils.chat("Saved $name NPC location. Real $name visitors are now getting detected correctly.") + } + + private var lastVisitorOpen = SimpleTimeMark.farPast() + + @SubscribeEvent + fun onVisitorOpen(event: VisitorOpenEvent) { + lastVisitorOpen = SimpleTimeMark.now() + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + barnSkinChangePattern.matchMatcher(event.message) { + storage?.npcVisitorLocations?.clear() + } + } + + fun findNametag(visitorName: String): EntityArmorStand? { + val nametags = findNametags(visitorName) + if (nametags.isEmpty()) return null + + if (visitorName !in staticVisitors) { + return nametags[0] + } + + val staticLocation = storage?.npcVisitorLocations?.get(visitorName) ?: return null + + for (entity in nametags.toMutableList()) { + val distance = entity.distanceTo(staticLocation) + if (distance < 3) { + nametags.remove(entity) + } + } + + return nametags.firstOrNull() + } + + private fun findNametags(visitorName: String): MutableList { + val foundVisitorNameTags = mutableListOf() + for (entity in EntityUtils.getEntities()) { + if (entity.name.removeColor() == visitorName) { + foundVisitorNameTags.add(entity) + } + } + return foundVisitorNameTags + } +} -- cgit