Cleaned up example code, and added more relevant examples and documentation.
This commit is contained in:
parent
f445322f61
commit
509dda3840
|
|
@ -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<Component> 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Block> 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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user