Jump to content

Recommended Posts

Posted

So I'm pretty new to modding, and I'm working on my first mod, which will add 70 monocolored blocks to the game. (14 colors with 5 shades stored in metadata and blockstates.) The blocks are made out items called "color essences" and you'll be able to craft combinations of essences to get the different colors and shades. I'm trying to shorten the code for all the registering and adding in of items, and I've run into a problem. I can't figure out how to do it. I've been trying to use arrays and maps and sets and all sorts of things to prevent from having to call the same method 14 times.

 

Here are some of my classes:

 

(main class) Colore.java

package com.supergeniuszeb.colore;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.supergeniuszeb.colore.init.ModBlocks;
import com.supergeniuszeb.colore.init.ModItems;
import com.supergeniuszeb.colore.init.ModRecipes;
import com.supergeniuszeb.colore.init.ModTools;
import com.supergeniuszeb.colore.proxy.CommonProxy;

@Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class Colore {

@SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
public static CommonProxy proxy;

public static final ModCreativeTabs tab1 = new ModCreativeTabs("tab1");

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
	//Initializes and registers blocks & items.
	ModBlocks.init();
	ModBlocks.register();
	ModItems.init();
	ModItems.register();
	ModTools.init();
	ModTools.register();

	//Runs the ore generation code. See ModGeneration.java.
	GameRegistry.registerWorldGenerator(new ModGeneration(), 0);
}

@EventHandler
public void init(FMLInitializationEvent event) {
	proxy.registerRenders();

	//Adds all crafting & smelting recipes.
	ModRecipes.addRecipes();

}
@EventHandler
public void postInit(FMLPostInitializationEvent event) {

}
}

 

(in package "proxy") ClientProxy.java

package com.supergeniuszeb.colore.proxy;

import com.supergeniuszeb.colore.init.ModBlocks;
import com.supergeniuszeb.colore.init.ModItems;
import com.supergeniuszeb.colore.init.ModTools;

//Everything in here is done client-side.
//This class and CommonProxy.java are used to keep server-side and client-side stuff separate.
public class ClientProxy extends CommonProxy {
@Override
public void registerRenders() {
	ModBlocks.registerRenders();
	ModItems.registerRenders();
	ModTools.registerRenders();
}
}

 

(in package "proxy") CommonProxy.java

package com.supergeniuszeb.colore.proxy;
//Everything in here is done server-side.
//This class and ClientProxy.java are used to keep server-side and client-side stuff separate.
public class CommonProxy {
public void registerRenders() {

}
}

 

 

(in package "init") ModItems.java

package com.supergeniuszeb.colore.init;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.supergeniuszeb.colore.Colore;
import com.supergeniuszeb.colore.Reference;

public class ModItems extends Item {

public static Item essence_of_red;
public static Item essence_of_reddish_orange;
public static Item essence_of_orange;
public static Item essence_of_orangish_yellow;
public static Item essence_of_yellow;
public static Item essence_of_yellowish_green;

public static Set<Item> colorSet = new LinkedHashSet<>(); //The empty "<>" will only work in Java 7+

{ //IMPORTANT! Methods MUST be called within this main class' methods or in curly braces.
	colorSet.add(essence_of_red);
	colorSet.add(essence_of_orange);
}

public static String[] essenceList = {
	"essence_of_red", "essence_of_orange"
};

public static Object[] colorArray = colorSet.toArray();
//refined essences


//unrefined essences
public static Item unrefined_red;

public static void init() {

	int i = 0;
	for (Object color : colorArray) {
		color = new Item().setUnlocalizedName(essenceList[i]).setCreativeTab(Colore.tab1);
		i++;
	}


	//refined essences
	//essence_of_red = new Item().setUnlocalizedName("essence_of_red").setCreativeTab(Colore.tab1);


	//unrefined essences
	unrefined_red = new Item().setUnlocalizedName("unrefined_red").setCreativeTab(Colore.tab1);

}

public static void register() {

	for (Item color : colorSet) {
		GameRegistry.registerItem(color, color.getUnlocalizedName().substring(5));
	}

	//refined essences
	//GameRegistry.registerItem(essence_of_red, essence_of_red.getUnlocalizedName().substring(5));


	//unrefined essences
	GameRegistry.registerItem(unrefined_red, unrefined_red.getUnlocalizedName().substring(5));

}

public static void registerRenders() {

	for (Item color : colorSet) {
		registerRender(color);
	}

	//refined essences
	//registerRender(essence_of_red);


	//unrefined essences
	registerRender(unrefined_red);

}

//This method is called by the registerRenders method every time it adds an item.
public static void registerRender(Item item) {
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}
}

 

