diff options
author | Linnea Gräf <nea@nea.moe> | 2024-08-04 21:10:08 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-08-04 21:10:08 +0200 |
commit | 9b277bd897490d13ee4549a086e8d1b5f4cd0e10 (patch) | |
tree | 8f966a8f5b981cb700becb337e43970eb0619fcc /src/main/kotlin/moe/nea/firmament/util | |
parent | 95c1b75a14d41b070c4e23addb3ac4788a014365 (diff) | |
download | Firmament-9b277bd897490d13ee4549a086e8d1b5f4cd0e10.tar.gz Firmament-9b277bd897490d13ee4549a086e8d1b5f4cd0e10.tar.bz2 Firmament-9b277bd897490d13ee4549a086e8d1b5f4cd0e10.zip |
Add SkyBlockIsland type
[no changelog]
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
4 files changed, 58 insertions, 7 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/Locraw.kt b/src/main/kotlin/moe/nea/firmament/util/Locraw.kt index f32bc5e..136d169 100644 --- a/src/main/kotlin/moe/nea/firmament/util/Locraw.kt +++ b/src/main/kotlin/moe/nea/firmament/util/Locraw.kt @@ -1,5 +1,6 @@ /* * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe> + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -7,8 +8,10 @@ package moe.nea.firmament.util import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient @Serializable data class Locraw(val server: String, val gametype: String? = null, val mode: String? = null, val map: String? = null) { - val skyblockLocation = if (gametype == "SKYBLOCK") mode else null + @Transient + val skyblockLocation = if (gametype == "SKYBLOCK") mode?.let(SkyBlockIsland::forMode) else null } diff --git a/src/main/kotlin/moe/nea/firmament/util/SBData.kt b/src/main/kotlin/moe/nea/firmament/util/SBData.kt index 1b6d180..5fd0106 100644 --- a/src/main/kotlin/moe/nea/firmament/util/SBData.kt +++ b/src/main/kotlin/moe/nea/firmament/util/SBData.kt @@ -1,5 +1,6 @@ /* * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe> + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -28,7 +29,7 @@ object SBData { private var hasReceivedProfile = false private var hasSentLocraw = false var locraw: Locraw? = null - val skyblockLocation: String? get() = locraw?.skyblockLocation + val skyblockLocation: SkyBlockIsland? get() = locraw?.skyblockLocation val hasValidLocraw get() = locraw?.server !in listOf("limbo", null) val isOnSkyblock get() = locraw?.gametype == "SKYBLOCK" diff --git a/src/main/kotlin/moe/nea/firmament/util/SkyBlockIsland.kt b/src/main/kotlin/moe/nea/firmament/util/SkyBlockIsland.kt new file mode 100644 index 0000000..6eebc2e --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/SkyBlockIsland.kt @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import moe.nea.firmament.repo.RepoManager + +@Serializable(with = SkyBlockIsland.Serializer::class) +class SkyBlockIsland +private constructor( + val locrawMode: String, +) { + + object Serializer : KSerializer<SkyBlockIsland> { + override val descriptor: SerialDescriptor + get() = PrimitiveSerialDescriptor("SkyBlockIsland", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): SkyBlockIsland { + return forMode(decoder.decodeString()) + } + + override fun serialize(encoder: Encoder, value: SkyBlockIsland) { + encoder.encodeString(value.locrawMode) + } + } + companion object { + private val allIslands = mutableMapOf<String, SkyBlockIsland>() + fun forMode(mode: String): SkyBlockIsland = allIslands.computeIfAbsent(mode, ::SkyBlockIsland) + val HUB = forMode("hub") + val PRIVATE_ISLAND = forMode("dynamic") + val RIFT = forMode("rift") + } + + val userFriendlyName + get() = RepoManager.neuRepo.constants.islands.areaNames + .getOrDefault(locrawMode, locrawMode) +} diff --git a/src/main/kotlin/moe/nea/firmament/util/WarpUtil.kt b/src/main/kotlin/moe/nea/firmament/util/WarpUtil.kt index 5a7911c..7a20346 100644 --- a/src/main/kotlin/moe/nea/firmament/util/WarpUtil.kt +++ b/src/main/kotlin/moe/nea/firmament/util/WarpUtil.kt @@ -30,9 +30,9 @@ object WarpUtil { private var lastAttemptedWarp = "" private var lastWarpAttempt = TimeMark.farPast() - fun findNearestWarp(locrawMode: String, pos: Position): Islands.Warp? { + fun findNearestWarp(island: SkyBlockIsland, pos: Position): Islands.Warp? { return warps.minByOrNull { - if (locrawMode != it.mode || (DConfig.data?.excludedWarps?.contains(it.warp) == true)) { + if (island.locrawMode != it.mode || (DConfig.data?.excludedWarps?.contains(it.warp) == true)) { return@minByOrNull Double.MAX_VALUE } else { return@minByOrNull squaredDist(pos, it) @@ -47,10 +47,10 @@ object WarpUtil { return dx * dx + dy * dy + dz * dz } - fun teleportToNearestWarp(locrawMode: String, pos: Position) { - val nearestWarp = findNearestWarp(locrawMode, pos) ?: return + fun teleportToNearestWarp(island: SkyBlockIsland, pos: Position) { + val nearestWarp = findNearestWarp(island, pos) ?: return - if (locrawMode == SBData.skyblockLocation + if (island == SBData.skyblockLocation && sqrt(squaredDist(pos, nearestWarp)) > 1.1 * sqrt(squaredDist((MC.player ?: return).pos, nearestWarp)) ) { return |