Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • How can I detect a change of enchantment properties?
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
Tavi007

How can I detect a change of enchantment properties?

By Tavi007, October 16, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

Tavi007    1

Tavi007

Tavi007    1

  • Stone Miner
  • Tavi007
  • Members
  • 1
  • 93 posts
Posted October 16, 2020 (edited)

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 October 16, 2020 by Tavi007
  • Quote

Share this post


Link to post
Share on other sites

ChampionAsh5357    160

ChampionAsh5357

ChampionAsh5357    160

  • World Shaper
  • ChampionAsh5357
  • Members
  • 160
  • 1008 posts
Posted October 16, 2020

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.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    1

Tavi007

Tavi007    1

  • Stone Miner
  • Tavi007
  • Members
  • 1
  • 93 posts
Posted October 16, 2020

PR stands for pull request, right? I don't have a problem with creating a new event hook, but I would have to learn how to do it. ^^"

 

Are there any 'rules', that should be followed? 

  • Quote

Share this post


Link to post
Share on other sites

ChampionAsh5357    160

ChampionAsh5357

ChampionAsh5357    160

  • World Shaper
  • ChampionAsh5357
  • Members
  • 160
  • 1008 posts
Posted October 16, 2020
1 hour ago, Tavi007 said:

Are there any 'rules', that should be followed? 

https://mcforge.readthedocs.io/en/latest/forgedev/prguidelines/

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7588

diesieben07

diesieben07    7588

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7588
  • 54951 posts
Posted October 21, 2020

I am not sure why you need to react to enchantment changes here. You should be able to have access to the ItemStack inside the capability, so you can just access it's enchantments whenever you need to and act accordingly. There is no need to "change data" when the enchantments change, just compute whatever data you need when it is asked for based on the current ItemStack.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    1

Tavi007

Tavi007    1

  • Stone Miner
  • Tavi007
  • Members
  • 1
  • 93 posts
Posted October 21, 2020

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.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7588

diesieben07

diesieben07    7588

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7588
  • 54951 posts
Posted October 21, 2020

No, in AttachCapabilitiesEvent you keep the ItemStack around, store it in the attached capability so that you can reference it later.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    1

Tavi007

Tavi007    1

  • Stone Miner
  • Tavi007
  • Members
  • 1
  • 93 posts
Posted October 21, 2020

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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7588

diesieben07

diesieben07    7588

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7588
  • 54951 posts
Posted October 21, 2020

I am not sure we are understanding each other. I am asking you to have your capability keep track of the ItemStack, then whenever it is asked for any data via it's getters it can enrich the returned data with the enchantments.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    1

Tavi007

Tavi007    1

  • Stone Miner
  • Tavi007
  • Members
  • 1
  • 93 posts
Posted October 22, 2020

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.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • poopoodice
      [1.16.4] How i can open a container by clicking on my mob

      By poopoodice · Posted 4 minutes ago

      No, it is looking for a container provider, so just pass an instance of it.
    • poopoodice
      [1.15.2] Render as 2D icon in GUI, 3D model in hand

      By poopoodice · Posted 5 minutes ago

      You can replace with your own model through ModelBakeEvent.
    • DaemonUmbra
      1.16.4 (Heavily decent modded with around 150ish mods) Crash with exit code -1 most of the time and sometimes exit code 0.

      By DaemonUmbra · Posted 9 minutes ago

      Use a paste site to upload the whole thing. (list in my signature)
    • Klarks
      [1.16.4] How i can open a container by clicking on my mob

      By Klarks · Posted 20 minutes ago

      I ve seen an example of it but i dont understand how i can apply this to my problem. Do I need to call createmenu from new class in networkhooks.opengui method? 
    • BobbyLikesCake
      1.16.4 (Heavily decent modded with around 150ish mods) Crash with exit code -1 most of the time and sometimes exit code 0.

      By BobbyLikesCake · Posted 24 minutes ago

      Sorry, Let me get the full one. Also, it is quite big... Which part do you need exactly since I cant upload all of it (Too big to upload)
  • Topics

    • Klarks
      19
      [1.16.4] How i can open a container by clicking on my mob

      By Klarks
      Started Yesterday at 09:56 PM

    • Woodside
      6
      [1.15.2] Render as 2D icon in GUI, 3D model in hand

      By Woodside
      Started 2 hours ago

    • BobbyLikesCake
      5
      1.16.4 (Heavily decent modded with around 150ish mods) Crash with exit code -1 most of the time and sometimes exit code 0.

      By BobbyLikesCake
      Started 2 hours ago

    • X4Ghost
      1
      Unable to install

      By X4Ghost
      Started 42 minutes ago

    • clermont.holiday
      1
      1.16.3 forge crashing

      By clermont.holiday
      Started 4 hours ago

  • Who's Online (See full list)

    • Woodside
    • X4Ghost
    • CosmosBench244
    • Mango106
    • TheidenHD
    • BobbyLikesCake
    • DaemonUmbra
    • jbredwards
    • GamingTiger101
    • Carzival
    • MetalMetagross
    • The_Raven
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • How can I detect a change of enchantment properties?
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community