Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class CoreDoubleCrop extends CoreCrop
- {
- public static final PropertyEnum<EnumCropHalf> HALF = PropertyEnum.<EnumCropHalf>create("half", EnumCropHalf.class);
- public CoreDoubleCrop(String name)
- {
- super(name);
- setDefaultState(withProperties(0, EnumCropHalf.LOWER));
- }
- @Override
- public void updateTick(World world, BlockPos pos, IBlockState state, Random rand)
- {
- if(!world.isAreaLoaded(pos, 1))
- return; // Forge: prevent loading unloaded chunks when checking neighbor's light
- if(getHalf(state) == EnumCropHalf.UPPER)
- return;
- if(world.getLightFromNeighbors(pos.up().up()) >= 9)
- {
- int i = getAge(state);
- if (i < getMaxAge())
- {
- float f = getGrowthChance(this, world, pos);
- if(ForgeHooks.onCropsGrowPre(world, pos, state, rand.nextInt((int) (25.0F / f) + 1) == 0))
- {
- boolean lower = world.setBlockState(pos, withProperties(i + 1, EnumCropHalf.LOWER), 2);
- boolean upper = world.setBlockState(pos.up(), withProperties(i + 1, EnumCropHalf.UPPER), 2);
- System.out.println("updateTick: lower = " + lower + ", upper = " + upper);
- ForgeHooks.onCropsGrowPost(world, pos, state, world.getBlockState(pos));
- }
- }
- }
- }
- @Override
- public void grow(World world, BlockPos pos, IBlockState state)
- {
- if(getHalf(state) == EnumCropHalf.UPPER)
- return;
- int i = getAge(state) + getBonemealAgeIncrease(world);
- int j = getMaxAge();
- if(i > j)
- i = j;
- boolean lower = world.setBlockState(pos, withProperties(i, EnumCropHalf.LOWER), 2);
- boolean upper = world.setBlockState(pos.up(), withProperties(i, EnumCropHalf.UPPER), 2);
- System.out.println("grow: lower = " + lower + ", upper = " + upper);
- }
- @Override
- public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
- {
- if(getHalf(state) == EnumCropHalf.UPPER)
- {
- drops.clear();
- return;
- }
- super.getDrops(drops, world, pos, state, 0);
- int age = getAge(state);
- Random rand = world instanceof World ? ((World) world).rand : new Random();
- if(age >= getMaxAge())
- {
- int k = 3 + fortune;
- for(int i = 0; i < 3 + fortune; ++i)
- {
- if(rand.nextInt(2 * getMaxAge()) <= age)
- drops.add(new ItemStack(this.getSeed(), 1, 0));
- }
- }
- }
- @Override
- public void dropBlockAsItemWithChance(World world, BlockPos pos, IBlockState state, float chance, int fortune)
- {
- if(getHalf(state) == EnumCropHalf.UPPER)
- return;
- super.dropBlockAsItemWithChance(world, pos, state, chance, fortune);
- if(false && !world.isRemote) // Forge: NOP all this.
- {
- int i = getAge(state);
- if(i >= getMaxAge())
- {
- int j = 3 + fortune;
- for(int k = 0; k < j; ++k)
- {
- if(world.rand.nextInt(2 * getMaxAge()) <= i)
- spawnAsEntity(world, pos, new ItemStack(getSeed()));
- }
- }
- }
- }
- @Override
- public Item getItemDropped(IBlockState state, Random rand, int fortune)
- {
- if(getHalf(state) == EnumCropHalf.UPPER)
- return Items.AIR;
- return isMaxAge(state) ? getCrop() : getSeed();
- }
- @Override
- public ItemStack getItem(World world, BlockPos pos, IBlockState state)
- {
- return new ItemStack(getSeed());
- }
- @Override
- public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack stack)
- {
- if(getHalf(state) == EnumCropHalf.UPPER)
- world.destroyBlock(pos.down(), true);
- else if(getHalf(state) == EnumCropHalf.LOWER)
- world.setBlockToAir(pos.up());
- super.harvestBlock(world, player, pos, state, te, stack);
- }
- @Override
- public boolean canBlockStay(World world, BlockPos pos, IBlockState state)
- {
- if(state.getBlock() != this)
- return super.canBlockStay(world, pos, state); //Forge: This function is called during world gen and placement, before this block is set, so if we are not 'here' then assume it's the pre-check.
- if(getHalf(state) == EnumCropHalf.UPPER)
- return world.getBlockState(pos.down()).getBlock() == this;
- else
- return world.getBlockState(pos.up()).getBlock() == this;
- }
- @Override
- public boolean canPlaceBlockAt(World world, BlockPos pos)
- {
- return super.canPlaceBlockAt(world, pos) && world.isAirBlock(pos.up());
- }
- @Override
- public void onBlockAdded(World world, BlockPos pos, IBlockState state)
- {
- if(getHalf(state) == EnumCropHalf.LOWER)
- world.setBlockState(pos.up(), getDefaultState().withProperty(HALF, EnumCropHalf.UPPER), 2);
- }
- @Override
- public boolean canGrow(World world, BlockPos pos, IBlockState state, boolean isClient)
- {
- return !isMaxAge(state);
- }
- @Override
- public boolean canUseBonemeal(World world, Random rand, BlockPos pos, IBlockState state)
- {
- return getHalf(state) == EnumCropHalf.LOWER;
- }
- @Override
- public void grow(World world, Random rand, BlockPos pos, IBlockState state)
- {
- if(getHalf(state) == EnumCropHalf.LOWER)
- grow(world, pos, state);
- }
- @Override
- public IBlockState getStateFromMeta(int meta)
- {
- int age = meta % 10;
- EnumCropHalf half = meta >= 10 ? EnumCropHalf.LOWER : EnumCropHalf.UPPER;
- return withProperties(age, half);
- }
- @Override
- public int getMetaFromState(IBlockState state)
- {
- int meta = getAge(state);
- if(getHalf(state) == EnumCropHalf.LOWER)
- meta += 10;
- return meta;
- }
- protected PropertyEnum<EnumCropHalf> getHalfProperty()
- {
- return HALF;
- }
- protected EnumCropHalf getHalf(IBlockState state)
- {
- return state.getValue(getHalfProperty());
- }
- public IBlockState withProperties(int age, EnumCropHalf half)
- {
- return getDefaultState().withProperty(getAgeProperty(), Integer.valueOf(age)).withProperty(getHalfProperty(), half);
- }
- @Override
- public abstract AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos);
- protected abstract PropertyInteger getAgeProperty();
- @Override
- protected abstract BlockStateContainer createBlockState();
- public static enum EnumCropHalf implements IStringSerializable
- {
- UPPER,
- LOWER;
- public String toString()
- {
- return this.getName();
- }
- public String getName()
- {
- return this == UPPER ? "upper" : "lower";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement