aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-27 17:26:42 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-27 17:26:42 +0100
commit8df225399f1932b8824d2fc44f4c964bc47fc6aa (patch)
treea2f1d7f64f68242aaaa5b97df2c15665eb7d12ce /src/main/kotlin/util
parentccb5c556def69ea16a52c00b3fbfe3a224f51ac2 (diff)
downloadFirmament-8df225399f1932b8824d2fc44f4c964bc47fc6aa.tar.gz
Firmament-8df225399f1932b8824d2fc44f4c964bc47fc6aa.tar.bz2
Firmament-8df225399f1932b8824d2fc44f4c964bc47fc6aa.zip
feat: Add pickobulus blocker on private island
Diffstat (limited to 'src/main/kotlin/util')
-rw-r--r--src/main/kotlin/util/MC.kt6
-rw-r--r--src/main/kotlin/util/TestUtil.kt1
-rw-r--r--src/main/kotlin/util/json/KJsonOps.kt131
-rw-r--r--src/main/kotlin/util/skyblock/ItemType.kt5
4 files changed, 140 insertions, 3 deletions
diff --git a/src/main/kotlin/util/MC.kt b/src/main/kotlin/util/MC.kt
index a60d5c4..294334a 100644
--- a/src/main/kotlin/util/MC.kt
+++ b/src/main/kotlin/util/MC.kt
@@ -92,12 +92,12 @@ object MC {
inline val inGameHud: InGameHud get() = instance.inGameHud
inline val font get() = instance.textRenderer
inline val soundManager get() = instance.soundManager
- inline val player: ClientPlayerEntity? get() = instance.player
+ inline val player: ClientPlayerEntity? get() = TestUtil.unlessTesting { instance.player }
inline val camera: Entity? get() = instance.cameraEntity
inline val guiAtlasManager get() = instance.guiAtlasManager
- inline val world: ClientWorld? get() = instance.world
+ inline val world: ClientWorld? get() = TestUtil.unlessTesting { instance.world }
inline var screen: Screen?
- get() = instance.currentScreen
+ get() = TestUtil.unlessTesting{ instance.currentScreen }
set(value) = instance.setScreen(value)
val screenName get() = screen?.title?.unformattedString?.trim()
inline val handledScreen: HandledScreen<*>? get() = instance.currentScreen as? HandledScreen<*>
diff --git a/src/main/kotlin/util/TestUtil.kt b/src/main/kotlin/util/TestUtil.kt
index 2d38f35..45e3dde 100644
--- a/src/main/kotlin/util/TestUtil.kt
+++ b/src/main/kotlin/util/TestUtil.kt
@@ -1,6 +1,7 @@
package moe.nea.firmament.util
object TestUtil {
+ inline fun <T> unlessTesting(block: () -> T): T? = if (isInTest) null else block()
val isInTest =
Thread.currentThread().stackTrace.any {
it.className.startsWith("org.junit.") || it.className.startsWith("io.kotest.")
diff --git a/src/main/kotlin/util/json/KJsonOps.kt b/src/main/kotlin/util/json/KJsonOps.kt
new file mode 100644
index 0000000..404ea5e
--- /dev/null
+++ b/src/main/kotlin/util/json/KJsonOps.kt
@@ -0,0 +1,131 @@
+package moe.nea.firmament.util.json
+
+import com.google.gson.internal.LazilyParsedNumber
+import com.mojang.datafixers.util.Pair
+import com.mojang.serialization.DataResult
+import com.mojang.serialization.DynamicOps
+import java.util.stream.Stream
+import kotlinx.serialization.json.JsonArray
+import kotlinx.serialization.json.JsonElement
+import kotlinx.serialization.json.JsonNull
+import kotlinx.serialization.json.JsonObject
+import kotlinx.serialization.json.JsonPrimitive
+import kotlinx.serialization.json.boolean
+import kotlinx.serialization.json.booleanOrNull
+import kotlin.streams.asSequence
+
+class KJsonOps : DynamicOps<JsonElement> {
+ companion object {
+ val INSTANCE = KJsonOps()
+ }
+
+ override fun empty(): JsonElement {
+ return JsonNull
+ }
+
+ override fun createNumeric(num: Number): JsonElement {
+ return JsonPrimitive(num)
+ }
+
+ override fun createString(str: String): JsonElement {
+ return JsonPrimitive(str)
+ }
+
+ override fun remove(input: JsonElement, key: String): JsonElement {
+ if (input is JsonObject) {
+ return JsonObject(input.filter { it.key != key })
+ } else {
+ return input
+ }
+ }
+
+ override fun createList(stream: Stream<JsonElement>): JsonElement {
+ return JsonArray(stream.toList())
+ }
+
+ override fun getStream(input: JsonElement): DataResult<Stream<JsonElement>> {
+ if (input is JsonArray)
+ return DataResult.success(input.stream())
+ return DataResult.error { "Not a json array: $input" }
+ }
+
+ override fun createMap(map: Stream<Pair<JsonElement, JsonElement>>): JsonElement {
+ return JsonObject(map.asSequence()
+ .map { ((it.first as JsonPrimitive).content) to it.second }
+ .toMap())
+ }
+
+ override fun getMapValues(input: JsonElement): DataResult<Stream<Pair<JsonElement, JsonElement>>> {
+ if (input is JsonObject) {
+ return DataResult.success(input.entries.stream().map { Pair.of(createString(it.key), it.value) })
+ }
+ return DataResult.error { "Not a JSON object: $input" }
+ }
+
+ override fun mergeToMap(map: JsonElement, key: JsonElement, value: JsonElement): DataResult<JsonElement> {
+ if (key !is JsonPrimitive || key.isString) {
+ return DataResult.error { "key is not a string: $key" }
+ }
+ val jKey = key.content
+ val extra = mapOf(jKey to value)
+ if (map == empty()) {
+ return DataResult.success(JsonObject(extra))
+ }
+ if (map is JsonObject) {
+ return DataResult.success(JsonObject(map + extra))
+ }
+ return DataResult.error { "mergeToMap called with not a map: $map" }
+ }
+
+ override fun mergeToList(list: JsonElement, value: JsonElement): DataResult<JsonElement> {
+ if (list == empty())
+ return DataResult.success(JsonArray(listOf(value)))
+ if (list is JsonArray) {
+ return DataResult.success(JsonArray(list + value))
+ }
+ return DataResult.error { "mergeToList called with not a list: $list" }
+ }
+
+ override fun getStringValue(input: JsonElement): DataResult<String> {
+ if (input is JsonPrimitive && input.isString) {
+ return DataResult.success(input.content)
+ }
+ return DataResult.error { "Not a string: $input" }
+ }
+
+ override fun getNumberValue(input: JsonElement): DataResult<Number> {
+ if (input is JsonPrimitive && !input.isString && input.booleanOrNull == null)
+ return DataResult.success(LazilyParsedNumber(input.content))
+ return DataResult.error { "not a number: $input" }
+ }
+
+ override fun createBoolean(value: Boolean): JsonElement {
+ return JsonPrimitive(value)
+ }
+
+ override fun getBooleanValue(input: JsonElement): DataResult<Boolean> {
+ if (input is JsonPrimitive) {
+ if (input.booleanOrNull != null)
+ return DataResult.success(input.boolean)
+ return super.getBooleanValue(input)
+ }
+ return DataResult.error { "Not a boolean: $input" }
+ }
+
+ override fun <U : Any?> convertTo(output: DynamicOps<U>, input: JsonElement): U {
+ if (input is JsonObject)
+ return output.createMap(
+ input.entries.stream().map { Pair.of(output.createString(it.key), convertTo(output, it.value)) })
+ if (input is JsonArray)
+ return output.createList(input.stream().map { convertTo(output, it) })
+ if (input is JsonNull)
+ return output.empty()
+ if (input is JsonPrimitive) {
+ if (input.isString)
+ return output.createString(input.content)
+ if (input.booleanOrNull != null)
+ return output.createBoolean(input.boolean)
+ }
+ error("Unknown json value: $input")
+ }
+}
diff --git a/src/main/kotlin/util/skyblock/ItemType.kt b/src/main/kotlin/util/skyblock/ItemType.kt
index b031b69..6ddb077 100644
--- a/src/main/kotlin/util/skyblock/ItemType.kt
+++ b/src/main/kotlin/util/skyblock/ItemType.kt
@@ -32,10 +32,15 @@ value class ItemType private constructor(val name: String) {
val SWORD = ofName("SWORD")
val DRILL = ofName("DRILL")
val PICKAXE = ofName("PICKAXE")
+ val GAUNTLET = ofName("GAUNTLET")
/**
* This one is not really official (it never shows up in game).
*/
val PET = ofName("PET")
}
+
+ override fun toString(): String {
+ return name
+ }
}