Jump to content

[1.10.2] Crops


WildHeart

Recommended Posts

I have a plant that grows in the two unit, but second unit age is greater than 0 then the top block changes its blockstates with upper on lower. How to fix?

Code:

Spoiler

public class BlockDoubleCrops extends BlockCrops
{
    public static final PropertyEnum<EnumCropHalf> HALF = PropertyEnum.create("half", EnumCropHalf.class);
    private Item setSeed, setCrop;

    public BlockDoubleCrops(final Item seed, final Item crop)
    {
        setCreativeTab(Farmland.FMCT);
        setTickRandomly(true);
        disableStats();
        setDefaultState(blockState.getBaseState().withProperty(this.getAgeProperty(), Integer.valueOf(0)).withProperty(HALF, EnumCropHalf.LOWER));
        this.setSeed = seed;
        this.setCrop = crop;
    }

    @Override
    protected PropertyInteger getAgeProperty()
    {
        return super.getAgeProperty();
    }

    @Override
    protected BlockStateContainer createBlockState()
    {
        return new BlockStateContainer(this, new IProperty[] {AGE, HALF});
    }

    @Override
    public boolean onBlockActivated(final World worldIn, final BlockPos pos, final IBlockState state, final EntityPlayer playerIn, final EnumHand hand, @Nullable final ItemStack heldItem, final EnumFacing side, final float hitX, final float hitY, final float hitZ)
    {
        if(!worldIn.isRemote)
        {
            if(isMaxAge(state))
            {
                final EntityItem crop = new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(getCrop()));
                worldIn.spawnEntityInWorld(crop);
                worldIn.setBlockState(pos, withAge(0));
                return true;
            }
        }
        return false;
    }

    @Override
    protected boolean canSustainBush(IBlockState state)
    {
        return state.getBlock() == Blocks.FARMLAND || state.getBlock() == this;
    }

    @Override
    public void grow(final World worldIn, final BlockPos pos, final IBlockState state)
    {
        super.grow(worldIn, pos, state);

        if(getAge(state) == getMaxAge())
        {
            //if(worldIn.isAirBlock(pos.offset(EnumFacing.UP)))
            {
                //worldIn.setBlockState(pos.offset(EnumFacing.UP), blockState.getBaseState().withProperty(HALF, EnumCropHalf.UPPER));
                worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(HALF, EnumCropHalf.UPPER), 2);
            }
        }
    }

    @Override
    public void onBlockHarvested(final World worldIn, final BlockPos pos, final IBlockState state, final EntityPlayer player)
    {
        super.onBlockHarvested(worldIn, pos, state, player);

        if (state.getValue(HALF) == EnumCropHalf.UPPER)
        {
            if (worldIn.getBlockState(pos.down()).getBlock() == this)
            {
                if (!player.capabilities.isCreativeMode)
                {
                    dropBlockAsItem(worldIn, pos, state, 0);
                }

                worldIn.setBlockToAir(pos);
            }
        }
        else if (state.getValue(HALF) == EnumCropHalf.LOWER)
        {
            if (worldIn.getBlockState(pos.up()).getBlock() == this)
            {
                if (!player.capabilities.isCreativeMode)
                {
                    dropBlockAsItem(worldIn, pos, state, 0);
                    dropBlockAsItem(worldIn, pos.up(), state, 0);
                }

                worldIn.setBlockToAir(pos.up());
            }
        }
    }

    @Override
    protected Item getSeed()
    {
        return this.setSeed;
    }

    @Override
    protected Item getCrop()
    {
        return this.setCrop;
    }

    @Override
    public boolean canUseBonemeal(final World worldIn, final Random rand, final BlockPos pos, final IBlockState state)
    {
        return true;
    }
}

 

 

Edited by WildHeart
Link to comment
Share on other sites

35 minutes ago, WildHeart said:

Don't quite understand how it should look can be an example?

Look at the class you're extending to see what it does in those methods. getMetaFromState takes an IBlockState and returns an int, getStateFromMeta takes an int and returns an IBlockState. They are used to store the block's state information in the form of a number and then convert it back.

Link to comment
Share on other sites

3 minutes ago, Jay Avery said:

Look at the class you're extending to see what it does in those methods. getMetaFromState takes an IBlockState and returns an int, getStateFromMeta takes an int and returns an IBlockState. They are used to store the block's state information in the form of a number and then convert it back.

Hrm, something like that?

public IBlockState getStateFromMeta(int meta)
    {
        return (meta & 1) > 0 ? this.getDefaultState().withProperty(HALF, EnumCropHalf.UPPER) : this.getDefaultState().withProperty(HALF, EnumCropHalf.LOWER);
    }

    public int getMetaFromState(IBlockState state)
    {
        return state.getValue(HALF) == EnumCropHalf.UPPER ? 1 : 0;
    }

 

Link to comment
Share on other sites

you get 4 bits total (16 values). no discussion there.

if you spend 1 bit on half (2 values) that leaves 3 bits for growth stages, meaning a maximum of 8 values.

 

if you need more than 8 stages, then you need to make top and bottom half separate blocks and maintain that multiblock structure.

Link to comment
Share on other sites

So, i create:

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(HALF, meta == 0 ? EnumCropHalf.LOWER : EnumCropHalf.UPPER);
    }

    @Override
    public int getMetaFromState(IBlockState state) {
        EnumCropHalf type = state.getValue(HALF);
        return type.getMeta();
    }

Now as I understand it, you need to create getSubBlocks?

Link to comment
Share on other sites

and do not override getSubBlocks. that's for when you want separate independent variants of the same block (like sandstone for example).

 

*sigh*  i'd make a horrible teacher....

 

go to http://minecraft.gamepedia.com/Wood

do you understand how two information pieces are encoded into 16 possible values of metadata?

on the Bed page you will see three information pieces encoded. is the storing part clear clear to you?

Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

That will not work, now you only save the HALF property. You need to save both HALF and AGE.

Right?

@Override
public IBlockState getStateFromMeta(int meta) {
    return getDefaultState().withProperty(HALF, meta == 0 ? EnumCropHalf.LOWER : EnumCropHalf.UPPER).withProperty(AGE_DOUBLE, Integer.valueOf(meta));
}

@Override
public int getMetaFromState(IBlockState state) {
    EnumCropHalf type = state.getValue(HALF);
    return type.getMeta() & ((Integer)state.getValue(AGE_DOUBLE)).intValue();
}
Link to comment
Share on other sites

21 minutes ago, diesieben07 said:

No. You need to learn bitwise operators.

Begin to understand mc code I found this code. He in fact is the answer to my question?

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7)).withProperty(NODROP, Boolean.valueOf((meta & 8) > 0));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | ((EnumFacing)state.getValue(FACING)).getIndex();

        if (((Boolean)state.getValue(NODROP)).booleanValue())
        {
            i |= 8;
        }

        return i;
    }

 

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Same answer.

So, I did what you wanted and works as I need. What did I do? I started using getActualState checking the block on the bottom. Finally works as I intended. Special thanks to diesieben07 for your help and nerves of steel. I will continue to explore Properties, in order not to ask such questions, thanks to all!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.