I've based a lot of my code on the

, in case you're wondering why my code is written in this way. If you need to see any more of my classes just let me know. I've tried looking at other modding tutorials but a lot of them set up the proxies and items and stuff in a different way, and I haven't got a clue as to what the most modular, efficient, and wisest solution is. Any suggestions? I keep getting errors when it tries to register the recipes for the blocks, which I assume is due to improper Item registering. I apologize for my somewhat messy code. I've commented out a lot of things during testing and I'm trying to focus on getting just one or 2 items working so I can duplicate it for all the others, rather than have to make changes to several different variables and methods and such.

Colore - The mod that adds monochrome blocks in every color of the rainbow!

http://www.minecraftforge.net/forum/index.php?topic=35149

 

If you're looking to learn how to make mods for 1.9.4, I wrote a helpful article with links to a lot of useful resources for learning Minecraft 1.9.4 modding!

 

http://supergeniuszeb.com/mods/a-helpful-list-of-minecraft-1-9-4-modding-resources/

Posted

While it may seem clever or interesting to do such things in an algorithmic, compressed way, there is nothing wrong with cutting and pasting same code multiple times.

 

You need to ask yourself why you were doing this, and I expect you'd say you thought it would save time. Yet, obviously you've already spent more time trying to save time than you would have saved and you're not even done yet! So what's the point?

 

I'd also propose that the simpler your code is logically, the less likely it is to have bugs. So it is better to cut and paste 14 sections of code that you know work than to try some sort of iteration through a map and such.

 

Of course if you're a high-level programmer, then you can get the map right on the first try and might as well do that. But there is no shame at all in simply having cut and paste code.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

When you have a lot of items that are exactly the same, use item subtypes and handle that in your Item class directly.

 

Don't store meta information such as colorSets outside of the class that uses it - encapsulation is your friend. If you find you are storing a lot of 'meta' type information, you should probably reevaluate your design.

 

If you want to simplify registration, you can easily do so with Reflection.

 

// call this method AFTER you have initialized all items
private static void registerItems() {
try {
	for (Field f : YourItems.class.getFields()) { // get all declared fields in your Items class
		if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register it
			Item item = (Item) f.get(null);
			if (item != null) {
				// while the unlocalized name is not intended for this purpose and some may object to its use
				// like this, I see no harm in it as it is a convenient way to automate registration
				String name = item.getUnlocalizedName();
				GameRegistry.registerItem(item, name.substring(name.lastIndexOf(".") + 1));
			}
		}
	}
} catch(Exception e) {
	// catch so you don't crash, but log so you can still find and fix the issue
	e.printStackTrace();
}
}

 

While you do still have to initialize every item individually, if you use item subtypes correctly you will have far fewer to register, and, using a few custom interfaces, you can set up a similar system to automate registration of item variants and model resource locations.

 

That said, Jabelar's point still stands: if you are not strong in Java, the time you spend trying to automate things will far outweigh the time it would have taken you to just get it working, so it's probably not worth trying until you have more experience.

Posted

Hi

Check this out. Its really simple, yet you only need one line of code when registering items not counting the item field:

public class ItemRegistry
{

    public static ItemBase YOUR_ITEM;

    public static void registerItems()
    {
        ItemRegistry.YOUR_ITEM = new ItemBase(64, "youritem");
    }

}

public class ItemBase extends Item
{
    
    public ItemBase(int maxStackSize, String unlocalizedName)
    {
        this.setUnlocalizedName(unlocalizedName);
        this.setMaxStackSize(maxStackSize);
        GameRegistry.registerItem(this, unlocalizedName);
    }

}

 

You may add extra parameters to your ItemBase constructor such as a CreativeTab if you would like. So simple, yet effective. If you need to add methods to your item such as onItemRightClick for example, use this code in your ItemRegistry class instead:

public static ItemBase YOUR_ITEM;

public static void registerItems()
{
    ItemRegistry.YOUR_ITEM = new YourItem();
}

 

In your YourItem.class, do this:

