Solved Regeneration of a broken mushroom

Discussion in 'Plugin Development' started by DrTURTLE2, Jun 12, 2014.

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

    DrTURTLE2

    Hello there, I've done some google searching, and still couldn't find the solution to my problem. How does one regenerate a red/brown mushroom after it is broken approximately 5 seconds after?
    Here is what I have so far.
    Code:
    @EventHandler
                public void onBlockBreak(BlockBreakEvent event){
                    if(event.getBlock().equals(Material.BROWN_MUSHROOM) || (event.getBlock().equals(Material.RED_MUSHROOM))){
                       
                        new BukkitRunnable(){
                            public void run(){
                             
                             
     
                            }
                        }.runTaskLater(plugin, 100L);
                     
                }
                }
    Thank you.
     
  2. Offline

    GeorgeeeHD

    DrTURTLE2 event.setBlock(event.getBlock().getType());
     
  3. Offline

    DrTURTLE2

    GeorgeeeHD setBlock is undefined for BlockBreakEvent
     
  4. Offline

    GeorgeeeHD

    DrTURTLE2 sorry i mistyped/derped, event.getBlock.setType(event.getBlock().getType());
     
  5. Offline

    DrTURTLE2

    Thanks GeorgeeeHD but it still isn't working. Here is the code.

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(final BlockBreakEvent event){
    3. if(event.getBlock().equals(Material.BROWN_MUSHROOM) || (event.getBlock().equals(Material.RED_MUSHROOM))){
    4.  
    5. new BukkitRunnable(){
    6. public void run(){
    7. event.getBlock().setType(event.getBlock().getType());
    8.  
    9.  
    10. }
    11. }.runTaskLater(plugin, 100L);
    12.  
    13. }
    14. }
    15.  
    16.  
    17. }
    18.  
     
  6. Offline

    GeorgeeeHD

    DrTURTLE2 idk, try event.getBlock().getLocation().getBlock().setType(e.getBlock.getType());
     
  7. Offline

    xTigerRebornx

    GeorgeeeHD You shouldn't help people if you don't know how to (properly) do it yourself.
    DrTURTLE2 What you are currently doing is setting the type to the type it already have, which does nothing.
    In the event, store the Block's BlockState, then call update() on it (pass in true to force it), or store the Material that it is before it changes.
     
  8. Offline

    Garris0n

    Storing the BlockState would be overkill unless you actually need all of the data it could have. In this case, all the OP is doing is regenerating mushrooms, so the material is sufficient.
     
  9. Offline

    xTigerRebornx

    Garris0n It'll help the OP know what to do when they need to restore more then just the Material.
    Offtopic-ish: Do Mushrooms actually have any data?
     
  10. Offline

    DrTURTLE2

  11. Offline

    xTigerRebornx

    DrTURTLE2 When you require more then just a Material, like when you have a Chest or a Log, use my method, otherwise you can use Garris0n 's method.
     
  12. Offline

    GeorgeeeHD

    DrTURTLE2 final Material mat = e.getBlock().getType(); then event.getBlock().setType(mat); inside the delayed task
     
  13. Offline

    DrTURTLE2

    GeorgeeeHD Hm just tested and doesn't work. Late reply sorry.
     
  14. Offline

    PatoTheBest

    DrTURTLE2
    Code:java
    1. @EventHandler
    2. public void onBlockBreak(final BlockBreakEvent event) {
    3. if (event.getBlock().equals(Material.BROWN_MUSHROOM) || (event.getBlock().equals(Material.RED_MUSHROOM))) {
    4. new BukkitRunnable() {
    5. public void run() {
    6. event.getBlock().getState().update();
    7. }
    8. }.runTaskLater(plugin, 100L);
    9.  
    10. }
    11. }
     
  15. Offline

    DrTURTLE2

    PatoTheBest Still doesn't work. I appreciate everyone who has helped.
     
  16. Offline

    PatoTheBest

    DrTURTLE2
    Code:java
    1. @EventHandler
    2. public void onBlockBreak(final BlockBreakEvent event) {
    3. if (event.getBlock().getType() == Material.BROWN_MUSHROOM || event.getBlock().getType() == Material.RED_MUSHROOM) {
    4. new BukkitRunnable() {
    5. public void run() {
    6. event.getBlock().getState().update();
    7. }
    8. }.runTaskLater(plugin, 100L);
    9.  
    10. }
    11. }
     
  17. Offline

    DrTURTLE2

  18. Offline

    PatoTheBest

    DrTURTLE2
    The operator == and .equals isn't the same thing. Besides, you were testing if a block was the same as a material which they are different. However, .getType() will return a material, thus you can test if a material is a material. Also, I don't know if brown mushroom is the mushroom block or if it is the mushroom you find in a cave.
     
  19. Offline

    DrTURTLE2


    Haha sorry, I mean it didn't work again. I saw the differences in the code.
     
  20. Offline

    ResultStatic

    DrTURTLE2 not sure if its solved but i helped someone on a thread to roll back an area this is what i gave them, i modified it a bit. the guy said it worked great i never tested it though.

    Code:
    HashMap<Location, Material> blocks = new HashMap<Location, Material>();
    @EventHandler
    public void blockbreak(BlockBreakEvent e){
     
    if (e.getBlock().getType().equals(Material.RED_MUSHROOM) || (e.getBlock().getType().equals(Material.BROWN_MUSHROOM))){
    blocks.put(e.getBlock().getLocation(),e.getBlock().getType());
    // add ur task here add to reset it after 5 seconds.
    }
    }
    public void reset(){
    for (Location loc : blocks.keySet()){
    loc.getBlock().setType(blocks.get(loc));
    }
    }
     
  21. Offline

    Konkz

    Just wondering, are you trying to make it regen a mushroom block (like when you make a huge mushroom) or just a normal small one?
     
  22. Offline

    DrTURTLE2


    Small one.
     
  23. Offline

    fefe2008

    DrTURTLE2 do you want it to drop the item when its broken?
     
  24. Offline

    DrTURTLE2


    I'm already removing all entities after they spawn so that doesn't really matter.
    So preferably no


    Wow, didn't see this. It works! Thanks!

    And thanks the rest of you for helping.

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

    xTigerRebornx

    DrTURTLE2 My god, people have a habit of spoonfeeding wrong code....
    Like I have mentioned before (and Garris0n has also mentioned), you can either save the Material/BlockState in a local variable before you call the scheduler, then in the scheduler use that stored local variable to restore the block.

    PatoTheBest You realize that the code you (spoon-fed) provided will do nothing except call an update on an air block (or whatever block is there at the time of calling), you are just calling update on its current state.

    ResultStatic That code is useful for restoring an area over a large period of time, but would be overkill since the OP is only working with one block in the timeframe of one event, where he can simply store it as a local variable in his handler and update it using a scheduler.
     
    Garris0n and Konkz like this.
Thread Status:
Not open for further replies.

Share This Page