Skip to content

Instantly share code, notes, and snippets.

@apple502j
Last active May 7, 2022 23:09
Show Gist options
  • Save apple502j/4f0eaabb1d178cc92f0940b40361933b to your computer and use it in GitHub Desktop.
Save apple502j/4f0eaabb1d178cc92f0940b40361933b to your computer and use it in GitHub Desktop.

TLDR, 22w06a

TLDR the TLDR

  • Tag is now TagKey
  • Registry entries have associated RegistryEntry
  • RegistryEntryList is a list of RegistryEntry, supports directly referencing items and referencing using tags
  • Registries are frozen unless you use Fabric API: to add custom entries to a registry, you need to add Fabric API as an dependency!
  • Instances of Block/Item/etc must be ALWAYS registered otherwise it'll crash.

TagKey

TagKey, like RegistryKey, is a key/identifier for a tag. Many methods that took Tag will now take TagKey instead, such as BlockState#isIn. Built-in TagKeys are still available in BlockTags class, etc.

TagKey is a singleton - and the code enforces that using an interner. To create a TagKey, call TagKey.of(RegistryKey, Identifier) - for example, TagKey.of(Registry.VILLAGER_PROFESSION_KEY, "blacksmith").

TagKey itself is not associated with any items. (We'll talk about this later.)

To get all tags in a registry, call Registry.BLOCK.streamTags().

RegistryEntry

RegistryEntry is an entry in a Registry. (Usually.) Like Map.Entry, it has a key and a value - key being Registry and value being RegistryKey or Object. (The former is used for things without an associated object like professions, called "stand-alone" by some code. The latter is used for things like blocks and items, which do have associated Block or Item objects.)

To get an RegistryEntry, you can use methods in Registry.

  • Get items of a tag (blocks, items, etc): Registry.BLOCK.iterateEntries(tagKey)
  • Get an entry from a RegistryKey: Registry.BLOCK.entryOf(key) (throws) or Registry.BLOCK.getEntry(key) (is Optional)
  • Get
  • Get random entry from a registry: Registry.BLOCK.getRandom(random)
  • Stream all entries: Registry.BLOCK.streamEntries()
  • Get an entry from a block/item/etc: Items.GOLDEN_APPLE.getRegistryEntry()
  • Check if an entry is in a tag (entry-based approach): Items.GOLDEN_APPLE.getRegistryEntry().isIn(ItemTags.PIGLIN_LOVED)

RegistryEntryList

RegistryEntryList is a list of RegistryEntry. This can either be a "named" list (previously Tag.Identified) or "direct" list (where values are specified directly).

  • Create "direct" list: RegistryEntryList.of(regEntry, regEntry2, ...)
  • Get "named" list from a tag (is Optional): Registry.BLOCK.getEntryList(BlockTags.BASE_STONE_OVERWORLD)
  • Stream pairs of TagKey and its values as RegistryEntryList: Registry.BLOCK.streamTagsAndEntries()
  • Get a random item (is Optional in case the tag is empty): registryEntryList.getRandom(random)
  • Other RegistryEntryList methods of interest: contains(RegistryEntry), isOf(Registry), size(), stream()

Code Examples

Setting a block to a random wool:

Registry.BLOCK.getEntryList(BlockTags.WOOL).flatMap(blocks -> blocks.getRandom(random)).map(RegistryEntry::value).ifPresent(block -> world.setBlockState(pos, block.getDefaultState(), Block.NOTIFY_LISTENERS))

Checking if something is in a tag:

// Tag-based approach (does the tag contain the entry?)
boolean piglinLovesGoldenApple = Registry.ITEM.getOrCreateEntryList(ItemTags.PIGLIN_LOVED).contains(Registry.ITEM.entryOf(Items.GOLDEN_APPLE));

// Entry-based approach (is the entry in a tag?)
// Only works for non-standalone objects such as blocks/items
boolean piglinLovesGoldenApple = Items.GOLDEN_APPLE.getRegistryEntry().isIn(Items.GOLDEN_APPLE);

// Shortcut methods available in some cases, like in previous versions:
boolean piglinLovesStack = stack.isIn(ItemTags.PIGLIN_LOVED);
boolean piglinLovesBlock = state.isIn(BlockTags.GUARDED_BY_PIGLINS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment