aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt25
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/async/input.kt45
2 files changed, 70 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt b/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt
index 6033577..f0998a7 100644
--- a/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt
+++ b/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt
@@ -27,11 +27,36 @@ import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.util.Identifier
+/**
+ * A skyblock item id, as used by the NEU repo.
+ * This is not exactly the format used by HyPixel, but is mostly the same.
+ * Usually this id splits an id used by HyPixel into more sub items. For example `PET` becomes `$PET_ID;$PET_RARITY`,
+ * with those values extracted from other metadata.
+ */
@JvmInline
+@Serializable
value class SkyblockId(val neuItem: String) {
val identifier get() = Identifier("skyblockitem", neuItem.lowercase().replace(";", "__"))
+ /**
+ * A bazaar stock item id, as returned by the HyPixel bazaar api endpoint.
+ * These are not equivalent to the in-game ids, or the NEU repo ids, and in fact, do not refer to items, but instead
+ * to bazaar stocks. The main difference from [SkyblockId]s is concerning enchanted books. There are probably more,
+ * but for now this holds.
+ */
+ @JvmInline
+ @Serializable
+ value class BazaarStock(val bazaarId: String) {
+ fun toRepoId(): SkyblockId {
+ bazaarEnchantmentRegex.matchEntire(bazaarId)?.let {
+ return SkyblockId("${it.groupValues[1]};${it.groupValues[2]}")
+ }
+ return SkyblockId(bazaarId.replace(":", "-"))
+ }
+ }
+
companion object {
+ private val bazaarEnchantmentRegex = "ENCHANTMENT_(\\D*)_(\\d+)".toRegex()
val NULL: SkyblockId = SkyblockId("null")
}
}
diff --git a/src/main/kotlin/moe/nea/firmament/util/async/input.kt b/src/main/kotlin/moe/nea/firmament/util/async/input.kt
new file mode 100644
index 0000000..ac1cfb4
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/async/input.kt
@@ -0,0 +1,45 @@
+package moe.nea.firmament.util.async
+
+import kotlinx.coroutines.suspendCancellableCoroutine
+import kotlin.coroutines.resume
+import moe.nea.firmament.events.HandledScreenKeyPressedEvent
+import moe.nea.firmament.keybindings.IKeyBinding
+
+private object InputHandler {
+ data class KeyInputContinuation(val keybind: IKeyBinding, val onContinue: () -> Unit)
+
+ private val activeContinuations = mutableListOf<KeyInputContinuation>()
+
+ fun registerContinuation(keyInputContinuation: KeyInputContinuation): () -> Unit {
+ synchronized(InputHandler) {
+ activeContinuations.add(keyInputContinuation)
+ }
+ return {
+ synchronized(this) {
+ activeContinuations.remove(keyInputContinuation)
+ }
+ }
+ }
+
+ init {
+ HandledScreenKeyPressedEvent.subscribe { event ->
+ synchronized(InputHandler) {
+ val toRemove = activeContinuations.filter {
+ event.matches(it.keybind)
+ }
+ toRemove.forEach { it.onContinue() }
+ activeContinuations.removeAll(toRemove)
+ }
+ }
+ }
+}
+
+suspend fun waitForInput(keybind: IKeyBinding): Unit = suspendCancellableCoroutine { cont ->
+ val unregister =
+ InputHandler.registerContinuation(InputHandler.KeyInputContinuation(keybind) { cont.resume(Unit) })
+ cont.invokeOnCancellation {
+ unregister()
+ }
+}
+
+