diff options
Diffstat (limited to 'src/main/java/com/example')
| -rw-r--r-- | src/main/java/com/example/examplemod/ExampleMod.java | 78 |
1 files changed, 43 insertions, 35 deletions
diff --git a/src/main/java/com/example/examplemod/ExampleMod.java b/src/main/java/com/example/examplemod/ExampleMod.java index 421fd07..48b8fd6 100644 --- a/src/main/java/com/example/examplemod/ExampleMod.java +++ b/src/main/java/com/example/examplemod/ExampleMod.java @@ -1,61 +1,69 @@ package com.example.examplemod; import com.mojang.logging.LogUtils; +import net.minecraft.client.Minecraft; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockBehaviour; +import net.minecraft.world.level.material.Material; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.InterModComms; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent; import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.RegistryObject; import org.slf4j.Logger; -import java.util.stream.Collectors; - // The value here should match an entry in the META-INF/mods.toml file -@Mod("examplemod") +@Mod(ExampleMod.MODID) public class ExampleMod { + // Define mod id in a common place for everything to reference + public static final String MODID = "examplemod"; // Directly reference a slf4j logger private static final Logger LOGGER = LogUtils.getLogger(); + // Create a Deferred Register to hold Blocks which will all be registered under the "examplemod" namespace + public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); + // Create a Deferred Register to hold Items which will all be registered under the "examplemod" namespace + public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); + + // Creates a new Block with the id "examplemod:example_block", combining the namespace and path + public static final RegistryObject<Block> EXAMPLE_BLOCK = BLOCKS.register("example_block", () -> new Block(BlockBehaviour.Properties.of(Material.STONE))); + // Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path + public static final RegistryObject<Item> EXAMPLE_BLOCK_ITEM = ITEMS.register("example_block", () -> new BlockItem(EXAMPLE_BLOCK.get(), new Item.Properties().tab(CreativeModeTab.TAB_BUILDING_BLOCKS))); public ExampleMod() { - // Register the setup method for modloading - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); - // Register the enqueueIMC method for modloading - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC); - // Register the processIMC method for modloading - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC); + IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); - // Register ourselves for server and other game events we are interested in - MinecraftForge.EVENT_BUS.register(this); - } + // Register the commonSetup method for modloading + modEventBus.addListener(this::commonSetup); - private void setup(final FMLCommonSetupEvent event) - { - // some preinit code - LOGGER.info("HELLO FROM PREINIT"); - LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName()); - } + // Register the Deferred Register to the mod event bus so blocks get registered + BLOCKS.register(modEventBus); + // Register the Deferred Register to the mod event bus so items get registered + ITEMS.register(modEventBus); - private void enqueueIMC(final InterModEnqueueEvent event) - { - // Some example code to dispatch IMC to another mod - InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";}); + // Register ourselves for server and other game events we are interested in + MinecraftForge.EVENT_BUS.register(this); } - private void processIMC(final InterModProcessEvent event) + private void commonSetup(final FMLCommonSetupEvent event) { - // Some example code to receive and process InterModComms from other mods - LOGGER.info("Got IMC {}", event.getIMCStream(). - map(m->m.messageSupplier().get()). - collect(Collectors.toList())); + // Some common setup code + LOGGER.info("HELLO FROM COMMON SETUP"); + LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT)); } // You can use SubscribeEvent and let the Event Bus discover methods to call @@ -66,16 +74,16 @@ public class ExampleMod LOGGER.info("HELLO from server starting"); } - // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD - // Event bus for receiving Registry Events) - @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) - public static class RegistryEvents + // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent + @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD) + public static class ClientModEvents { @SubscribeEvent - public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) + public static void onClientSetup(FMLClientSetupEvent event) { - // Register a new block here - LOGGER.info("HELLO from Register Block"); + // Some client setup code + LOGGER.info("HELLO FROM CLIENT SETUP"); + LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName()); } } } |
