Jump to content

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


SuperGeniusZeb

Recommended Posts

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • हथियार व्यापारियों की चमक आज के समय में भारत और दुनिया के विभिन्न हिस्सों में तेजी से बढ़ रही है। स्वीडन की स्थित संस्था सिपरी की नवीनतम रिपोर्ट के अनुसार, विश्व भर में विभिन्न देश अब पूर्व कभी नहीं देखे गए हथियारों और सैन्य सामग्रियों पर धन का व्यय कर रहे हैं। वैश्विक संघर्षों के बढ़ते संकेतों के कारण, विश्व के कई हिस्सों में युद्ध की संभावनाएं बढ़ रही हैं। पश्चिमी एशिया में युद्ध की आग फैल रही है, जबकि दक्षिण चीन सागर और ताइवान में भी तनाव बढ़ रहा है। भारत-चीन सीमा पर भी स्थिति चिंताजनक है। इन घटनाओं के परिणामस्वरूप, विभिन्न देशों की प्राथमिकताएं बदल रही हैं, और हथियार व्यापारियों का धंधा आजकल चमक उठा है। सिपरी की रिपोर्ट के अनुसार, 2023 में वैश्विक सैन्य खर्च एक नया रिकॉर्ड स्थापित किया गया है। 2022 की तुलना में, 2023 में सैन्य खर्च 6.8 फीसदी बढ़कर 24.4 ट्रिलियन डॉलर पर पहुंच गया। इसके पीछे कुछ विशेष कारण हैं, जिनमें दुनिया भर में तनाव बढ़ना और अद्यतित सैन्य तैयारियों की आवश्यकता शामिल है। इस वृद्धि में, भारत जैसे देशों का योगदान भी अधिक है। भारत ने भी अपने सैन्य खर्चों में वृद्धि की है, और यह दुनिया के सबसे बड़े और सबसे प्रभावशाली राष्ट्रों में एक बन गया है। इसके अलावा, अमेरिका, चीन, और रूस भी सैन्य खर्च में वृद्धि की हैं, जिससे विश्व सुरक्षा में नए दायरे खुले हैं। संभावनाओं के इस नए मंच पर, दुनिया के सक्षम देश अब अपने सैन्य तैयारियों में ज्यादा ध्यान दे रहे हैं। यह वृद्धि विकास और प्रगति के बजट पर भी प्रभाव डाल रही है, जिससे विभिन्न देशों के आर्थिक प्रगति में बदलाव आ सकता है।   https://www.nayaindia.com/editorial-column/editorial1/arms-sales-increased-in-the-world-454072.html
    • Hello, Hi and I am making a forge 1.12.2 mod and also trying to get a custom model helmet item (I made a joker mask) and exported it to .json format and put it in the ~/namespace/models/item folder. Though, I'm struggling to get the item to render what I want it to be rendered like as I've positioned it in the 'head' window view in blockbench. How did you render out your model? I checked your code and it looks like no references to the actual model are being made. I'm new to this forge modding stuff (was a fabric modder) and I desperately need some help. Is there a way to standardly render out armor models made using blockbench? any methods that one can use? My model in JSON: ```json { "credit": "Made with Blockbench", "texture_size": [160, 160], "textures": { "0": "ydyrn:items/joker_mask_texture", "particle": "ydyrn:items/joker_mask_texture" }, "elements": [ { "from": [0, 0, 0], "to": [16, 16, 1], "faces": { "north": {"uv": [0, 1.7, 16, 14.2], "texture": "#0"}, "east": {"uv": [0, 4, 0.9, 5.6], "texture": "#0"}, "south": {"uv": [0, 5.8, 1.6, 7.4], "texture": "#0"}, "west": {"uv": [0, 1.9, 1.3, 3.5], "texture": "#0"}, "up": {"uv": [0, 3.4, 1.6, 4.2], "texture": "#0"}, "down": {"uv": [0, 9, 1.6, 9.7], "texture": "#0"} } }, { "from": [15.8, 11, 0.5], "to": [16.2, 13, 15], "faces": { "north": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "east": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "south": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "west": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "up": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"}, "down": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"} } }, { "from": [0.2, 11, 14.6], "to": [15.8, 13, 15], "faces": { "north": {"uv": [7.9, 7.9, 9.46, 8.1], "texture": "#0"}, "east": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "south": {"uv": [7.9, 7.9, 9.46, 8.1], "texture": "#0"}, "west": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "up": {"uv": [7.9, 7.9, 9.46, 7.94], "texture": "#0"}, "down": {"uv": [7.9, 7.9, 9.46, 7.94], "texture": "#0"} } }, { "from": [-0.2, 11, 0.5], "to": [0.2, 13, 15], "faces": { "north": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "east": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "south": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "west": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "up": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"}, "down": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"} } } ], "display": { "thirdperson_righthand": { "translation": [-0.25, -2.75, 0], "scale": [0.3, 0.3, 0.3] }, "thirdperson_lefthand": { "translation": [-0.25, -2.75, 0], "scale": [0.3, 0.3, 0.3] }, "firstperson_righthand": { "rotation": [-9, 115, 15], "translation": [0, 1.25, -1.75], "scale": [0.5, 0.5, 0.5] }, "firstperson_lefthand": { "rotation": [-9, 115, 15], "translation": [0, 1.25, -1.75], "scale": [0.5, 0.5, 0.5] }, "ground": { "rotation": [40, 0, 0], "translation": [0, -2.5, 0], "scale": [0.5, 0.5, 0.5] }, "gui": { "rotation": [0, 180, 0] }, "head": { "translation": [0, 0, 0.25], "scale": [0.82773, 0.73594, 0.9] }, "fixed": { "translation": [0, 0, 8.75] } }, "groups": [ 0, { "name": "straps", "origin": [0, 0, 0], "color": 0, "children": [1, 2, 3] } ] } ```
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
    • In your config folder, delete the sophisticatedcore-common.toml file and test it again
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
  • Topics

×
×
  • Create New...

Important Information

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