How to add/give NBT tags to items through a procedure?

Started by Hollowplayz on

Topic category: Help with Minecraft modding (Java Edition)

Joined Jan 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to add/give NBT tags to items through a procedure?

Hey, I've seen a handful of topics about this but never got a clear answer for my case

 

How would you go about specifying or adding NBT tags to items through procedures?

 

For example, if you crafted something together and give the result an NBT tag, how do you check if that tag exists in a procedure?

 

And just to be specific, I mean items that you hold, not item entities

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
What are you trying to do…
Mon, 03/31/2025 - 10:49

What are you trying to do with it? Like custom data?
This worked in a custom code snippet for setting the unbreakable component,

itemstack.applyComponents(DataComponentMap.builder().set(DataComponents.UNBREAKABLE, new Unbreakable(true)).build());

Joined Jan 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm trying to put different…
Mon, 03/31/2025 - 17:24

I'm trying to put different tags on foods that correspond to potion effects

 

So if you combine food with a specific item, the result is the same food you used with an NBT tag that specifies which effect you put on it, then when you eat it, you get the effect

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Well you can use the "new"…
Mon, 03/31/2025 - 17:44

Well you can use the "new" consumable component to apply effects when you consume it, but it gets quite long

itemStack.applyComponents(DataComponentMap.builder().set(DataComponents.CONSUMABLE,
		new Consumable(
				2f, // consume time
				ItemUseAnimation.DRINK,
				SoundEvents.GENERIC_EAT, 
				true, // has consume particles
				java.util.Collections.singletonList(new ApplyStatusEffectsConsumeEffect(new MobEffectInstance(MobEffects.OOZING), 10f))
		)
).build());
Joined Jan 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
So I'm not super great at…
Mon, 03/31/2025 - 18:57

So I'm not super great at line coding, could you break this down for me and explain how to implement it? 

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
sure,implementing it is easy…
Mon, 03/31/2025 - 19:23

sure,
implementing it is easy, just put it in a custom code snippet like https://mcreator.net/comment/284160/#comment-284160
 

# Breakdown
so you can basically just ignore the first line itemStack.applyComponents(DataComponentMap.builder().set(DataComponents.CONSUMABLE,
It basically is just like telling the code that the following lines will be a consumable component which will be added to the item.

 

Then we create the actual consumable component, each parameter explained in comments (lines starting with //)

new Consumable(
		// The amount of time it takes to consume
		//   the item might be in ticks
		2f,
		
		// The animation to play when the item is
		//   being consumed, there are quite a few,
		//   the most common ones being;
		//   ItemUseAnimation.DRINK, and ItemUseAnimation.EAT
		ItemUseAnimation.DRINK, 
		
		// The sound that should be played when the item is consumed
		//   you can find a list of built-in sounds here,
		//   https://mappings.dev/1.21.4/net/minecraft/sounds/SoundEvents.html
		SoundEvents.GENERIC_EAT, 
		
		// If particles should be shown
		//   when the item is consumed
		true,
		
		// And finally the thing you're problably interested in,
		//   this defines what potion effect to apply when the
		//   item is consumed.
		//
		//   The first part of it,
		//   `java.util.Collections.singletonList` is just telling
		//   java to make the following into a list.
		//
		//   Then, `MobEffects.OOZING` sets the potion effect type
		//   to apply to the oozing effect,
		//   And, 10f sets the duration of that potion effect to
		//   10, probably ticks.
		java.util.Collections.singletonList(new ApplyStatusEffectsConsumeEffect(new MobEffectInstance(MobEffects.OOZING), 10f))
)

Hope that helps, feel free to ask if not!

Joined Jan 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yes, this helps a ton! Thank…
Mon, 03/31/2025 - 19:27

Yes, this helps a ton! Thank you so much!

I'll update the Forum once I get it working