aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/inventory/storageoverlay/StoragePageSlot.kt
blob: 92594150c14d3e5d26d5b2943dbd71a275849b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package moe.nea.firmament.features.inventory.storageoverlay

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.util.MC

@Serializable(with = StoragePageSlot.Serializer::class)
data class StoragePageSlot(val index: Int) : Comparable<StoragePageSlot> {
    object Serializer : KSerializer<StoragePageSlot> {
        override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("StoragePageSlot", PrimitiveKind.INT)

        override fun deserialize(decoder: Decoder): StoragePageSlot {
            return StoragePageSlot(decoder.decodeInt())
        }

        override fun serialize(encoder: Encoder, value: StoragePageSlot) {
            encoder.encodeInt(value.index)
        }
    }

    init {
        assert(index in 0 until (3 * 9))
    }

    val isEnderChest get() = index < 9
    val isBackPack get() = !isEnderChest
    val slotIndexInOverviewPage get() = if (isEnderChest) index + 9 else index + 18
    fun defaultName(): String = if (isEnderChest) "Ender Chest #${index + 1}" else "Backpack #${index - 9 + 1}"

    fun navigateTo() {
        if (isBackPack) {
            MC.sendCommand("backpack ${index - 9 + 1}")
        } else {
            MC.sendCommand("enderchest ${index + 1}")
        }
    }

    companion object {
        fun fromOverviewSlotIndex(slot: Int): StoragePageSlot? {
            if (slot in 9 until 18) return StoragePageSlot(slot - 9)
            if (slot in 27 until 45) return StoragePageSlot(slot - 27 + 9)
            return null
        }

        fun ofEnderChestPage(slot: Int): StoragePageSlot {
            assert(slot in 1..9)
            return StoragePageSlot(slot - 1)
        }

        fun ofBackPackPage(slot: Int): StoragePageSlot {
            assert(slot in 1..18)
            return StoragePageSlot(slot - 1 + 9)
        }
    }

    override fun compareTo(other: StoragePageSlot): Int {
        return this.index - other.index
    }
}