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.

×
×
  • Create New...

Important Information

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