# TagEventJS

This event is fired when a tag collection is loaded, to modify it with script. You can add and remove tags for items, blocks, fluids and entity types.

This goes into server scripts.

<p class="callout warning">Tags are per item/block/fluid/entity type and as such cannot be added based on things like NBT data!</p>

##### Parent class

[EventJS](https://mods.latvian.dev/books/kubejs-legacy/page/eventjs)

##### Can be cancelled

No

#### Variables and Functions

<table border="1" id="bkmrk-name-type-info-parse" style="border-collapse: collapse; width: 100%; height: 256px;"><thead><tr style="height: 29px;"><td style="width: 32.0986%; height: 29px;">Name</td><td style="width: 11.6049%; height: 29px;">Type</td><td style="width: 56.2962%; height: 29px;">Info</td></tr></thead><tbody><tr style="height: 29px;"><td style="width: 32.0986%; height: 29px;"><span style="text-decoration: underline;">type</span></td><td style="width: 11.6049%; height: 29px;">[String](https://mods.latvian.dev/books/kubejs-legacy/page/string)</td><td style="width: 56.2962%; height: 29px;">Tag collection type.</td></tr><tr style="height: 64px;"><td style="width: 32.0986%; height: 64px;">get([String](https://mods.latvian.dev/books/kubejs-legacy/page/string) tag)</td><td style="width: 11.6049%; height: 64px;">TagWrapper</td><td style="width: 56.2962%; height: 64px;">Returns specific tag container which you can use to add or remove objects to. tag parameter can be something like 'forge:ingots/copper'. If tag doesn't exist, it will create a new one.</td></tr><tr style="height: 35px;"><td style="width: 32.0986%; height: 35px;">add([String](https://mods.latvian.dev/books/kubejs-legacy/page/string) tag, [String](https://mods.latvian.dev/books/kubejs-legacy/page/string)\[\]/Regex ids)</td><td style="width: 11.6049%; height: 35px;">TagWrapper</td><td style="width: 56.2962%; height: 35px;">Shortcut method for event.get(tag).add(ids).</td></tr><tr style="height: 35px;"><td style="width: 32.0986%; height: 35px;">remove([String](https://mods.latvian.dev/books/kubejs-legacy/page/string) tag, [String](https://mods.latvian.dev/books/kubejs-legacy/page/string)\[\]/Regex ids)</td><td style="width: 11.6049%; height: 35px;">TagWrapper</td><td style="width: 56.2962%; height: 35px;">Shortcut method for event.get(tag).remove(ids).</td></tr><tr style="height: 35px;"><td style="width: 32.0986%; height: 35px;">removeAll([String](https://mods.latvian.dev/books/kubejs-legacy/page/string) tag)</td><td style="width: 11.6049%; height: 35px;">TagWrapper</td><td style="width: 56.2962%; height: 35px;">Shortcut method for event.get(tag).removeAll().</td></tr><tr style="height: 29px;"><td style="width: 32.0986%; height: 29px;">removeAllTagsFrom(String\[\] ids)</td><td style="width: 11.6049%; height: 29px;">void</td><td style="width: 56.2962%; height: 29px;">Removes all tags from object</td></tr></tbody></table>

### TagWrapper class

#### Variables and Functions

<table border="1" id="bkmrk-name-return-type-inf-0" style="border-collapse: collapse; width: 100%; height: 132px;"><thead><tr style="height: 29px;"><td style="width: 22.8394%; height: 29px;">Name</td><td style="width: 17.284%; height: 29px;">Type</td><td style="width: 59.8765%; height: 29px;">Info</td></tr></thead><tbody><tr style="height: 45px;"><td style="width: 22.8394%; height: 45px;">add([String](https://mods.latvian.dev/books/kubejs-legacy/page/string)\[\]/Regex ids)</td><td style="width: 17.284%; height: 45px;">TagWrapper (itself)</td><td style="width: 59.8765%; height: 45px;">Adds an object to this tag. If string starts with # then it will add all objects from the second tag. It can be either single string, regex (/regex/flags) or array of either.</td></tr><tr style="height: 29px;"><td style="width: 22.8394%; height: 29px;">remove([String](https://mods.latvian.dev/books/kubejs-legacy/page/string)\[\]/Regex ids)</td><td style="width: 17.284%; height: 29px;">TagWrapper (itself)</td><td style="width: 59.8765%; height: 29px;">Removes an object from tag, works the same as add().</td></tr><tr style="height: 29px;"><td style="width: 22.8394%; height: 29px;">removeAll()</td><td style="width: 17.284%; height: 29px;">TagWrapper (itself)</td><td style="width: 59.8765%; height: 29px;">Removes all entries from tag.</td></tr><tr><td style="width: 22.8394%;">getObjectIds()</td><td style="width: 17.284%;"><span class="pl-smi">Collection</span>&lt;<span class="pl-smi">ResourceLocation</span>&gt;</td><td style="width: 59.8765%;">Returns a list of all entries in a tag. Will resolve any sub-tags.</td></tr></tbody></table>

#### Examples

```JavaScript
// Listen to item tag event
onEvent('item.tags', event => {
  // Get the #forge:cobblestone tag collection and add Diamond Ore to it
  event.add('forge:cobblestone', 'minecraft:diamond_ore')
  
  // Get the #forge:cobblestone tag collection and remove Mossy Cobblestone from it
  event.remove('forge:cobblestone', 'minecraft:mossy_cobblestone')
  
  // Get #forge:ingots/copper tag and remove all entries from it
  event.removeAll('forge:ingots/copper')
  
  // Required for FTB Quests to check item NBT
  event.add('itemfilters:check_nbt', 'some_item:that_has_nbt_types')
  
  // You can create new tags the same way you add to existing, just give it a name
  event.add('forge:completely_new_tag', 'minecraft:clay_ball')
  
  // Removes all tags from this entry
  event.removeAllTagsFrom('minecraft:stick')
  
  // Add all items from the forge:stone tag to the c:stone tag, unless the id contains diorite
  const stones = event.get('forge:stone').getObjectIds()
  const blacklist = Ingredient.of(/.*diorite.*/)
  stones.forEach(stone => {
    if (!blacklist.test(stone)) {
      event.add('c:stone', stone)
    }
  })
})
```

<p class="callout warning">Recipes use item tags, not block or fluid tags, even if items representing those are blocks. Like `minecraft:cobblestone` even if it's a block, it will still be an item tag for recipes.</p>

<p class="callout info">`tags.blocks` and `tags.fluids` are for adding tags to block and fluid types, they work the same way. You can find existing block and fluid tags if you look at a block with F3 mode enabled, on side. These are mostly only used for technical reasons, and like mentioned above, if its for recipes/inventory, you will want to use `tags.items` even for blocks.</p>