Jump to content

Changing the color of an item programmatically


Boxtop5000

Recommended Posts

I've been working, off and on, a mod that adds Diablo-style random loot to the game (I know DiabloDrops exists but it's Bukkit only). I've gotten to the point where I can generate standard items with random lore, names, loot tiers, enchantments, and even the item's durability (by default). I'm currently reorganizing the code so I can add more control over generation.

 

To add a bit more randomness to loot generation, I wanted to have the armor and tools generated to be randomly colored (preferably with one of Minecraft's default 16 colors). I'm sure I can use Minecraft's leather armor coloring code to change the color of my custom armor drops, so I'm not too worried about that, but I'm at a loss as to how to change the color of an item. I've looked around the 'Net to find out how to do this, and have found some tutorials here and there--including the Custom 2D inventory item renderer tutorial and a few on how LWJGL and OpenGL itself handles blending and transparency--yet I'm still unsure of a few things.

 

I'm also not looking for someone to write the code for me; I just need a little direction.

 

1. I know I need to make a separate renderer for the item, but I'm not quite sure on how to go about building it. Do I need to load the item's texture, set the blend mode/blend function, then color the item and display the colored texture? Do I need to have separate renderers for inventory view and first person mode? I read the Custom 2D Inventory Item tutorial, but that only talked about drawing something on top of the icon in the inventory.

 

2. How exactly does Minecraft format the color codes for leather armor (i.e., the data stored in the armor's color NBT tag)? Is it a hex value? All RGBA values as one number? I'm asking this because, from the code in ItemArmor, it appears that Minecraft gets the RGB color values through bit manipulation. Discovering that kinda threw me for a loop.

 

Thanks for the help.

Link to comment
Share on other sites

to expand on that, use java's built in random number generator to give a number and then just compare a set of pre-defined colors to that random number, defaulting to another color.

I took over Hunting Traps Mod and work on helping the forge community as much as I can. View my work here: http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/wip-mods/1443756-1-7-2-1-6-4-1-5-2-1-4-7-hunting-traps-mod-v-0-4-0

Link to comment
Share on other sites

to expand on that, use java's built in random number generator to give a number and then just compare a set of pre-defined colors to that random number, defaulting to another color.

Or you could use random.nextInt(16**6), or do an HSV-to-RGB conversion with values (random.nextInt(16**2),0,100).

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

my question is...are we actually helping this guy out a lot tho? its like taking him to a bar with all his equally liked beverages and asking him to pick one......just saying that too much information isn't always a good thing.

I took over Hunting Traps Mod and work on helping the forge community as much as I can. View my work here: http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/wip-mods/1443756-1-7-2-1-6-4-1-5-2-1-4-7-hunting-traps-mod-v-0-4-0

Link to comment
Share on other sites

I mean, how do you *think* he got to be the fifth most prolific poster? He had way too much time on my hands.

 

I'd just like to point out that I'm ninth.

 

And a newer user than ALL THE OTHER NINE.

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

Or you could use random.nextInt(16**6), or do an HSV-to-RGB conversion with values (random.nextInt(16**2),0,100).

 

Ahm just a short question. What does the double-*-sign do? I have never seen that in any javacode yet.

 

PS: keep them answers coming, the more, the better. And opne a possibility for me to ask something back :) just for the post-count

Link to comment
Share on other sites

Let's return to topic real quick! ;)

 

Despite you guys suggestions, Minecraft actually does offer such a feature already. I used it in my mod. The method's called

Item.getColorFromItemStack( ItemStack, int );

and expects you to return an integer in the RGB format. Obviously then no semi-transparency. It's a little tricky though. I am simply coloring a white item, so coloring something brown or yellow might turn out unexpected. I think the brightness of the individual pixels is what matters.

 

Also try not to use a colored item as creative tab icon. One of the succeeding tabs will have the same color...

Link to comment
Share on other sites

Despite you guys suggestions, Minecraft actually does offer such a feature already.

 

Why am I not surprised?

 

Oh right.  Leather armor does it.

 

Who here thought to open up Leather Armor's java files and take a peek?

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

I use

        @SideOnly(Side.CLIENT)
public int getColorFromItemStack(ItemStack par1ItemStack, int par2)
{
	return colorA;
}

 

Where colorA is the same color as html except with a 0x in front of it.

e.g. go here and pick a color. Copy the selected color box (#CCCCFF) take out the hashtag and add in a 0x (0xCCCCFF).

 

Then just make it random. If you want to do multiple colors you'll have to make multiple renders and then check which render it is in the getColor method.

Link to comment
Share on other sites

I mean, how do you *think* he got to be the fifth most prolific poster? He had way too much time on my hands.

 

I'd just like to point out that I'm ninth.

 

And a newer user than ALL THE OTHER NINE.

Oh yeah? Well, I predate you! (lol, hipster)

 

Actually, I have so few because I had to leave Minecraft modding for a while when school took over. But now I'm back!

Or you could use random.nextInt(16**6), or do an HSV-to-RGB conversion with values (random.nextInt(16**2),0,100).

 

Ahm just a short question. What does the double-*-sign do? I have never seen that in any javacode yet.

 

PS: keep them answers coming, the more, the better. And opne a possibility for me to ask something back :) just for the post-count

Ah, silly me. That's a Python feature; it means "to the power of." Java doesn't have that, which is stupid. Java has a habit of being stupid.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

I'd just like to point out that I'm ninth.

 

And a newer user than ALL THE OTHER NINE.

Oh yeah? Well, I predate you! (lol, hipster)

 

I meant that I've overtaken 95% of the forum user base in posts in the last two months.  My average posts per day is 3 times what it is for the other prolific users.

Not that I see this as an accomplishment, it's just staggering.

 

Ah, silly me. That's a Python feature; it means "to the power of." Java doesn't have that, which is stupid. Java has a habit of being stupid.

 

Math.power(a,b);

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

I didn't. Other items use it too, though, so it comes to the same solution. :P

 

I meant besides you.

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

I'd just like to point out that I'm ninth.

 

And a newer user than ALL THE OTHER NINE.

Oh yeah? Well, I predate you! (lol, hipster)

 

I meant that I've overtaken 95% of the forum user base in posts in the last two months.  My average posts per day is 3 times what it is for the other prolific users.

Not that I see this as an accomplishment, it's just staggering.

Oh? I came out of stasis two weeks ago and had about 50 posts then. 8)

