Jump to content

More enchantment events


Tavi007

Recommended Posts

Hello!

For my mod I need an event, that detects a change of the enchantments on an itemstack. This would include adding/removing enchantments, but also increasing a level of an enchantment. I've been working on this problem on my own forge branch, but I ran into some problems.

 

There seems to be two methods, that writes the nbt data of an itemstack directly. These are ItemStack#addEnchantment (called by EnchantmentContainer) and EnchantmentHelper#setEnchantment (called by RepairContainer and GrindstoneContainer). These methods would be a good place for the forge event hook, because then the event would also fire, if any other mod would change the enchantments (as long as they are using these vanilla methods). However for my actual problem of detecting enchantment changes, they are not working as I hoped.

 

Currently I have 2 new events on my branch. The first is EnchantmentAddEvent, whichs fires in ItemStack#addEnchantment.

//the hook in ItemStack
public void addEnchantment(Enchantment ench, int level) {
	  net.minecraftforge.event.enchanting.EnchantmentAddEvent event = new net.minecraftforge.event.enchanting.EnchantmentAddEvent(this, ench, level);
	  net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event);
      ...
   }

//the event class
public class EnchantmentAddEvent extends net.minecraftforge.eventbus.api.Event {
	private ItemStack itemstack;
	private Enchantment ench;
	private int level;
	
	public EnchantmentAddEvent(ItemStack itemstack, Enchantment ench, int level) {
		this.itemstack = itemstack;
		this.ench = ench;
		this.level = level;
	}
}

This way, one could check the current enchantments on the itemstacks, because the new one hasn't be written to it. Sadly, one would also have to check, if ench can even be applied to the itemstack. Maybe It would be better to move the hook a little bit further down the line, so the enchantments are already applied to the itemstack. However then I would also have to add a variable for the old enchantment map.

 

The second event is EnchantmentSetEvent

//event hook in EnchantmentHelper
   public static void setEnchantments(Map<Enchantment, Integer> enchMap, ItemStack stack) {
	  net.minecraftforge.event.enchanting.EnchantmentSetEvent event = new net.minecraftforge.event.enchanting.EnchantmentSetEvent(stack, enchMap);
	  net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event);
     ...
     }

//event
public class EnchantmentSetEvent extends net.minecraftforge.eventbus.api.Event {
	@Nonnull
	private final ItemStack itemstack;
	private final Map<Enchantment, Integer> enchMap;
	
	public EnchantmentSetEvent(ItemStack itemstack, Map<Enchantment, Integer> enchMap)
    {
		this.itemstack = itemstack;
		Map<Enchantment, Integer> enchMapStack = EnchantmentHelper.getEnchantments(itemstack);
		
		
		this.enchMap = enchMap;
    }
	
	public ItemStack getItemStack() {
		return this.itemstack;
	}
	
	public Map<Enchantment, Integer> getEnchantments() {
		return enchMap;
	}
	
}

I noticed, that the setEnchantment method is called multiple times both on client and on server side (for both container) and I don't really get why this is happenning.

 

Currently I'm also missing a hook, that detects, if an enchantment gets removed. Looking at GrindstoneContainer#removeEnchantments I noticed, that enchantments can be removed, by deleting the childTag "Enchantments" (or "StorredEnchantments"). So I supposed I would have to add another hook in ItemStack#removeChildTag (together with checking the tag), right? Is it okay, if the patch includes an if statement?

 

To summerize: I want an event, that is essentially LivingEquipmentChangeEvent, but for ItemStacks. So the event would have 'Map<Enchantment, Integer> before' and 'Map<Enchantment, Integer> after' and the corresponding itemstack. This Event should then be fired, whenever the enchantment data of any ItemStack changes. Am I on the right track or should I tackle the problem completly differently?

Edited by Tavi007
Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

Why do you need this event?

see here for my actual problem: https://forums.minecraftforge.net/topic/92508-how-can-i-detect-a-change-of-enchantment-properties/?tab=comments#comment-426659

 

 

9 minutes ago, diesieben07 said:

And just a heads up: Saying that you are currently just coremodding to achieve this is not a good way to get your request accepted.

I was just tinkering a bit trying to understand how minecraft is handling enchantments. I honestly don't know of a usecase for this kinda event besides my own so far...

Edited by Tavi007
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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • BD303 merupakan salah satu situs slot mudah scatter paling populer dan digemari oleh kalangan slot online di tahun 2024 mainkan sekarang dengan kesempatan yang mudah menang jackpot jutaan rupiah.
  • Topics

×
×
  • Create New...

Important Information

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