|
| 1 | +package ${PACKAGE_NAME}; |
| 2 | + |
| 3 | +import com.mojang.logging.LogUtils; |
| 4 | +import net.minecraft.client.Minecraft; |
| 5 | +import net.minecraft.core.registries.Registries; |
| 6 | +import net.minecraft.world.food.FoodProperties; |
| 7 | +import net.minecraft.world.item.BlockItem; |
| 8 | +import net.minecraft.world.item.CreativeModeTab; |
| 9 | +import net.minecraft.world.item.CreativeModeTabs; |
| 10 | +import net.minecraft.world.item.Item; |
| 11 | +import net.minecraft.world.level.block.Block; |
| 12 | +import net.minecraft.world.level.block.Blocks; |
| 13 | +import net.minecraft.world.level.block.state.BlockBehaviour; |
| 14 | +import net.minecraft.world.level.material.MapColor; |
| 15 | +import net.minecraftforge.api.distmarker.Dist; |
| 16 | +import net.minecraftforge.common.MinecraftForge; |
| 17 | +import net.minecraftforge.event.BuildCreativeModeTabContentsEvent; |
| 18 | +import net.minecraftforge.event.server.ServerStartingEvent; |
| 19 | +import net.minecraftforge.eventbus.api.IEventBus; |
| 20 | +import net.minecraftforge.eventbus.api.SubscribeEvent; |
| 21 | +import net.minecraftforge.fml.ModLoadingContext; |
| 22 | +import net.minecraftforge.fml.common.Mod; |
| 23 | +import net.minecraftforge.fml.config.ModConfig; |
| 24 | +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; |
| 25 | +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; |
| 26 | +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; |
| 27 | +import net.minecraftforge.registries.DeferredRegister; |
| 28 | +import net.minecraftforge.registries.ForgeRegistries; |
| 29 | +import net.minecraftforge.registries.RegistryObject; |
| 30 | +import org.slf4j.Logger; |
| 31 | + |
| 32 | +// The value here should match an entry in the META-INF/mods.toml file |
| 33 | +@Mod(${CLASS_NAME}.MODID) |
| 34 | +public class ${CLASS_NAME} { |
| 35 | + |
| 36 | + // Define mod id in a common place for everything to reference |
| 37 | + public static final String MODID = "${MOD_ID}"; |
| 38 | + // Directly reference a slf4j logger |
| 39 | + private static final Logger LOGGER = LogUtils.getLogger(); |
| 40 | + // Create a Deferred Register to hold Blocks which will all be registered under the "${MOD_ID}" namespace |
| 41 | + public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); |
| 42 | + // Create a Deferred Register to hold Items which will all be registered under the "${MOD_ID}" namespace |
| 43 | + public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); |
| 44 | + // Create a Deferred Register to hold CreativeModeTabs which will all be registered under the "examplemod" namespace |
| 45 | + public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID); |
| 46 | + |
| 47 | + // Creates a new Block with the id "${MOD_ID}:example_block", combining the namespace and path |
| 48 | + public static final RegistryObject<Block> EXAMPLE_BLOCK = BLOCKS.register("example_block", () -> new Block(BlockBehaviour.Properties.of().mapColor(MapColor.STONE))); |
| 49 | + // Creates a new BlockItem with the id "${MOD_ID}:example_block", combining the namespace and path |
| 50 | + public static final RegistryObject<Item> EXAMPLE_BLOCK_ITEM = ITEMS.register("example_block", () -> new BlockItem(EXAMPLE_BLOCK.get(), new Item.Properties())); |
| 51 | + |
| 52 | + // Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2 |
| 53 | + public static final RegistryObject<Item> EXAMPLE_ITEM = ITEMS.register("example_item", () -> new Item(new Item.Properties().food(new FoodProperties.Builder() |
| 54 | + .alwaysEdible().nutrition(1).saturationModifier(2f).build()))); |
| 55 | + |
| 56 | + // Creates a creative tab with the id "examplemod:example_tab" for the example item, that is placed after the combat tab |
| 57 | + public static final RegistryObject<CreativeModeTab> EXAMPLE_TAB = CREATIVE_MODE_TABS.register("example_tab", () -> CreativeModeTab.builder() |
| 58 | + .withTabsBefore(CreativeModeTabs.COMBAT) |
| 59 | + .icon(() -> EXAMPLE_ITEM.get().getDefaultInstance()) |
| 60 | + .displayItems((parameters, output) -> { |
| 61 | + output.accept(EXAMPLE_ITEM.get()); // Add the example item to the tab. For your own tabs, this method is preferred over the event |
| 62 | + }).build()); |
| 63 | + |
| 64 | + public ${CLASS_NAME}() { |
| 65 | + IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); |
| 66 | + |
| 67 | + // Register the commonSetup method for modloading |
| 68 | + modEventBus.addListener(this::commonSetup); |
| 69 | + |
| 70 | + // Register the Deferred Register to the mod event bus so blocks get registered |
| 71 | + BLOCKS.register(modEventBus); |
| 72 | + // Register the Deferred Register to the mod event bus so items get registered |
| 73 | + ITEMS.register(modEventBus); |
| 74 | + // Register the Deferred Register to the mod event bus so tabs get registered |
| 75 | + CREATIVE_MODE_TABS.register(modEventBus); |
| 76 | + |
| 77 | + // Register ourselves for server and other game events we are interested in |
| 78 | + MinecraftForge.EVENT_BUS.register(this); |
| 79 | + |
| 80 | + // Register the item to a creative tab |
| 81 | + modEventBus.addListener(this::addCreative); |
| 82 | + |
| 83 | + // Register our mod's ForgeConfigSpec so that Forge can create and load the config file for us |
| 84 | + ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.SPEC); |
| 85 | + } |
| 86 | + |
| 87 | + private void commonSetup(final FMLCommonSetupEvent event) { |
| 88 | + // Some common setup code |
| 89 | + LOGGER.info("HELLO FROM COMMON SETUP"); |
| 90 | + LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT)); |
| 91 | + |
| 92 | + if (Config.logDirtBlock) |
| 93 | + LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT)); |
| 94 | + |
| 95 | + LOGGER.info(Config.magicNumberIntroduction + Config.magicNumber); |
| 96 | + |
| 97 | + Config.items.forEach((item) -> LOGGER.info("ITEM >> {}", item.toString())); |
| 98 | + } |
| 99 | + |
| 100 | + // Add the example block item to the building blocks tab |
| 101 | + private void addCreative(BuildCreativeModeTabContentsEvent event) |
| 102 | + { |
| 103 | + if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS) |
| 104 | + event.accept(EXAMPLE_BLOCK_ITEM); |
| 105 | + } |
| 106 | + // You can use SubscribeEvent and let the Event Bus discover methods to call |
| 107 | + @SubscribeEvent |
| 108 | + public void onServerStarting(ServerStartingEvent event) { |
| 109 | + // Do something when the server starts |
| 110 | + LOGGER.info("HELLO from server starting"); |
| 111 | + } |
| 112 | + |
| 113 | + // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent |
| 114 | + @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) |
| 115 | + public static class ClientModEvents { |
| 116 | + |
| 117 | + @SubscribeEvent |
| 118 | + public static void onClientSetup(FMLClientSetupEvent event) |
| 119 | + { |
| 120 | + // Some client setup code |
| 121 | + LOGGER.info("HELLO FROM CLIENT SETUP"); |
| 122 | + LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName()); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments