diff options
| author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-04-16 02:34:51 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-15 18:34:51 +0200 |
| commit | 66b98b62ec0ef0aa56d47af48131d661d43de579 (patch) | |
| tree | faaf994f850cc1303d699654a1129d9fd6449353 /src/main/java/at/hannibal2/skyhanni/utils | |
| parent | 377fd979821296cde9d6ceb2fa4b365e4637232a (diff) | |
| download | skyhanni-66b98b62ec0ef0aa56d47af48131d661d43de579.tar.gz skyhanni-66b98b62ec0ef0aa56d47af48131d661d43de579.tar.bz2 skyhanni-66b98b62ec0ef0aa56d47af48131d661d43de579.zip | |
Improvement: Code cleanup, bug fix and new functions (#1445)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt | 11 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt | 7 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt | 4 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt | 10 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt | 43 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt | 3 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt (renamed from src/main/java/at/hannibal2/skyhanni/utils/Season.kt) | 6 |
7 files changed, 76 insertions, 8 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index eaae104ac..b3861ce27 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -159,14 +159,21 @@ object ChatUtils { * Sends a message to the user that they can click and run an action * @param message The message to be sent * @param onClick The runnable to be executed when the message is clicked + * @param expireAt When the click action should expire, default never * @param prefix Whether to prefix the message with the chat prefix, default true * @param prefixColor Color that the prefix should be, default yellow (§e) * * @see CHAT_PREFIX */ - fun clickableChat(message: String, onClick: () -> Any, prefix: Boolean = true, prefixColor: String = "§e") { + fun clickableChat( + message: String, + onClick: () -> Any, + expireAt: SimpleTimeMark = SimpleTimeMark.farFuture(), + prefix: Boolean = true, + prefixColor: String = "§e" + ) { val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else "" - ChatClickActionManager.oneTimeClick(msgPrefix + message, onClick) + ChatClickActionManager.oneTimeClick(msgPrefix + message, onClick, expireAt) } /** diff --git a/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt b/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt index 3c6491ac3..0b4c3adc9 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt @@ -1,5 +1,6 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.CollectionUtils.drainTo import java.util.concurrent.ConcurrentLinkedQueue import kotlin.time.Duration @@ -22,7 +23,11 @@ object DelayedRun { tasks.removeIf { (runnable, time) -> val inPast = time.isInPast() if (inPast) { - runnable() + try { + runnable() + } catch (e: Exception) { + ErrorManager.logErrorWithData(e, "DelayedRun task crashed while executing") + } } inPast } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt index 610a0c270..9bc8ff5ae 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/InventoryUtils.kt @@ -100,5 +100,9 @@ object InventoryUtils { } } + fun getItemAtSlotIndex(slotIndex: Int): ItemStack? { + return getItemsInOpenChest().find { it.slotIndex == slotIndex }?.stack + } + fun NEUInternalName.getAmountInInventory(): Int = countItemsInLowerInventory { it.getInternalNameOrNull() == this } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt index ca651ea08..9f069f19c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt @@ -95,5 +95,15 @@ object LocationUtils { fun AxisAlignedBB.getCenter() = this.getEdgeLengths().multiply(0.5).add(this.minBox()) fun AxisAlignedBB.getTopCenter() = this.getCenter().add(y = (maxY - minY) / 2) + + fun AxisAlignedBB.clampTo(other: AxisAlignedBB): AxisAlignedBB { + val minX = max(this.minX, other.minX) + val minY = max(this.minY, other.minY) + val minZ = max(this.minZ, other.minZ) + val maxX = min(this.maxX, other.maxX) + val maxY = min(this.maxY, other.maxY) + val maxZ = min(this.maxZ, other.maxZ) + return AxisAlignedBB(minX, minY, minZ, maxX, maxY, maxZ) + } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt index e8715703f..a03795f59 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzVec.kt @@ -18,6 +18,7 @@ import kotlin.math.round import kotlin.math.sin import kotlin.math.sqrt + data class LorenzVec( val x: Double, val y: Double, @@ -56,6 +57,17 @@ data class LorenzVec( return (dx * dx + dz * dz) } + operator fun plus(other: LorenzVec) = LorenzVec(x + other.x, y + other.y, z + other.z) + + operator fun minus(other: LorenzVec) = LorenzVec(x - other.x, y - other.y, z - other.z) + + operator fun times(other: LorenzVec) = LorenzVec(x * other.x, y * other.y, z * other.z) + operator fun times(other: Double) = LorenzVec(x * other, y * other, z * other) + operator fun times(other: Int) = LorenzVec(x * other, y * other, z * other) + + operator fun div(other: LorenzVec) = LorenzVec(x / other.x, y / other.y, z / other.z) + operator fun div(other: Double) = LorenzVec(x / other, y / other, z / other) + fun add(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0): LorenzVec = LorenzVec(this.x + x, this.y + y, this.z + z) @@ -63,17 +75,21 @@ data class LorenzVec( override fun toString() = "LorenzVec{x=$x, y=$y, z=$z}" + @Deprecated("Use operator fun times instead", ReplaceWith("this * LorenzVec(x, y, z)")) fun multiply(d: Double): LorenzVec = LorenzVec(x multiplyZeroSave d, y multiplyZeroSave d, z multiplyZeroSave d) + @Deprecated("Use operator fun times instead", ReplaceWith("this * LorenzVec(x, y, z)")) fun multiply(d: Int): LorenzVec = LorenzVec(x multiplyZeroSave d.toDouble(), y multiplyZeroSave d.toDouble(), z multiplyZeroSave d.toDouble()) + @Deprecated("Use operator fun div instead", ReplaceWith("this / LorenzVec(x, y, z)")) fun divide(d: Double) = multiply(1.0 / d) + @Deprecated("Use operator fun times instead", ReplaceWith("this * LorenzVec(x, y, z)")) fun multiply(v: LorenzVec) = LorenzVec(x multiplyZeroSave v.x, y multiplyZeroSave v.y, z multiplyZeroSave v.z) fun dotProduct(other: LorenzVec): Double = - x multiplyZeroSave other.x + y multiplyZeroSave other.y + z multiplyZeroSave other.z + (x multiplyZeroSave other.x) + (y multiplyZeroSave other.y) + (z multiplyZeroSave other.z) fun angleAsCos(other: LorenzVec) = this.normalize().dotProduct(other.normalize()) @@ -81,8 +97,10 @@ data class LorenzVec( fun angleInDeg(other: LorenzVec) = Math.toDegrees(this.angleInRad(other)) + @Deprecated("Use operator fun plus instead", ReplaceWith("this + other")) fun add(other: LorenzVec) = LorenzVec(x + other.x, y + other.y, z + other.z) + @Deprecated("Use operator fun minus instead", ReplaceWith("this - other")) fun subtract(other: LorenzVec) = LorenzVec(x - other.x, y - other.y, z - other.z) fun normalize() = length().let { LorenzVec(x / it, y / it, z / it) } @@ -111,7 +129,8 @@ data class LorenzVec( fun toCleanString(): String = "$x $y $z" - fun length(): Double = sqrt(x * x + y * y + z * z) + fun lengthSquared(): Double = x * x + y * y + z * z + fun length(): Double = sqrt(this.lengthSquared()) fun isZero(): Boolean = x == 0.0 && y == 0.0 && z == 0.0 @@ -183,6 +202,24 @@ data class LorenzVec( fun rotateXZ(theta: Double) = LorenzVec(x * cos(theta) + z * sin(theta), y, -x * sin(theta) + z * cos(theta)) fun rotateYZ(theta: Double) = LorenzVec(x, y * cos(theta) - z * sin(theta), y * sin(theta) + z * cos(theta)) + fun nearestPointOnLine(startPos: LorenzVec, endPos: LorenzVec): LorenzVec { + var d = endPos - startPos + val w = this - startPos + + val dp = d.lengthSquared() + var dt = 0.0 + + if (dp != dt) dt = (w.dotProduct(d) / dp).coerceIn(0.0, 1.0) + + d *= dt + d += startPos + return d + } + + fun distanceToLine(startPos: LorenzVec, endPos: LorenzVec): Double { + return (nearestPointOnLine(startPos, endPos) - this).lengthSquared() + } + companion object { fun getFromYawPitch(yaw: Double, pitch: Double): LorenzVec { @@ -231,3 +268,5 @@ fun Array<Double>.toLorenzVec(): LorenzVec { fun RenderUtils.translate(vec: LorenzVec) = GlStateManager.translate(vec.x, vec.y, vec.z) fun AxisAlignedBB.expand(vec: LorenzVec) = this.expand(vec.x, vec.y, vec.z) + +fun AxisAlignedBB.expand(amount: Double) = this.expand(amount, amount, amount) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt b/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt index c6ebdd836..4022b382c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt @@ -35,6 +35,9 @@ class MinecraftConsoleFilter(private val loggerConfigName: String) : Filter { } } + // prevents error sending on every shutdown + fun stop() {} + override fun filter(event: LogEvent?): Filter.Result { if (event == null) return Filter.Result.ACCEPT diff --git a/src/main/java/at/hannibal2/skyhanni/utils/Season.kt b/src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt index 33dd9a221..4e0409465 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/Season.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SkyblockSeason.kt @@ -4,7 +4,7 @@ import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.UtilsPatterns.seasonPattern import io.github.moulberry.notenoughupdates.util.SkyBlockTime -enum class Season( +enum class SkyblockSeason( val season: String, val abbreviatedPerk: String, val perk: String, @@ -21,9 +21,9 @@ enum class Season( companion object { - fun getCurrentSeason(): Season? = getSeasonByName(SkyBlockTime.now().monthName) + fun getCurrentSeason(): SkyblockSeason? = getSeasonByName(SkyBlockTime.now().monthName) - private fun getSeasonByName(name: String): Season? = + private fun getSeasonByName(name: String): SkyblockSeason? = seasonPattern.matchMatcher(name) { entries.find { it.season.endsWith(group("season")) } } } } |
