@@ -3,6 +3,8 @@ package dev.sterner.api
3
3
import com.sammy.malum.client.VoidRevelationHandler
4
4
import com.sammy.malum.common.container.WeaversWorkbenchContainer.component
5
5
import com.sammy.malum.core.systems.recipe.SpiritWithCount
6
+ import dev.sterner.api.item.ItemAbility
7
+ import dev.sterner.api.item.ItemAbilityWithLevel
6
8
import dev.sterner.listener.EnchantSpiritDataReloadListener
7
9
import dev.sterner.registry.VoidBoundComponentRegistry
8
10
import dev.sterner.registry.VoidBoundItemRegistry
@@ -13,6 +15,7 @@ import net.minecraft.core.registries.BuiltInRegistries
13
15
import net.minecraft.network.chat.Component
14
16
import net.minecraft.world.entity.EquipmentSlot
15
17
import net.minecraft.world.entity.player.Player
18
+ import net.minecraft.world.item.ItemStack
16
19
import net.minecraft.world.item.enchantment.Enchantment
17
20
import net.minecraft.world.level.Level
18
21
import team.lodestar.lodestone.helpers.TrinketsHelper
@@ -139,4 +142,55 @@ object VoidBoundApi {
139
142
}
140
143
return false
141
144
}
145
+
146
+ fun getItemAbility (stack : ItemStack ): List <ItemAbilityWithLevel > {
147
+ val abilities = mutableListOf<ItemAbilityWithLevel >()
148
+ val tag = stack.tag ? : return abilities // Return empty if no NBT
149
+
150
+ val abilitiesTag = tag.getList(" Abilities" , 10 ) // 10 is the NBT type for CompoundTag
151
+ for (i in 0 until abilitiesTag.size) {
152
+ val abilityTag = abilitiesTag.getCompound(i)
153
+ val ability = ItemAbilityWithLevel .readNbt(abilityTag)
154
+ abilities.add(ability)
155
+ }
156
+ return abilities
157
+ }
158
+
159
+ // Function to add an ItemAbilityWithLevel to an ItemStack's NBT
160
+ fun addItemAbility (stack : ItemStack , abilityWithLevel : ItemAbilityWithLevel ) {
161
+ val tag = stack.orCreateTag // Ensures the stack has NBT
162
+ val abilitiesTag = tag.getList(" Abilities" , 10 ) // Fetch or create list
163
+
164
+ // Check if ability already exists, if so, skip adding a duplicate
165
+ for (i in 0 until abilitiesTag.size) {
166
+ val abilityTag = abilitiesTag.getCompound(i)
167
+ val existingAbility = ItemAbilityWithLevel .readNbt(abilityTag)
168
+ if (existingAbility.itemAbility == abilityWithLevel.itemAbility) {
169
+ return // Ability already exists, exit without adding
170
+ }
171
+ }
172
+
173
+ // Add new ability
174
+ abilitiesTag.add(abilityWithLevel.writeNbt())
175
+ tag.put(" Abilities" , abilitiesTag)
176
+ }
177
+
178
+ // Function to modify the level of an existing ItemAbility in NBT
179
+ fun modifyItemAbilityLevel (stack : ItemStack , itemAbility : ItemAbility , newLevel : Int ) {
180
+ val tag = stack.tag ? : return // No NBT, nothing to modify
181
+ val abilitiesTag = tag.getList(" Abilities" , 10 )
182
+
183
+ // Find the ability and modify its level
184
+ for (i in 0 until abilitiesTag.size) {
185
+ val abilityTag = abilitiesTag.getCompound(i)
186
+ val ability = ItemAbilityWithLevel .readNbt(abilityTag)
187
+ if (ability.itemAbility == itemAbility) {
188
+ // Modify the level and update the NBT
189
+ abilityTag.putInt(" Level" , newLevel)
190
+ abilitiesTag[i] = abilityTag // Replace the modified ability in the list
191
+ tag.put(" Abilities" , abilitiesTag)
192
+ return
193
+ }
194
+ }
195
+ }
142
196
}
0 commit comments