Jump to content

[1.10.2] Add color to ItemBlock (IItemColor)


arie2002

Recommended Posts

Hello again!

I'm currently messing around with IItemColors. I have made an item change colour depending on how damaged it is. However, now I want to do the same with an ItemBlock! I made a block and an itemblock and registered them correctly, and I registered the same ItemColorHandler as before to the itemblock. But the ItemBlock doesn't change color... The code doesn't even get activated?

 

 

This is my code. ScientificMethodAPI.i_heatedIronBlock is the ItemBlock

 

Main class

package scientificmethod;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.Instance;
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 net.minecraftforge.fml.relauncher.Side;
import scientificmethod.item.ItemBlockHeated;
import scientificmethod.item.ItemColorHeated;
import scientificmethod.item.ItemHeated;
import scientificmethod.proxy.ProxyCommon;

@Mod(modid = ScientificMethod.MODID, version = ScientificMethod.VERSION)
public class ScientificMethod {

public static final String MODID = "scientificmethod";
public static final String VERSION = "Minecraft 1.10.2";

@Instance(value = MODID)
public static ScientificMethod instance;

@SidedProxy(clientSide = "scientificmethod.proxy.ProxyClient", serverSide = "scientificmethod.proxy.ProxyCommon")
public static ProxyCommon proxy;

private void register(ISMObject obj) {
	if (obj instanceof ItemBlock)
		registerItemBlock((ItemBlock) obj, obj.getName());
	else if (obj instanceof Item)
		registerItem((Item) obj, obj.getName());
	else if (obj instanceof Block)
		registerBlock((Block) obj, obj.getName());
	else
		throw new IllegalArgumentException();
}

private void registerItemBlock(ItemBlock item, String name) {
	Block block = item.getBlock();

	block.setRegistryName(name);
	GameRegistry.register(block);

	registerItem(item, name);
}

private void registerItem(Item item, String name) {
	item.setRegistryName(name);
	GameRegistry.register(item);
}

private void registerBlock(Block block, String name) {
	block.setRegistryName(name);
	GameRegistry.register(block);

	Item item = new ItemBlock(block);
	registerItem(item, name);
}

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
	ScientificMethodAPI.i_heatedIron = new ItemHeated(ScientificMethodAPI.n_heatedIron, Items.IRON_INGOT);
	ScientificMethodAPI.i_heatedIronBlock = new ItemBlockHeated(ScientificMethodAPI.n_heatedIronBlock, Blocks.IRON_BLOCK);

	register((ISMObject) ScientificMethodAPI.i_heatedIron);
	register((ISMObject) ScientificMethodAPI.i_heatedIronBlock);
}

@Mod.EventHandler
public void init(FMLInitializationEvent event) {
	if (event.getSide() == Side.CLIENT) {
		RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();

		renderItem.getItemModelMesher().register(ScientificMethodAPI.i_heatedIron, 0, new ModelResourceLocation("minecraft:iron_ingot", "inventory"));
		renderItem.getItemModelMesher().register(ScientificMethodAPI.i_heatedIronBlock, 0, new ModelResourceLocation("minecraft:iron_block", "inventory"));

		Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new ItemColorHeated(), ScientificMethodAPI.i_heatedIron, ScientificMethodAPI.i_heatedIronBlock);
	}
}

@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event) {

}

}

 

ItemColorHeated

I know durability only exists when you have an item, but it's only supposed to be colored in item form.

package scientificmethod.item;

import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.item.ItemStack;