public class YourItem extends ItemBase //must include specific constructor
{
    
    public YourItem() //No parameters, call super here
    {
        super(64, "youritem"); //@Param: int maxStackSize, String name
    }

    //Now you have a class to include regular item methods

}

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Posted

@coolAlias and EverythingGames: Thanks! I tried your suggestions and they worked? I've created a new class/.java file for my items called ItemEssence and pretty much deprecated the older ModItems.java file (in other words I've commented out most of the references to it in the mian code and in the proxies so it isn't in use but can still be accessed for the time being in case I need to borrow any code from it or something.) I tried starting up the test client and it works! Here's what is in the new ItemEssence.java file:

package com.supergeniuszeb.colore.init;

import java.lang.reflect.Field;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.supergeniuszeb.colore.ModCreativeTabs;
import com.supergeniuszeb.colore.Reference;

//Special thanks to coolAlias and EverythingGames, users from the Forge forums, for helping
//me with figuring out how to optimize my item-and-block-registering code.

public class ItemEssence extends Item {

public ItemEssence(String unlocalizedName) {
	this.setUnlocalizedName(unlocalizedName);
	this.setMaxStackSize(64);
	this.setCreativeTab(ModCreativeTabs.itemTab);
}

public static class ItemRegistry {

	//refined essences
	public static ItemEssence essence_of_red = new ItemEssence("essence_of_red");
	public static ItemEssence essence_of_reddish_orange = new ItemEssence("essence_of_reddish_orange");
	public static ItemEssence essence_of_orange = new ItemEssence("essence_of_orange");
	public static ItemEssence essence_of_orangish_yellow = new ItemEssence("essence_of_orangish_yellow");
	public static ItemEssence essence_of_yellow = new ItemEssence("essence_of_yellow");
	public static ItemEssence essence_of_yellowish_green = new ItemEssence("essence_of_yellowish_green");
	public static ItemEssence essence_of_green = new ItemEssence("essence_of_green");
	public static ItemEssence essence_of_cyan = new ItemEssence("essence_of_cyan");
	public static ItemEssence essence_of_blue = new ItemEssence("essence_of_blue");
	public static ItemEssence essence_of_indigo = new ItemEssence("essence_of_indigo");
	public static ItemEssence essence_of_purple = new ItemEssence("essence_of_purple");
	public static ItemEssence essence_of_magenta = new ItemEssence("essence_of_magenta");
	public static ItemEssence essence_of_brown = new ItemEssence("essence_of_brown");
	public static ItemEssence essence_of_grayscale = new ItemEssence("essence_of_grayscale");
	public static ItemEssence rainbow_essence = new ItemEssence("rainbow_essence");

	//unrefined essences
	public static ItemEssence unrefined_red = new ItemEssence("unrefined_red");
	public static ItemEssence unrefined_orange = new ItemEssence("unrefined_orange");
	public static ItemEssence unrefined_yellow = new ItemEssence("unrefined_yellow");
	public static ItemEssence unrefined_green = new ItemEssence("unrefined_green");
	public static ItemEssence unrefined_blue = new ItemEssence("unrefined_blue");
	public static ItemEssence unrefined_purple = new ItemEssence("unrefined_purple");
	public static ItemEssence unrefined_brown = new ItemEssence("unrefined_brown");
	public static ItemEssence unrefined_white = new ItemEssence("unrefined_white");
	public static ItemEssence unrefined_black = new ItemEssence("unrefined_black");

	public static void registerItems() {
		try {
			for (Field f : ItemRegistry.class.getFields()) { // get all declared fields in the ItemRegistry class
				if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register it
					Item item = (Item) f.get(null);
					if (item != null) {
						String name = item.getUnlocalizedName();
						GameRegistry.registerItem(item, name.substring(name.lastIndexOf(".") + 1));
					}
				}
			}
		} catch(Exception e) {
			// catch so game won't crash, but log so issue can still be found and fixed
			e.printStackTrace();
		}
	}

	public static void registerRenders() {

		try {
			for (Field f : ItemRegistry.class.getFields()) { // get all declared fields in the ItemRegistry class
				if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register its renders
					Item item = (Item) f.get(null);
					if (item != null) {
						registerRender(item);
					}
				}
			}
		} catch(Exception e) {
			// catch so game won't crash, but log so issue can still be found and fixed
			e.printStackTrace();
		}

	}

	//This method is called by the registerRenders method every time it adds an item.
	public static void registerRender(Item item) {
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
	}
}
}

 

