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
  • Minecraft Forge
  • Suggestions
  • More enchantment events
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
Tavi007

More enchantment events

By Tavi007, October 21, 2020 in Suggestions

  • Reply to this topic
  • Start new topic

Recommended Posts

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 111 posts
Posted October 21, 2020 (edited)

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

Share this post


Link to post
Share on other sites

diesieben07    7711

diesieben07

diesieben07    7711

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7711
  • 56560 posts
Posted October 21, 2020

This event would theoretically fire a lot.

Why do you need this event?

 

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.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 111 posts
Posted October 21, 2020 (edited)
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 October 21, 2020 by Tavi007
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7711

diesieben07

diesieben07    7711

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7711
  • 56560 posts
Posted October 21, 2020

I've replied in your other thread. You don't need events for this.

  • 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

    • DARKHAWX
      [1.16] InputEvent.KeyInputEvent never fired

      By DARKHAWX · Posted 8 minutes ago

      Hey there,   I'm trying to setup a basic keybinding, but I can't seem to get the event to fire. Here's my code for subscribing to the event:   @Mod.EventBusSubscriber public class ModKeyInputs { @SubscribeEvent public void onKeyInput(InputEvent.KeyInputEvent event) { if (ModKeyBindings.OPEN_DIVINE_FAVOUR_RELATIONSHIP.isPressed()) { Minecraft.getInstance().displayGuiScreen(new DivineFavourRelationshipsScreen(Minecraft.getInstance().player)); } } }   Now I've put a breakpoint in the method there at the start, and I can never get it to fire. Am I subscribing to this event incorrectly?   As a side note, is there much difference between using @Mod.EventBusSubscriber on a class versus MOD_EVENT_BUS.register(ModKeyInputs.class); in the constructor of the main mod class?
    • Tavi007
      Question about itemstack capabilities of crafting results

      By Tavi007 · Posted 9 minutes ago

      just two maps and two strings. I use json files, so anyone can override them with datapacks. Basically each item, that should have default values, also has a corresponding json file. (if an item does not have a corresponding json file, then it will get default default values.)   The information in the files are loaded into a Map<ResourceLocation, DataFromJson> once when the server starts, so i can get them whenever i need them. When the attach event (or the item craft event) trigger i resolve the resourceLocation, get the DataFromJson and then set the itemstack capability. As long as the Map doesn't get humongous, this method shouldn't be a performance issure.
    • StormyRiley1
      When I generate a world it stays at 100%

      By StormyRiley1 · Posted 13 minutes ago

      https://drive.google.com/file/d/1uhUglwuMq_nroPQpDWXsBxggv9MwzXyS/view?usp=sharing there you go
    • diesieben07
      When I generate a world it stays at 100%

      By diesieben07 · Posted 20 minutes ago

      The debug.log from the logs folder.
    • StormyRiley1
      When I generate a world it stays at 100%

      By StormyRiley1 · Posted 21 minutes ago

      Which log
  • Topics

    • DARKHAWX
      0
      [1.16] InputEvent.KeyInputEvent never fired

      By DARKHAWX
      Started 8 minutes ago

    • Tavi007
      2
      Question about itemstack capabilities of crafting results

      By Tavi007
      Started 43 minutes ago

    • StormyRiley1
      4
      When I generate a world it stays at 100%

      By StormyRiley1
      Started 12 hours ago

    • Fizedi
      0
      Loot table change for chest in plains house

      By Fizedi
      Started 40 minutes ago

    • diseasedworm
      1
      A problem occurred running the Server launcher.java.lang.reflect.InvocationTargetException

      By diseasedworm
      Started 4 hours ago

  • Who's Online (See full list)

    • Beethoven92
    • Tavi007
    • randomdude12300
    • red-pxl
    • StormyRiley1
    • DARKHAWX
    • Fizedi
    • Choonster
    • CookieLukas
    • forgnog
    • diesieben07
    • discens
    • Tessa
  • All Activity
  • Home
  • Minecraft Forge
  • Suggestions
  • More enchantment events
  • Theme

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