Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: IllegalStateException from Pest Traps #2956

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LocationUtils.distanceSqToPlayer
import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.toInternalName
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher
Expand All @@ -36,6 +38,7 @@ import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.entity.item.EntityArmorStand
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.input.Keyboard
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -125,6 +128,17 @@ object PestAPI {
"§cThere are not any Pests on your Garden right now! Keep farming!",
)

/**
* REGEX-TEST: §eMouse Trap #1§r
* REGEX-TEST: §eMouse Trap #2§r
* REGEX-TEST: §eMouse Trap #3§r
* REGEX-TEST: §aPest Trap #3§r
*/
private val pestTrapPattern by patternGroup.pattern(
"entity.pesttrap",
"(?:§.)+(?:Pest|Mouse) Trap(?: #\\d+)?(?:§.)+"
)

var gardenJoinTime = SimpleTimeMark.farPast()
var firstScoreboardCheck = false

Expand Down Expand Up @@ -281,6 +295,10 @@ object PestAPI {

fun getNearestInfestedPlot() = getInfestedPlots().minByOrNull { it.middle.distanceSqToPlayer() }

fun isNearPestTrap() = EntityUtils.getAllEntities().filterIsInstance<EntityArmorStand>().any {
it.distanceToPlayer() < 10 && pestTrapPattern.matches(it.displayName.formattedText)
}

private fun removePests(removedPests: Int) {
if (removedPests < 1) return
repeat(removedPests) {
Expand All @@ -289,8 +307,10 @@ object PestAPI {
}

private fun removeNearestPest() {
val plot = getNearestInfestedPlot()
?: ErrorManager.skyHanniError("Can not remove nearest pest: No infested plots detected.")
val plot = getNearestInfestedPlot() ?: run {
if (isNearPestTrap()) return
else ErrorManager.skyHanniError("Can not remove nearest pest: No infested plots detected.")
}

if (!plot.isPestCountInaccurate) plot.pests--
scoreboardPests--
Expand Down
Loading