Jump to content

Recommended Posts

Posted

goods

 

i need help whith mi custome crop, this crop grow only on flower pots. everything is working well except for the amount of seeds its drop when breaken when full grow, it can spawn from 1 to 8 seeds this is to much i wana this to only spawn one plant whith one seed and whith a 20% chance to spawn a second seed.

i been doing some changes to the code but nop it still droping 1 to 8 seeds 

 

this is the code

package extructurasmercenarias.blocks.plantas;

import java.util.Random;

import extructurasmercenarias.ExtructurasM;

import extructurasmercenarias.items.ExtructurasMItems;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.IGrowable;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;


//#################################################################################################3
public class hierbaAzul_blocke extends BlockCrops  implements IGrowable
{
    public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 7);
    //private static final String __OBFID = "CL_00000222";

    
//#################################################################################################3    
    public hierbaAzul_blocke()
    {
        setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
        setTickRandomly(true);
        float f = 0.5F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
    	
        String name = "hierbaAzul_blocke";
        setUnlocalizedName(ExtructurasM.MODID + "_" + name);
        GameRegistry.registerBlock(this, name);
        //setCreativeTab(ExtructurasM.blockes);
        
        setHardness(0.0F);
        setStepSound(soundTypeGrass);
        disableStats();
    }

  //#################################################################################################3    
    /**
     * is the block grass, dirt or farmland
     */
    protected boolean canPlaceBlockOn(Block ground)
    {
        return true;//ground == Blocks.farmland; //place wherever i want
    }
  //#################################################################################################3
    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        super.updateTick(worldIn, pos, state, rand);

        if (worldIn.getLightFromNeighbors(pos.up()) >= 2)
        {
            int i = ((Integer)state.getValue(AGE)).intValue();

            if (i < 7)
            {
                float f = getGrowthChance(this, worldIn, pos);

                if (rand.nextInt((int)(25.0F / f) + 1) == 0)
                {
                    worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i + 1)), 2);
                }
            }
        }
    }
  //#################################################################################################3
    public void grow(World worldIn, BlockPos pos, IBlockState state)
    {
        int i = ((Integer)state.getValue(AGE)).intValue() + MathHelper.getRandomIntegerInRange(worldIn.rand, 2, 5);

        if (i > 7)
        {
            i = 7;
        }

        worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i)), 2);
    }
  //#################################################################################################3
    protected static float getGrowthChance(Block blockIn, World worldIn, BlockPos pos)
    {
        float f = 1.0F;
        BlockPos blockpos1 = pos.down();

        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                float f1 = 0.0F;
                IBlockState iblockstate = worldIn.getBlockState(blockpos1.add(i, 0, j));

                    f1 = 1.5F; // betwing drifth and humidified


                if (i != 0 || j != 0)
                {
                    f1 /= 4.0F;
                }

                f += f1;
            }
        }

  
        return f;
    }
  //#################################################################################################3
    public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
    {
    	//yes
        return true;//(worldIn.getLight(pos) >= 8 || worldIn.canSeeSky(pos)) && worldIn.getBlockState(pos.down()).getBlock().canSustainPlant(worldIn, pos.down(), net.minecraft.util.EnumFacing.UP, this);
    }
  //#################################################################################################3
    protected Item getSeed()
    {
        return ExtructurasMItems.hierbaAzulSemilla;
    }
  //#################################################################################################3
    protected Item getCrop()
    {
        return ExtructurasMItems.hierbaAzulPlanta;
    }
  //#################################################################################################3
    /**
     * Spawns this Block's drops into the World as EntityItems.
     *  
     * @param chance The chance that each Item is actually spawned (1.0 = always, 0.0 = never)
     * @param fortune The player's fortune level
     */
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
    {
        super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
    }
  //#################################################################################################3
    /**
     * Get the Item that this Block should drop when harvested.
     *  
     * @param fortune the level of the Fortune enchantment on the player's tool
     */
    public Item getItemDropped(IBlockState state, Random rand, int fortune)
    {
        return ((Integer)state.getValue(AGE)).intValue() == 7 ? this.getCrop() : this.getSeed();
    }
//#################################################################################################3
    @Override
    public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state) {
    	IBlockState materast = Blocks.flower_pot.getDefaultState();
    	worldIn.setBlockState(pos, materast);
    	
    }
  //#################################################################################################3    
    /**
     * Returns the quantity of items to drop on block destruction.
     */
    @Override
    public int quantityDropped(Random random)
    {
        return 1;
    }    
  //#################################################################################################3   
    /**
     * Whether this IGrowable can grow
     */
    public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
    {
        return ((Integer)state.getValue(AGE)).intValue() < 7;
    }
  //#################################################################################################3
    public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
    {
        return true;
    }
  //#################################################################################################3
    @SideOnly(Side.CLIENT)
    public Item getItem(World worldIn, BlockPos pos)
    {
        return this.getSeed();
    }
  //#################################################################################################3
    public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
    {
        this.grow(worldIn, pos, state);
    }
  //#################################################################################################3
    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(AGE, Integer.valueOf(meta));
    }
  //#################################################################################################3
    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(IBlockState state)
    {
        return ((Integer)state.getValue(AGE)).intValue();
    }
  //#################################################################################################3
    protected BlockState createBlockState()
    {
        return new BlockState(this, new IProperty[] {AGE});
    }
  //#################################################################################################3
    @Override
    public java.util.List<ItemStack> getDrops(net.minecraft.world.IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
    {
        java.util.List<ItemStack> ret = super.getDrops(world, pos, state, fortune);
        int age = ((Integer)state.getValue(AGE)).intValue();
        Random rand = world instanceof World ? ((World)world).rand : new Random();
        int R = ((int) (Math.random() * 10)) ;
        

        if ( age >= 7 & R > 8 )
        {
        ret.add(new ItemStack(this.getSeed(), 1, 0));
        }

     
        
        
        return ret;
    }
  //#################################################################################################3
}//fin de la class

 

all this code is based on the block crops from the wheat and wheat dont spawn so many seeds

what you think could be wrong ??

Posted

Well, the getDrops() method is the one that determines the drops. Your code in that is a bit weird.

 

First of all you get the drops from the super method. That will already have stuff in it. Is that what you want?

 

Then you make a random where you check whether world is instanceof world -- that is a useless check since it is always true.

 

Then you make yet another random with Math.random().

 

Lastly, the age needs to be greater than 7 otherwise the drops won't change.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

The if statement in your class will never run, since you are using a bitwise "and" instead of a logical "and". I'm assuming it's just a typo.

 

if ( age >= 7 & R > 8 ) // Will always be false in this context, should have two ampersands: ((age >= 7) && (R > )
        {
        ret.add(new ItemStack(this.getSeed(), 1, 0));
        }

 

Also as Jabelar said, you are retrieving the drops list from the superclass which probably isn't what you want at all. It looks like you just copy pasted the method from BlockCrops. I think you'd be better off writing a new method that does exactly what you want, since what you want is very simple:

 

  • Make a new List containing only one ItemStack of your seed
  • Perform your 20% chance check
  • If the check passes, add another seed ItemStack to the list
  • Return that List.

 

Also, I had a question regarding your post, Jabelar: You say "world instance of World" will always return true, but won't it only return true server side?

Posted

I didn't look at the code before asking.

 

Might want to start doing that.  Eclipse has a nice feature called "open type hierarchy."

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.

Announcements



×
×
  • Create New...

Important Information

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