What do you think? Any other improvements I could make? Now that I have the item code working it should be fairly easy for me to duplicate this for the blocks and other things I'm adding in my mod.

Colore - The mod that adds monochrome blocks in every color of the rainbow!

http://www.minecraftforge.net/forum/index.php?topic=35149

 

If you're looking to learn how to make mods for 1.9.4, I wrote a helpful article with links to a lot of useful resources for learning Minecraft 1.9.4 modding!

 

http://supergeniuszeb.com/mods/a-helpful-list-of-minecraft-1-9-4-modding-resources/

Posted

I wouldn't nest those classes and methods inside of an Item class - just make one giant class that contains all of your Item references and you should be fine.

 

The advantage of using Reflection vs. a simpler solution such as @EverythingGames proposed is that you can do other things with your Items at the time of registration, such as making calls based on implemented interfaces or whatever else. Pretty handy, but you may not need it.

 

It's much more difficult to provide a similar system for Blocks because Block registration is far more complicated than Item registration. You may have to provide custom ItemBlocks for each block, and those may take custom arguments, plus the registerBlock method might require additional arguments, not to mention registering TileEntities if needed.

 

Unless you really just have cookie-cutter blocks, I wouldn't advise a similar system.

Posted

It's more complicated with blocks, as @coolAlias said, but its basically the same as the item code I provided, just a little more code in the base block class. This class includes more features such as block sound, material, hardness, resistance, tool harvest, random generation, etc. Really handy when creating many blocks and items if I should say so myself.

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Posted

I wouldn't nest those classes and methods inside of an Item class - just make one giant class that contains all of your Item references and you should be fine.

~snip~

Unless you really just have cookie-cutter blocks, I wouldn't advise a similar system.

Luckily, my whole mod is about blocks and items that are identical aside from their color.

 

Anyway, literally right after posting my previous post I noticed something. I had set up my packages and classes like so:

 

 

 

blocks

>>ModBlock.java (used in place of the official Block class which is private)

>>ModBlockOre.java (an ore version of the above class)

 

init

>>ModItems.java (the item classes, and initializing & registering methods were all located here - now deprecated)

>>ModBlocks.java (similar to the above, but using the already-defined ModBlock & ModBlockOre classes)

>>ModTools.java (tool initialization & registration)

>>ModRecipes.java (registration of all recipes)

>>ModArmor.java (empty/WIP)

>>ModMetaItems(unused test class that I was testing meta blocks in)

>>ItemEssence.java (my new item class for holding all item classes and initialization and registration of items)

 

items

>>ModAxe.java (axe class)

>>ModHoe.java (hoe class)

>>ModPickaxe.java (pickaxe class)

>>ModShovel.java (shovel class)

>>ModSword.java (sword class)

 

proxy

>>ClientProxy.java

>>CommonProxy.java

 

(and then there were some other classes located in the main Colore package such as ModCreativeTabs.java and Reference.java)

 

 

 

I realized that if I have an items package and a blocks package, then wouldn't it make more sense to put the base item/block classes in there and have the init package contain the registration classes? So I began doing some heavy refactoring and changed my code structure to this:

 

 

blocks

>>BlockColore.java (used in place of the official Block class which is private)

>>BlockColoreOre.java (an ore version of the above class)

 

init

(ModItems.java and ModBlocks.java were deleted)

>>ItemRegistry.java (I extracted the ItemRegistry class from ItemEssence.java)

>>BlockRegistry.java (block initialization & registration)

>>ToolRegistry.java (tool initialization & registration)

>>ModRecipes.java (recipe initialization & registration)

