Jump to content

[1.16.1] I need help with getting my item to have colored layers.


Recommended Posts

Posted (edited)

My code is probably horrendous and an abomination, but I need some help figuring out where I went wrong. It's kinda difficult to find help on this by using google.

Item Class:

package soii.races.items;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import soii.races.RaceType;
import soii.races.Races;

public class ChangerItem extends Item{
	
	static ArrayList<String> tokenTypes = new ArrayList<String>();
    //Colors I want to use
	static ArrayList<Integer> tokenColors = new ArrayList<Integer>(Arrays.asList(0x00AFAF, 0xD3D3D3, 0x13AA12, 0xCC00FA, 0x5061A4, 0xA80E0E, 0x565656, 0xE69494, 0xE67973, 0xFF0000, 0x967234, 0x2C8004, 0x6C6454, 0xCCC130, 0x5EA496, 0x525858));
	private static final String NBT_COLOR = "TokenColor";
	private static final String NBT_TYPE = "tokenType";
	
	public ChangerItem() {
		super(new Item.Properties().group(Races.TAB).maxStackSize(1));
	}
	
	public static void tokenTypeMaker(List<RaceType> races) {
		for(RaceType i : races) {
			tokenTypes.add(i.getName());
		}
	}
	
	@Override
	public String getTranslationKey(ItemStack stack) {
		if(tokenTypes.size() == 0)
		{
			tokenTypeMaker(Races.RACES);
		}
		if(stack.hasTag())
		{
			CompoundNBT itemData = stack.getTag();
			if(itemData.contains(NBT_TYPE))
			{
				return "item." + itemData.getString(NBT_TYPE) + "Token";
			}
		}
		return "item.nullToken";
	}
	
	public static int getTokenColor(ItemStack stack) {
		return stack.getOrCreateTag().getInt(NBT_COLOR);
	}
	public static void setTokenColor(ItemStack stack, int color) {
		stack.getOrCreateTag().putInt(NBT_COLOR, color);
	}
	
	public static String getTokenType(ItemStack stack) {
		return stack.getOrCreateTag().getString(NBT_TYPE);
	}
	public static void setTokenType(ItemStack stack, String name) {
		stack.getOrCreateTag().putString(NBT_TYPE, name);
	}
	
	public static int getItemColor(ItemStack stack, int tintIndex) {
		if(tintIndex == 0)
		{
			return getTokenColor(stack);
		}
		return 0xFFFFFF;
	}
	
	@Override
	public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) {
		if(this.isInGroup(group)) {
			for (int i = 0; i < tokenTypes.size(); i++) {
				ItemStack stack = new ItemStack(this);
				setTokenType(stack, tokenTypes.get(i));
				setTokenColor(stack, tokenColors.get(i));
				items.add(stack);
			}
		}
	}
	
}

Color Handler:

package soii.races.client;

import net.minecraftforge.client.event.ColorHandlerEvent;
import soii.races.ChangerColor;
import soii.races.items.ChangerItem;

public class ColorHandlers {
	
	public static void registerItemColors(ColorHandlerEvent.Item event) {
		ChangerItem item = new ChangerItem();
		event.getItemColors().register(new ChangerColor(), item);
	}
	
}

Intermediary between IItemColor and other 2 classes (I don't know what i'm doing here):

package soii.races;

import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.item.ItemStack;
import soii.races.items.ChangerItem;

public class ChangerColor implements IItemColor{
	
	@Override
	public int getColor(ItemStack stack, int tintIndex) {
		return ChangerItem.getItemColor(stack, tintIndex);
	}
	
}

There isn't an error code or anything, the textures just show up as the default texture instead of the colored one.

Let me know if you have any input or if you need more information.

Edited by Silivek
Posted
4 minutes ago, Silivek said:

I have a listener set to the ColorHandler's function if that's what you mean

But are you sure this event is actually triggering? That's what poopoodice meant..you can test if the event is triggering for example by logging something on the console or any other action that produce a visible result (of course inside the event handler)

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Posted

Sorry I took so long to get back to you. I was a little bit busy.

I did a println in the event handler and it did not output anything, so does that mean I'm using the wrong event or that my item isn't triggering the event?

Posted

Okay, I added a tag to the Handler, is this more correct? It caused the println to happen once at the beginning, but not ingame.

package soii.races.client;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import soii.races.ChangerColor;
import soii.races.items.ChangerItem;

@EventBusSubscriber(value = Dist.CLIENT)
public class ColorHandlers {
	
	@SubscribeEvent
	public static void registerItemColors(ColorHandlerEvent.Item event) {
		System.out.println("Working!");
		event.getItemColors().register(new ChangerColor(), new ChangerItem());
	}
	
}

 

Posted

I got it working! Thanks for your help!

package soii.races.client;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import soii.races.items.ChangerItem;
import soii.races.util.RegistryHandler;

@EventBusSubscriber(value = Dist.CLIENT)
public class ColorHandlers {
	
	@SubscribeEvent
	public static void registerItemColors(ColorHandlerEvent.Item event) {
		event.getItemColors().register(ChangerItem::getItemColor, RegistryHandler.CHANGER_TOKEN.get());
	}
	
}

 

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.