How do I handle the contents of a registry? #1812
Answered
by
Daomephsta
haykam821
asked this question in
Mod Dev Support
-
How do I run code for every element of a registry, such as the item or block registry? |
Beta Was this translation helpful? Give feedback.
Answered by
Daomephsta
Nov 8, 2021
Replies: 1 comment
-
A simple approach is // Iterate over existing entries and perform action
for (Entry<RegistryKey<Item>, Item> entry : Registry.ITEM.getEntries())
{
// Example: print identifier and instance
System.out.println(entry.getKey().getValue() + " = " + entry.getValue());
}
// Register a callback to perform the action on future entries
RegistryEntryAddedCallback.event(Registry.ITEM).register((rawId, id, object) ->
{
// Example: print identifier and instance
System.out.println(id + " = " + object);
}); It can be generalised with this method public static <T> void forRegistryEntries(Registry<T> registry, BiConsumer<Identifier, T> action)
{
for (Entry<RegistryKey<T>, T> entry : registry.getEntries())
action.accept(entry.getKey().getValue(), entry.getValue());
RegistryEntryAddedCallback.event(registry).register((rawId, id, object) -> action.accept(id, object));
} which is called like so forRegistryEntries(Registry.ITEM, (id, item) -> System.out.println(id + " = " + item)); It's possible to iterate over only the registry values, which would be simpler. Since the identifier is very useful (e.g. logging, debugging), I opted to show iteration over registry entries instead. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
haykam821
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple approach is