RegistryHelper - Lemonszz/LKLib GitHub Wiki

RegistryHelper can be found in party.lemons.lklib.util.registry

This utility allows you to easily register all fields of a specific type in a class.

Simple Usage

All objects you wish to register need to be static and final

   public class ModItems
   {
      public static final Item IRON_SPEAR = new SpearItem();
      public static final Item BEAR_TRAP = new TrapItem();

      public static void init()
      {
         RegistryHelper.register("mymodid", Registry.ITEM, Item.class, ModItems.class);
      }
   }

Call ModItems.init() from your ModInitalizer. The call to RegistryHelper does not need to be in the same class as the fields.

After ModItems.init() has been called, all the fields that are or inherit from Item.class (in this case IRON_SPEAR & BEAR_TRAP) will have been registered.

Callbacks

Callbacks allow you to do something after each object is registered. LKLib comes with one built in callback you may use that automatically registers a BlockItem for blocks that implement the BlockWithItem interface.

   public class ModBlocks
   {
      public static final ShelfMushroomBlock SHELF_MUSHROOM = new ShelfMushroomBlock();
      public static final Block EXTRA_STICKY_HONEY_BLOCK = new ExtraStickyHoneyBlock();

      public static void init()
      {
         RegistryHelper.register("mymodid", Registry.BLOCK, Block.class, ModItems.class, new RegistryHelper.BlockWithItemCallBack(MyMod.ITEM_GROUP);
      }
   }

If you would like you use your own callback, you can

   public class ModBoats
   {
      public static final BoatType IRON_BOAT = new BoatType(new Identifier("mymodid", "iron", ()->ModItems.IRON_BOAT);
      public static final BoatType CHEST_BOAT = new BoatType(new Identifier("mymodid", "chest", ()->ModItems.CHEST_BOAT);
      public static void init()
      {
         RegistryHelper.register("mymodid", BoatTypes.REGISTRY, BoatType.class, ModBoats.class, (registry, boat, id)->{
            if(boat == IRON_BOAT)
            {
                System.out.println("How does it float??");
            }
         });
      }
   }

Extra Examples

These are some real examples of how this is implemented in Biome Makeover.

Potion Stuff:

		RegistryHelper.register(BiomeMakeover.MODID, Registry.POTION, Potion.class, BMPotions.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.STATUS_EFFECT, StatusEffect.class, BMPotions.class);

In the structure class:

		RegistryHelper.register(BiomeMakeover.MODID, BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE, ConfiguredStructureFeature.class, BMStructures.class);

In the world gen class:

		RegistryHelper.register(BiomeMakeover.MODID, Registry.STRUCTURE_PIECE, StructurePieceType.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.STRUCTURE_PROCESSOR, StructureProcessorType.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE, ConfiguredStructureFeature.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.FEATURE, Feature.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.CARVER, Carver.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, BuiltinRegistries.CONFIGURED_FEATURE, ConfiguredFeature.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, BuiltinRegistries.CONFIGURED_CARVER, ConfiguredCarver.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.TRUNK_PLACER_TYPE, TrunkPlacerType.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.FOLIAGE_PLACER_TYPE, FoliagePlacerType.class, BMWorldGen.class);
		RegistryHelper.register(BiomeMakeover.MODID, Registry.TREE_DECORATOR_TYPE, TreeDecoratorType.class, BMWorldGen.class);