Jump to content

[1.8][SOLVED] Having Trouble Implementing Metadata Blocks/Items


Recommended Posts

Posted

EDIT: After rewriting a lot of my code, restructuring the classes and packages, and reading deeper into tutorials, I eventually figured out how to get it done. I wound up using just a single "BlockColore" class with 5 metadata states for the 5 different shades. I considered making the 14 colors be the metadata instead, but I decided against it, as I didn't want to limit myself to 16 base colors, although I don't currently plan on having more than my current 14. Actually, now that I think about it, I probably should've made the colors be the metadata. But I've finished coding it all and getting it working, so I'm not going to bother changing it unless I need to. (As in I have to rewrite my mod from ground up because of a huge Minecraft/Forge change or the official Mod API finally gets released.) I gave up on trying to color the blocks/items using a color-map or something... maybe I'll try that again in the future. Thanks to everyone who responded and helped me on this question. :D

 

I'm currently working on my first-ever Minecraft mod, which will add mono-colored blocks (and "essences of color" items used to craft them) that come in 14 base colors. My plan is to set it up so that I use metadata to define whether an essence item/color block is a "lighter", "light", "normal", "dark", or "darker" variant to make a total of 70 different blocks and 70 different colors. For example, there would be a "block_of_red" block which would use metadata to determine whether it is a "Block of Red", "Block of Light Red", "Block of Lighter Red", "Block of Dark Red", or "Block of Darker Red". (And yes, I am aware I could just call it "Red Block", but "Block of Red" sounds cooler.) My problem is that I'm having trouble trying to figure out how to implement the metadata. (And I REALLY don't want to just take the lazy/boring route and make 70 separate blocks.)

 

How some of my classes are currently set up:

 

I have an "init" package which contains classes used for registering all the individual items/blocks, an "items" package containing the base classes for different item types ("essences", swords, pickaxes, etc.), and a "blocks" package containing the base classes for different block types (regular blocks and ore blocks).

 

blocks package - BlockColore.java (base basic block class - as you can see I've commented out the code I'm working on for the metadata)

package com.supergeniuszeb.colore.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.util.IStringSerializable;

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);
}
/**	
public static final PropertyEnum SHADE = PropertyEnum.create("shade", BlockColore.EnumType.class);

public enum EnumType implements IStringSerializable {
	DARKER(0, "darker"),
	DARK(1, "dark"),
	NORMAL(2, "normal"),
	LIGHT(3, "light"),
	LIGHTER(4, "lighter");

	private int ID;
	private String name;

	private EnumType(int ID, String name) {
		this.ID = ID;
		this.name = name;
	}

	@Override
	public String getName() {
		return null;
	}

	@Override
	public String toString() {
		return getName();
	}

}**/
/**
@Override
protected BlockState createBlockState() {
	return new BlockState(this, new IProperty[] {SHADE});
}**/
/**
@Override
public IBlockState getStateFromMeta(int meta) {
	return getDefaultState().withProperty(SHADE, value)
}**/
}

 

items package - ItemEssence.java (base "essence" item class)

package com.supergeniuszeb.colore.items;

import java.util.List;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

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) {
	super();
	this.setHasSubtypes(true);
	this.setUnlocalizedName(unlocalizedName);
	this.setMaxStackSize(64);
	this.setCreativeTab(ModCreativeTabs.itemTab);
}

public String essenceShade = new String();

public String getUnlocalizedName(ItemStack stack) {
	if (stack.getItemDamage() == -2) {
		essenceShade = "darker";
	} else if (stack.getItemDamage() == -1) {
		essenceShade = "dark";
	} else if (stack.getItemDamage() == 0) {
		essenceShade = "normal";
	} else if (stack.getItemDamage() == 1) {
		essenceShade = "light";
	} else if (stack.getItemDamage() == 2) {
		essenceShade = "lighter";
	}
	return super.getUnlocalizedName() + "." + (essenceShade);
}

public void getSubItems(Item itemIn, ModCreativeTabs tab, List subItems) {
	subItems.add(new ItemStack(itemIn, 1, -2));
	subItems.add(new ItemStack(itemIn, 1, -1));
	subItems.add(new ItemStack(itemIn, 1, 0));
	subItems.add(new ItemStack(itemIn, 1, 1));
	subItems.add(new ItemStack(itemIn, 1, 2));
}
}

 

init package - BlockRegistry.java (class containing code for registering the individual blocks)

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"));
}
}

 

init package - ItemRegistry.java (code for registering "essence" items)

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, meta, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}

}

 

I've tried reading some tutorials, but due to differences between the code in the tutorials and the way I've written my code, I've had trouble trying to implement it and it's become very confusing. Do I need to make a separate base class file for all 14 base color blocks (and likewise for the "essence items")? I would prefer not to do that, since all the "essence" items are literally identical except for their color, and likewise for the blocks. (And to be honest, if it weren't for the 4-bit limitations of metadata, I would only have one block with 70 different blockstates, and likewise for the "essence" items.) Or is that actually how it's supposed to be done? Am I taking the wrong approach? What's the best way to approach this? (without using tile-entities, since I want these to be pushable by pistons)

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

M-Kay - Lemme start with: "Oh! Ma eyes! Thy paste wall of maddening code!" // I don't much time so can't look closely, sorry.

 

And now - if that is your 1st mod, I suggest looking up good-practice examples:

www.minecraftforge.net/forum/index.php/topic,26267.0.html

 

Everything you need to know (template) is there.

 

For more knowledge:

http://greyminecraftcoder.blogspot.co.at/p/list-of-topics.html

and

http://jabelarminecraft.blogspot.com/

1.7.10 is no longer supported by forge, you are on your own.

Posted

Since you're just filling color, you might be able to gain something from seeing how potions render the fill color in their bottles (but since they're items, not blocks, you might need to write your own abstract class to mimic colored items).

 

Also look at how grass changes color in different biomes.

 

Metadata is still limited to 4 data bits (16 values 0-15), so storing 14x5 (70 values) is slightly awkward. You might do an abstract class that extends Block and defines your 14 hues (the best use of 16 possible metadata values), then extend it to 5 actual block classes (your 5 brightness levels). In this scheme, your block classes would be darker, dark, normal, light and lighter. Each would inherit your hue definitions from the parent abstract class.

 

You'll need an item block (or five) to deal with inventory.

 

PS: I wonder how far you can get with vanilla stained glass blocks back-lit by lamps with five brightness settings (probably not far since glass blocks have such low hue saturation).

 

 

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.