/
ItemDyedWaterBottle.java
85 lines (70 loc) · 2.25 KB
/
ItemDyedWaterBottle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package knightminer.inspirations.recipes.item;
import knightminer.inspirations.library.Util;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
public class ItemDyedWaterBottle extends Item {
public static final String TAG_COLOR = "color";
public ItemDyedWaterBottle() {
this.setCreativeTab(CreativeTabs.MATERIALS);
this.setMaxStackSize(16);
this.setContainerItem(Items.GLASS_BOTTLE);
this.setHasSubtypes(true);
}
/* Color logic */
/**
* Return the color for the specified armor ItemStack.
*/
public int getColor(ItemStack stack) {
int meta = stack.getMetadata();
if(meta < 16) {
return EnumDyeColor.byDyeDamage(meta).colorValue;
}
NBTTagCompound tags = stack.getTagCompound();
if (tags != null) {
NBTTagCompound display = tags.getCompoundTag("display");
if(display != null && display.hasKey(TAG_COLOR, 3)) {
return display.getInteger(TAG_COLOR);
}
}
return -1;
}
public ItemStack getStackWithColor(int color) {
EnumDyeColor dyeColor = Util.getDyeForColor(color);
if(dyeColor != null) {
return new ItemStack(this, 1, dyeColor.getDyeDamage());
}
ItemStack result = new ItemStack(this, 1, 16);
NBTTagCompound tags = new NBTTagCompound();
NBTTagCompound display = new NBTTagCompound();
display.setInteger(TAG_COLOR, color);
tags.setTag("display", display);
result.setTagCompound(tags);
return result;
}
/* Color types */
/**
* Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
* different names based on their damage or NBT.
*/
@Override
public String getUnlocalizedName(ItemStack stack) {
int meta = stack.getMetadata();
if(meta < 16) {
return super.getUnlocalizedName(stack) + "." + EnumDyeColor.byDyeDamage(meta).getName();
}
return super.getUnlocalizedName(stack);
}
@Override
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> subItems) {
if(this.isInCreativeTab(tab)) {
for(EnumDyeColor color : EnumDyeColor.values()) {
subItems.add(new ItemStack(this, 1, color.getDyeDamage()));
}
}
}
}