Tutorials/Configuration files

From Minecraft Forge
Jump to: navigation, search

How-To Icon.png

This is a How-To guide or Tutorial detailing a practice or process for Minecraft Forge or related software.

Information Icon.png

This page was made for Minecraft 1.6.4. It might not work with other versions.

This is a part of Telinc's Tutorial Mod. Content covers Minecraft 1.4.7 to Minecraft 1.6.4. Its difficulty is rated as Beginner.

In this tutorial we will learn about config files. This is a bit more complicated, so be sure to follow along!

Contents

Finally using @PreInit!

Yes, that's right, we finally have a use for our preInit method. The first thing you're gonna want to do in there is add this:

Configuration config = new Configuration(event.getSuggestedConfigurationFile());

config.load();

// Configuration goes here.
	
config.save();
The first line creates a variable called config. It contains data for the file "<base folder>\config\<your mod id>.cfg".

The second line loads the configuration file into memory. The comment after it just tells you where to put declarations.

Finally, we have a method that saves the configuration the file.

Use the config for block and item IDs

You know how most mods have changeable block and item IDs? You can do that for your mod too!

Creating a customizable ID for our block

After the declaration of the variables for your blocks/items put:

public static int myFirstBlockID

This is basically an integer (number). It will be blank until we don't set it. The way I prefer to name this is to put my variable for my block name followed by "ID".

Remember that comment we made in the config section? In its place put:

myFirstBlockID = config.getBlock("myFirstBlock", 538).getInt();
This sets the ID variable.
getBlock(String key, int defaultId) is a method from the Configuration class. It gets (or creates) a block ID property.
.getInt() converts the return value to an integer, otherwise we will get an error, as getBlock returns a Property.
Parameters for getBlock():

key is a String. It's how the ID is gonna be referenced in the <modid>.cfg file. You can even put "thisIsTheIDForMyNewAndReallyReallyAwesomeBlock" in there, but that's long and unclear.

defaultID is an Integer. It's the default ID that will be set. I recommend setting it to the ID you put for your block (538 in my case).

Making your block use the set ID

This is super simple. Go to the declaration of your block and change the ID to the name of the variable. Look at the complete code if you don't understand.

Creating a customizable ID for our item(s)

This is almost the same as blocks. After the "public static int myFirstBlockID" make another int that's public and static. Name it something like "myFirstItemID". Basically the name of your item followed by "ID".

After the declaration of your Block ID, add:

myFirstItemID = config.getItem("myFirstItem", 900).getInt();

This is all the same as blocks: first parameter is the key, second one is the default ID. Only differences are getItem instead of getBlock and a different variable name.

Making your item use the set ID

This is again, the same as blocks. Just change the item ID to the variable name.

Creating a boolean

Let's say you want to set for example does a block do damage to you. This is what you can use this for. Create a new field that instead of int is "boolean". Here is the code fore setting the variable:

yourVariableName = config.get(config.CATEGORY_GENERAL, "doesRandomBlockHurtYou", false).getBoolean(false);
The get(String category, String key, boolean defaultValue) gets (or creates) a boolean from the config file. getBoolean(boolean _default) converts the Property returned to a boolean.
Parameters for get():

category is a String representing the category. config.CATEGORY_GENERAL is the "general" category. You can put whatever you want in here. key is the key. Be sure to explain it decently without making it long. defaultValue the value that will be used if the key doesn't exist.

Parameters for getBoolean():

'_default is what this is going to return by default. Set it to be the same as defaultValue in get().

Final Code

TutorialMain.java

package telinc.tutorialmod.common;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = "tutorial", name = "Tutorial Mod", version = "1.0.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)

public class TutorialMain {
	
	@Instance("tutorial")
	public static TutorialMain instance;
	
	@SidedProxy(clientSide="telinc.tutorialmod.client.TutorialClientProxy", serverSide="telinc.tutorialmod.common.TutorialCommonProxy")
	public static TutorialCommonProxy proxy;
	
	public static Block myFirstBlock;
	
	public static Item myFirstItem, myFirstFuel;
	
	public static int myFirstBlockID;
	public static int myFirstItemID;
	public static boolean tutorialBool;
	
	@PreInit
	public void preInit(FMLPreInitializationEvent event) {
		Configuration config = new Configuration(event.getSuggestedConfigurationFile());
		
		config.load();
		
		myFirstBlockID = config.getBlock("myFirstBlock", 538).getInt();
		myFirstItemID = config.getItem("myFirstItem", 900).getInt();
		tutorialBool = config.get(config.CATEGORY_GENERAL, "doesRandomBlockHurtYou", false).getBoolean(false);
		
		config.save();
	}

	@Init
	public void init(FMLInitializationEvent event) {
		myFirstBlock = (new BlockFirst(myFirstBlockID, 0)).setBlockName("myFirstBlock");
		LanguageRegistry.addName(myFirstBlock, "My first block");
		MinecraftForge.setBlockHarvestLevel(myFirstBlock, "pickaxe", 3);
		GameRegistry.registerBlock(myFirstBlock);
		
		myFirstItem = (new ItemFirst(900)).setIconIndex(0).setItemName("firstItem");
		LanguageRegistry.addName(myFirstItem, "My first item");
		
		myFirstFuel = (new ItemFuel(901)).setIconIndex(0).setItemName("fuelItem");
		LanguageRegistry.addName(myFirstFuel, "My fuel item");
		
		GameRegistry.registerFuelHandler(new TutorialFuelHandler());
	}

	@PostInit
	public static void postInit(FMLPostInitializationEvent event) {
		
	}
}
Personal tools
Namespaces
Variants
Actions
Navigation
tutorials
Toolbox