| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.block.Block;
|
| 5 | import net.minecraft.block.BlockPlanks;
|
| 6 | import net.minecraft.block.IGrowable;
|
| 7 | import net.minecraft.block.material.Material;
|
| 8 | import net.minecraft.block.state.IBlockState;
|
| 9 | import net.minecraft.creativetab.CreativeTabs;
|
| 10 | import net.minecraft.entity.EntityLivingBase;
|
| 11 | import net.minecraft.entity.passive.EntitySheep;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.init.Blocks;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.util.EnumFacing;
|
| 16 | import net.minecraft.util.EnumParticleTypes;
|
| 17 | import net.minecraft.world.World;
|
| 18 |
|
| 19 | public class ItemDye extends Item
|
| 20 | {
|
| 21 | public static final int[] dyeColors = new int[] {1973019, 11743532, 3887386, 5320730, 2437522, 8073150, 2651799, 11250603, 4408131, 14188952, 4312372, 14602026, 6719955, 12801229, 15435844, 15790320};
|
| 22 |
|
| 23 | public ItemDye()
|
| 24 | {
|
| 25 | this.setHasSubtypes(true);
|
| 26 | this.setMaxDamage(0);
|
| 27 | this.setCreativeTab(CreativeTabs.tabMaterials);
|
| 28 | }
|
| 29 |
|
| 30 | /**
|
| 31 | * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
|
| 32 | * different names based on their damage or NBT.
|
| 33 | */
|
| 34 | public String getUnlocalizedName(ItemStack stack)
|
| 35 | {
|
| 36 | int i = stack.getMetadata();
|
| 37 | return super.getUnlocalizedName() + "." + EnumDyeColor.byDyeDamage(i).getUnlocalizedName();
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Called when a Block is right-clicked with this Item
|
| 42 | */
|
| 43 | public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 44 | {
|
| 45 | if (!playerIn.canPlayerEdit(pos.offset(side), side, stack))
|
| 46 | {
|
| 47 | return false;
|
| 48 | }
|
| 49 | else
|
| 50 | {
|
| 51 | EnumDyeColor enumdyecolor = EnumDyeColor.byDyeDamage(stack.getMetadata());
|
| 52 |
|
| 53 | if (enumdyecolor == EnumDyeColor.WHITE)
|
| 54 | {
|
| 55 | if (applyBonemeal(stack, worldIn, pos))
|
| 56 | {
|
| 57 | if (!worldIn.isRemote)
|
| 58 | {
|
| 59 | worldIn.playAuxSFX(2005, pos, 0);
|
| 60 | }
|
| 61 |
|
| 62 | return true;
|
| 63 | }
|
| 64 | }
|
| 65 | else if (enumdyecolor == EnumDyeColor.BROWN)
|
| 66 | {
|
| 67 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 68 | Block block = iblockstate.getBlock();
|
| 69 |
|
| 70 | if (block == Blocks.log && iblockstate.getValue(BlockPlanks.VARIANT) == BlockPlanks.EnumType.JUNGLE)
|
| 71 | {
|
| 72 | if (side == EnumFacing.DOWN)
|
| 73 | {
|
| 74 | return false;
|
| 75 | }
|
| 76 |
|
| 77 | if (side == EnumFacing.UP)
|
| 78 | {
|
| 79 | return false;
|
| 80 | }
|
| 81 |
|
| 82 | pos = pos.offset(side);
|
| 83 |
|
| 84 | if (worldIn.isAirBlock(pos))
|
| 85 | {
|
| 86 | IBlockState iblockstate1 = Blocks.cocoa.onBlockPlaced(worldIn, pos, side, hitX, hitY, hitZ, 0, playerIn);
|
| 87 | worldIn.setBlockState(pos, iblockstate1, 2);
|
| 88 |
|
| 89 | if (!playerIn.capabilities.isCreativeMode)
|
| 90 | {
|
| 91 | --stack.stackSize;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return true;
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | return false;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public static boolean applyBonemeal(ItemStack stack, World worldIn, BlockPos target)
|
| 104 | {
|
| 105 | IBlockState iblockstate = worldIn.getBlockState(target);
|
| 106 |
|
| 107 | if (iblockstate.getBlock() instanceof IGrowable)
|
| 108 | {
|
| 109 | IGrowable igrowable = (IGrowable)iblockstate.getBlock();
|
| 110 |
|
| 111 | if (igrowable.canGrow(worldIn, target, iblockstate, worldIn.isRemote))
|
| 112 | {
|
| 113 | if (!worldIn.isRemote)
|
| 114 | {
|
| 115 | if (igrowable.canUseBonemeal(worldIn, worldIn.rand, target, iblockstate))
|
| 116 | {
|
| 117 | igrowable.grow(worldIn, worldIn.rand, target, iblockstate);
|
| 118 | }
|
| 119 |
|
| 120 | --stack.stackSize;
|
| 121 | }
|
| 122 |
|
| 123 | return true;
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | return false;
|
| 128 | }
|
| 129 |
|
| 130 | public static void spawnBonemealParticles(World worldIn, BlockPos pos, int amount)
|
| 131 | {
|
| 132 | if (amount == 0)
|
| 133 | {
|
| 134 | amount = 15;
|
| 135 | }
|
| 136 |
|
| 137 | Block block = worldIn.getBlockState(pos).getBlock();
|
| 138 |
|
| 139 | if (block.getMaterial() != Material.air)
|
| 140 | {
|
| 141 | block.setBlockBoundsBasedOnState(worldIn, pos);
|
| 142 |
|
| 143 | for (int i = 0; i < amount; ++i)
|
| 144 | {
|
| 145 | double d0 = itemRand.nextGaussian() * 0.02D;
|
| 146 | double d1 = itemRand.nextGaussian() * 0.02D;
|
| 147 | double d2 = itemRand.nextGaussian() * 0.02D;
|
| 148 | worldIn.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, (double)((float)pos.getX() + itemRand.nextFloat()), (double)pos.getY() + (double)itemRand.nextFloat() * block.getBlockBoundsMaxY(), (double)((float)pos.getZ() + itemRand.nextFloat()), d0, d1, d2, new int[0]);
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | /**
|
| 154 | * Returns true if the item can be used on the given entity, e.g. shears on sheep.
|
| 155 | */
|
| 156 | public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer playerIn, EntityLivingBase target)
|
| 157 | {
|
| 158 | if (target instanceof EntitySheep)
|
| 159 | {
|
| 160 | EntitySheep entitysheep = (EntitySheep)target;
|
| 161 | EnumDyeColor enumdyecolor = EnumDyeColor.byDyeDamage(stack.getMetadata());
|
| 162 |
|
| 163 | if (!entitysheep.getSheared() && entitysheep.getFleeceColor() != enumdyecolor)
|
| 164 | {
|
| 165 | entitysheep.setFleeceColor(enumdyecolor);
|
| 166 | --stack.stackSize;
|
| 167 | }
|
| 168 |
|
| 169 | return true;
|
| 170 | }
|
| 171 | else
|
| 172 | {
|
| 173 | return false;
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | /**
|
| 178 | * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
|
| 179 | */
|
| 180 | public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
|
| 181 | {
|
| 182 | for (int i = 0; i < 16; ++i)
|
| 183 | {
|
| 184 | subItems.add(new ItemStack(itemIn, 1, i));
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|