Jump to content

How can I detect a change of enchantment properties?


Tavi007

Recommended Posts

Hi!

 

So I've been scraching my head a lot on how I could solve my problem. I have a capability for itemstacks, that is called defenseData and which is supposed to be used not only by me, but also by other devs (it's part of my API). Now my mod also uses enchantments to interact with the defense capability. For example if an armor piece has the fire protection enchantment, the capability should get the mapping "fire":factor, with factor being a calculated integer value. If the item already has fire reduction (i.e. it has it as base value), then the factor of the enchantment should be added to the base value.

 

Now I want to change the data in the capability, whenever a change of the enchantment happens in any form. This includes enchanting, disenchanting and also combing enchantments on an anvil. There is probably a lot more occasion, if we include other mods, which I would like to detect aswell. So how could I detect such a thing?

 

Alternatively I thought about adding the enchantment value, whenever the capability gets attached to the itemstack. However this will result in incorrect data, because then the enchantment data will also be added, if I move the stack in the inventory. In an attempt to solve this issue I added another capability, called areEnchantmentsApplied (a boolean with default value false), which is turned to true, after adding the enchantment data for the first time (even if there weren't any). Sadly this wasn't not working as indented, because the newly enchanted itemstack uses the same capability data from it's origin (rightfully tho), which means that the enchantment data never gets added to the new itemstack, because areEnchantmentsApplied was already true. It did however solve the issue with moving items in the inventory.

 

Currently I'm cheating my own system by getting a copy of the itemstack capability and adding the enchantment data onto it. It works, but that's not how I intented my API to work. Here you can see my current code. Any help is much appreciated. :)

Edited by Tavi007
Link to comment
Share on other sites

Unfortunately, I don't believe there is any such event or hook that calls when an enchantment is applied. You would probably need to add a new event that checks when an enchantment is applied to an item which would require a PR. There are other methods of achieving it that don't require a PR, but they all involve things that are not allowed on this forum.

Link to comment
Share on other sites

Do you mean, I should check for the enchantments of the itemstack in the attackCapabilitiesEvent? I tried that, but last time I checked, the itemstack hasn't had any enchantments added to it yet. So EnchantmentHelper.getEnchantments(itemstack) would always return an empty map.

 

If you were suggesting something different, then please elaborate a bit more, because I didn't understand, what you meant.

Link to comment
Share on other sites

I'm not sure, if this will solve my problem. I want to get rid of the method ElementalCombatAPI#getDefenseDataWithEnchantment, because unlike ElementalCombat#getDefenseData this only returns a copy of the capability and not a reference. If it wouldn't return a copy, it would add the enchantment data to the capability over and over. It's also important, that ElementalCombat#getDefenseData returns the reference, becuase otherwise the API would be useless.

 

How can I achieve, that the data from the enchantments gets applied only once?

Link to comment
Share on other sites

After sleeping a night I found a way to detect the change. It's not really necessary to keep track of the full itemstack, but rather just its enchantment mapping. So I did that in combination with a boolean, so it won't check over and over again. So yeah, no need for a new event hook.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.