From 509dda3840e21b247d295ac8609c018720f5e5c2 Mon Sep 17 00:00:00 2001 From: Tyler Hancock Date: Tue, 5 Oct 2021 22:33:10 -0600 Subject: [PATCH] Cleaned up example code, and added more relevant examples and documentation. --- .../com/example/examplemod/CommonClass.java | 27 +++++++ .../com/example/examplemod/ExampleMod.java | 15 ++-- .../com/example/examplemod/ExampleMod.java | 70 +++++-------------- 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/Common/src/main/java/com/example/examplemod/CommonClass.java b/Common/src/main/java/com/example/examplemod/CommonClass.java index eca7b0c..f5659ad 100644 --- a/Common/src/main/java/com/example/examplemod/CommonClass.java +++ b/Common/src/main/java/com/example/examplemod/CommonClass.java @@ -1,13 +1,40 @@ package com.example.examplemod; import net.minecraft.core.Registry; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.world.food.FoodProperties; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import net.minecraft.world.item.TooltipFlag; + +import java.util.List; public class CommonClass { + // This method serves as an initialization hook for the mod. The vanilla + // game has no mechanism to load tooltip listeners so this must be + // invoked from a mod loader specific project like Forge or Fabric. public static void init() { Constants.LOG.info("Hello from Common init!"); Constants.LOG.info("Diamond Item >> {}", Registry.ITEM.getKey(Items.DIAMOND)); } + + // This method serves as a hook to modify item tooltips. The vanilla game + // has no mechanism to load tooltip listeners so this must be registered + // by a mod loader like Forge or Fabric. + public static void onItemTooltip(ItemStack stack, TooltipFlag context, List tooltip) { + + if (!stack.isEmpty()) { + + final FoodProperties food = stack.getItem().getFoodProperties(); + + if (food != null) { + + tooltip.add(new TextComponent("Nutrition: " + food.getNutrition())); + tooltip.add(new TextComponent("Saturation: " + food.getSaturationModifier())); + } + } + } } \ No newline at end of file diff --git a/Fabric/src/main/java/com/example/examplemod/ExampleMod.java b/Fabric/src/main/java/com/example/examplemod/ExampleMod.java index 5d2e28f..9dddb74 100644 --- a/Fabric/src/main/java/com/example/examplemod/ExampleMod.java +++ b/Fabric/src/main/java/com/example/examplemod/ExampleMod.java @@ -1,16 +1,23 @@ package com.example.examplemod; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback; public class ExampleMod implements ModInitializer { @Override public void onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. - + + // This method is invoked by the Fabric mod loader when it is ready + // to load your mod. You can access Fabric and Common code in this + // project. + + // Use Fabric to bootstrap the Common mod. Constants.LOG.info("Hello Fabric world!"); CommonClass.init(); + + // Some code like events require special initialization from the + // loader specific code. + ItemTooltipCallback.EVENT.register(CommonClass::onItemTooltip); } } diff --git a/Forge/src/main/java/com/example/examplemod/ExampleMod.java b/Forge/src/main/java/com/example/examplemod/ExampleMod.java index 397cfc4..22d578d 100644 --- a/Forge/src/main/java/com/example/examplemod/ExampleMod.java +++ b/Forge/src/main/java/com/example/examplemod/ExampleMod.java @@ -1,69 +1,33 @@ package com.example.examplemod; -import java.util.stream.Collectors; - -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.RegistryEvent; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.InterModComms; +import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; -import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent; -import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.fmlserverevents.FMLServerStartingEvent; @Mod(Constants.MOD_ID) public class ExampleMod { public ExampleMod() { - - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC); - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC); - - MinecraftForge.EVENT_BUS.register(this); - } - private void setup(final FMLCommonSetupEvent event) { - - Constants.LOG.info("HELLO FROM PREINIT"); - Constants.LOG.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName()); + // This method is invoked by the Forge mod loader when it is ready + // to load your mod. You can access Forge and Common code in this + // project. + + // Use Forge to bootstrap the Common mod. + Constants.LOG.info("Hello Forge world!"); CommonClass.init(); - } - private void enqueueIMC(final InterModEnqueueEvent event) { - - InterModComms.sendTo("examplemod", "helloworld", () -> { - Constants.LOG.info("Hello world from the MDK"); - return "Hello world"; - }); - } - - private void processIMC(final InterModProcessEvent event) { - - Constants.LOG.info("Got IMC {}", event.getIMCStream() - .map(m -> m.messageSupplier().get()) - .collect(Collectors.toList())); - } - - @SubscribeEvent - public void onServerStarting(FMLServerStartingEvent event) { - - Constants.LOG.info("HELLO from server starting"); - } - - @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) - public static class RegistryEvents { - - @SubscribeEvent - public static void onBlocksRegistry(final RegistryEvent.Register blockRegistryEvent) { - // register a new block here - Constants.LOG.info("HELLO from Register Block"); - } + // Some code like events require special initialization from the + // loader specific code. + MinecraftForge.EVENT_BUS.addListener(this::onItemTooltip); } + // This method exists as a wrapper for the code in the Common project. + // It takes Forge's event object and passes the parameters along to + // the Common listener. + private void onItemTooltip(ItemTooltipEvent event) { + + CommonClass.onItemTooltip(event.getItemStack(), event.getFlags(), event.getToolTip()); + } } \ No newline at end of file