diff options
author | Empa <42304516+ItsEmpa@users.noreply.github.com> | 2024-08-09 10:32:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-09 10:32:00 +0200 |
commit | 8e4bb1b3c3ef8d7bf9553e56d12738d290f0cb2b (patch) | |
tree | 26d25779b944e5235a3748ee907d5e0652f498fe /src/main/java/at/hannibal2 | |
parent | 9ef75ce1c01beefbcd075d9c8ebc2b50ddaf727b (diff) | |
download | skyhanni-8e4bb1b3c3ef8d7bf9553e56d12738d290f0cb2b.tar.gz skyhanni-8e4bb1b3c3ef8d7bf9553e56d12738d290f0cb2b.tar.bz2 skyhanni-8e4bb1b3c3ef8d7bf9553e56d12738d290f0cb2b.zip |
Fix: Ore Mined Event (#2307)
Co-authored-by: ItsEmpa <itsempa@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt index be36bc11b..616b618b4 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MiningAPI.kt @@ -20,13 +20,15 @@ import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.inAnyIsland import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland import at.hannibal2.skyhanni.utils.LorenzVec -import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst +import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.TimeUtils.format import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern +import io.netty.util.internal.ConcurrentSet import net.minecraft.init.Blocks import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.concurrent.ConcurrentLinkedQueue import kotlin.math.absoluteValue import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds @@ -37,14 +39,10 @@ object MiningAPI { private val group = RepoPattern.group("data.miningapi") private val glaciteAreaPattern by group.pattern("area.glacite", "Glacite Tunnels|Glacite Lake") private val dwarvenBaseCampPattern by group.pattern("area.basecamp", "Dwarven Base Camp") - val coldReset by group.pattern( + private val coldReset by group.pattern( "cold.reset", "§6The warmth of the campfire reduced your §r§b❄ Cold §r§6to §r§a0§r§6!|§c ☠ §r§7You froze to death§r§7.", ) - private val coldResetDeath by group.pattern( - "cold.deathreset", - "§c ☠ §r§7§r§.(?<name>.+)§r§7 (?<reason>.+)", - ) private data class MinedBlock(val ore: OreBlock, var confirmed: Boolean, val time: SimpleTimeMark = SimpleTimeMark.now()) @@ -68,8 +66,8 @@ object MiningAPI { private var lastSkyblockArea: String? = null - private var recentClickedBlocks = mutableMapOf<LorenzVec, MinedBlock>() - private var surroundingMinedBlocks = mutableMapOf<LorenzVec, MinedBlock>() + private val recentClickedBlocks = ConcurrentSet<Pair<LorenzVec, SimpleTimeMark>>() + private val surroundingMinedBlocks = ConcurrentLinkedQueue<Pair<MinedBlock, LorenzVec>>() private val allowedSoundNames = listOf("dig.glass", "dig.stone", "dig.gravel", "dig.cloth", "random.orb") var cold: Int = 0 @@ -103,7 +101,7 @@ object MiningAPI { @SubscribeEvent fun onScoreboardChange(event: ScoreboardUpdateEvent) { - val newCold = event.scoreboard.matchFirst(ScoreboardPattern.coldPattern) { + val newCold = ScoreboardPattern.coldPattern.firstMatcher(event.scoreboard) { group("cold").toInt().absoluteValue } ?: return @@ -116,10 +114,8 @@ object MiningAPI { fun onBlockClick(event: BlockClickEvent) { if (!inCustomMiningIsland()) return if (event.clickType != ClickType.LEFT_CLICK) return - val position = event.position - val blockState = event.getBlockState - val ore = OreBlock.getByStateOrNull(blockState) ?: return - recentClickedBlocks[position] = MinedBlock(ore, false) + if (OreBlock.getByStateOrNull(event.getBlockState) == null) return + recentClickedBlocks += event.position to SimpleTimeMark.now() } @SubscribeEvent @@ -146,7 +142,7 @@ object MiningAPI { if (waitingForInitSound) { if (event.soundName != "random.orb" && event.pitch == 0.7936508f) { val pos = event.location.roundLocationToBlock() - if (pos !in recentClickedBlocks) return + if (recentClickedBlocks.none { it.first == pos }) return waitingForInitSound = false waitingForInitBlock = true waitingForInitBlockPos = event.location.roundLocationToBlock() @@ -155,8 +151,7 @@ object MiningAPI { return } if (waitingForEffMinerSound) { - if (surroundingMinedBlocks.isEmpty()) return - val lastBlock = surroundingMinedBlocks.values.minByOrNull { it.time.passedSince() } ?: return + val lastBlock = surroundingMinedBlocks.lastOrNull()?.first ?: return if (lastBlock.confirmed) return waitingForEffMinerSound = false lastBlock.confirmed = true @@ -169,23 +164,24 @@ object MiningAPI { if (!inCustomMiningIsland()) return if (event.newState.block != Blocks.air) return if (event.oldState.block == Blocks.air) return - if (event.location.distanceToPlayer() > 7) return + val pos = event.location + if (pos.distanceToPlayer() > 7) return if (lastInitSound.passedSince() > 100.milliseconds) return val ore = OreBlock.getByStateOrNull(event.oldState) ?: return if (waitingForInitBlock) { - if (waitingForInitBlockPos != event.location) return + if (waitingForInitBlockPos != pos) return waitingForInitBlock = false - surroundingMinedBlocks[event.location] = MinedBlock(ore, true) + surroundingMinedBlocks += MinedBlock(ore, true) to pos waitingForEffMinerBlock = true return } if (waitingForEffMinerBlock) { - if (event.location in surroundingMinedBlocks) return + if (surroundingMinedBlocks.any { it.second == pos }) return waitingForEffMinerBlock = false - surroundingMinedBlocks[event.location] = MinedBlock(ore, false) + surroundingMinedBlocks += MinedBlock(ore, false) to pos waitingForEffMinerSound = true return } @@ -201,8 +197,8 @@ object MiningAPI { if (currentAreaOreBlocks.isEmpty()) return // if somehow you take more than 20 seconds to mine a single block, congrats - recentClickedBlocks = recentClickedBlocks.filter { it.value.time.passedSince() <= 20.seconds }.toMutableMap() - surroundingMinedBlocks = surroundingMinedBlocks.filter { it.value.time.passedSince() <= 20.seconds }.toMutableMap() + recentClickedBlocks.removeIf { it.second.passedSince() >= 20.seconds } + surroundingMinedBlocks.removeIf { it.first.time.passedSince() >= 20.seconds } if (waitingForInitSound) return if (lastInitSound.passedSince() < 200.milliseconds) return @@ -211,19 +207,18 @@ object MiningAPI { if (surroundingMinedBlocks.isEmpty()) return - val originalBlock = surroundingMinedBlocks.maxByOrNull { it.value.time.passedSince() }?.takeIf { it.value.confirmed }?.value - ?: run { - surroundingMinedBlocks.clear() - recentClickedBlocks.clear() - return - } + val originalBlock = surroundingMinedBlocks.firstOrNull { it.first.confirmed }?.first ?: run { + surroundingMinedBlocks.clear() + recentClickedBlocks.clear() + return + } - val extraBlocks = surroundingMinedBlocks.values.filter { it.confirmed }.countBy { it.ore } + val extraBlocks = surroundingMinedBlocks.filter { it.first.confirmed }.countBy { it.first.ore } OreMinedEvent(originalBlock.ore, extraBlocks).post() surroundingMinedBlocks.clear() - recentClickedBlocks = recentClickedBlocks.filter { it.value.time.passedSince() < originalBlock.time.passedSince() }.toMutableMap() + recentClickedBlocks.removeIf { it.second.passedSince() >= originalBlock.time.passedSince() } } @SubscribeEvent @@ -264,7 +259,7 @@ object MiningAPI { add("waitingForInitBlockPos: $waitingForInitBlockPos") add("waitingForEffMinerSound: $waitingForEffMinerSound") add("waitingForEffMinerBlock: $waitingForEffMinerBlock") - add("recentClickedBlocks: ${recentClickedBlocks.entries.joinToString { it.key.toCleanString() }}") + add("recentlyClickedBlocks: ${recentClickedBlocks.joinToString { "(${it.first.toCleanString()}" }}") } } |