Ah, silly me. That's a Python feature; it means "to the power of." Java doesn't have that, which is stupid. Java has a habit of being stupid.

 

Math.power(a,b);

Stupider [sic] than **.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

Oh? I came out of stasis two weeks ago and had about 50 posts then. 8)

 

Harder to determine a short-term average.

*Shrug*

 

Ah, silly me. That's a Python feature; it means "to the power of." Java doesn't have that, which is stupid. Java has a habit of being stupid.

 

Math.power(a,b);

Stupider [sic] than **.

 

Probably.

 

I'm still waiting for a computer language that allows for Knuth up-arrow notation. ;D

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

Oh? I came out of stasis two weeks ago and had about 50 posts then. 8)

 

Harder to determine a short-term average.

*Shrug*

You just say that because you're jealous. ;D

Ah, silly me. That's a Python feature; it means "to the power of." Java doesn't have that, which is stupid. Java has a habit of being stupid.

 

Math.power(a,b);

Stupider [sic] than **.

 

Probably.

 

I'm still waiting for a computer language that allows for Knuth up-arrow notation. ;D

Why would you need... never mind, I shouldn't ask. Actually, what would be nice is... well, I own a TI-85, which understands what is meant by 2(x-1)(x+2) whereas computers would want it to be written 2*(x-1)*(x+2). That would be a great feature.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

Why would you need... never mind, I shouldn't ask. Actually, what would be nice is... well, I own a TI-85, which understands what is meant by 2(x-1)(x+2) whereas computers would want it to be written 2*(x-1)*(x+2). That would be a great feature.

 

*Innocent look*

So I can calculate things like the XKCD number.

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

Actually, what would be nice is... well, I own a TI-85, which understands what is meant by 2(x-1)(x+2) whereas computers would want it to be written 2*(x-1)*(x+2). That would be a great feature.

As someone who has made a parser for a defined language that my teacher wanted us to implement, I can say that there are very good reasons why we cant program using 2(x+1) instead of 2*(x+1). Parsing code is not a simple thing, especially when the language needs to be deterministic.

Additionally, a language should ideally have orthogonality. Basically, one way to do something, and one way only, because that makes it easy to read and write. How frustrated would you be to read through someone's code and see them switching between every possible way of writing 2*(x+1) every time they use it? By minimizing the ways that it works, a language becomes more readable.

 

Also, 2(x+1) reminds me more of a method call than a multiplication. Not a good thing. :)

Read my thoughts on my summer mod work and tell me what you think!

http://www.minecraftforge.net/forum/index.php/topic,8396.0.html

 

I absolutely love her when she smiles

Link to comment
Share on other sites

Actually, what would be nice is... well, I own a TI-85, which understands what is meant by 2(x-1)(x+2) whereas computers would want it to be written 2*(x-1)*(x+2). That would be a great feature.

As someone who has made a parser for a defined language that my teacher wanted us to implement, I can say that there are very good reasons why we cant program using 2(x+1) instead of 2*(x+1). Parsing code is not a simple thing, especially when the language needs to be deterministic.

Additionally, a language should ideally have orthogonality. Basically, one way to do something, and one way only, because that makes it easy to read and write. How frustrated would you be to read through someone's code and see them switching between every possible way of writing 2*(x+1) every time they use it? By minimizing the ways that it works, a language becomes more readable.

 

Also, 2(x+1) reminds me more of a method call than a multiplication. Not a good thing. :)

That makes me think of Lojban. A language designed to be entirely abstract, without any ambiguity, and with one form of syntax for everything, would be nigh unreadable. I mean, look at Lisp.

 

I agree in principle, though; I'm pointing out that there are situations where such syntax could be useful.

Why would you need... never mind, I shouldn't ask. Actually, what would be nice is... well, I own a TI-85, which understands what is meant by 2(x-1)(x+2) whereas computers would want it to be written 2*(x-1)*(x+2). That would be a great feature.

 

*Innocent look*

So I can calculate things like the XKCD number.

"Can" also means that you need a computer powerful enough. Besides, unless computing large numbers is a hobby (I wouldn't put it past you) or some part of your job, I'd think you'd be better off writing your own functions. That's probably what such a feature would look like in any language anyway, even as a builtin.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

I mean, look at Lisp.

Lisp is a functional language, not an imperative language.

While you obviously cant only have one way of doing things for everything, I'm sure it is one of the reasons why the parenthesis trick usually doesn't work. I'm sure it could be implemented without too much trouble, but is avoided for other reasons.

Read my thoughts on my summer mod work and tell me what you think!

http://www.minecraftforge.net/forum/index.php/topic,8396.0.html

 

I absolutely love her when she smiles

Link to comment
Share on other sites

*Innocent look*

So I can calculate things like the XKCD number.

"Can" also means that you need a computer powerful enough. Besides, unless computing large numbers is a hobby (I wouldn't put it past you) or some part of your job, I'd think you'd be better off writing your own functions. That's probably what such a feature would look like in any language anyway, even as a builtin.

 

Point.

I just think it would be amusing.

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.