From b67dc7ffe67f2da961c88c9516b12ea998fb9570 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Wed, 5 Jul 2023 15:19:50 +0200 Subject: using ParkourHelper in ParkourWaypointSaver --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 2 +- .../features/rift/RiftUpsideDownParkour.kt | 23 ++-- .../skyhanni/test/ParkourWaypointSaver.kt | 81 +++++++++++++ .../at/hannibal2/skyhanni/test/WaypointSaver.kt | 65 ----------- .../at/hannibal2/skyhanni/utils/LorenzUtils.kt | 5 + .../java/at/hannibal2/skyhanni/utils/LorenzVec.kt | 8 +- .../at/hannibal2/skyhanni/utils/ParkourHelper.kt | 127 ++++++++++++++------- 7 files changed, 189 insertions(+), 122 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/test/ParkourWaypointSaver.kt delete mode 100644 src/main/java/at/hannibal2/skyhanni/test/WaypointSaver.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 2513c1860..4dac5f6c7 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -329,7 +329,7 @@ class SkyHanniMod { loadModule(TestBingo) loadModule(TestCopyRngMeterValues) loadModule(HighlightMissingRepoItems()) - loadModule(WaypointSaver()) + loadModule(ParkourWaypointSaver()) } @Mod.EventHandler diff --git a/src/main/java/at/hannibal2/skyhanni/features/rift/RiftUpsideDownParkour.kt b/src/main/java/at/hannibal2/skyhanni/features/rift/RiftUpsideDownParkour.kt index b81018bb5..ba1a16146 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/rift/RiftUpsideDownParkour.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/rift/RiftUpsideDownParkour.kt @@ -14,13 +14,18 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class RiftUpsideDownParkour { private val config get() = SkyHanniMod.feature.rift.mirrorVerse.upsideDownParkour - private var parkourHelper: ParkourHelper? = null @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { val data = event.getConstant("RiftUpsideDownParkour") ?: return - parkourHelper = ParkourHelper(data.locations, data.shortCuts) + parkourHelper = ParkourHelper( + data.locations.map { it.add(-1.0, -1.0, -1.0) },// TODO remove offset. change repo instead + data.shortCuts, + platformSize = 2.0, + detectionRange = 2.0 + ) + updateConfig() } @SubscribeEvent @@ -47,11 +52,15 @@ class RiftUpsideDownParkour { @SubscribeEvent fun onConfigLoad(event: ConfigLoadEvent) { LorenzUtils.onToggle(config.rainbowColor, config.monochromeColor, config.lookAhead) { - parkourHelper?.run { - rainbowColor = config.rainbowColor.get() - monochromeColor = config.monochromeColor.get().toChromaColor() - lookAhead = config.lookAhead.get() + 1 - } + updateConfig() + } + } + + private fun updateConfig() { + parkourHelper?.run { + rainbowColor = config.rainbowColor.get() + monochromeColor = config.monochromeColor.get().toChromaColor() + lookAhead = config.lookAhead.get() + 1 } } diff --git a/src/main/java/at/hannibal2/skyhanni/test/ParkourWaypointSaver.kt b/src/main/java/at/hannibal2/skyhanni/test/ParkourWaypointSaver.kt new file mode 100644 index 000000000..1020a5a9c --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/test/ParkourWaypointSaver.kt @@ -0,0 +1,81 @@ +package at.hannibal2.skyhanni.test + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.utils.* +import at.hannibal2.skyhanni.utils.RenderUtils.drawFilledBoundingBox +import at.hannibal2.skyhanni.utils.RenderUtils.expandBlock +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.inventory.GuiChest +import net.minecraft.client.gui.inventory.GuiEditSign +import net.minecraft.client.gui.inventory.GuiInventory +import net.minecraftforge.client.event.RenderWorldLastEvent +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.minecraftforge.fml.common.gameevent.TickEvent +import org.lwjgl.input.Keyboard + +class ParkourWaypointSaver { + private val config get() = SkyHanniMod.feature.dev.waypoint + private var timeLastSaved: Long = 0 + private var locations = mutableListOf() + private var parkourHelper: ParkourHelper? = null + + @SubscribeEvent + fun onTick(event: TickEvent.ClientTickEvent) { + if (!LorenzUtils.inSkyBlock) return + if (!Keyboard.getEventKeyState()) return + if (NEUItems.neuHasFocus()) return + if (System.currentTimeMillis() - timeLastSaved < 250) return + + Minecraft.getMinecraft().currentScreen?.let { + if (it !is GuiInventory && it !is GuiChest && it !is GuiEditSign) return + } + + val key = if (Keyboard.getEventKey() == 0) Keyboard.getEventCharacter().code + 256 else Keyboard.getEventKey() + if (config.deleteKey == key) { + locations = locations.dropLast(1).toMutableList() + update() + } + if (config.saveKey == key) { + val newLocation = LocationUtils.playerLocation().roundLocation() + if (locations.isNotEmpty()) { + if (newLocation == locations.last()) return + } + locations.add(newLocation) + update() + } + } + + private fun update() { + locations.copyLocations() + parkourHelper = ParkourHelper(locations, emptyList()).also { + it.showEverything = true + it.rainbowColor = true + } + } + + private fun MutableList.copyLocations() { + val resultList = mutableListOf() + timeLastSaved = System.currentTimeMillis() + for (location in this) { + val x = location.x.toString().replace(",", ".") + val y = location.y.toString().replace(",", ".") + val z = location.z.toString().replace(",", ".") + resultList.add("\"$x:$y:$z\"") + } + OSUtils.copyToClipboard(resultList.joinToString((",\n"))) + } + + @SubscribeEvent + fun onRenderWorld(event: RenderWorldLastEvent) { + if (!LorenzUtils.inSkyBlock) return + + if (locations.size > 2) { + parkourHelper?.render(event) + } else { + for (location in locations) { + val aabb = location.boundingToOffset(1.0, 1.0, 1.0).expandBlock() + event.drawFilledBoundingBox(aabb, LorenzColor.GREEN.toColor(), 1f) + } + } + } +} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/test/WaypointSaver.kt b/src/main/java/at/hannibal2/skyhanni/test/WaypointSaver.kt deleted file mode 100644 index cc3ae7e9f..000000000 --- a/src/main/java/at/hannibal2/skyhanni/test/WaypointSaver.kt +++ /dev/null @@ -1,65 +0,0 @@ -package at.hannibal2.skyhanni.test - -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.test.GriffinUtils.drawWaypointFilled -import at.hannibal2.skyhanni.utils.* -import net.minecraft.client.Minecraft -import net.minecraft.client.gui.inventory.GuiChest -import net.minecraft.client.gui.inventory.GuiEditSign -import net.minecraft.client.gui.inventory.GuiInventory -import net.minecraftforge.client.event.RenderWorldLastEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import net.minecraftforge.fml.common.gameevent.TickEvent -import org.lwjgl.input.Keyboard - -class WaypointSaver { - private val config get() = SkyHanniMod.feature.dev.waypoint - private var timeLastSaved: Long = 0 - private var locations = mutableListOf() - - @SubscribeEvent - fun onTick(event: TickEvent.ClientTickEvent) { - if (!LorenzUtils.inSkyBlock) return - if (!Keyboard.getEventKeyState()) return - if (NEUItems.neuHasFocus()) return - if (System.currentTimeMillis() - timeLastSaved < 250) return - - Minecraft.getMinecraft().currentScreen?.let { - if (it !is GuiInventory && it !is GuiChest && it !is GuiEditSign) return - } - - val key = if (Keyboard.getEventKey() == 0) Keyboard.getEventCharacter().code + 256 else Keyboard.getEventKey() - if (config.deleteKey == key) { - locations = locations.dropLast(1).toMutableList() - locations.copyLocations() - } - if (config.saveKey == key) { - val newLocation = LocationUtils.playerLocation().roundLocation() - if (locations.isNotEmpty()) { - if (newLocation == locations.last()) return - } - locations.add(newLocation) - locations.copyLocations() - } - } - - private fun MutableList.copyLocations() { - val resultList = mutableListOf() - timeLastSaved = System.currentTimeMillis() - for (location in this) { - val x = location.x.toString().replace(",", ".") - val y = location.y.toString().replace(",", ".") - val z = location.z.toString().replace(",", ".") - resultList.add("\"$x:$y:$z\"") - } - OSUtils.copyToClipboard(resultList.joinToString((",\n"))) - } - - @SubscribeEvent - fun onRenderWorld(event: RenderWorldLastEvent) { - if (!LorenzUtils.inSkyBlock) return - for (location in locations) { - event.drawWaypointFilled(location, LorenzColor.GREEN.toColor()) - } - } -} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 129cec02e..d0b970187 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -384,4 +384,9 @@ object LorenzUtils { get(index) } else null } + + fun T?.toSingletonListOrEmpty(): List { + if (this == null) return emptyList() + return listOf(this) + } } \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt index 1d1909875..7380c4c37 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt @@ -7,11 +7,7 @@ import net.minecraft.util.AxisAlignedBB import net.minecraft.util.BlockPos import net.minecraft.util.Rotations import net.minecraft.util.Vec3 -import kotlin.math.cos -import kotlin.math.pow -import kotlin.math.round -import kotlin.math.sin -import kotlin.math.sqrt +import kotlin.math.* data class LorenzVec( val x: Double, @@ -107,7 +103,7 @@ data class LorenzVec( return LorenzVec(x, y, z) } - fun boundingToOffset(offX: Int, offY: Int, offZ: Int) = AxisAlignedBB(x, y, z, x + offX, y + offY, z + offZ) + fun boundingToOffset(offX: Double, offY: Double, offZ: Double) = AxisAlignedBB(x, y, z, x + offX, y + offY, z + offZ) fun scale(scalar: Double): LorenzVec { return LorenzVec(scalar * x, scalar * y, scalar * z) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt b/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt index b33f6bc99..081641c9a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ParkourHelper.kt @@ -1,9 +1,12 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.test.command.CopyErrorCommand import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer +import at.hannibal2.skyhanni.utils.LorenzUtils.toSingletonListOrEmpty import at.hannibal2.skyhanni.utils.RenderUtils.draw3DLine import at.hannibal2.skyhanni.utils.RenderUtils.drawDynamicText import at.hannibal2.skyhanni.utils.RenderUtils.drawFilledBoundingBox +import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.expandBlock import at.hannibal2.skyhanni.utils.jsonobjects.ParkourJson import net.minecraft.client.Minecraft @@ -11,13 +14,19 @@ import net.minecraftforge.client.event.RenderWorldLastEvent import java.awt.Color import kotlin.time.Duration.Companion.seconds -class ParkourHelper(val locations: List, private val shortCuts: List) { +class ParkourHelper( + val locations: List, + private val shortCuts: List, + val platformSize: Double = 1.0, + val detectionRange: Double = 1.0 +) { private var current = -1 private var visible = false - var rainbowColor = true + var rainbowColor = false var monochromeColor: Color = Color.WHITE var lookAhead = 2 + var showEverything = false fun inParkour() = current != -1 @@ -27,54 +36,91 @@ class ParkourHelper(val locations: List, private val shortCuts: List< } fun render(event: RenderWorldLastEvent) { - if (current == locations.size - 1) visible = false - - val distanceToPlayer = locations.first().distanceToPlayer() - if (distanceToPlayer < 2) { - visible = true - } else if (distanceToPlayer > 15) { - if (current < 1) { - visible = false - } + if (locations.isEmpty()) { + CopyErrorCommand.logError( + IllegalArgumentException("locations is empty"), + "Trying to render an empty parkour" + ) + return } - if (!visible) return + try { + if (!showEverything) { + if (current == locations.size - 1) visible = false + + val distanceToPlayer = locations.first().offsetCenter().distanceToPlayer() + if (distanceToPlayer < detectionRange) { + visible = true + } else if (distanceToPlayer > 15) { + if (current < 1) { + visible = false + } + } + + if (!visible) return - for ((index, location) in locations.withIndex()) { - if (location.distanceToPlayer() < 2) { - if (Minecraft.getMinecraft().thePlayer.onGround) { - current = index + for ((index, location) in locations.withIndex()) { + if (location.offsetCenter().distanceToPlayer() < detectionRange) { + if (Minecraft.getMinecraft().thePlayer.onGround) { + current = index + } + } } - } - } - if (current < 0) return - val inProgressVec = getInProgressPair().toSingletonListOrEmpty() - for ((prev, next) in locations.asSequence().withIndex().zipWithNext().drop(current) - .take(lookAhead - 1) + inProgressVec) { - event.draw3DLine(prev.value, next.value, colorForIndex(prev.index), 5, false, colorForIndex(next.index)) - } - val nextShortcuts = current until current + lookAhead - for (shortCut in shortCuts) { - if (shortCut.from in nextShortcuts && shortCut.to in locations.indices) { - event.draw3DLine(locations[shortCut.from], locations[shortCut.to], Color.RED, 3, false) - event.drawFilledBoundingBox(axisAlignedBB(locations[shortCut.to]), Color.RED, 1f) - event.drawDynamicText(locations[shortCut.to].add(-0.5, 1.0, -0.5), "§cShortcut", 2.5) + if (current < 0) return + } else { + current = 0 + lookAhead = locations.size } - } + val inProgressVec = getInProgressPair().toSingletonListOrEmpty() + for ((prev, next) in locations.asSequence().withIndex().zipWithNext().drop(current) + .take(lookAhead - 1) + inProgressVec) { + event.draw3DLine( + prev.value.offsetCenter(), + next.value.offsetCenter(), + colorForIndex(prev.index), + 5, + false, + colorForIndex(next.index) + ) + } + val nextShortcuts = current until current + lookAhead + for (shortCut in shortCuts) { + if (shortCut.from in nextShortcuts && shortCut.to in locations.indices) { + event.draw3DLine( + locations[shortCut.from].offsetCenter(), + locations[shortCut.to].offsetCenter(), + Color.RED, + 3, + false + ) + event.drawFilledBoundingBox(axisAlignedBB(locations[shortCut.to]), Color.RED, 1f) + event.drawDynamicText(locations[shortCut.to].add(-0.5, 1.0, -0.5), "§cShortcut", 2.5) + } - for ((index, location) in locations.asSequence().withIndex().drop(current) - .take(lookAhead) + inProgressVec.map { it.second }) { - var aabb = axisAlignedBB(location) - if (location !in locations) { - aabb = aabb.expandBlock() } - event.drawFilledBoundingBox(aabb, colorForIndex(index), 1f) + for ((index, location) in locations.asSequence().withIndex().drop(current) + .take(lookAhead) + inProgressVec.map { it.second }) { + var aabb = axisAlignedBB(location) + val isMovingPlattform = location !in locations + if (isMovingPlattform) { + aabb = aabb.expandBlock() + } + + event.drawFilledBoundingBox(aabb, colorForIndex(index), 1f) + if (!isMovingPlattform) { + event.drawString(location.offsetCenter().add(0, 1, 0), "§a§l$index", seeThroughBlocks = true) + } + } + } catch (e: Throwable) { + CopyErrorCommand.logError(e, "Error while rendering a parkour") } } + private fun LorenzVec.offsetCenter() = add(platformSize / 2, 1.0, platformSize / 2) + private fun getInProgressPair(): Pair, IndexedValue>? { if (current < 0 || current + lookAhead >= locations.size) return null val currentPosition = locations[current] @@ -92,14 +138,9 @@ class ParkourHelper(val locations: List, private val shortCuts: List< ) } - private fun axisAlignedBB(loc: LorenzVec) = loc.add(-1.0, 0.0, -1.0).boundingToOffset(2, -1, 2).expandBlock() + private fun axisAlignedBB(loc: LorenzVec) = loc.boundingToOffset(platformSize, 1.0, platformSize).expandBlock() private fun colorForIndex(index: Int) = if (rainbowColor) { RenderUtils.chromaColor(4.seconds, offset = -index / 12f, brightness = 0.7f) } else monochromeColor -} - -private fun T?.toSingletonListOrEmpty(): List { - if (this == null) return emptyList() - return listOf(this) } \ No newline at end of file -- cgit