Jump to content

[1.18.2] Get TagKey object from string


XPModder
 Share

Recommended Posts

I am trying to perform some operations with all items with a certain tag.

At the moment I am doing this by iterating over all items with the tag using ForgeRegistries.ITEMS.tags().getTag().

This works, but I am passing in the specific tag directly.

Now I am in the process of moving some things into a config file. In the config file file I want to have a list of items or tags (aka the list will contain both items and tags). I have the list working with just items, but now I need to add the tags.

So the question is: How do I get a TagKey object that can be passed into the getTag() method mentioned above from its string representation?

So as an example I am reading "forge:logs" from the config and want to get ItemTags.LOGS. How can I do this?

Looking into it, I think I could do it by iterating over the output of ForgeRegistries.ITEMS.tags().getTagNames() and then somehow comparing those to the string, but I don't even see an easy way to do that. Also I would like to avoid iterating over a potentially large list, if there is a better way of doing it...

 

I have looked into the ITagManager and TagKey classes, but haven't found a solution.

Link to comment
Share on other sites

        var tagManager = ForgeRegistries.ITEMS.tags();
        var tagKey = tagManager.createTagKey(new ResourceLocation("minecraft:logs"));
        var tag = tagManager.getTag(tagKey);

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

7 hours ago, warjort said:
        var tagManager = ForgeRegistries.ITEMS.tags();
        var tagKey = tagManager.createTagKey(new ResourceLocation("minecraft:logs"));
        var tag = tagManager.getTag(tagKey);

 

Thank you!

That worked!

I had seen the createTagKey method before, but I thought it was there to create an entirely new tag.... I guess the method is simply named weird or I haven't fully understood some aspect of how these tags work in the background... I would have expected a method doing this to be named 'getTagKey' or 'getKey' or similar...

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