-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathapiTools.kt
More file actions
198 lines (173 loc) · 6.88 KB
/
Copy pathapiTools.kt
File metadata and controls
198 lines (173 loc) · 6.88 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Skytils - Hypixel Skyblock Quality of Life Mod
* Copyright (C) 2020-2023 Skytils
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package gg.skytils.skytilsmod.utils
import gg.skytils.hypixel.types.player.Player
import gg.skytils.hypixel.types.skyblock.Profile
import gg.skytils.hypixel.types.util.Inventory
import gg.skytils.skytilsmod.features.impl.dungeons.catlas.core.CatlasConfig
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.IntArraySerializer
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import net.minecraft.item.ItemStack
import net.minecraft.nbt.CompressedStreamTools
import net.minecraft.util.BlockPos
import net.minecraftforge.common.util.Constants
import java.awt.Color
import java.util.*
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi
@Serializable
sealed class HypixelResponse {
abstract val success: Boolean
val cause: String? = null
}
@Serializable
data class TypesProfileResponse(
override val success: Boolean,
val profiles: List<Profile>? = null
) : HypixelResponse()
@Serializable
data class PlayerResponse(override val success: Boolean, val player: Player? = null) : HypixelResponse()
enum class DungeonClass {
ARCHER,
BERSERK,
MAGE,
HEALER,
TANK,
EMPTY;
val className = name.toTitleCase()
val apiName = name.lowercase()
val color: Color
get() = when (this) {
ARCHER -> CatlasConfig.colorPlayerArcher
BERSERK -> CatlasConfig.colorPlayerBerserk
HEALER -> CatlasConfig.colorPlayerHealer
MAGE -> CatlasConfig.colorPlayerMage
TANK -> CatlasConfig.colorPlayerTank
else -> Color.BLACK
}
override fun toString(): String {
return this.className
}
companion object {
fun getClassFromName(name: String): DungeonClass {
return name.lowercase().let { entries.find { c -> c.apiName == it } }
?: throw IllegalArgumentException("No class could be found for the name $name")
}
}
}
@Serializable
data class GithubRelease(
@SerialName("tag_name")
val tagName: String,
val assets: List<GithubAsset>,
val body: String
)
@Serializable
data class GithubAsset(
val name: String,
@SerialName("browser_download_url")
val downloadUrl: String,
val size: Long,
@SerialName("download_count")
val downloadCount: Long,
val uploader: GithubUser
)
@Serializable
data class GithubUser(
@SerialName("login")
val username: String
)
object RegexAsString : KSerializer<Regex> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Regex", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Regex = Regex(decoder.decodeString())
override fun serialize(encoder: Encoder, value: Regex) = encoder.encodeString(value.pattern)
}
object BlockPosCSV : KSerializer<BlockPos> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("BlockPos", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): BlockPos = decoder.decodeString().split(',').let {
BlockPos(it[0].toInt(), it[1].toInt(), it[2].toInt())
}
override fun serialize(encoder: Encoder, value: BlockPos) = encoder.encodeString("${value.x},${value.y},${value.z}")
}
object IntColorSerializer : KSerializer<Color> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Color", PrimitiveKind.INT)
override fun deserialize(decoder: Decoder): Color = Color(decoder.decodeInt(), true)
override fun serialize(encoder: Encoder, value: Color) = encoder.encodeInt(value.rgb)
}
object BlockPosArraySerializer : KSerializer<BlockPos> {
private val delegateSerializer = IntArraySerializer()
override val descriptor: SerialDescriptor = delegateSerializer.descriptor
override fun deserialize(decoder: Decoder): BlockPos = decoder.decodeSerializableValue(delegateSerializer).let {
BlockPos(it[0], it[1], it[2])
}
override fun serialize(encoder: Encoder, value: BlockPos) =
encoder.encodeSerializableValue(delegateSerializer, intArrayOf(value.x, value.y, value.z))
}
object BlockPosObjectSerializer : KSerializer<BlockPos> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("BlockPos") {
element<Int>("x")
element<Int>("y")
element<Int>("z")
}
override fun serialize(encoder: Encoder, value: BlockPos) =
encoder.encodeStructure(descriptor) {
encodeIntElement(descriptor, 0, value.x)
encodeIntElement(descriptor, 1, value.y)
encodeIntElement(descriptor, 2, value.z)
}
override fun deserialize(decoder: Decoder): BlockPos =
decoder.decodeStructure(descriptor) {
var x = -1
var y = -1
var z = -1
while (true) {
when (val index = decodeElementIndex(descriptor)) {
0 -> x = decodeIntElement(descriptor, 0)
1 -> y = decodeIntElement(descriptor, 1)
2 -> z = decodeIntElement(descriptor, 2)
CompositeDecoder.DECODE_DONE -> break
else -> error("Unexpected index: $index")
}
}
BlockPos(x, y, z)
}
}
object UUIDAsString : KSerializer<UUID> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): UUID = UUID.fromString(decoder.decodeString().toDashedUUID())
override fun serialize(encoder: Encoder, value: UUID) = encoder.encodeString(value.toString())
}
@OptIn(ExperimentalEncodingApi::class)
fun Inventory.toMCItems() =
data.let { data ->
if (data.isEmpty()) {
emptyList()
} else {
val list = CompressedStreamTools.readCompressed(Base64.decode(data).inputStream()).getTagList("i", Constants.NBT.TAG_COMPOUND)
(0 until list.tagCount()).map { idx ->
list.getCompoundTagAt(idx).takeUnless { it.hasNoTags() }?.let { ItemStack.loadItemStackFromNBT(it) }
}
}
}
fun UUID.nonDashedString(): String {
return this.toString().replace("-", "")
}