Skip to content

Commit b71a3c7

Browse files
committed
rework knowledge api
1 parent 13d4760 commit b71a3c7

File tree

12 files changed

+227
-208
lines changed

12 files changed

+227
-208
lines changed

src/main/kotlin/dev/sterner/api/item/ItemAbility.kt

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dev.sterner.common.item.equipment.ichor.IchoriumEdge
55
import dev.sterner.common.item.equipment.ichor.IchoriumTerraformer
66
import dev.sterner.common.item.equipment.ichor.IchoriumVorpal
77
import net.minecraft.nbt.CompoundTag
8+
import net.minecraft.nbt.ListTag
89
import net.minecraft.util.StringRepresentable
910
import net.minecraft.world.item.Item
1011

@@ -26,14 +27,35 @@ enum class ItemAbility : StringRepresentable {
2627
return this.name.lowercase()
2728
}
2829

29-
fun writeNbt(): CompoundTag {
30+
fun writeSingleNbt(): CompoundTag {
3031
val tag = CompoundTag()
3132
tag.putString("Ability", name)
3233
return tag
3334
}
3435

3536
companion object {
36-
fun readNbt(abilityTag: CompoundTag): ItemAbility {
37+
38+
fun writeToNbt(unlockedItemAbilities: MutableSet<ItemAbility>, tag: CompoundTag) {
39+
val unlockedList = ListTag()
40+
unlockedItemAbilities.forEach { unlockedTag ->
41+
val abilityTag = unlockedTag.writeSingleNbt()
42+
unlockedList.add(abilityTag)
43+
}
44+
tag.put("UnlockedItems", unlockedList)
45+
}
46+
47+
fun readNbt(tag: CompoundTag): MutableSet<ItemAbility> {
48+
val unlockedItemAbilities = mutableSetOf<ItemAbility>()
49+
val unlockedList = tag.getList("UnlockedItems", 10)
50+
for (i in 0 until unlockedList.size) {
51+
val item = unlockedList.getCompound(i)
52+
val itemAbility = ItemAbility.readSingleNbt(item)
53+
unlockedItemAbilities.add(itemAbility)
54+
}
55+
return unlockedItemAbilities
56+
}
57+
58+
fun readSingleNbt(abilityTag: CompoundTag): ItemAbility {
3759
return valueOf(abilityTag.getString("Ability"))
3860
}
3961

@@ -62,5 +84,7 @@ enum class ItemAbility : StringRepresentable {
6284

6385
return list
6486
}
87+
88+
6589
}
6690
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dev.sterner.api.revelation
2+
3+
import net.minecraft.nbt.CompoundTag
4+
import net.minecraft.nbt.ListTag
5+
6+
data class KnowledgeData(
7+
val knowledgeType: KnowledgeType,
8+
var thoughtSent: Boolean = false
9+
) {
10+
companion object {
11+
fun readFromNbt(tag: CompoundTag): MutableSet<KnowledgeData> {
12+
val dataSet = mutableSetOf<KnowledgeData>()
13+
val list = tag.getList("KnowledgeDataSet", 10)
14+
for (i in 0 until list.size) {
15+
val knowledgeTag = list.getCompound(i)
16+
val knowledgeType = KnowledgeType.valueOf(knowledgeTag.getString("KnowledgeType"))
17+
val thoughtSent = knowledgeTag.getBoolean("ThoughtSent")
18+
dataSet.add(KnowledgeData(knowledgeType, thoughtSent))
19+
}
20+
return dataSet
21+
}
22+
23+
fun writeToNbt(dataSet: MutableSet<KnowledgeData>, tag: CompoundTag) {
24+
val list = ListTag()
25+
dataSet.forEach { data ->
26+
val knowledgeTag = CompoundTag()
27+
knowledgeTag.putString("KnowledgeType", data.knowledgeType.name)
28+
knowledgeTag.putBoolean("ThoughtSent", data.thoughtSent)
29+
list.add(knowledgeTag)
30+
}
31+
tag.put("KnowledgeDataSet", list)
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.sterner.api.revelation
2+
3+
enum class KnowledgeType {
4+
WEEPING_WELL,
5+
END,
6+
NETHER,
7+
GRIMCULT,
8+
ICHOR;
9+
10+
companion object {
11+
val prerequisites = mapOf(
12+
NETHER to setOf(WEEPING_WELL),
13+
END to setOf(WEEPING_WELL),
14+
GRIMCULT to setOf(END, NETHER),
15+
ICHOR to setOf(GRIMCULT)
16+
)
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dev.sterner.api.revelation
2+
3+
import net.minecraft.nbt.CompoundTag
4+
import net.minecraft.nbt.ListTag
5+
import net.minecraft.network.chat.Component
6+
7+
data class ThoughtData(var duration: Int, var delay: Int) {
8+
9+
companion object {
10+
fun readFromNbt(tag: CompoundTag): MutableMap<Component, ThoughtData> {
11+
val thoughtsQueue = mutableMapOf<Component, ThoughtData>()
12+
val thoughtsList = tag.getList("ThoughtsQueue", 10) // 10 = CompoundTag type
13+
for (i in 0 until thoughtsList.size) {
14+
val thoughtTag = thoughtsList.getCompound(i)
15+
val thought = Component.Serializer.fromJson(thoughtTag.getString("Text"))
16+
val duration = thoughtTag.getInt("Duration")
17+
val delay = thoughtTag.getInt("Delay")
18+
if (thought != null) {
19+
thoughtsQueue[thought] = ThoughtData(duration, delay)
20+
}
21+
}
22+
return thoughtsQueue
23+
}
24+
25+
fun writeToNbt(thoughtsQueue: Map<Component, ThoughtData>, tag: CompoundTag) {
26+
val thoughtsList = ListTag()
27+
thoughtsQueue.forEach { (thought, data) ->
28+
val thoughtTag = CompoundTag().apply {
29+
putString("Text", Component.Serializer.toJson(thought))
30+
putInt("Duration", data.duration)
31+
putInt("Delay", data.delay)
32+
}
33+
thoughtsList.add(thoughtTag)
34+
}
35+
tag.put("ThoughtsQueue", thoughtsList)
36+
}
37+
}
38+
}

src/main/kotlin/dev/sterner/api/util/VoidBoundItemUtils.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object VoidBoundItemUtils {
113113
val abilitiesTag = tag.getList("Abilities", 10) // 10 is the NBT type for CompoundTag
114114
for (i in 0 until abilitiesTag.size) {
115115
val abilityTag = abilitiesTag.getCompound(i)
116-
val ability = ItemAbility.readNbt(abilityTag)
116+
val ability = ItemAbility.readSingleNbt(abilityTag)
117117
abilities.add(ability) // Add to the set, prevents duplicates
118118
}
119119

@@ -128,14 +128,14 @@ object VoidBoundItemUtils {
128128
// Check if ability already exists, if so, exit
129129
for (i in 0 until abilitiesTag.size) {
130130
val abilityTag = abilitiesTag.getCompound(i)
131-
val existingAbility = ItemAbility.readNbt(abilityTag)
131+
val existingAbility = ItemAbility.readSingleNbt(abilityTag)
132132
if (existingAbility == ability) {
133133
return // Ability already exists, exit without adding
134134
}
135135
}
136136

137137
// Add new ability
138-
abilitiesTag.add(ability.writeNbt())
138+
abilitiesTag.add(ability.writeSingleNbt())
139139
tag.put("Abilities", abilitiesTag)
140140

141141
// Optionally set the new ability as the active one

src/main/kotlin/dev/sterner/api/util/VoidBoundPlayerUtils.kt

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.sterner.api.util
22

33
import dev.sterner.api.item.ItemAbility
4+
import dev.sterner.api.revelation.KnowledgeType
45
import dev.sterner.registry.VoidBoundComponentRegistry
56
import dev.sterner.registry.VoidBoundItemRegistry
67
import net.minecraft.client.Minecraft
@@ -44,29 +45,11 @@ object VoidBoundPlayerUtils {
4445
return !comp.isPosBoundToAnotherPlayer(player, GlobalPos.of(player.level().dimension(), blockPos))
4546
}
4647

47-
fun hasTearKnowledgeClient(): Boolean {
48+
fun hasKnowledge(knowledge: KnowledgeType): Boolean {
4849
val player = Minecraft.getInstance().player
4950
if (player != null) {
5051
val comp = VoidBoundComponentRegistry.VOID_BOUND_REVELATION_COMPONENT.get(player)
51-
return comp.isTearKnowledgeComplete()
52-
}
53-
return false
54-
}
55-
56-
fun hasIchorKnowledgeClient(): Boolean {
57-
val player = Minecraft.getInstance().player
58-
if (player != null) {
59-
val comp = VoidBoundComponentRegistry.VOID_BOUND_REVELATION_COMPONENT.get(player)
60-
return comp.hasIchorKnowledge && comp.hasGrimcultKnowledge && comp.hasWellKnowledge && comp.hasEndKnowledge && comp.hasNetherKnowledge
61-
}
62-
return false
63-
}
64-
65-
fun hasGrimcultKnowledgeClient(): Boolean {
66-
val player = Minecraft.getInstance().player
67-
if (player != null) {
68-
val comp = VoidBoundComponentRegistry.VOID_BOUND_REVELATION_COMPONENT.get(player)
69-
return comp.hasGrimcultKnowledge && comp.hasWellKnowledge && comp.hasEndKnowledge && comp.hasNetherKnowledge
52+
return comp.hasKnowledge(knowledge)
7053
}
7154
return false
7255
}
@@ -77,20 +60,15 @@ object VoidBoundPlayerUtils {
7760
}
7861
}
7962

80-
fun hasNetherMessage(): Boolean {
63+
fun hasThoughtSentOrUnlocked(knowledge: KnowledgeType) : Boolean {
8164
val component =
8265
VoidBoundComponentRegistry.VOID_BOUND_REVELATION_COMPONENT.maybeGet(Minecraft.getInstance().player)
8366
if (component.isPresent) {
84-
return component.get().hasReceivedNetherMessage
85-
}
86-
return false
87-
}
67+
if (component.get().hasKnowledge(knowledge)) {
68+
return true
69+
}
8870

89-
fun hasEndMessage(): Boolean {
90-
val component =
91-
VoidBoundComponentRegistry.VOID_BOUND_REVELATION_COMPONENT.maybeGet(Minecraft.getInstance().player)
92-
if (component.isPresent) {
93-
return component.get().hasReceivedEndMessage
71+
return component.get().hasThoughtSentForKnowledge(knowledge)
9472
}
9573
return false
9674
}

src/main/kotlin/dev/sterner/client/event/MalumCodexEvent.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.sammy.malum.client.screen.codex.screens.ArcanaProgressionScreen
1414
import com.sammy.malum.client.screen.codex.screens.VoidProgressionScreen
1515
import dev.sterner.VoidBound
1616
import dev.sterner.VoidBound.modid
17+
import dev.sterner.api.revelation.KnowledgeType
1718
import dev.sterner.api.util.VoidBoundPlayerUtils
1819
import dev.sterner.registry.VoidBoundItemRegistry
1920
import net.minecraft.nbt.CompoundTag
@@ -163,7 +164,7 @@ object MalumCodexEvent {
163164
)
164165
)
165166
.setEntryVisibleWhen {
166-
VoidBoundPlayerUtils.hasTearKnowledgeClient()
167+
VoidBoundPlayerUtils.hasKnowledge(KnowledgeType.GRIMCULT)
167168
}
168169

169170
}
@@ -184,7 +185,7 @@ object MalumCodexEvent {
184185
)
185186
}.addPage(HeadlineTextPage("void.thoughts_about_nether", "void.thoughts_about_nether.1"))
186187
.setEntryVisibleWhen {
187-
VoidBoundPlayerUtils.hasNetherMessage()
188+
VoidBoundPlayerUtils.hasThoughtSentOrUnlocked(KnowledgeType.NETHER)
188189
}
189190
}
190191

@@ -204,7 +205,7 @@ object MalumCodexEvent {
204205
)
205206
}.addPage(HeadlineTextPage("void.thoughts_about_end", "void.thoughts_about_end.1"))
206207
.setEntryVisibleWhen {
207-
VoidBoundPlayerUtils.hasEndMessage()
208+
VoidBoundPlayerUtils.hasThoughtSentOrUnlocked(KnowledgeType.END)
208209
}
209210
}
210211

@@ -225,7 +226,7 @@ object MalumCodexEvent {
225226
)
226227
)
227228
.setEntryVisibleWhen {
228-
VoidBoundPlayerUtils.hasGrimcultKnowledgeClient()
229+
VoidBoundPlayerUtils.hasThoughtSentOrUnlocked(KnowledgeType.GRIMCULT)
229230
}
230231
}
231232

@@ -245,7 +246,7 @@ object MalumCodexEvent {
245246
)
246247
)
247248
.setEntryVisibleWhen {
248-
VoidBoundPlayerUtils.hasIchorKnowledgeClient()
249+
VoidBoundPlayerUtils.hasKnowledge(KnowledgeType.ICHOR)
249250
}
250251
}
251252

@@ -265,7 +266,7 @@ object MalumCodexEvent {
265266
)
266267
)
267268
.setEntryVisibleWhen {
268-
VoidBoundPlayerUtils.hasIchorKnowledgeClient()
269+
VoidBoundPlayerUtils.hasKnowledge(KnowledgeType.ICHOR)
269270
}
270271
}
271272

@@ -285,7 +286,7 @@ object MalumCodexEvent {
285286
)
286287
)
287288
.setEntryVisibleWhen {
288-
VoidBoundPlayerUtils.hasIchorKnowledgeClient()
289+
VoidBoundPlayerUtils.hasKnowledge(KnowledgeType.ICHOR)
289290
}
290291
}
291292

@@ -305,7 +306,7 @@ object MalumCodexEvent {
305306
)
306307
)
307308
.setEntryVisibleWhen {
308-
VoidBoundPlayerUtils.hasIchorKnowledgeClient()
309+
VoidBoundPlayerUtils.hasKnowledge(KnowledgeType.ICHOR)
309310
}
310311
}
311312
}

0 commit comments

Comments
 (0)