2024-11-14 14:24:21 -08:00
|
|
|
package net.minecraftforge.example;
|
2013-11-17 18:21:24 -06:00
|
|
|
|
2022-03-01 14:59:27 -06:00
|
|
|
import com.mojang.logging.LogUtils;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraft.client.Minecraft;
|
2023-04-01 20:45:29 -05:00
|
|
|
import net.minecraft.core.registries.Registries;
|
|
|
|
|
import net.minecraft.world.food.FoodProperties;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraft.world.item.BlockItem;
|
2023-04-01 20:45:29 -05:00
|
|
|
import net.minecraft.world.item.CreativeModeTab;
|
2022-11-22 13:32:47 -06:00
|
|
|
import net.minecraft.world.item.CreativeModeTabs;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraft.world.item.Item;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
|
import net.minecraft.world.level.block.Blocks;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
2023-04-01 20:45:29 -05:00
|
|
|
import net.minecraft.world.level.material.MapColor;
|
2022-11-08 20:16:49 +00:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
2018-09-30 14:29:03 -04:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2023-04-01 20:45:29 -05:00
|
|
|
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
|
2022-11-22 13:32:47 -06:00
|
|
|
import net.minecraftforge.event.server.ServerStartingEvent;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraftforge.eventbus.api.IEventBus;
|
2018-09-30 14:29:03 -04:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2014-09-22 22:01:24 -07:00
|
|
|
import net.minecraftforge.fml.common.Mod;
|
2023-06-27 12:57:20 +01:00
|
|
|
import net.minecraftforge.fml.config.ModConfig;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
2019-01-14 22:42:53 -05:00
|
|
|
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
2019-02-13 22:13:59 -05:00
|
|
|
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
2022-06-09 13:37:52 -05:00
|
|
|
import net.minecraftforge.registries.DeferredRegister;
|
|
|
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
|
|
|
|
import net.minecraftforge.registries.RegistryObject;
|
2022-03-01 14:59:27 -06:00
|
|
|
import org.slf4j.Logger;
|
2013-11-17 18:21:24 -06:00
|
|
|
|
2018-09-30 14:29:03 -04:00
|
|
|
// The value here should match an entry in the META-INF/mods.toml file
|
2022-06-09 13:37:52 -05:00
|
|
|
@Mod(ExampleMod.MODID)
|
2024-11-14 14:24:21 -08:00
|
|
|
public class ExampleMod {
|
2022-06-09 13:37:52 -05:00
|
|
|
// Define mod id in a common place for everything to reference
|
|
|
|
|
public static final String MODID = "examplemod";
|
2022-03-01 14:59:27 -06:00
|
|
|
// Directly reference a slf4j logger
|
|
|
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
2022-06-09 13:37:52 -05:00
|
|
|
// 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);
|
2023-04-01 20:45:29 -05:00
|
|
|
// Create a Deferred Register to hold CreativeModeTabs which will all be registered under the "examplemod" namespace
|
|
|
|
|
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID);
|
2022-06-09 13:37:52 -05:00
|
|
|
|
|
|
|
|
// Creates a new Block with the id "examplemod:example_block", combining the namespace and path
|
2024-11-14 14:24:21 -08:00
|
|
|
public static final RegistryObject<Block> EXAMPLE_BLOCK = BLOCKS.register("example_block",
|
|
|
|
|
() -> new Block(BlockBehaviour.Properties.of()
|
|
|
|
|
.setId(BLOCKS.key("example_block"))
|
|
|
|
|
.mapColor(MapColor.STONE)
|
|
|
|
|
)
|
|
|
|
|
);
|
2022-06-09 13:37:52 -05:00
|
|
|
// Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path
|
2024-11-14 14:24:21 -08:00
|
|
|
public static final RegistryObject<Item> EXAMPLE_BLOCK_ITEM = ITEMS.register("example_block",
|
|
|
|
|
() -> new BlockItem(EXAMPLE_BLOCK.get(), new Item.Properties().setId(ITEMS.key("example_block")))
|
|
|
|
|
);
|
2018-01-12 23:54:29 -08:00
|
|
|
|
2023-04-01 20:45:29 -05:00
|
|
|
// Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2
|
2024-11-14 14:24:21 -08:00
|
|
|
public static final RegistryObject<Item> EXAMPLE_ITEM = ITEMS.register("example_item",
|
|
|
|
|
() -> new Item(new Item.Properties()
|
|
|
|
|
.setId(ITEMS.key("example_item"))
|
|
|
|
|
.food(new FoodProperties.Builder()
|
|
|
|
|
.alwaysEdible()
|
|
|
|
|
.nutrition(1)
|
|
|
|
|
.saturationModifier(2f)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
2023-04-01 20:45:29 -05:00
|
|
|
|
|
|
|
|
// Creates a creative tab with the id "examplemod:example_tab" for the example item, that is placed after the combat tab
|
|
|
|
|
public static final RegistryObject<CreativeModeTab> EXAMPLE_TAB = CREATIVE_MODE_TABS.register("example_tab", () -> CreativeModeTab.builder()
|
|
|
|
|
.withTabsBefore(CreativeModeTabs.COMBAT)
|
|
|
|
|
.icon(() -> EXAMPLE_ITEM.get().getDefaultInstance())
|
|
|
|
|
.displayItems((parameters, output) -> {
|
|
|
|
|
output.accept(EXAMPLE_ITEM.get()); // Add the example item to the tab. For your own tabs, this method is preferred over the event
|
|
|
|
|
}).build());
|
|
|
|
|
|
2024-11-14 14:24:21 -08:00
|
|
|
public ExampleMod(FMLJavaModLoadingContext context) {
|
2024-08-25 13:38:31 -07:00
|
|
|
IEventBus modEventBus = context.getModEventBus();
|
2018-01-12 23:54:29 -08:00
|
|
|
|
2022-06-09 13:37:52 -05:00
|
|
|
// Register the commonSetup method for modloading
|
|
|
|
|
modEventBus.addListener(this::commonSetup);
|
2018-09-30 14:29:03 -04:00
|
|
|
|
2022-06-09 13:37:52 -05:00
|
|
|
// 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);
|
2023-04-01 20:45:29 -05:00
|
|
|
// Register the Deferred Register to the mod event bus so tabs get registered
|
|
|
|
|
CREATIVE_MODE_TABS.register(modEventBus);
|
2019-01-14 22:42:53 -05:00
|
|
|
|
2022-06-09 13:37:52 -05:00
|
|
|
// Register ourselves for server and other game events we are interested in
|
|
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
2022-11-22 13:32:47 -06:00
|
|
|
|
|
|
|
|
// Register the item to a creative tab
|
2022-12-10 15:03:23 -08:00
|
|
|
modEventBus.addListener(this::addCreative);
|
2023-06-27 12:57:20 +01:00
|
|
|
|
|
|
|
|
// Register our mod's ForgeConfigSpec so that Forge can create and load the config file for us
|
2024-08-25 13:38:31 -07:00
|
|
|
context.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
|
2018-09-30 14:29:03 -04:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 14:24:21 -08:00
|
|
|
private void commonSetup(final FMLCommonSetupEvent event) {
|
2022-06-09 13:37:52 -05:00
|
|
|
// Some common setup code
|
|
|
|
|
LOGGER.info("HELLO FROM COMMON SETUP");
|
2023-06-27 12:57:20 +01:00
|
|
|
|
|
|
|
|
if (Config.logDirtBlock)
|
|
|
|
|
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
|
|
|
|
|
|
|
|
|
|
LOGGER.info(Config.magicNumberIntroduction + Config.magicNumber);
|
|
|
|
|
|
|
|
|
|
Config.items.forEach((item) -> LOGGER.info("ITEM >> {}", item.toString()));
|
2019-01-14 22:42:53 -05:00
|
|
|
}
|
2022-03-01 14:59:27 -06:00
|
|
|
|
2023-04-01 20:45:29 -05:00
|
|
|
// Add the example block item to the building blocks tab
|
2024-11-14 14:24:21 -08:00
|
|
|
private void addCreative(BuildCreativeModeTabContentsEvent event) {
|
2023-04-01 20:45:29 -05:00
|
|
|
if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS)
|
2022-12-10 15:03:23 -08:00
|
|
|
event.accept(EXAMPLE_BLOCK_ITEM);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 22:42:53 -05:00
|
|
|
// You can use SubscribeEvent and let the Event Bus discover methods to call
|
2018-09-30 14:29:03 -04:00
|
|
|
@SubscribeEvent
|
2024-11-14 14:24:21 -08:00
|
|
|
public void onServerStarting(ServerStartingEvent event) {
|
2022-03-01 14:59:27 -06:00
|
|
|
// Do something when the server starts
|
2019-02-13 22:13:59 -05:00
|
|
|
LOGGER.info("HELLO from server starting");
|
2018-09-30 14:29:03 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-09 13:37:52 -05:00
|
|
|
// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
|
2022-10-21 21:22:57 +01:00
|
|
|
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
|
2024-11-14 14:24:21 -08:00
|
|
|
public static class ClientModEvents {
|
2019-01-14 22:42:53 -05:00
|
|
|
@SubscribeEvent
|
2024-11-14 14:24:21 -08:00
|
|
|
public static void onClientSetup(FMLClientSetupEvent event) {
|
2022-06-09 13:37:52 -05:00
|
|
|
// Some client setup code
|
|
|
|
|
LOGGER.info("HELLO FROM CLIENT SETUP");
|
|
|
|
|
LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName());
|
2019-01-14 22:42:53 -05:00
|
|
|
}
|
2013-11-17 18:21:24 -06:00
|
|
|
}
|
|
|
|
|
}
|