SHOW:
|
|
- or go back to the newest paste.
| 1 | package top.mod.item; | |
| 2 | import java.util.List; | |
| 3 | import net.minecraft.block.Block; | |
| 4 | import net.minecraft.block.BlockOldLog; | |
| 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.item.EnumDyeColor; | |
| 15 | import net.minecraft.item.Item; | |
| 16 | import net.minecraft.item.ItemStack; | |
| 17 | import net.minecraft.util.EnumActionResult; | |
| 18 | import net.minecraft.util.EnumFacing; | |
| 19 | import net.minecraft.util.EnumHand; | |
| 20 | import net.minecraft.util.EnumParticleTypes; | |
| 21 | import net.minecraft.util.math.BlockPos; | |
| 22 | import net.minecraft.world.World; | |
| 23 | import net.minecraftforge.fml.relauncher.Side; | |
| 24 | import net.minecraftforge.fml.relauncher.SideOnly; | |
| 25 | public class GrowthItem extends Item | |
| 26 | {
| |
| 27 | public static final int[] DYE_COLORS = new int[] {1973019, 11743532, 3887386, 5320730, 2437522, 8073150, 2651799, 11250603, 4408131, 14188952, 4312372, 14602026, 6719955, 12801229, 15435844, 15790320};
| |
| 28 | public GrowthItem() | |
| 29 | {
| |
| 30 | this.setHasSubtypes(true); | |
| 31 | this.setMaxDamage(0); | |
| 32 | this.setCreativeTab(CreativeTabs.MATERIALS); | |
| 33 | } | |
| 34 | /** | |
| 35 | * Called when a Block is right-clicked with this Item | |
| 36 | */ | |
| 37 | public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) | |
| 38 | {
| |
| 39 | ItemStack itemstack = player.getHeldItem(hand); | |
| 40 | if (!player.canPlayerEdit(pos.offset(facing), facing, itemstack)) | |
| 41 | {
| |
| 42 | return EnumActionResult.FAIL; | |
| 43 | } | |
| 44 | else | |
| 45 | {
| |
| 46 | EnumDyeColor enumdyecolor = EnumDyeColor.byDyeDamage(itemstack.getMetadata()); | |
| 47 | if (enumdyecolor == EnumDyeColor.WHITE) | |
| 48 | {
| |
| 49 | if (applyBonemeal(itemstack, worldIn, pos, player)) | |
| 50 | {
| |
| 51 | if (!worldIn.isRemote) | |
| 52 | {
| |
| 53 | worldIn.playEvent(2005, pos, 0); | |
| 54 | } | |
| 55 | return EnumActionResult.SUCCESS; | |
| 56 | } | |
| 57 | } | |
| 58 | else if (enumdyecolor == EnumDyeColor.BROWN) | |
| 59 | {
| |
| 60 | IBlockState iblockstate = worldIn.getBlockState(pos); | |
| 61 | Block block = iblockstate.getBlock(); | |
| 62 | if (block == Blocks.LOG && iblockstate.getValue(BlockOldLog.VARIANT) == BlockPlanks.EnumType.JUNGLE) | |
| 63 | {
| |
| 64 | if (facing == EnumFacing.DOWN || facing == EnumFacing.UP) | |
| 65 | {
| |
| 66 | return EnumActionResult.FAIL; | |
| 67 | } | |
| 68 | pos = pos.offset(facing); | |
| 69 | if (worldIn.isAirBlock(pos)) | |
| 70 | {
| |
| 71 | IBlockState iblockstate1 = Blocks.COCOA.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, 0, player, hand); | |
| 72 | worldIn.setBlockState(pos, iblockstate1, 10); | |
| 73 | if (!player.capabilities.isCreativeMode) | |
| 74 | {
| |
| 75 | itemstack.shrink(1); | |
| 76 | } | |
| 77 | return EnumActionResult.SUCCESS; | |
| 78 | } | |
| 79 | } | |
| 80 | return EnumActionResult.FAIL; | |
| 81 | } | |
| 82 | return EnumActionResult.PASS; | |
| 83 | } | |
| 84 | } | |
| 85 | public static boolean applyBonemeal(ItemStack stack, World worldIn, BlockPos target) | |
| 86 | {
| |
| 87 | if (worldIn instanceof net.minecraft.world.WorldServer) | |
| 88 | return applyBonemeal(stack, worldIn, target, net.minecraftforge.common.util.FakePlayerFactory.getMinecraft((net.minecraft.world.WorldServer)worldIn)); | |
| 89 | return false; | |
| 90 | } | |
| 91 | public static boolean applyBonemeal(ItemStack stack, World worldIn, BlockPos target, EntityPlayer player) | |
| 92 | {
| |
| 93 | IBlockState iblockstate = worldIn.getBlockState(target); | |
| 94 | int hook = net.minecraftforge.event.ForgeEventFactory.onApplyBonemeal(player, worldIn, target, iblockstate, stack); | |
| 95 | if (hook != 0) return hook > 0; | |
| 96 | if (iblockstate.getBlock() instanceof IGrowable) | |
| 97 | {
| |
| 98 | IGrowable igrowable = (IGrowable)iblockstate.getBlock(); | |
| 99 | if (igrowable.canGrow(worldIn, target, iblockstate, worldIn.isRemote)) | |
| 100 | {
| |
| 101 | if (!worldIn.isRemote) | |
| 102 | {
| |
| 103 | if (igrowable.canUseBonemeal(worldIn, worldIn.rand, target, iblockstate)) | |
| 104 | {
| |
| 105 | igrowable.grow(worldIn, worldIn.rand, target, iblockstate); | |
| 106 | } | |
| 107 | stack.shrink(1); | |
| 108 | } | |
| 109 | return true; | |
| 110 | } | |
| 111 | } | |
| 112 | return false; | |
| 113 | } | |
| 114 | @SideOnly(Side.CLIENT) | |
| 115 | public static void spawnBonemealParticles(World worldIn, BlockPos pos, int amount) | |
| 116 | {
| |
| 117 | if (amount == 0) | |
| 118 | {
| |
| 119 | amount = 15; | |
| 120 | } | |
| 121 | IBlockState iblockstate = worldIn.getBlockState(pos); | |
| 122 | if (iblockstate.getMaterial() != Material.AIR) | |
| 123 | {
| |
| 124 | for (int i = 0; i < amount; ++i) | |
| 125 | {
| |
| 126 | double d0 = itemRand.nextGaussian() * 0.02D; | |
| 127 | double d1 = itemRand.nextGaussian() * 0.02D; | |
| 128 | double d2 = itemRand.nextGaussian() * 0.02D; | |
| 129 | worldIn.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, (double)((float)pos.getX() + itemRand.nextFloat()), (double)pos.getY() + (double)itemRand.nextFloat() * iblockstate.getBoundingBox(worldIn, pos).maxY, (double)((float)pos.getZ() + itemRand.nextFloat()), d0, d1, d2, new int[0]); | |
| 130 | } | |
| 131 | } | |
| 132 | else | |
| 133 | {
| |
| 134 | for (int i1 = 0; i1 < amount; ++i1) | |
| 135 | {
| |
| 136 | double d0 = itemRand.nextGaussian() * 0.02D; | |
| 137 | double d1 = itemRand.nextGaussian() * 0.02D; | |
| 138 | double d2 = itemRand.nextGaussian() * 0.02D; | |
| 139 | worldIn.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, (double)((float)pos.getX() + itemRand.nextFloat()), (double)pos.getY() + (double)itemRand.nextFloat() * 1.0f, (double)((float)pos.getZ() + itemRand.nextFloat()), d0, d1, d2, new int[0]); | |
| 140 | } | |
| 141 | } | |
| 142 | } | |
| 143 | } |