Line 2: |
Line 2: |
| | | |
| == Declaring Your Own Groupings == | | == Declaring Your Own Groupings == |
− | Tags are declared in your mod’s [https://mcforge.readthedocs.io/en/latest/utilities/tags/datapacks.md datapack]. For example, <code><nowiki>/data/modid/tags/blocks/foo/tagname.json</nowiki></code> will declare a <code><nowiki>Tag<Block></nowiki></code> with ID <code><nowiki>modid:foo/tagname</nowiki></code>. Similarly, you may append to or override tags declared in other domains, such as Vanilla, by declaring your own JSONs. For example, to add your own mod’s saplings to the Vanilla sapling tag, you would specify it in <code><nowiki>/data/minecraft/tags/blocks/saplings.json</nowiki></code>, and Vanilla will merge everything into one tag at reload, if the <code><nowiki>replace</nowiki></code> option is false. If <code><nowiki>replace</nowiki></code> is true, then all entries before the json specifying <code><nowiki>replace</nowiki></code> will be removed. See the [https://minecraft.gamepedia.com/Tag#JSON_format Vanilla wiki] for a description of the base syntax. | + | Tags are declared in your mod’s [https://mcforge.readthedocs.io/en/latest/utilities/tags/datapacks.md datapack]. For example, <code><nowiki>/data/modid/tags/blocks/foo/tagname.json</nowiki></code> will declare a <code><nowiki>Tag$Named<Block></nowiki></code> with ID <code><nowiki>modid:foo/tagname</nowiki></code>. Similarly, you may append to or override tags declared in other domains, such as Vanilla, by declaring your own JSONs. For example, to add your own mod’s saplings to the Vanilla sapling tag, you would specify it in <code><nowiki>/data/minecraft/tags/blocks/saplings.json</nowiki></code>, and Vanilla will merge everything into one tag at reload, if the <code><nowiki>replace</nowiki></code> option is false. If <code><nowiki>replace</nowiki></code> is true, then all entries before the json specifying <code><nowiki>replace</nowiki></code> will be removed. See the [https://minecraft.gamepedia.com/Tag#JSON_format Vanilla wiki] for a description of the base syntax. |
| | | |
| Forge provides two extensions on the Vanilla syntax: * You may declare an <code><nowiki>optional</nowiki></code> array of the same format as the <code><nowiki>values</nowiki></code> array, but any values listed here that are not present will not cause the tag loading to error. This is useful to provide integration for mods that may or may not be present at runtime. | | Forge provides two extensions on the Vanilla syntax: * You may declare an <code><nowiki>optional</nowiki></code> array of the same format as the <code><nowiki>values</nowiki></code> array, but any values listed here that are not present will not cause the tag loading to error. This is useful to provide integration for mods that may or may not be present at runtime. |
Line 10: |
Line 10: |
| Block, Item, and Fluid tags are automatically sent from the server to any remote clients on login and reload. Function tags are not synced. | | Block, Item, and Fluid tags are automatically sent from the server to any remote clients on login and reload. Function tags are not synced. |
| | | |
− | <code><nowiki>BlockTags.getCollection()</nowiki></code> and <code><nowiki>ItemTags.getCollection()</nowiki></code> will retrieve the current <code><nowiki>TagCollection</nowiki></code>, from which you can retrieve a <code><nowiki>Tag</nowiki></code> object by its ID. With a <code><nowiki>Tag</nowiki></code> object in hand, membership can be tested with <code><nowiki>tag.contains(thing)</nowiki></code>, or all the objects in the tag queried with <code><nowiki>tag.getAllElements()</nowiki></code>. | + | <code><nowiki>BlockTags#getAllTags</nowiki></code> and <code><nowiki>ItemTags#getAllTags()</nowiki></code> will retrieve the current <code><nowiki>TagCollection</nowiki></code>, from which you can retrieve a <code><nowiki>Tag</nowiki></code> object by its ID. With a <code><nowiki>Tag</nowiki></code> object in hand, membership can be tested with <code><nowiki>tag.contains(thing)</nowiki></code>, or all the objects in the tag queried with <code><nowiki>tag.getAllElements()</nowiki></code>. |
| | | |
| As an example: | | As an example: |
| <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
− | ResourceLocation myTagId = new ResourceLocation("mymod", "myitemgroup");
| + | public static final Tag.Named<Item> myTag = ItemTags.bind("mymod:myitemgroup"); |
| + | |
| + | // In some method |
| Item unknownItem = stack.getItem(); | | Item unknownItem = stack.getItem(); |
− | boolean isInGroup = ItemTags.getCollection().getOrCreateTag(myTagId).contains(unknownItem); | + | boolean isInGroup = unknownItem.is(myTag); |
− | // alternatively, can use getTag and perform a null check
| |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | {{Colored box|title=tip|content=The <code><nowiki>TagCollection</nowiki></code> returned by <code><nowiki>getCollection()</nowiki></code> (and the <code><nowiki>Tag</nowiki></code>s within it) may expire if a reload happens, so you should always query the collection anew every time you need it. The static <code><nowiki>Tag</nowiki></code> fields in <code><nowiki>BlockTags</nowiki></code> and <code><nowiki>ItemTags</nowiki></code> avoid this by introducing a wrapper that handles this expiring. Alternatively, a resource reload listener can be used to refresh any cached tags.}} | + | {{Colored box|title=tip|content=The <code><nowiki>TagCollection</nowiki></code> returned by <code><nowiki>#getAllTags</nowiki></code> (and the <code><nowiki>Tag</nowiki></code>s within it) may expire if a reload happens, so you should always query the collection anew every time you need it. The static <code><nowiki>Tag$Named</nowiki></code> fields in <code><nowiki>BlockTags</nowiki></code> and <code><nowiki>ItemTags</nowiki></code> avoid this by introducing a wrapper that handles this expiring. Alternatively, a resource reload listener can be used to refresh any cached tags.}} |
| | | |
| == Conventions == | | == Conventions == |