diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
7 files changed, 127 insertions, 12 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/BazaarPriceStrategy.kt b/src/main/kotlin/moe/nea/firmament/util/BazaarPriceStrategy.kt new file mode 100644 index 0000000..8b746cc --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/BazaarPriceStrategy.kt @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util + +import moe.nea.firmament.repo.HypixelStaticData + +enum class BazaarPriceStrategy { + BUY_ORDER, + SELL_ORDER, + NPC_SELL; + + fun getSellPrice(skyblockId: SkyblockId): Double { + val bazaarEntry = HypixelStaticData.bazaarData[skyblockId] ?: return 0.0 + return when (this) { + BUY_ORDER -> bazaarEntry.quickStatus.sellPrice + SELL_ORDER -> bazaarEntry.quickStatus.buyPrice + NPC_SELL -> TODO() + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/util/FragmentGuiScreen.kt b/src/main/kotlin/moe/nea/firmament/util/FragmentGuiScreen.kt index 4927d65..22756d0 100644 --- a/src/main/kotlin/moe/nea/firmament/util/FragmentGuiScreen.kt +++ b/src/main/kotlin/moe/nea/firmament/util/FragmentGuiScreen.kt @@ -91,7 +91,7 @@ abstract class FragmentGuiScreen( verticalAmount: Double ): Boolean { return ifPopup { - it.mouseScrolled(mouseX, mouseY, verticalAmount) + it.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount) } } } diff --git a/src/main/kotlin/moe/nea/firmament/util/MC.kt b/src/main/kotlin/moe/nea/firmament/util/MC.kt index f3fc754..5c4bf5c 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MC.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MC.kt @@ -14,6 +14,7 @@ import net.minecraft.client.gui.screen.ingame.HandledScreen import net.minecraft.network.message.ArgumentSignatureDataMap import net.minecraft.network.message.LastSeenMessagesCollector.LastSeenMessages import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket +import net.minecraft.resource.ReloadableResourceManagerImpl import net.minecraft.text.Text import net.minecraft.util.math.BlockPos import moe.nea.firmament.events.TickEvent @@ -59,6 +60,7 @@ object MC { player?.networkHandler?.sendCommand(command) } + inline val resourceManager get() = (MinecraftClient.getInstance().resourceManager as ReloadableResourceManagerImpl) inline val networkHandler get() = player?.networkHandler inline val instance get() = MinecraftClient.getInstance() inline val keyboard get() = MinecraftClient.getInstance().keyboard diff --git a/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt b/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt index bea3bc6..9ca0a73 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt @@ -8,10 +8,60 @@ package moe.nea.firmament.util import io.github.moulberry.moulconfig.common.MyResourceLocation import io.github.moulberry.moulconfig.gui.GuiContext +import io.github.moulberry.moulconfig.xml.ChildCount +import io.github.moulberry.moulconfig.xml.XMLContext +import io.github.moulberry.moulconfig.xml.XMLGuiLoader import io.github.moulberry.moulconfig.xml.XMLUniverse +import javax.xml.namespace.QName +import me.shedaniel.math.Color +import org.w3c.dom.Element +import moe.nea.firmament.gui.BarComponent object MoulConfigUtils { - val universe = XMLUniverse.getDefaultUniverse() + val firmUrl = "http://nea.moe/Firmament" + val universe = XMLUniverse.getDefaultUniverse().also { uni -> + uni.registerMapper(java.awt.Color::class.java) { + if (it.startsWith("#")) { + val hexString = it.substring(1) + val hex = hexString.toInt(16) + if (hexString.length == 6) { + return@registerMapper java.awt.Color(hex) + } + if (hexString.length == 8) { + return@registerMapper java.awt.Color(hex, true) + } + error("Hexcolor $it needs to be exactly 6 or 8 hex digits long") + } + return@registerMapper java.awt.Color(it.toInt(), true) + } + uni.registerMapper(Color::class.java) { + val color = uni.mapXMLObject(it, java.awt.Color::class.java) + Color.ofRGBA(color.red, color.green, color.blue, color.alpha) + } + uni.registerLoader(object : XMLGuiLoader<BarComponent> { + override fun getName(): QName { + return QName(firmUrl, "Bar") + } + + override fun createInstance(context: XMLContext<*>, element: Element): BarComponent { + return BarComponent( + context.getPropertyFromAttribute(element, QName("progress"), Double::class.java)!!, + context.getPropertyFromAttribute(element, QName("total"), Double::class.java)!!, + context.getPropertyFromAttribute(element, QName("fillColor"), Color::class.java)!!.get(), + context.getPropertyFromAttribute(element, QName("emptyColor"), Color::class.java)!!.get(), + ) + } + + override fun getChildCount(): ChildCount { + return ChildCount.NONE + } + + override fun getAttributeNames(): Map<String, Boolean> { + return mapOf("progress" to true, "total" to true, "emptyColor" to true, "fillColor" to true) + } + }) + } + fun loadGui(name: String, bindTo: Any): GuiContext { return GuiContext(universe.load(bindTo, MyResourceLocation("firmament", "gui/$name.xml"))) } diff --git a/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt b/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt index f3526be..41a196d 100644 --- a/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt +++ b/src/main/kotlin/moe/nea/firmament/util/TimeMark.kt @@ -7,22 +7,42 @@ package moe.nea.firmament.util import kotlin.time.Duration -import kotlin.time.ExperimentalTime -import kotlin.time.TimeSource +import kotlin.time.Duration.Companion.milliseconds -@OptIn(ExperimentalTime::class) -class TimeMark private constructor(private val timeMark: TimeSource.Monotonic.ValueTimeMark?) : Comparable<TimeMark> { - fun passedTime() = timeMark?.elapsedNow() ?: Duration.INFINITE +class TimeMark private constructor(private val timeMark: Long) : Comparable<TimeMark> { + fun passedTime() = if (timeMark == 0L) Duration.INFINITE else (System.currentTimeMillis() - timeMark).milliseconds + + operator fun minus(other: TimeMark): Duration { + if (other.timeMark == timeMark) + return 0.milliseconds + if (other.timeMark == 0L) + return Duration.INFINITE + if (timeMark == 0L) + return -Duration.INFINITE + return (timeMark - other.timeMark).milliseconds + } companion object { - fun now() = TimeMark(TimeSource.Monotonic.markNow()) - fun farPast() = TimeMark(null) + fun now() = TimeMark(System.currentTimeMillis()) + fun farPast() = TimeMark(0L) + fun ago(timeDelta: Duration): TimeMark { + if (timeDelta.isFinite()) { + return TimeMark(System.currentTimeMillis() - timeDelta.inWholeMilliseconds) + } + require(timeDelta.isPositive()) + return farPast() + } + } + + override fun hashCode(): Int { + return timeMark.hashCode() + } + + override fun equals(other: Any?): Boolean { + return other is TimeMark && other.timeMark == timeMark } override fun compareTo(other: TimeMark): Int { - if (this.timeMark == other.timeMark) return 0 - if (this.timeMark == null) return -1 - if (other.timeMark == null) return -1 return this.timeMark.compareTo(other.timeMark) } } diff --git a/src/main/kotlin/moe/nea/firmament/util/regex.kt b/src/main/kotlin/moe/nea/firmament/util/regex.kt index 97c2797..4cc3f03 100644 --- a/src/main/kotlin/moe/nea/firmament/util/regex.kt +++ b/src/main/kotlin/moe/nea/firmament/util/regex.kt @@ -6,5 +6,13 @@ package moe.nea.firmament.util +import java.util.regex.Matcher +import java.util.regex.Pattern + inline fun <T> String.ifMatches(regex: Regex, block: (MatchResult) -> T): T? = regex.matchEntire(this)?.let(block) + +inline fun <T> Pattern.useMatch(string: String, block: Matcher.() -> T): T? = + matcher(string) + .takeIf(Matcher::matches) + ?.let(block) diff --git a/src/main/kotlin/moe/nea/firmament/util/stringutil.kt b/src/main/kotlin/moe/nea/firmament/util/stringutil.kt new file mode 100644 index 0000000..21625d4 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/stringutil.kt @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util + +fun parseIntWithComma(string: String): Int { + return string.replace(",", "").toInt() +} |