Jump to content

[SOLVED] [1.10.2] Trouble colorizing item block for custom leaves


Daeruin

Recommended Posts

I created some custom leaves and got everything working, except the item version lacks any color. I get that I have to register an item color handler, but the code isn't working. I copied it almost directly from the vanilla ItemColors class, but I'm getting a null pointer exception. I don't understand why it works for the vanilla code but not mine. This is in init in my ClientProxy. The first part works fine and gives me nice green leaf blocks when placed in the world. The second part gives me the null pointer exception.

 

Minecraft.getMinecraft().getBlockColors().registerBlockColorHandler(new IBlockColor() {
@Override
public int colorMultiplier(IBlockState state, IBlockAccess worldIn, BlockPos pos, int tintIndex) {
	return worldIn.getBiomeGenForCoords(pos).getFoliageColorAtPos(pos);
} 
}, PrimalBlockRegistry.leafHickory);

Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new IItemColor() {
@Override
public int getColorFromItemstack(ItemStack stack, int tintIndex) {
	@SuppressWarnings("deprecation")
	IBlockState iblockstate = ((ItemBlock) stack.getItem()).getBlock().getStateFromMeta(stack.getMetadata());
	return Minecraft.getMinecraft().getBlockColors().colorMultiplier(iblockstate, (IBlockAccess)null, (BlockPos)null, tintIndex);
}
}, PrimalBlockRegistry.leafHickory);

 

The error (lines 45 and 54 are the two return statements in the above code):

 

[00:37:57] [Client thread/FATAL]: Reported exception thrown!
net.minecraft.util.ReportedException: Rendering item
at net.minecraft.client.renderer.RenderItem.renderItemAndEffectIntoGUI(RenderItem.java:390) ~[RenderItem.class:?]
at net.minecraft.client.gui.GuiIngame.renderHotbarItem(GuiIngame.java:1144) ~[GuiIngame.class:?]
at net.minecraft.client.gui.GuiIngame.renderHotbar(GuiIngame.java:528) ~[GuiIngame.class:?]
at net.minecraftforge.client.GuiIngameForge.renderHotbar(GuiIngameForge.java:329) ~[GuiIngameForge.class:?]
at net.minecraftforge.client.GuiIngameForge.renderGameOverlay(GuiIngameForge.java:140) ~[GuiIngameForge.class:?]
at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1125) ~[EntityRenderer.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1139) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:406) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_73]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_73]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_73]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_73]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.NullPointerException
at com.daeruin.primalcraft.ClientProxy$1.colorMultiplier(ClientProxy.java:45) ~[ClientProxy$1.class:?]
at net.minecraft.client.renderer.color.BlockColors.colorMultiplier(BlockColors.java:167) ~[blockColors.class:?]
at com.daeruin.primalcraft.ClientProxy$2.getColorFromItemstack(ClientProxy.java:54) ~[ClientProxy$2.class:?]
at net.minecraft.client.renderer.color.ItemColors.getColorFromItemstack(ItemColors.java:139) ~[itemColors.class:?]
at net.minecraft.client.renderer.RenderItem.renderQuads(RenderItem.java:222) ~[RenderItem.class:?]
at net.minecraft.client.renderer.RenderItem.renderModel(RenderItem.java:133) ~[RenderItem.class:?]
at net.minecraft.client.renderer.RenderItem.renderModel(RenderItem.java:117) ~[RenderItem.class:?]
at net.minecraft.client.renderer.RenderItem.renderItem(RenderItem.java:155) ~[RenderItem.class:?]
at net.minecraft.client.renderer.RenderItem.renderItemModelIntoGUI(RenderItem.java:317) ~[RenderItem.class:?]
at net.minecraft.client.renderer.RenderItem.renderItemAndEffectIntoGUI(RenderItem.java:356) ~[RenderItem.class:?]
... 20 more

 

 

Any advice you can give is greatly appreciated. Thanks in advance.

Link to comment
Share on other sites

Your

IBlockColor#colorMultiplier

implementation calls

IBlockAccess#getBiomeGenForCoords

on the

IBlockAccess

argument but your

IItemColor#getColorFromItemstack

passes

null

for this argument, causing a

NullPointerException

.

 

Both the

IBlockAccess

and

BlockPos

arguments of

IBlockColor#colorMultiplier

are marked as

@Nullable

, so your implementation needs to account for them being

null

. The vanilla implementations do this.

 

If you copy the package-info.java file added by MCP from a vanilla package into your own packages, all of your method parameters and return values will be marked

@Nonnull

by default. This will ensure that your IDE warns you about using

@Nullable

values without

null

-checking or overriding a method (or parameter) marked as

@Nullable

without including

@Nullable

in your override.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

OK, that makes sense. I got rid of the null pointer exception. It still wasn't working, but after a while of poking around in the BlockColors class I realized I was using the wrong colorMultiplier method. After copying the right method (which already had the null checks built in, as you pointed out), it's now working perfectly. The main difference seems to be the use of BiomeColorHelper instead of getBiomeGenForCoords, along with a backup return value in case of null arguments. I also streamlined my old methods a little.

 

Finished code, for those who may be interested in this later:

 

 

private static final Minecraft minecraft = Minecraft.getMinecraft();
@Override
public void init(FMLInitializationEvent event)
{
final BlockColors blockColors = minecraft.getBlockColors();
final ItemColors itemColors = minecraft.getItemColors();
blockColors.registerBlockColorHandler(new IBlockColor() {
	@Override
	public int colorMultiplier(IBlockState state, @Nullable IBlockAccess blockAccess, @Nullable BlockPos pos, int tintIndex) {
                return blockAccess != null && pos != null ? BiomeColorHelper.getFoliageColorAtPos(blockAccess, pos) : ColorizerFoliage.getFoliageColorBasic();
	} 
}, YOUR LEAF BLOCK HERE);

itemColors.registerItemColorHandler(new IItemColor() {
	@Override
	public int getColorFromItemstack(ItemStack stack, int tintIndex) {
		@SuppressWarnings("deprecation")
		IBlockState iblockstate = ((ItemBlock) stack.getItem()).getBlock().getStateFromMeta(stack.getMetadata());
		return blockColors.colorMultiplier(iblockstate, (IBlockAccess)null, (BlockPos)null, tintIndex);
	}
}, YOUR LEAF BLOCK HERE);

 

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.



×
×
  • Create New...

Important Information

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