Jump to content

How to change the color of an ItemStack when it's created? (Solved)


AshIndigo

Recommended Posts

I'm trying to set the color of an ItemStack based on 3 nbt int (Corresponding to rgb)  But I cant seem to figure out how to change the color ingame.

package com.ashindigo.alloycraft.items;

import java.awt.Color;
import java.util.List;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.client.renderer.color.ItemColors;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;

public class AlloyItem extends Item {

public AlloyItem(String modid, String name){
	super();
	setUnlocalizedName(modid + "_" + name);
	setCreativeTab(CreativeTabs.MISC);
	GameRegistry.register(this, new ResourceLocation(modid, name));
	maxStackSize = 1;
}
@Override
public void onCreated(ItemStack itemstack, World world, EntityPlayer player) {
	GL11.glColor3d(255, 255, 255);
        if (itemstack.getTagCompound() == null)
        {
            itemstack.setTagCompound(new NBTTagCompound());
            itemstack.getTagCompound().setInteger("Durability", 0);
            itemstack.getTagCompound().setInteger("Enchantability", 0);
            itemstack.getTagCompound().setInteger("Strength", 0);
        }

ItemColors itemcolors = new ItemColors();
        itemcolors.registerItemColorHandler(new IItemColor()
        {
            public int getColorFromItemstack(ItemStack stack, int tintIndex)
            {
                return tintIndex > 0 ? -1 : ((ItemArmor)stack.getItem()).getColor(stack);
            }
        }, new Item[] {this});
}
public void addInformation(ItemStack itemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
    {
	if (itemStack.getTagCompound() != null) {
	par3List.add("§4Strength: §7" + Integer.toString(itemStack.getTagCompound().getInteger("Strength")));
	par3List.add("§2Durability: §7" + Integer.toString(itemStack.getTagCompound().getInteger("Durability")));
	par3List.add("§1Enchantability: §7" + Integer.toString(itemStack.getTagCompound().getInteger("Enchantability")));
	}
}
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	if (FMLCommonHandler.instance().getSide() == Side.CLIENT) {
	//GL11.glBegin(0); 
	GL11.glColor3d(255, 255, 255);
	//GL11.glEnd();
	}
}
}

 

But if I try to launch with the OpenGL code then I crash with this stacktrace

 

Caused by: java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124) ~[lwjgl-2.9.4-nightly-20150209.jar:?]
at org.lwjgl.opengl.GL11.glColor3d(GL11.java:867) ~[lwjgl-2.9.4-nightly-20150209.jar:?]
at com.ashindigo.alloycraft.items.AlloyItem.onUpdate(AlloyItem.java:58) ~[AlloyItem.class:?]
at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:526) ~[itemStack.class:?]
at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389) ~[inventoryPlayer.class:?]
at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:559) ~[EntityPlayer.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2218) ~[EntityLivingBase.class:?]
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:259) ~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?]
at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174) ~[NetHandlerPlayServer.class:?]
at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:215) ~[NetworkDispatcher$1.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:308) ~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:195) ~[NetworkSystem.class:?]
... 5 more

 

And the ItemColor#registerItemColorHandler fails too

Link to comment
Share on other sites

Why did you do this

 

@Override
public void onCreated(ItemStack itemstack, World world, EntityPlayer player) {
                // This GL call
	GL11.glColor3d(255, 255, 255);
        if (itemstack.getTagCompound() == null)
        {
            itemstack.setTagCompound(new NBTTagCompound());
            itemstack.getTagCompound().setInteger("Durability", 0);
            itemstack.getTagCompound().setInteger("Enchantability", 0);
            itemstack.getTagCompound().setInteger("Strength", 0);
        }
}

 

Also you are registering a ItemColorHandler every item update tick.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Do not use OpenGL in your

Item

class. Do not create your own

ItemColors

instance. Do not try to register an

IItemColor

every tick.

 

In init, get Minecraft's

ItemColors

instance (

Minecraft#getItemColors

) and then create and register a single

IItemColor

for your

Item

. In the

IItemColor

implementation, use the three integers stored in the

ItemStack

's NBT to determine the colour to return. The format is

r << 16 | g << 8 | b

(where

r

,

g

and

b

are the red, green and blue components of the colour).

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

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.