>>ModArmor.java (still empty - will probably rename to ArmorRegistry once I start working on my mod's armor)

>>ModMetaItems(still unused - will probably delete)

 

items

>>ItemEssence.java (now contains only ItemEssence base class & constructor)

>>ModAxe.java (axe class)

>>ModHoe.java (hoe class)

>>ModPickaxe.java (pickaxe class)

>>ModShovel.java (shovel class)

>>ModSword.java (sword class)

 

proxy

>>ClientProxy.java

>>CommonProxy.java

 

 

 

So I've tested everything and it all works fine, aside from some missing textures which are missing because I haven't made them yet. (:P) I've basically separated the base item/block classes from the initialization/registration of the items/blocks that use them, which I think helps with making my code more modular and organized. What do you think? For reference, here are the new classes:

 

 

 

(from blocks package) BlockColore.java

package com.supergeniuszeb.colore.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;

import com.supergeniuszeb.colore.ModCreativeTabs;

public class BlockColore extends Block {

public BlockColore(String unlocalizedName) {
	super(Material.rock);
	this.setHardness(1.5f);
	this.setHarvestLevel("pickaxe", 0);
	this.setResistance(10.0f);
	this.setUnlocalizedName(unlocalizedName);
	this.setCreativeTab(ModCreativeTabs.blockTab);
}
}

 

BlockColoreOre.java

package com.supergeniuszeb.colore.blocks;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;

import com.supergeniuszeb.colore.ModCreativeTabs;
import com.supergeniuszeb.colore.init.BlockRegistry;
import com.supergeniuszeb.colore.init.ItemRegistry;

public class BlockColoreOre extends Block {
public BlockColoreOre(String unlocalizedName) {
	super(Material.rock);
	this.setHardness(1.5f);
	this.setHarvestLevel("pickaxe", 0);
	this.setResistance(10.0f);
	this.setUnlocalizedName(unlocalizedName);
	this.setCreativeTab(ModCreativeTabs.blockTab);
}

@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune){
	if (this == BlockRegistry.red_ore){
		return ItemRegistry.unrefined_red;
	}
	if (this == BlockRegistry.orange_ore){
		return ItemRegistry.unrefined_orange;
	}
	if (this == BlockRegistry.yellow_ore){
		return ItemRegistry.unrefined_yellow;
	}
	if (this == BlockRegistry.green_ore){
		return ItemRegistry.unrefined_green;
	}
	if (this == BlockRegistry.blue_ore){
		return ItemRegistry.unrefined_blue;
	}
	if (this == BlockRegistry.purple_ore){
		return ItemRegistry.unrefined_purple;
	}
	if (this == BlockRegistry.brown_ore){
		return ItemRegistry.unrefined_brown;
	}
	if (this == BlockRegistry.white_ore){
		return ItemRegistry.unrefined_white;
	}
	if (this == BlockRegistry.black_ore){
		return ItemRegistry.unrefined_black;
	}
	return null;
}

@Override
public int quantityDropped(Random rand){
	return 4 + rand.nextInt(3);
}
}

 

(items package) ItemEssence.java

package com.supergeniuszeb.colore.items;

import net.minecraft.item.Item;

import com.supergeniuszeb.colore.ModCreativeTabs;

//Special thanks to coolAlias and EverythingGames, users from the Forge forums, for helping
//me with figuring out how to optimize my item-and-block-registering code.

public class ItemEssence extends Item {

public ItemEssence(String unlocalizedName) {
	this.setUnlocalizedName(unlocalizedName);
	this.setMaxStackSize(64);
	this.setCreativeTab(ModCreativeTabs.itemTab);
}
}

 

(init package) ItemRegistry.java

package com.supergeniuszeb.colore.init;

