-
-
Notifications
You must be signed in to change notification settings - Fork 0
Fabric API
Forge and NeoForge offer systems to simplify the registration of game objects like items, blocks, entity types, etc.
Fabric lacks this functionality, so Cobweb aims to bridge this gap and provide feature parity.
If you're working in a multiloader environment, such as the one provided by our official template or generator, we recommend using the Unified Registration System instead of this Fabric-only system.
The RegisterProvider is the core of this system, as it allows you to create a Register instance for each different Minecraft Registry
.
public class ModLoader implements ModInitializer {
/**
* This is your unique Register Provider.
* There must be only one per mod, and it must be created as a public static final property in your ModLoader class.
*/
public static final RegisterProvider REGISTER_PROVIDER = new RegisterProvider(Constants.MOD_ID);
@Override
public void onInitialize() {
CommonModLoader.init();
}
}
Once you've setup your RegisterProvider
, you can get a Register
instance for any Minecraft Registry
you need.
The suggested convention is to create a -Registry
class where you get the specific Register
instance for the kind of objects you register there.
This helps keep your code base organized and decoupled.
Below, an example for registering EntityType
s, its custom spawn egg, a custom Item
, and a custom CreativeModeTab
.
EntityRegistry
public final class EntityRegistry {
/**
* Get the Register for EntityTypes.
*/
private static final Register<EntityType<?>> ENTITY_REGISTER = ModLoader.REGISTER_PROVIDER.of(BuiltInRegistries.ENTITY_TYPE);
/**
* Create the singleton instance for your custom EntityType.
*/
public static final EntityType<MyEntity> MY_ENTITY = FabricEntityTypeBuilder.create(MobCategory.MONSTER, MyEntity::new).build();
private EntityRegistry() {}
public static void register() {
ENTITY_REGISTER.apply("entity_id", MY_ENTITY);
}
}
ItemRegistry
public final class ItemRegistry {
/**
* Get the Register for Items.
*/
private static final Register<Item> ITEM_REGISTER = ModLoader.REGISTER_PROVIDER.of(BuiltInRegistries.ITEM);
/**
* Get the Register for CreativeModeTabs.
*/
private static final Register<CreativeModeTab> TAB_REGISTER = ModLoader.REGISTER_PROVIDER.of(BuiltInRegistries.CREATIVE_MODE_TAB);
/**
* Create the singleton instance for your custom Item.
*/
public static final MyItem MY_ITEM = new MyItem();
/**
* Create the singleton instance for your custom Entity spawn egg.
*/
public static final Item MY_ENTITY_EGG = new SpawnEggItem(EntityRegistry.MY_ENTITY, 0xDD4477, 0x909733, new FabricItemSettings());
/**
* Create the singleton instance for your custom ItemGroup.
*/
public static final CreativeModeTab MY_TAB = FabricItemGroup.builder()
.entries((features, output) -> {
output.add(MY_ITEM);
output.add(MY_ENTITY_EGG);
})
.build();
private ItemRegistry() {}
public static void register() {
ITEM_REGISTER.apply("item_id", MY_ITEM);
TAB_REGISTER.apply("group_id", MY_TAB);
}
}
// In your ModLoader class...
@Override
public void onInitialize() {
CommonModLoader.init();
EntityRegistry.register();
ItemRegistry.register();
}
If you have any suggestions or questions about this wiki, please open an issue following the Documentation enhancement template.