Add example usage for configs to the MDK (#9596)
Demonstrates proper usage of different config value types, validation, transformation/parsing, load/reload handling and direct field access.
This commit is contained in:
parent
7e52cbf56a
commit
6d6d2a3732
2 changed files with 76 additions and 1 deletions
64
src/main/java/com/example/examplemod/Config.java
Normal file
64
src/main/java/com/example/examplemod/Config.java
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package com.example.examplemod;
|
||||||
|
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.item.Item;
|
||||||
|
import net.minecraftforge.common.ForgeConfigSpec;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
|
import net.minecraftforge.fml.common.Mod;
|
||||||
|
import net.minecraftforge.fml.event.config.ModConfigEvent;
|
||||||
|
import net.minecraftforge.registries.ForgeRegistries;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
// An example config class. This is not required, but it's a good idea to have one to keep your config organized.
|
||||||
|
// Demonstrates how to use Forge's config APIs
|
||||||
|
@Mod.EventBusSubscriber(modid = ExampleMod.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||||
|
public class Config
|
||||||
|
{
|
||||||
|
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
|
||||||
|
|
||||||
|
private static final ForgeConfigSpec.BooleanValue LOG_DIRT_BLOCK = BUILDER
|
||||||
|
.comment("Whether to log the dirt block on common setup")
|
||||||
|
.define("logDirtBlock", true);
|
||||||
|
|
||||||
|
private static final ForgeConfigSpec.IntValue MAGIC_NUMBER = BUILDER
|
||||||
|
.comment("A magic number")
|
||||||
|
.defineInRange("magicNumber", 42, 0, Integer.MAX_VALUE);
|
||||||
|
|
||||||
|
public static final ForgeConfigSpec.ConfigValue<String> MAGIC_NUMBER_INTRODUCTION = BUILDER
|
||||||
|
.comment("What you want the introduction message to be for the magic number")
|
||||||
|
.define("magicNumberIntroduction", "The magic number is... ");
|
||||||
|
|
||||||
|
// a list of strings that are treated as resource locations for items
|
||||||
|
private static final ForgeConfigSpec.ConfigValue<List<? extends String>> ITEM_STRINGS = BUILDER
|
||||||
|
.comment("A list of items to log on common setup.")
|
||||||
|
.defineListAllowEmpty(Collections.singletonList("items"), () -> List.of("minecraft:iron_ingot"), Config::validateItemName);
|
||||||
|
|
||||||
|
static final ForgeConfigSpec SPEC = BUILDER.build();
|
||||||
|
|
||||||
|
public static boolean logDirtBlock;
|
||||||
|
public static int magicNumber;
|
||||||
|
public static String magicNumberIntroduction;
|
||||||
|
public static Set<Item> items;
|
||||||
|
|
||||||
|
private static boolean validateItemName(final Object obj)
|
||||||
|
{
|
||||||
|
return obj instanceof final String itemName && ForgeRegistries.ITEMS.containsKey(new ResourceLocation(itemName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
static void onLoad(final ModConfigEvent event)
|
||||||
|
{
|
||||||
|
logDirtBlock = LOG_DIRT_BLOCK.get();
|
||||||
|
magicNumber = MAGIC_NUMBER.get();
|
||||||
|
magicNumberIntroduction = MAGIC_NUMBER_INTRODUCTION.get();
|
||||||
|
|
||||||
|
// convert the list of strings into a set of items
|
||||||
|
items = ITEM_STRINGS.get().stream()
|
||||||
|
.map(itemName -> ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemName)))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,7 +18,9 @@ import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
|
||||||
import net.minecraftforge.event.server.ServerStartingEvent;
|
import net.minecraftforge.event.server.ServerStartingEvent;
|
||||||
import net.minecraftforge.eventbus.api.IEventBus;
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
|
import net.minecraftforge.fml.ModLoadingContext;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
|
import net.minecraftforge.fml.config.ModConfig;
|
||||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||||
|
@ -78,13 +80,22 @@ public class ExampleMod
|
||||||
|
|
||||||
// Register the item to a creative tab
|
// Register the item to a creative tab
|
||||||
modEventBus.addListener(this::addCreative);
|
modEventBus.addListener(this::addCreative);
|
||||||
|
|
||||||
|
// Register our mod's ForgeConfigSpec so that Forge can create and load the config file for us
|
||||||
|
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.SPEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commonSetup(final FMLCommonSetupEvent event)
|
private void commonSetup(final FMLCommonSetupEvent event)
|
||||||
{
|
{
|
||||||
// Some common setup code
|
// Some common setup code
|
||||||
LOGGER.info("HELLO FROM COMMON SETUP");
|
LOGGER.info("HELLO FROM COMMON SETUP");
|
||||||
|
|
||||||
|
if (Config.logDirtBlock)
|
||||||
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
|
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
|
||||||
|
|
||||||
|
LOGGER.info(Config.magicNumberIntroduction + Config.magicNumber);
|
||||||
|
|
||||||
|
Config.items.forEach((item) -> LOGGER.info("ITEM >> {}", item.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the example block item to the building blocks tab
|
// Add the example block item to the building blocks tab
|
||||||
|
|
Loading…
Reference in a new issue