import java.lang.reflect.Field;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.supergeniuszeb.colore.Reference;
import com.supergeniuszeb.colore.items.ItemEssence;
//is static needed?
public class ItemRegistry {

//refined essences
public static ItemEssence essence_of_red = new ItemEssence("essence_of_red");
public static ItemEssence essence_of_reddish_orange = new ItemEssence("essence_of_reddish_orange");
public static ItemEssence essence_of_orange = new ItemEssence("essence_of_orange");
public static ItemEssence essence_of_orangish_yellow = new ItemEssence("essence_of_orangish_yellow");
public static ItemEssence essence_of_yellow = new ItemEssence("essence_of_yellow");
public static ItemEssence essence_of_yellowish_green = new ItemEssence("essence_of_yellowish_green");
public static ItemEssence essence_of_green = new ItemEssence("essence_of_green");
public static ItemEssence essence_of_cyan = new ItemEssence("essence_of_cyan");
public static ItemEssence essence_of_blue = new ItemEssence("essence_of_blue");
public static ItemEssence essence_of_indigo = new ItemEssence("essence_of_indigo");
public static ItemEssence essence_of_purple = new ItemEssence("essence_of_purple");
public static ItemEssence essence_of_magenta = new ItemEssence("essence_of_magenta");
public static ItemEssence essence_of_brown = new ItemEssence("essence_of_brown");
public static ItemEssence essence_of_grayscale = new ItemEssence("essence_of_grayscale");
public static ItemEssence rainbow_essence = new ItemEssence("rainbow_essence");

//unrefined essences
public static ItemEssence unrefined_red = new ItemEssence("unrefined_red");
public static ItemEssence unrefined_orange = new ItemEssence("unrefined_orange");
public static ItemEssence unrefined_yellow = new ItemEssence("unrefined_yellow");
public static ItemEssence unrefined_green = new ItemEssence("unrefined_green");
public static ItemEssence unrefined_blue = new ItemEssence("unrefined_blue");
public static ItemEssence unrefined_purple = new ItemEssence("unrefined_purple");
public static ItemEssence unrefined_brown = new ItemEssence("unrefined_brown");
public static ItemEssence unrefined_white = new ItemEssence("unrefined_white");
public static ItemEssence unrefined_black = new ItemEssence("unrefined_black");

public static void registerItems() {
	try {
		for (Field f : ItemRegistry.class.getFields()) { // get all declared fields in the ItemRegistry class
			if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register it
				Item item = (Item) f.get(null);
				if (item != null) {
					String name = item.getUnlocalizedName();
					GameRegistry.registerItem(item, name.substring(name.lastIndexOf(".") + 1));
				}
			}
		}
	} catch(Exception e) {
		// catch so game won't crash, but log so issue can still be found and fixed
		e.printStackTrace();
	}
}

public static void registerRenders() {

	try {
		for (Field f : ItemRegistry.class.getFields()) { // get all declared fields in the ItemRegistry class
			if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register its renders
				Item item = (Item) f.get(null);
				if (item != null) {
					registerRender(item);
				}
			}
		}
	} catch(Exception e) {
		// catch so game won't crash, but log so issue can still be found and fixed
		e.printStackTrace();
	}

}

//This method is called by the registerRenders method every time it adds an item.
public static void registerRender(Item item) {
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}

}

 

BlockRegistry.java

package com.supergeniuszeb.colore.init;

import java.lang.reflect.Field;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.supergeniuszeb.colore.Reference;
import com.supergeniuszeb.colore.blocks.BlockColore;
import com.supergeniuszeb.colore.blocks.BlockColoreOre;

//Special thanks to coolAlias and EverythingGames, users from the Forge forums, for helping
//me with figuring out how to optimize my item-and-block-registering code.

public class BlockRegistry {

//pure blocks
public static BlockColore block_of_red = new BlockColore("block_of_red");
public static BlockColore block_of_reddish_orange = new BlockColore("block_of_reddish_orange");
public static BlockColore block_of_orange = new BlockColore("block_of_orange");
public static BlockColore block_of_orangish_yellow = new BlockColore("block_of_orangish_yellow");
public static BlockColore block_of_yellow = new BlockColore("block_of_yellow");
public static BlockColore block_of_yellowish_green = new BlockColore("block_of_yellowish_green");
public static BlockColore block_of_green = new BlockColore("block_of_green");
public static BlockColore block_of_cyan = new BlockColore("block_of_cyan");
public static BlockColore block_of_blue = new BlockColore("block_of_blue");
public static BlockColore block_of_indigo = new BlockColore("block_of_indigo");
public static BlockColore block_of_purple = new BlockColore("block_of_purple");
public static BlockColore block_of_magenta = new BlockColore("block_of_magenta");
public static BlockColore block_of_brown = new BlockColore("block_of_brown");
public static BlockColore block_of_grayscale = new BlockColore("block_of_grayscale");
public static BlockColore rainbow_block = new BlockColore("rainbow_block");

//ore blocks
public static BlockColoreOre red_ore = new BlockColoreOre("red_ore");
public static BlockColoreOre orange_ore = new BlockColoreOre("orange_ore");
public static BlockColoreOre yellow_ore = new BlockColoreOre("yellow_ore");
public static BlockColoreOre green_ore = new BlockColoreOre("green_ore");
public static BlockColoreOre blue_ore = new BlockColoreOre("blue_ore");
public static BlockColoreOre purple_ore = new BlockColoreOre("purple_ore");
public static BlockColoreOre brown_ore = new BlockColoreOre("brown_ore");
public static BlockColoreOre white_ore = new BlockColoreOre("white_ore");
public static BlockColoreOre black_ore = new BlockColoreOre("black_ore");

public static void registerBlocks() {
	try {
		for (Field f : BlockRegistry.class.getFields()) { // get all declared fields in the BlockRegistry class
			if (Block.class.isAssignableFrom(f.getType())) { // if a field is a block, fetch it and register it
				Block block = (Block) f.get(null);
				if (block != null) {
					String name = block.getUnlocalizedName();
					GameRegistry.registerBlock(block, name.substring(name.lastIndexOf(".") + 1));
				}
			}
		}
	} catch(Exception e) {
		// catch so game won't crash, but log so issue can still be found and fixed
		e.printStackTrace();
	}
}

public static void registerRenders() {

	try {
		for (Field f : BlockRegistry.class.getFields()) { // get all declared fields in the BlockRegistry class
			if (Block.class.isAssignableFrom(f.getType())) { // if a field is a block, fetch it and register its renders
				Block block = (Block) f.get(null);
				if (block != null) {
					registerRender(block);
				}
			}
		}
	} catch(Exception e) {
		// catch so game won't crash, but log so issue can still be found and fixed
		e.printStackTrace();
	}

}

//This method is called by the registerRenders method every time it adds an item.
public static void registerRender(Block block) {
	Item item = Item.getItemFromBlock(block);
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}
}

 

