Solved make wheat and trees grow instantly

Discussion in 'Plugin Development' started by Kyorax, Jan 5, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Kyorax

    Hi everyone,
    I know it must be simple but how can I get the position of a block and replace it by a tree?

    How can I replace seeds with fully grown wheat instantly when placed?
    This doesn't work:

    Code:java
    1. if(block.getType() == Material.SEEDS && bg.playerHasAbility(player, 113)) {
    2. block.setType(Material.CROPS);
    3. }


    To the tree question:
    Is it possible to let a tree spawn even if a player is near?
    At the moment simply nothing happens when I try to place one while standing near it.

    Thanks in advance!
     
  2. Offline

    leet4044

    You could get the location of the players target block, then generate a tree?
    Code:java
    1. Location location = p.getTargetBlock(null, 50).getLocation();
    2. location.getBlock().getWorld().generateTree(arg0, arg1)
     
  3. Offline

    Kyorax

    This should work out, shouldn't it?
    Code:java
    1. if(block.getType() == Material.SAPLING && bg.playerHasAbility(player, 113)) {
    2. Location bloc = block.getLocation();
    3. bloc.getWorld().generateTree(bloc, TreeType.TREE);
    4. }

    (sry for the noob questions, I'm new to java)
     
  4. Offline

    leet4044

    Looks good to me, go ahead and try it out to make sure
     
  5. Offline

    Kyorax

    Does no one have an idea? ...:(
     
  6. Offline

    Monkeyboystein

    Its ok! Everyone was new at one point, but i think the last time i tryed the generateTree method it did not work no matter what i did. If you get this working (or if they updated bukkit) please let me know ;)

    And as for your crop method.... Check for a material for it, or assign i believe crop to a nearby crop block

    PHP:
    //Get the near by crop blocks in a Block[]
     
    for(Block b blocks)
    {
    if(
    b.getType() == Material.CROP)
    {
    Crop c = (Crop)b;
    b.setStage(4);
     
    }
     
     
    }
    That is probably not perfect
     
  7. Offline

    Kyorax

    Monkeyboystein Yes, the tree method works, but only if you stand away from the sapling when placing it.
    Are seeds, after planted, treated as crops at the lowest stage?
     
  8. Offline

    Monkeyboystein

  9. Offline

    Kyorax

    Gonna test this:
    Code:java
    1. if(block.getType() == Material.SEEDS && bg.playerHasAbility(player, 113)) {
    2. block.setType(Material.CROPS);
    3. Crops c = (Crops)block;
    4. c.setState(CropState.RIPE);
    5.  
    6. }


    Doesn't work and I have no idea why.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  10. Offline

    jensdeboom

    I cant test it for now but maybe you need to take one block higher ? It would be something like this:
    Location loc = new location(block.getX, block.getY + 1, block.getX);
    Than do the stuff you did above.
    As i said, i cant test it but you can always try :).

    ~ jens
     
  11. Offline

    Kyorax

    I'll try that out, thanks.

    nothing happens

    It isn't exactly what I was looking for but here is my alternate solution:
    Code:java
    1. if(block.getType() == Material.CROPS && bg.playerHasAbility(player, 113)) {
    2. Location loc = block.getLocation();
    3. World w = block.getWorld();
    4. block.setType(Material.AIR);
    5. w.dropItemNaturally(loc, new ItemStack(Material.WHEAT, 1));
    6. w.dropItemNaturally(loc, new ItemStack(Material.SEEDS, 2));
    7. w.playEffect(loc, Effect.STEP_SOUND, Material.GRASS);
    8. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. Offline

    Waffles87

    Hey guys,

    I was struggling with this wheat problem and this thread, specifically Kyorax's code here helped me figure it out.


    Here's my adaptation that works for me:

    // Get the block you want to turn into crops.
    // You can't see it here, but in this case mine is 1 block above a block of material type MaterialType.SOIL
    Block b = world.getBlockAt(new Location(world,x,y,z));

    // Set the type to CROPS. This makes it into "baby" crops.
    b.setType(Material.CROPS);

    // Update the BlockState
    if (b.getType().equals(Material.CROPS)){
    Crops c = new Crops(CropState.RIPE);
    BlockState bs = b.getState();
    bs.setData(c);
    bs.update();
    }


    Note that you should only call getState() ONCE for your block and save it, because it seems to return the same snapshot as when the block was created every time. For example this will NOT work:

    b.getState().setData(c);
    b.getState().update();
     
    Kyorax likes this.
Thread Status:
Not open for further replies.

Share This Page