Jump to content

The Ultimate Guide to Configs


Recommended Posts

You can find very helpful guides here and here on how to create a config, but I want to make a tutorial so here. 
 

1.) Create the config file in your mod. Not the .toml, the .java in src/main/java/com/yourname/modid. Put it wherever in that directory you like, just name it something like ClientConfig. Inside, copy-paste:

Quote

public class ClientConfig {
    public static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
    public static final ForgeConfigSpec SPEC;

    static {
        BUILDER.push("Example Mod Client Configs");

        BUILDER.pop();
        SPEC = BUILDER.build();
    }
}

Replace "Example Mod Client Configs" in BUILDER.push("Example Mod Client Configs" to whatever you want to be at the top of the .toml file. That will set up your config, but now we have to register it. In your main mod file (usually named modid.java), inside public ModID() put

Quote

ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ClientConfig.SPEC, "modid-client-config.toml");

Here is where you will actually rename stuff. If you want a config other than client, replace CLIENT in ModConfig.Type.CLIENT with COMMON or SERVER, whichever one you want. I will explain those at the end, just copy the code for now. You will also have to rename ClientConfig in ClientConfig.SPEC to whatever your config.java is called. Then, the final argument is "modid-client-config.toml", just replace that with what you want it to be named. Make sure to put your modid in there, so you can find it easier in modpacks. 

That's great and all, but how do we actually add a variable to our config? Go back to your config file, and add

Quote

public static final ForgeConfigSpec.ConfigValue<Integer> EXAMPLE_INTEGER;

after

Quote

public static final ForgeConfigSpec SPEC;

You probably have some errors, but we're not done. In static, after

Quote

BUILDER.push("Example Mod Client Configs");

add

Quote

EXAMPLE_INTEGER = BUILDER.comment("Here is where your example integer comment will go.").define("Example Integer", 0);

Rename EXAMPLE_INTEGER to whatever you wrote before. The .comment() adds a comment when generated since the mod doesn't come with the .toml file pre-made. The .define() will be the name of the variable, and the 0 at the end is the default value. There are things like range, but I can't figure it out so you'll have to check link one or google it. If you haven't modified anything yet, the file should look like this:

Quote

import net.minecraftforge.common.ForgeConfigSpec;

public class Modules {
    public static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
    public static final ForgeConfigSpec SPEC;

    public static final ForgeConfigSpec.ConfigValue<Integer> EXAMPLE_INTEGER;

    static {
        BUILDER.push("Example Mod Client Configs");

        EXAMPLE_INTEGER = BUILDER.comment("Here is where your example integer comment will go.").define("Example Integer", 0);

        BUILDER.pop();
        SPEC = BUILDER.build();
    }
}

That's great and all, but how do we actually use it in our code? Well, you can use

Quote

ClientConfig.EXAMPLE_INTEGER.get()

for getting the value, or 

Quote

ClientConfig.EXAMPLE_INTEGER.equals(0)

to return a true/false. If you want to have modules similar to Apotheosis or Quark, then that is as simple as wrapping the registers in an if connected to a server client config boolean. That would look like this:

Quote

if (ServerConfig.EnableItems.equals(true)) {
    Items.register(modEventBus);
}

in your modid.java file. This assumes that modEventBus is declared like 

Quote

IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

If you want to create seperate module files, then you have to make it public and static. Also, move it to the public class instead of the public inside the public class. That shouldn't break anything, or at least it didn't in my mod. 

Quick Note: This may have an issue regarding the modules, but if I put false in the IF loop it disables the items. The issue seems to be in the getting of the value, and the server config is not being generated when running in the MDK. Outside it works fine, but setting the config to false and re-running does not get rid of the items. If anybody knows how I can fix this, please let me know. 

Now here's the part of the tutorial where I said I would explain more about config and variable types. There are three types of configs; Client, Common, and Server.

Client is usually reserved for things like textures and image sizes, as they only show up on client-side. Examples for client-side mods are Journeymap, Appleskin, and JEI (mostly, the button to move items to crafting table only works when server-side is installed). 

Common is on both sides, client and server. 

Server is per-world, and act pretty much like gamerules. 

You can probably get away with only using Integers, Booleans, and Strings as your variable types. If you want more information about variable types, check link one. I have stuff to do and this tutorial is already super long. 

Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.