Jump to content

[Solved] Detect if an ItemStack object matches certain tags


FrancoRoura
 Share

Recommended Posts

Hello everyone, I'm trying to override `Mob#wantsToPickUp` for custom living entities.

Is there a way to detect whether an object matches something like the `minecraft:logs` tag?

I'm currently doing this, but I'd like to go from O(n) to O(1) time complexity.

public static final Set<Item> WANTED_ITEMS;
// ...
@Override
public boolean wantsToPickUp(ItemStack itemStack) {
  return WANTED_ITEMS.contains(itemStack.getItem());
}

I was thinking it would be useful if there was some sort of way to do `itemStack.hasTag(Tags.LOGS);` or something along those lines, but I couldn't find any info about tags in the docs.
Also, this would make my mod compatible with trees from other mods, unlike a set where I hardcode all of the vanilla minecraft logs.

Thanks in advance,
Franco.

Edited by FrancoRoura
Solved
Link to comment
Share on other sites

If you've created your own tag

Then you can use the is() function on the ItemStack.

public static final TagKey<Item> myItemTag = ItemTags.create(new ResourceLocation("mymod", "my_tag_name"));
 
//later...
stack.is(myItemTag);

If you want to use a vanilla tag, they are all defined for you...

stack.is(ItemTags.LOGS)

I'm not sure this results in O(1) complexity, as it may use something like contains() under the hood, but at least your code is simple and if an optimization is made, you will take advantage of it.

  • Thanks 1
Link to comment
Share on other sites

  • FrancoRoura changed the title to [Solved] Detect if an ItemStack object matches certain tags

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
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.

 Share



×
×
  • Create New...

Important Information

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