LogoTravelers Lib

Registry System Basics

Registry System Basics

A basic rundown on how the registry system works

How do I make a registry?

public static final TravelersRegistry<Block> BLOCKS = new TravelersRegistry<>(BuiltInRegistries.BLOCK, {YOUR_MOD_ID});

As shown above, any of the BuiltInRegistries can be used with TravelersRegistry. Some registries require a type parameter inside the angle brackets (<>) for example, EntityType<?> uses a wildcard since the specific entity type isn't known at this point.

If you need the objects for an later purpose (like going thru them in DataGen) you can store the values to the registry like this:

public static final TravelersRegistry<Block> BLOCKS = new TravelersRegistry<>(BuiltInRegistries.BLOCK, {YOUR_MOD_ID}).storeValues();

It's in theory the same as before but this time we added .storeValues() at the end.

How do i register my objects to this registry?

// Gives back the default BlockItem
public static Supplier<Block> registerBlockWItem(String name, Supplier<Block> block, Item.Properties properties) {
return registerBlockWItem(name, block, properties, BlockItem::new);
}
// For custom Block Items
public static Supplier<Block> registerBlockWItem(String name, Supplier<Block> block, Item.Properties properties,
BiFunction<Block, Item.Properties, ? extends BlockItem> itemFactory) {
Supplier<Block> registered = BLOCKS.register(name, block::get);
Supplier<Block> blockRegistryObject = registered::get;
Items.registerItem(name, () -> itemFactory.apply(blockRegistryObject.get(), properties));
// Return the same registryObject as the item got
return blockRegistryObject;
}

As you can see there are several ways to do this, just experiment for which one works for you best!

Important Note

For every single example i've made an example class so you can see how the registry is supposed to look.

Never initialize your registry in an static object always use an static void init or anything that is simulair:

public class ExampleBlockRegistry {
public static final TravelersRegistry<Block> BLOCKS = new TravelersRegistry<>(BuiltInRegistries.BLOCK, "{YOUR_MOD_ID}");
public static Supplier<Block> EXAMPLE_BLOCK;
public static void init() {
EXAMPLE_BLOCK = registerBlockWItem("example_block", () -> new Block(BlockBehaviour.Properties.of()), new Item.Properties());
}
// Gives back the default BlockItem
public static Supplier<Block> registerBlockWItem(String name, Supplier<Block> block, Item.Properties properties) {
return registerBlockWItem(name, block, properties, BlockItem::new);
}
// For custom Block Items
public static Supplier<Block> registerBlockWItem(String name, Supplier<Block> block, Item.Properties properties,
BiFunction<Block, Item.Properties, ? extends BlockItem> itemFactory) {
Supplier<Block> registered = BLOCKS.register(name, block::get);
Supplier<Block> blockRegistryObject = registered::get;
ExampleItemRegistry.registerItem(name, () -> itemFactory.apply(blockRegistryObject.get(), properties));
// Return the same registryObject as the item got
return blockRegistryObject;
}
}