diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2024-03-22 21:41:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 21:41:48 +0100 |
commit | a82b6f8d2567cc68a97128180bc75997da3daf32 (patch) | |
tree | 4c1548910e0f5a6d9e6502679bbe949106daa128 /src/main/java/at/hannibal2 | |
parent | 37ba2ec0f6e0340b29656ec0f43b36f52d27d0d5 (diff) | |
download | skyhanni-a82b6f8d2567cc68a97128180bc75997da3daf32.tar.gz skyhanni-a82b6f8d2567cc68a97128180bc75997da3daf32.tar.bz2 skyhanni-a82b6f8d2567cc68a97128180bc75997da3daf32.zip |
Fix: Lane Detection (#1239)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2')
7 files changed, 65 insertions, 24 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/garden/laneswitch/LaneSwitchSoundSettings.java b/src/main/java/at/hannibal2/skyhanni/config/features/garden/laneswitch/LaneSwitchSoundSettings.java index dd16d0576..b682baef2 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/garden/laneswitch/LaneSwitchSoundSettings.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/garden/laneswitch/LaneSwitchSoundSettings.java @@ -23,6 +23,14 @@ public class LaneSwitchSoundSettings { @ConfigEditorButton(buttonText = "Test") public Runnable testSound = FarmingLaneFeatures::playUserSound; + @Expose + @ConfigOption( + name = "Repeat Duration", + desc = "Change how often the sound should be repeated in ticks. Change to 20 for only once per second." + ) + @ConfigEditorSlider(minValue = 1, maxValue = 20, minStep = 1) + public int repeatDuration = 20; + @ConfigOption(name = "List of Sounds", desc = "A list of available sounds.") @ConfigEditorButton(buttonText = "Open") public Runnable listOfSounds = () -> OSUtils.openBrowser("https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/mapping-and-modding-tutorials/2213619-1-8-all-playsound-sound-arguments"); diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt index 4b27e97f2..8c2546f6c 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt @@ -143,7 +143,12 @@ object GardenAPI { fun inGarden() = IslandType.GARDEN.isInIsland() - fun isCurrentlyFarming() = inGarden() && GardenCropSpeed.averageBlocksPerSecond > 0.0 + fun isCurrentlyFarming() = inGarden() && GardenCropSpeed.averageBlocksPerSecond > 0.0 && hasFarmingToolInHand() + + fun hasFarmingToolInHand() = InventoryUtils.getItemInHand()?.let { + val crop = it.getCropType() + getToolInHand(it, crop) != null + } ?: false fun ItemStack.getCropType(): CropType? { val internalName = getInternalName() diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneAPI.kt index 5df0b1358..a130228dd 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneAPI.kt @@ -17,21 +17,15 @@ object FarmingLaneAPI { val lanes get() = GardenAPI.storage?.farmingLanes var currentLane: FarmingLane? = null private var lastNoLaneWarning = SimpleTimeMark.farPast() - private var lastCrop: CropType? = null @SubscribeEvent fun onGardenToolChange(event: GardenToolChangeEvent) { - handleLaneSwitch(event.crop) + currentLane = null } @SubscribeEvent fun onCropClick(event: CropClickEvent) { - handleLaneSwitch(event.crop) - } - - private fun handleLaneSwitch(crop: CropType?) { - if (crop == lastCrop) return - lastCrop = crop + val crop = event.crop val lanes = lanes ?: return val lane = lanes[crop] @@ -46,6 +40,8 @@ object FarmingLaneAPI { private fun warnNoLane(crop: CropType?) { if (crop == null || currentLane != null) return + if (!GardenAPI.isCurrentlyFarming()) return + if (FarmingLaneCreator.detection) return if (!config.distanceDisplay && !config.laneSwitchNotification.enabled) return if (lastNoLaneWarning.passedSince() < 30.seconds) return diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneCreator.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneCreator.kt index b0c5b8d78..f555d4048 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneCreator.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneCreator.kt @@ -19,7 +19,7 @@ import kotlin.math.min object FarmingLaneCreator { val config get() = FarmingLaneAPI.config - private var detection = false + var detection = false private var start: LorenzVec? = null private var lastLocation: LorenzVec? = null private var potentialEnd: LorenzVec? = null @@ -54,7 +54,7 @@ object FarmingLaneCreator { reset() return } - if (lastLocation.distance(location) < 1) return + if (lastLocation.distance(location) < 0.5) return this.lastLocation = location val start = start ?: error("start can not be null") @@ -67,7 +67,7 @@ object FarmingLaneCreator { potentialEnd = location return } - if (potentialEnd.distance(location) > 5) { + if (potentialEnd.distance(location) > 2) { val crop = crop ?: error("crop can not be null") saveLane(start, potentialEnd, crop) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneFeatures.kt index ccaf83b43..adbd2b9d2 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/lane/FarmingLaneFeatures.kt @@ -1,5 +1,6 @@ package at.hannibal2.skyhanni.features.garden.farming.lane +import at.hannibal2.skyhanni.events.GardenToolChangeEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent import at.hannibal2.skyhanni.events.LorenzTickEvent @@ -7,6 +8,7 @@ import at.hannibal2.skyhanni.events.farming.FarmingLaneSwitchEvent import at.hannibal2.skyhanni.features.garden.GardenAPI import at.hannibal2.skyhanni.features.garden.farming.lane.FarmingLaneAPI.getValue import at.hannibal2.skyhanni.features.garden.farming.lane.FarmingLaneAPI.setValue +import at.hannibal2.skyhanni.features.misc.MovementSpeedDisplay import at.hannibal2.skyhanni.test.GriffinUtils.drawWaypointFilled import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzColor @@ -19,6 +21,7 @@ import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SoundUtils import at.hannibal2.skyhanni.utils.SoundUtils.playSound import at.hannibal2.skyhanni.utils.TimeUtils.format +import at.hannibal2.skyhanni.utils.TimeUtils.ticks import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import kotlin.math.absoluteValue import kotlin.time.Duration @@ -35,6 +38,7 @@ object FarmingLaneFeatures { private var lastSpeed = 0.0 private var validSpeed = false private var lastTimeFarming = SimpleTimeMark.farPast() + private var lastPlaySound = SimpleTimeMark.farPast() private var lastDirection = 0 @SubscribeEvent @@ -43,6 +47,11 @@ object FarmingLaneFeatures { } @SubscribeEvent + fun onGardenToolChange(event: GardenToolChangeEvent) { + display = emptyList() + } + + @SubscribeEvent fun onTick(event: LorenzTickEvent) { if (!GardenAPI.inGarden()) return if (!event.isMod(2)) return @@ -114,13 +123,16 @@ object FarmingLaneFeatures { with(config.laneSwitchNotification) { if (enabled) { LorenzUtils.sendTitle(text.replace("&", "ยง"), 2.seconds) - playUserSound() + if (lastPlaySound.passedSince() >= sound.repeatDuration.ticks) { + lastPlaySound = SimpleTimeMark.now() + playUserSound() + } } } } private fun calculateSpeed(): Boolean { - val speedPerSecond = LocationUtils.distanceFromPreviousTick().round(2) + val speedPerSecond = MovementSpeedDisplay.speedInLastTick.round(2) if (speedPerSecond == 0.0) return false val speedTooSlow = speedPerSecond < 1 if (speedTooSlow) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/MovementSpeedDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/MovementSpeedDisplay.kt index a30635fb5..42308466b 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/MovementSpeedDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/MovementSpeedDisplay.kt @@ -3,20 +3,48 @@ package at.hannibal2.skyhanni.features.misc import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.enums.OutsideSbFeature import at.hannibal2.skyhanni.events.GuiRenderEvent -import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.round +import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.RenderUtils.renderString +import net.minecraft.client.Minecraft import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.concurrent.fixedRateTimer class MovementSpeedDisplay { private val config get() = SkyHanniMod.feature.misc + private var display = "" + + companion object { + var speedInLastTick = 0.0 + } + + init { + fixedRateTimer(name = "skyhanni-movement-speed-display", period = 250, initialDelay = 1_000) { + checkSpeed() + } + } + + private fun checkSpeed() { + if (!isEnabled()) return + + speedInLastTick = with(Minecraft.getMinecraft().thePlayer) { + val oldPos = LorenzVec(prevPosX, prevPosY, prevPosZ) + val newPos = LorenzVec(posX, posY, posZ) + + // Distance from previous tick, multiplied by TPS + oldPos.distance(newPos) * 20 + } + display = "Movement Speed: ${speedInLastTick.round(2)}" + } + @SubscribeEvent fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) { if (!isEnabled()) return - config.playerMovementSpeedPos.renderString("Movement Speed: ${LocationUtils.distanceFromPreviousTick()}", posLabel = "Movement Speed") + config.playerMovementSpeedPos.renderString(display, posLabel = "Movement Speed") } fun isEnabled() = LorenzUtils.onHypixel && diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt index 24b84dd7f..29cc1d569 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt @@ -1,6 +1,5 @@ package at.hannibal2.skyhanni.utils -import at.hannibal2.skyhanni.utils.LorenzUtils.round import net.minecraft.client.Minecraft import net.minecraft.entity.Entity import net.minecraft.util.AxisAlignedBB @@ -12,13 +11,6 @@ object LocationUtils { fun playerLocation() = Minecraft.getMinecraft().thePlayer.getLorenzVec() - fun distanceFromPreviousTick(): Double = with(Minecraft.getMinecraft().thePlayer) { - val oldPos = LorenzVec(prevPosX, prevPosY, prevPosZ) - val newPos = LorenzVec(posX, posY, posZ) - - (oldPos.distance(newPos) * 20).round(2) - } - fun LorenzVec.distanceToPlayer() = distance(playerLocation()) fun LorenzVec.distanceToPlayerIgnoreY() = distanceIgnoreY(playerLocation()) |