-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemPaintbrush.java
138 lines (122 loc) · 3.75 KB
/
ItemPaintbrush.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import java.util.List;
import java.util.Locale;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blocks.IColorRemovable;
import buildcraft.api.core.EnumColor;
import buildcraft.core.lib.items.ItemBuildCraft;
import buildcraft.core.lib.utils.NBTUtils;
public class ItemPaintbrush extends ItemBuildCraft {
public ItemPaintbrush() {
super();
setFull3D();
setMaxStackSize(1);
setMaxDamage(63);
}
private int getColor(ItemStack stack) {
if (!stack.hasTagCompound()) {
return -1;
}
NBTTagCompound compound = NBTUtils.getItemData(stack);
return compound.hasKey("color") ? compound.getByte("color") : -1;
}
@Override
public void setDamage(ItemStack stack, int damage) {
if (damage > getMaxDamage()) {
stack.setTagCompound(null);
super.setDamage(stack, 0);
} else {
super.setDamage(stack, damage);
}
}
@Override
public String[] getIconNames() {
String[] names = new String[17];
names[0] = "paintbrush/clean";
for (int i = 0; i < 16; i++) {
names[1 + i] = "paintbrush/" + EnumColor.fromId(i).name().toLowerCase(Locale.ENGLISH);
}
return names;
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister par1IconRegister) {
super.registerIcons(par1IconRegister);
IIcon[] brushColors = new IIcon[16];
System.arraycopy(icons, 1, brushColors, 0, 16);
EnumColor.setIconArray(brushColors);
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIconIndex(ItemStack stack) {
this.itemIcon = icons[(getColor(stack) + 1) % icons.length];
return itemIcon;
}
@Override
public String getItemStackDisplayName(ItemStack stack) {
String base = super.getItemStackDisplayName(stack);
int dye = getColor(stack);
if (dye >= 0) {
return base + " (" + EnumColor.fromId(dye).getLocalizedName() + ")";
} else {
return base;
}
}
@Override
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) {
int dye = getColor(stack);
Block block = world.getBlock(x, y, z);
if (block == null) {
return false;
}
if (dye >= 0) {
if (block.recolourBlock(world, x, y, z, ForgeDirection.getOrientation(side), 15 - dye)) {
player.swingItem();
setDamage(stack, getDamage(stack) + 1);
return !world.isRemote;
}
} else {
// NOTE: Clean paintbrushes never damage.
if (block instanceof IColorRemovable) {
if (((IColorRemovable) block).removeColorFromBlock(world, x, y, z, ForgeDirection.getOrientation(side))) {
player.swingItem();
return !world.isRemote;
}
}
}
return false;
}
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tab, List itemList) {
itemList.add(new ItemStack(this));
for (int i = 0; i < 16; i++) {
ItemStack stack = new ItemStack(this);
NBTUtils.getItemData(stack).setByte("color", (byte) i);
itemList.add(stack);
}
}
@Override
public boolean doesSneakBypassUse(World world, int x, int y, int z, EntityPlayer player) {
return true;
}
}