Jump to content

[1.18.2] Get TagKey object from string


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);



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

  • 7 months later...

It's kind of a convoluted process unlike what was evidently available in 1.18.1. They really made a breaking change according to what Kaupenjoe demonstrated in a 1.18.1 tutorial.

I created something similar to what Kaupenjoe has here.

package com.badkraft.foundations.tags;

import com.badkraft.foundations.Foundations;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.tags.ITag;

public class ModTags {
    public static class Items {

        public static final TagKey<Item> FLINT_TOOL_MATERIALS = bind("flint_tool_materials");
        public static final ITag<Item> STONE_ITEMS = createTag("stone");
        public static final ITag<Item> MASONRY_ITEMS = createTag("masonry");

        private static ITag<Item> createTag(String name) {
            return ForgeRegistries.ITEMS.tags().getTag(tag(name));
        private static TagKey<Item> tag(String name) {
            return ItemTags.create(new ResourceLocation(Foundations.MOD_ID, name));
        private static TagKey<Item> forgeTag(String name) {
            return ItemTags.create(new ResourceLocation("forge", name));

    private static TagKey<Item> bind(String name) {
        return TagKey.create(Registry.ITEM_REGISTRY, new ResourceLocation(name));


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.

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.