태그는 여러 아이템을 하나의 그룹으로 묶는 데이터팩 기반 메커니즘입니다. 레시피에서 "나무 판자 아무거나"를 허용하거나, 코드에서 "보석 계열 아이템인지" 검사할 때 태그를 씁니다. 하드코딩 없이 JSON 파일 하나로 그룹을 정의하고, 다른 모드가 그 그룹에 자신의 아이템을 추가할 수도 있습니다.
// examplemod-template-26.1.2/src/main/java/com/example/examplemod/ExampleMod.javaimport net.minecraft.tags.ItemTags;import net.minecraft.tags.TagKey;import net.minecraft.resources.Identifier;public class ExampleMod { public static final TagKey<Item> GEMS = ItemTags.create(Identifier.fromNamespaceAndPath(MODID, "gems"));}
ItemTags.create()는 TagKey<Item>을 반환합니다. NeoForge 26.1.2에서는 Identifier.fromNamespaceAndPath()를 써야 합니다. 구버전의 ResourceLocation 클래스는 삭제되어 net.minecraft.resources.Identifier로 대체되었고, 두 인자를 받는 생성자 new ResourceLocation(namespace, path)도 더 이상 존재하지 않습니다.
ItemStack.is(TagKey<Item>) 메서드로 아이템이 특정 태그에 속하는지 확인합니다.
// 아이템 사용 이벤트 등에서ItemStack stack = player.getMainHandItem();if (stack.is(ExampleMod.GEMS)) { // 보석 계열 아이템 처리 player.sendSystemMessage(Component.literal("보석을 들고 있습니다!"));}
Item.builtInRegistryHolder().is(TagKey<Item>) 방식도 있지만, ItemStack.is()가 더 간결합니다.