Grow cocoa bean plant

Discussion in 'Plugin Development' started by Elimnator, Apr 27, 2015.

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

    Elimnator

    I am trying to set a cocoa bean plant as fully grown:

    Code:
    CocoaPlant c = (CocoaPlant) b.getState().getData();
    c.setSize(CocoaPlantSize.LARGE);
    I also tried setting the data, but it just changed the stem direction.

    But this does not work, can anyone help.
     
  2. @Elimnator Don't cast to the data, just cast to the BlockState.
     
  3. Offline

    Elimnator

    @DJSkepter
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.block.CraftBlockState cannot be cast to org.bukkit.material.CocoaPlant
     
  4. Offline

    stormneo7

    Last edited: Apr 27, 2015
  5. @Elimnator My bad. Try setting the cocoa plant's data as the new data which has had the size change.
    (get the block state, set it as c after changing the size)
     
  6. Offline

    Elimnator

    @stormneo7
    I am not casting anything to a byte, I am casting it to a CocoaPlant. Which is what I am trying to get.

    @DJSkepter
    I tried:
    c.setData(b.getData());
    But nothing happens.

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

    stormneo7

    Code:
    CocoaPlant c = (CocoaPlant) b.getState().getData();
    [​IMG]
     
  8. Offline

    Elimnator

    So? I am still not casting to a byte, I am casting a byte to a CocoaPlant.

    Which by the way does not cause an error, where casting the block state does cause one.
     
  9. Offline

    stormneo7

    In which universe can you cast a byte towards a BlockState?
    You cannot cast a byte to a BlockState.
    You can retrieve the block's BlockState, then cast it to a class that extends it like CocoaPlant...
     
    Skionz likes this.
  10. Offline

    Elimnator

    @stormneo7
    Du du duuuuuu:
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.block.CraftBlockState cannot be cast to org.bukkit.material.CocoaPlant
     
  11. Offline

    stormneo7

    So why cast at all if you don't know how? You're casting a Material to a BlockState.
    You can only cast Blocks.
    [​IMG]
    Just use this...
    Code:
    <BlockState>.getData().setData((byte)3);
     
  12. Offline

    Elimnator

    That also does nothing.
     
  13. Offline

    SuperOriginal

    @stormneo7
    CocoaPlant extends MaterialData, not BlockState.
     
  14. Offline

    Elimnator

    Whatever, can anyone help me make it grow?
     
  15. Offline

    stormneo7

    When did I say that CocoaPlant extends BlockState?...
     
  16. Offline

    SuperOriginal

    @stormneo7
     
  17. Offline

    Elimnator

    @SuperOriginal
    I did that:
    CocoaPlant c = (CocoaPlant) b.getState();

    It crashes with:
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.block.CraftBlockState cannot be cast to org.bukkit.material.CocoaPlant
     
  18. Offline

    stormneo7

    That was prior to me realizing that there were no CocoaPlant Blocks and that it was just a material. That's why I posted the picture of the imports. There were no CocoaPlant Blocks...
     
  19. Offline

    Elimnator

    Cool cool, so how do I do it?
     
  20. Offline

    stormneo7

    <World>.getBlockAt(<Location>).setData(<Byte>);
     
  21. Offline

    Elimnator

  22. Offline

    stormneo7

    Post code. You're definitely doing something wrong. I tested everything I told you above and they all work.
     
  23. Offline

    Elimnator

    Here, G: 1 is printed, but the bean stays the same.
    Code:
    
                      Location l = p.getLocation();
                      int r = 3;
                      for(double x = l.getX() - r; x < l.getX() + (r+1); x++)
                          for(double y = l.getY() - r; y < l.getY() + (r+1); y++)
                              for(double z = l.getZ() - r; z < l.getZ() + (r+1); z++){
                                  Block b = p.getWorld().getBlockAt((int)x, (int)y, (int)z);
                                   if(b.getTypeId() == 127){
                                      //Coco beans
                                      b.getWorld().getBlockAt(b.getLocation()).setData((byte)3);
                                      System.out.println("G: 1");
                                  }
    
    
     
  24. Offline

    stormneo7

    Change 3 to 2.
     
  25. Offline

    Elimnator

    @stormneo7
    That just changes the stems direction.
     
  26. Offline

    stormneo7

    Code:
    CocoaPlant bean = (CocoaPlant) b.getState().getData();
    bean.setSize(CocoaPlantSize.LARGE);
    b.getState().setData(bean);
    b.getState().update();
     
    Last edited: Apr 27, 2015
  27. Offline

    Elimnator

  28. Offline

    stormneo7

    bean
     
  29. Offline

    Elimnator

  30. Offline

    stormneo7

    I really shouldn't spoon feed but whatever....
    You need to improvise next time. If you know that the byte edits how the beans face, then increasing / decreasing the bytes will also affect how they face as they grow... It took me like 5 minutes to play around and get this to work. You should do that next time instead of waiting for an answer.

    Code:
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(sender instanceof Player){
                if(commandLabel.equalsIgnoreCase("test")){
                    final Player p = (Player) sender;
                    final int radius = 5;
                    for(int x = -radius; x < radius; x++){
                        for(int y = -radius; y < radius; y++){
                            for(int z = -radius; z < radius; z++){
                                Block b = p.getWorld().getBlockAt(p.getLocation().add(x, y, z));
                                if(b.getType() == Material.COCOA){
                                    CocoaPlant bean = (CocoaPlant) b.getState().getData();
                                    switch (bean.getFacing()) {
                                        case EAST: b.setData((byte)11);
                                            break;
                                        case NORTH: b.setData((byte)10);
                                            break;
                                        case SOUTH: b.setData((byte)8);
                                            break;
                                        case WEST: b.setData((byte)9);
                                            break;
                                        default: b.setData((byte)0);
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
     
Thread Status:
Not open for further replies.

Share This Page