diff options
Diffstat (limited to 'src/main/kotlin/util')
100 files changed, 3413 insertions, 1464 deletions
diff --git a/src/main/kotlin/util/Base64Util.kt b/src/main/kotlin/util/Base64Util.kt index 44bcdfd..0b7b3ea 100644 --- a/src/main/kotlin/util/Base64Util.kt +++ b/src/main/kotlin/util/Base64Util.kt @@ -1,10 +1,23 @@ - package moe.nea.firmament.util +import java.util.Base64 + object Base64Util { - fun String.padToValidBase64(): String { - val align = this.length % 4 - if (align == 0) return this - return this + "=".repeat(4 - align) - } + fun decodeString(str: String): String { + return decodeBytes(str).decodeToString() + } + + fun decodeBytes(str: String): ByteArray { + return Base64.getDecoder().decode(str.padToValidBase64()) + } + + fun String.padToValidBase64(): String { + val align = this.length % 4 + if (align == 0) return this + return this + "=".repeat(4 - align) + } + + fun encodeToString(bytes: ByteArray): String { + return Base64.getEncoder().encodeToString(bytes) + } } diff --git a/src/main/kotlin/util/BazaarPriceStrategy.kt b/src/main/kotlin/util/BazaarPriceStrategy.kt index 002eedb..13b6d95 100644 --- a/src/main/kotlin/util/BazaarPriceStrategy.kt +++ b/src/main/kotlin/util/BazaarPriceStrategy.kt @@ -9,7 +9,7 @@ enum class BazaarPriceStrategy { NPC_SELL; fun getSellPrice(skyblockId: SkyblockId): Double { - val bazaarEntry = HypixelStaticData.bazaarData[skyblockId] ?: return 0.0 + val bazaarEntry = HypixelStaticData.bazaarData[skyblockId.asBazaarStock] ?: return 0.0 return when (this) { BUY_ORDER -> bazaarEntry.quickStatus.sellPrice SELL_ORDER -> bazaarEntry.quickStatus.buyPrice diff --git a/src/main/kotlin/util/ChromaColourUtil.kt b/src/main/kotlin/util/ChromaColourUtil.kt new file mode 100644 index 0000000..0130326 --- /dev/null +++ b/src/main/kotlin/util/ChromaColourUtil.kt @@ -0,0 +1,10 @@ +package moe.nea.firmament.util + +import io.github.notenoughupdates.moulconfig.ChromaColour +import java.awt.Color + +fun ChromaColour.getRGBAWithoutAnimation() = + Color(ChromaColour.specialToSimpleRGB(toLegacyString()), true) + +fun Color.toChromaWithoutAnimation(timeForFullRotationInMillis: Int = 0) = + ChromaColour.fromRGB(red, green, blue, timeForFullRotationInMillis, alpha) diff --git a/src/main/kotlin/util/CommonSoundEffects.kt b/src/main/kotlin/util/CommonSoundEffects.kt index a97a2cb..a4d7129 100644 --- a/src/main/kotlin/util/CommonSoundEffects.kt +++ b/src/main/kotlin/util/CommonSoundEffects.kt @@ -2,18 +2,18 @@ package moe.nea.firmament.util -import net.minecraft.client.sound.PositionedSoundInstance -import net.minecraft.sound.SoundEvent -import net.minecraft.util.Identifier +import net.minecraft.client.resources.sounds.SimpleSoundInstance +import net.minecraft.sounds.SoundEvent +import net.minecraft.resources.ResourceLocation // TODO: Replace these with custom sound events that just re use the vanilla ogg s object CommonSoundEffects { - fun playSound(identifier: Identifier) { - MC.soundManager.play(PositionedSoundInstance.master(SoundEvent.of(identifier), 1F)) + fun playSound(identifier: ResourceLocation) { + MC.soundManager.play(SimpleSoundInstance.forUI(SoundEvent.createVariableRangeEvent(identifier), 1F)) } fun playFailure() { - playSound(Identifier.of("minecraft", "block.anvil.place")) + playSound(ResourceLocation.fromNamespaceAndPath("minecraft", "block.anvil.place")) } fun playSuccess() { @@ -21,6 +21,6 @@ object CommonSoundEffects { } fun playDing() { - playSound(Identifier.of("minecraft", "entity.arrow.hit_player")) + playSound(ResourceLocation.fromNamespaceAndPath("minecraft", "entity.arrow.hit_player")) } } diff --git a/src/main/kotlin/util/DurabilityBarEvent.kt b/src/main/kotlin/util/DurabilityBarEvent.kt index 993462c..c2f863f 100644 --- a/src/main/kotlin/util/DurabilityBarEvent.kt +++ b/src/main/kotlin/util/DurabilityBarEvent.kt @@ -2,7 +2,7 @@ package moe.nea.firmament.util import me.shedaniel.math.Color -import net.minecraft.item.ItemStack +import net.minecraft.world.item.ItemStack import moe.nea.firmament.events.FirmamentEvent import moe.nea.firmament.events.FirmamentEventBus diff --git a/src/main/kotlin/util/ErrorUtil.kt b/src/main/kotlin/util/ErrorUtil.kt index 190381d..3db4ecd 100644 --- a/src/main/kotlin/util/ErrorUtil.kt +++ b/src/main/kotlin/util/ErrorUtil.kt @@ -29,15 +29,31 @@ object ErrorUtil { inline fun softError(message: String, exception: Throwable) { if (aggressiveErrors) throw IllegalStateException(message, exception) - else Firmament.logger.error(message, exception) + else logError(message, exception) + } + + fun logError(message: String, exception: Throwable) { + Firmament.logger.error(message, exception) + } + fun logError(message: String) { + Firmament.logger.error(message) } inline fun softError(message: String) { if (aggressiveErrors) error(message) - else Firmament.logger.error(message) + else logError(message) + } + + fun <T> Result<T>.intoCatch(message: String): Catch<T> { + return this.map { Catch.succeed(it) }.getOrElse { + softError(message, it) + Catch.fail(it) + } } class Catch<T> private constructor(val value: T?, val exc: Throwable?) { + fun orNull(): T? = value + inline fun or(block: (exc: Throwable) -> T): T { contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) @@ -73,4 +89,9 @@ object ErrorUtil { return nullable } + fun softUserError(string: String) { + if (TestUtil.isInTest) + error(string) + MC.sendChat(tr("firmament.usererror", "Firmament encountered a user caused error: $string")) + } } diff --git a/src/main/kotlin/util/FirmFormatters.kt b/src/main/kotlin/util/FirmFormatters.kt index a660f51..c4cffc3 100644 --- a/src/main/kotlin/util/FirmFormatters.kt +++ b/src/main/kotlin/util/FirmFormatters.kt @@ -12,8 +12,8 @@ import kotlin.math.absoluteValue import kotlin.math.roundToInt import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds -import net.minecraft.text.Text -import net.minecraft.util.math.BlockPos +import net.minecraft.network.chat.Component +import net.minecraft.core.BlockPos object FirmFormatters { @@ -103,7 +103,7 @@ object FirmFormatters { return sb.toString() } - fun debugPath(path: Path): Text { + fun debugPath(path: Path): Component { if (!path.exists()) { return tr("firmament.path.missing", "$path (missing)").red() } @@ -127,12 +127,16 @@ object FirmFormatters { fun formatBool( boolean: Boolean, trueIsGood: Boolean = true, - ): Text { - val text = Text.literal(boolean.toString()) + ): Component { + val text = Component.literal(boolean.toString()) return if (boolean == trueIsGood) text.lime() else text.red() } - fun formatPosition(position: BlockPos): Text { - return Text.literal("x: ${position.x}, y: ${position.y}, z: ${position.z}") + fun formatPosition(position: BlockPos): Component { + return Component.literal("x: ${position.x}, y: ${position.y}, z: ${position.z}") + } + + fun formatPercent(value: Double, decimals: Int = 1): String { + return "%.${decimals}f%%".format(value * 100) } } diff --git a/src/main/kotlin/util/FragmentGuiScreen.kt b/src/main/kotlin/util/FragmentGuiScreen.kt index 5e13d51..8797a31 100644 --- a/src/main/kotlin/util/FragmentGuiScreen.kt +++ b/src/main/kotlin/util/FragmentGuiScreen.kt @@ -6,26 +6,25 @@ import io.github.notenoughupdates.moulconfig.gui.GuiContext import me.shedaniel.math.Dimension import me.shedaniel.math.Point import me.shedaniel.math.Rectangle -import net.minecraft.client.gui.DrawContext -import net.minecraft.client.gui.screen.Screen -import net.minecraft.text.Text +import net.minecraft.client.input.MouseButtonEvent +import net.minecraft.client.gui.GuiGraphics +import net.minecraft.client.gui.screens.Screen +import net.minecraft.client.input.CharacterEvent +import net.minecraft.client.input.KeyEvent +import net.minecraft.network.chat.Component |