ToolRegistry.java

package com.supergeniuszeb.colore.init;

import java.lang.reflect.Field;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.supergeniuszeb.colore.ModCreativeTabs;
import com.supergeniuszeb.colore.Reference;
import com.supergeniuszeb.colore.items.ModAxe;
import com.supergeniuszeb.colore.items.ModHoe;
import com.supergeniuszeb.colore.items.ModPickaxe;
import com.supergeniuszeb.colore.items.ModShovel;
import com.supergeniuszeb.colore.items.ModSword;

public class ToolRegistry {
public static final Item.ToolMaterial coloreMaterial = EnumHelper.addToolMaterial("coloreMaterial", 2, 175, 9.0f, 7.0f, 15);

// swords
public static Item red_sword = new ModSword(coloreMaterial).setUnlocalizedName("red_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item reddish_orange_sword = new ModSword(coloreMaterial).setUnlocalizedName("reddish_orange_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item orangish_yellow_sword = new ModSword(coloreMaterial).setUnlocalizedName("orangish_yellow_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellow_sword = new ModSword(coloreMaterial).setUnlocalizedName("yellow_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellowish_green_sword = new ModSword(coloreMaterial).setUnlocalizedName("yellowish_green_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item green_sword = new ModSword(coloreMaterial).setUnlocalizedName("green_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item cyan_sword = new ModSword(coloreMaterial).setUnlocalizedName("cyan_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item blue_sword = new ModSword(coloreMaterial).setUnlocalizedName("blue_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item indigo_sword = new ModSword(coloreMaterial).setUnlocalizedName("indigo_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item purple_sword = new ModSword(coloreMaterial).setUnlocalizedName("purple_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item brown_sword = new ModSword(coloreMaterial).setUnlocalizedName("brown_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item white_sword = new ModSword(coloreMaterial).setUnlocalizedName("white_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item gray_sword = new ModSword(coloreMaterial).setUnlocalizedName("gray_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item black_sword = new ModSword(coloreMaterial).setUnlocalizedName("black_sword").setCreativeTab(ModCreativeTabs.toolTab);
public static Item rainbow_sword = new ModSword(coloreMaterial).setUnlocalizedName("rainbow_sword").setCreativeTab(ModCreativeTabs.toolTab);

// pickaxes
public static Item red_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("red_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item reddish_orange_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("reddish_orange_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item orangish_yellow_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("orangish_yellow_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellow_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("yellow_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellowish_green_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("yellowish_green_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item green_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("green_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item cyan_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("cyan_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item blue_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("blue_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item indigo_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("indigo_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item purple_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("purple_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item brown_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("brown_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item white_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("white_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item gray_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("gray_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item black_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("black_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item rainbow_pickaxe = new ModPickaxe(coloreMaterial).setUnlocalizedName("rainbow_pickaxe").setCreativeTab(ModCreativeTabs.toolTab);

// axes
public static Item red_axe = new ModAxe(coloreMaterial).setUnlocalizedName("red_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item reddish_orange_axe = new ModAxe(coloreMaterial).setUnlocalizedName("reddish_orange_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item orangish_yellow_axe = new ModAxe(coloreMaterial).setUnlocalizedName("orangish_yellow_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellow_axe = new ModAxe(coloreMaterial).setUnlocalizedName("yellow_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellowish_green_axe = new ModAxe(coloreMaterial).setUnlocalizedName("yellowish_green_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item green_axe = new ModAxe(coloreMaterial).setUnlocalizedName("green_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item cyan_axe = new ModAxe(coloreMaterial).setUnlocalizedName("cyan_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item blue_axe = new ModAxe(coloreMaterial).setUnlocalizedName("blue_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item indigo_axe = new ModAxe(coloreMaterial).setUnlocalizedName("indigo_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item purple_axe = new ModAxe(coloreMaterial).setUnlocalizedName("purple_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item brown_axe = new ModAxe(coloreMaterial).setUnlocalizedName("brown_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item white_axe = new ModAxe(coloreMaterial).setUnlocalizedName("white_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item gray_axe = new ModAxe(coloreMaterial).setUnlocalizedName("gray_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item black_axe = new ModAxe(coloreMaterial).setUnlocalizedName("black_axe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item rainbow_axe = new ModAxe(coloreMaterial).setUnlocalizedName("rainbow_axe").setCreativeTab(ModCreativeTabs.toolTab);

// shovels
public static Item red_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("red_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item reddish_orange_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("reddish_orange_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item orangish_yellow_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("orangish_yellow_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellow_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("yellow_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellowish_green_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("yellowish_green_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item green_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("green_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item cyan_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("cyan_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item blue_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("blue_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item indigo_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("indigo_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item purple_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("purple_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item brown_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("brown_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item white_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("white_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item gray_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("gray_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item black_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("black_shovel").setCreativeTab(ModCreativeTabs.toolTab);
public static Item rainbow_shovel = new ModShovel(coloreMaterial).setUnlocalizedName("rainbow_shovel").setCreativeTab(ModCreativeTabs.toolTab);

// hoes
public static Item red_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("red_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item reddish_orange_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("reddish_orange_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item orangish_yellow_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("orangish_yellow_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellow_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("yellow_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item yellowish_green_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("yellowish_green_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item green_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("green_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item cyan_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("cyan_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item blue_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("blue_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item indigo_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("indigo_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item purple_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("purple_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item brown_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("brown_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item white_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("white_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item gray_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("gray_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item black_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("black_hoe").setCreativeTab(ModCreativeTabs.toolTab);
public static Item rainbow_hoe = new ModHoe(coloreMaterial).setUnlocalizedName("rainbow_hoe").setCreativeTab(ModCreativeTabs.toolTab);


public static void registerItems() {
	try {
		for (Field f : ToolRegistry.class.getFields()) { // get all declared fields in the ItemRegistry class
			if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register it
				Item item = (Item) f.get(null);
				if (item != null) {
					String name = item.getUnlocalizedName();
					GameRegistry.registerItem(item, name.substring(name.lastIndexOf(".") + 1));
				}
			}
		}
	} catch(Exception e) {
		// catch so game won't crash, but log so issue can still be found and fixed
		e.printStackTrace();
	}
}

public static void registerRenders() {

	try {
		for (Field f : ToolRegistry.class.getFields()) { // get all declared fields in the ItemRegistry class
			if (Item.class.isAssignableFrom(f.getType())) { // if a field is an item, fetch it and register its renders
				Item item = (Item) f.get(null);
				if (item != null) {
					registerRender(item);
				}
			}
		}
	} catch(Exception e) {
		// catch so game won't crash, but log so issue can still be found and fixed
		e.printStackTrace();
	}

}

//This method is called by the registerRenders method every time it adds an item.
public static void registerRender(Item item) {
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}
}

 

 

Colore - The mod that adds monochrome blocks in every color of the rainbow!

http://www.minecraftforge.net/forum/index.php?topic=35149

 

If you're looking to learn how to make mods for 1.9.4, I wrote a helpful article with links to a lot of useful resources for learning Minecraft 1.9.4 modding!

 

http://supergeniuszeb.com/mods/a-helpful-list-of-minecraft-1-9-4-modding-resources/

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.