1
+ package dev.sterner.api
2
+
3
+ import com.sammy.malum.client.VoidRevelationHandler
4
+ import com.sammy.malum.common.container.WeaversWorkbenchContainer.component
5
+ import com.sammy.malum.core.systems.recipe.SpiritWithCount
6
+ import dev.sterner.api.item.ItemAbility
7
+ import dev.sterner.api.item.ItemAbilityWithLevel
8
+ import dev.sterner.listener.EnchantSpiritDataReloadListener
9
+ import dev.sterner.registry.VoidBoundComponentRegistry
10
+ import dev.sterner.registry.VoidBoundItemRegistry
11
+ import net.minecraft.client.Minecraft
12
+ import net.minecraft.core.BlockPos
13
+ import net.minecraft.core.GlobalPos
14
+ import net.minecraft.core.registries.BuiltInRegistries
15
+ import net.minecraft.network.chat.Component
16
+ import net.minecraft.world.entity.EquipmentSlot
17
+ import net.minecraft.world.entity.player.Player
18
+ import net.minecraft.world.item.ItemStack
19
+ import net.minecraft.world.item.enchantment.Enchantment
20
+ import net.minecraft.world.level.Level
21
+ import team.lodestar.lodestone.helpers.TrinketsHelper
22
+
23
+ object VoidBoundApi {
24
+
25
+ /* *
26
+ * Returns true if a client player has the hallowed goggles or monocle equipped
27
+ */
28
+ fun hasGoggles (): Boolean {
29
+ val player = Minecraft .getInstance().player
30
+ if (player != null ) {
31
+ val bl = TrinketsHelper .hasTrinketEquipped(player, VoidBoundItemRegistry .HALLOWED_MONOCLE .get())
32
+ val bl2 = Minecraft .getInstance().player!! .getItemBySlot(EquipmentSlot .HEAD )
33
+ .`is `(
34
+ VoidBoundItemRegistry .HALLOWED_GOGGLES .get()
35
+ )
36
+ return bl || bl2
37
+ }
38
+ return false
39
+ }
40
+
41
+ /* *
42
+ * Returns true if a player has the hallowed goggles or monocle equipped
43
+ */
44
+ fun hasGoggles (player : Player ): Boolean {
45
+ val bl = TrinketsHelper .hasTrinketEquipped(player, VoidBoundItemRegistry .HALLOWED_MONOCLE .get())
46
+ val bl2 = Minecraft .getInstance().player!! .getItemBySlot(EquipmentSlot .HEAD )
47
+ .`is `(
48
+ VoidBoundItemRegistry .HALLOWED_GOGGLES .get()
49
+ )
50
+ return bl || bl2
51
+ }
52
+
53
+ /* *
54
+ * Returns how many spirits of each kind a enchantment is worth for the osmotic enchanter
55
+ */
56
+ fun getSpiritFromEnchant (enchantment : Enchantment , level : Int ): List <SpiritWithCount > {
57
+
58
+ val reg = BuiltInRegistries .ENCHANTMENT .getKey(enchantment)
59
+ val list = EnchantSpiritDataReloadListener .ENCHANTING_DATA [reg]
60
+ val out = mutableListOf<SpiritWithCount >()
61
+ if (list != null ) {
62
+ for (spiritIn in list.spirits) {
63
+ out .add(SpiritWithCount (spiritIn.type, spiritIn.count * level))
64
+ }
65
+ }
66
+
67
+ return out
68
+ }
69
+
70
+ /* *
71
+ * Returns false if the block being broken is warded by another player
72
+ */
73
+ fun canPlayerBreakBlock (level : Level , player : Player , blockPos : BlockPos ): Boolean {
74
+ val comp = VoidBoundComponentRegistry .VOID_BOUND_WORLD_COMPONENT .get(level)
75
+ if (comp.isEmpty()) {
76
+ return true
77
+ }
78
+
79
+ return ! comp.isPosBoundToAnotherPlayer(player, GlobalPos .of(player.level().dimension(), blockPos))
80
+ }
81
+
82
+ /* *
83
+ * Returns false if the block being broken is warded by any player
84
+ */
85
+ fun canBlockBreak (level : Level , blockPos : BlockPos ): Boolean {
86
+ val comp = VoidBoundComponentRegistry .VOID_BOUND_WORLD_COMPONENT .get(level)
87
+ if (comp.isEmpty()) {
88
+ return true
89
+ }
90
+
91
+ if (comp.hasBlockPos(GlobalPos .of(level.dimension(), blockPos))) {
92
+ return false
93
+ }
94
+ return true
95
+ }
96
+
97
+ fun hasTearKnowledgeClient (): Boolean {
98
+ val player = Minecraft .getInstance().player
99
+ if (player != null ) {
100
+ val comp = VoidBoundComponentRegistry .VOID_BOUND_REVELATION_COMPONENT .get(player)
101
+ return comp.isTearKnowledgeComplete()
102
+ }
103
+ return false
104
+ }
105
+
106
+ fun addThought (player : Player , text : Component , duration : Int = 20 * 5){
107
+ VoidBoundComponentRegistry .VOID_BOUND_REVELATION_COMPONENT .maybeGet(player).ifPresent {
108
+ it.addThought(text, duration, 20 * 5 )
109
+ }
110
+ }
111
+
112
+ fun getItemAbility (stack : ItemStack ): List <ItemAbilityWithLevel > {
113
+ val abilities = mutableListOf<ItemAbilityWithLevel >()
114
+ val tag = stack.tag ? : return abilities // Return empty if no NBT
115
+
116
+ val abilitiesTag = tag.getList(" Abilities" , 10 ) // 10 is the NBT type for CompoundTag
117
+ for (i in 0 until abilitiesTag.size) {
118
+ val abilityTag = abilitiesTag.getCompound(i)
119
+ val ability = ItemAbilityWithLevel .readNbt(abilityTag)
120
+ abilities.add(ability)
121
+ }
122
+ return abilities
123
+ }
124
+
125
+ // Function to add an ItemAbilityWithLevel to an ItemStack's NBT
126
+ fun addItemAbility (stack : ItemStack , abilityWithLevel : ItemAbilityWithLevel ) {
127
+ val tag = stack.orCreateTag // Ensures the stack has NBT
128
+ val abilitiesTag = tag.getList(" Abilities" , 10 ) // Fetch or create list
129
+
130
+ // Check if ability already exists, if so, skip adding a duplicate
131
+ for (i in 0 until abilitiesTag.size) {
132
+ val abilityTag = abilitiesTag.getCompound(i)
133
+ val existingAbility = ItemAbilityWithLevel .readNbt(abilityTag)
134
+ if (existingAbility.itemAbility == abilityWithLevel.itemAbility) {
135
+ return // Ability already exists, exit without adding
136
+ }
137
+ }
138
+
139
+ // Add new ability
140
+ abilitiesTag.add(abilityWithLevel.writeNbt())
141
+ tag.put(" Abilities" , abilitiesTag)
142
+ }
143
+
144
+ // Function to modify the level of an existing ItemAbility in NBT
145
+ fun modifyItemAbilityLevel (stack : ItemStack , itemAbility : ItemAbility , newLevel : Int ) {
146
+ val tag = stack.tag ? : return // No NBT, nothing to modify
147
+ val abilitiesTag = tag.getList(" Abilities" , 10 )
148
+
149
+ // Find the ability and modify its level
150
+ for (i in 0 until abilitiesTag.size) {
151
+ val abilityTag = abilitiesTag.getCompound(i)
152
+ val ability = ItemAbilityWithLevel .readNbt(abilityTag)
153
+ if (ability.itemAbility == itemAbility) {
154
+ // Modify the level and update the NBT
155
+ abilityTag.putInt(" Level" , newLevel)
156
+ abilitiesTag[i] = abilityTag // Replace the modified ability in the list
157
+ tag.put(" Abilities" , abilitiesTag)
158
+ return
159
+ }
160
+ }
161
+ }
162
+
163
+ fun hasItemAbility (stack : ItemStack , ability : ItemAbility ): Boolean {
164
+ return ! getItemAbility(stack).none { it.itemAbility == ability }
165
+ }
166
+ }
0 commit comments