public class ItemColorHeated implements IItemColor {

@Override
public int getColorFromItemstack(ItemStack stack, int i) {
	int durability = 255 - (stack.getItemDamage() - 1);
	if (durability == 256)
		return 0xFFFFFF;
	if (durability <= 185) {
		durability *= (127.0 / 185.0);
		return ((0xFF0000 - ((durability / 2) << 16)) + (0xFF00 - ((int) (durability * 1.5) << ) + (0xFF - (int) (durability * 1.5)));
	} else {
		durability -= 185;
		durability *= (127.0 / 130.0);
		return 0xBF0000 + ((durability / 2) << 16) + 0x4000 + ((int) (durability * 1.2) <<  + 0x40 - (int) (durability * 0.2);
	}
}

}

 

ItemBlockHeated

package scientificmethod.item;

import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import scientificmethod.ISMObject;
import scientificmethod.block.BlockHeated;

public class ItemBlockHeated extends ItemBlock implements IItemHeated, ISMObject {

private final String name;
private final Item ingot;

public ItemBlockHeated(String name, Block ingot) {
	super(new BlockHeated(name, ingot));
	this.name = name;
	this.ingot = Item.getItemFromBlock(ingot);
	this.setMaxDamage(256);
}

@Override
public boolean canPlaceBlockOnSide(World world, BlockPos pos, EnumFacing facing, EntityPlayer player, ItemStack stack) {
	return false;
}

@Override
public Item getIngot() {
	return ingot;
}

@Override
public boolean bendable(ItemStack stack) {
	return 255 - (stack.getItemDamage() - 1) > 185;
}

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

}

 

BlockHeated

package scientificmethod.block;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class BlockHeated extends Block {

public BlockHeated(String name, Block b) {
	super(b.getDefaultState().getMaterial());
	this.setUnlocalizedName(name);
	this.setCreativeTab(CreativeTabs.MATERIALS);
}

public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block) {
	world.destroyBlock(pos, true);
}

public void onBlockAdded(World world, BlockPos pos, IBlockState state) {
	world.destroyBlock(pos, true);
}

}

 

 

1f7e5f87e7ec709c4ee3a5918c55b571.png

I never post on the forums, but when I do, I have no clue how to do a certain thing.

Link to comment
Share on other sites

Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new ItemColorHeated(), ScientificMethodAPI.i_heatedIron, ScientificMethodAPI.i_heatedIronBlock);

 

Class Minecraft may not exist in a dedicated server, so you might need to put this in a proxy before it can run outside of SP.

 

Have you tried to reverse the order of the ingot and the block? If that does nothing, then you may need to step through your initialization to see if you accidentally use/store the vanilla iron block somewhere you intended to have your heated iron block.

 

The mesher is obsolete, its use discouraged.

 

 

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

Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new ItemColorHeated(), ScientificMethodAPI.i_heatedIron, ScientificMethodAPI.i_heatedIronBlock);

 

Class Minecraft may not exist in a dedicated server, so you might need to put this in a proxy before it can run outside of SP.

if (event.getSide() == Side.CLIENT) {

I'm checking if the side is the Client, so I don't have to worry 'bout that.

 

Have you tried to reverse the order of the ingot and the block?

Well, I have now. Does nothing.

 

If that does nothing, then you may need to step through your initialization to see if you accidentally use/store the vanilla iron block somewhere you intended to have your heated iron block.

For as far as I can see I did the initialization correctly...

I never post on the forums, but when I do, I have no clue how to do a certain thing.

Link to comment
Share on other sites

Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new ItemColorHeated(), ScientificMethodAPI.i_heatedIron, ScientificMethodAPI.i_heatedIronBlock);

 

Class Minecraft may not exist in a dedicated server, so you might need to put this in a proxy before it can run outside of SP.

if (event.getSide() == Side.CLIENT) {

I'm checking if the side is the Client, so I don't have to worry 'bout that.

Doesn't matter. Java still needs to load the class "in case the class gets called". It doesn't exist on the server side, so it crashes with a
ClassNotFoundException

. Putting that code in a proxy will solve that issue.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Right -- There's a difference between an instance not existing and a class not existing. If missing an instance, you can avoid the null with a runtime branch like you had. However, if a class hasn't even been loaded, then your class will fail to load (the import statement alone will blow sky high). Runtime avoidance needs reflection, hence the proxy system.

 

As for your color problem, you still need to step through the block's initialization using the debugger. You either have something out of order or a subtle wrong-field-referenced mistake. These are tedious to spot from just reading code, but they become blindingly obvious in a debugger.

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

However, if a class hasn't even been loaded, then your class will fail to load (the import statement alone will blow sky high).

 

(Imports aren't actually compiled in, they're syntatic sugar.  Using fully qualified names instead won't fix the problem, whereas importing a class that isn't used, won't)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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



×
×
  • Create New...

Important Information

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