[Lib] Regenerating Blocks

Discussion in 'Resources' started by TryB4, Dec 23, 2013.

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

    TryB4

    Just thought i'd share this, incase anyone needs it.

    Code:java
    1. public void regen(final List<BlockState> blocks, final boolean effect, final int speed) {
    2.  
    3. new BukkitRunnable() {
    4. int i = -1;
    5. @SuppressWarnings("deprecation")
    6. public void run() {
    7. if (i != blocks.size()-1) {
    8. i++;
    9. BlockState bs = blocks.get(i);
    10. bs.getBlock().setType(bs.getType());
    11. bs.getBlock().setData(bs.getBlock().getData());
    12. if (effect)
    13. bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, bs.getBlock().getType());
    14. }else{
    15. for (BlockState bs : blocks) {
    16. bs.getBlock().setType(bs.getType());
    17. bs.getBlock().setData(bs.getBlock().getData());
    18. }
    19. blocks.clear();
    20. this.cancel();
    21. }
    22. }
    23. }.runTaskTimer(this, speed, speed);
    24. }





    Example usage:


    Code:java
    1. @EventHandler
    2. public void explode(EntityExplodeEvent e) {
    3. if (!e.blockList().isEmpty()) {
    4. final List<BlockState> blocks = new ArrayList<BlockState>();
    5. for (Block b : e.blockList()) {
    6. if (b.getType() != Material.AIR) {
    7. if (!blocks.contains(b.getState())) {
    8. blocks.add(b.getState());
    9. FallingBlock fb = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
    10. fb.setDropItem(false);
    11. b.setType(Material.AIR);
    12. }
    13. }
    14. }
    15. new BukkitRunnable() {
    16. int i = 17;
    17. public void run() {
    18. if (i>0){
    19. i--;
    20. }else{
    21. regen(blocks, false, 6);
    22. this.cancel();
    23. }
    24. }
    25. }.runTaskTimer(this, 15,15);
    26.  
    27.  
    28. e.blockList().clear();
    29. }
    30. }
    31.  
















    Video (using: regen(<list>, true, 1)):


     
  2. Offline

    jusjus112

    TryB4
    Nice LIB!
    But i got an error in eclipse in "regen(blocks);"
    this is the code:
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.BlockState;
    10. import org.bukkit.entity.FallingBlock;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.EntityExplodeEvent;
    14. import org.bukkit.plugin.Plugin;
    15. import org.bukkit.scheduler.BukkitRunnable;
    16.  
    17.  
    18. public class RegenBlocks implements Listener {
    19.  
    20. Plugin plugin;
    21.  
    22. @SuppressWarnings("deprecation")
    23. @EventHandler
    24. public void explode(EntityExplodeEvent e) {
    25. if (!e.blockList().isEmpty()) {
    26. final List<BlockState> blocks = new ArrayList<BlockState>();
    27. for (Block b : e.blockList()) {
    28. if (b.getType() != Material.AIR) {
    29. if (!blocks.contains(b.getState())) {
    30. blocks.add(b.getState());
    31. FallingBlock fb = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
    32. fb.setDropItem(false);
    33. b.setType(Material.AIR);
    34. }
    35. }
    36. }
    37. new BukkitRunnable() {
    38. int i = 17;
    39. public void run() {
    40. if (i>0){
    41. i--;
    42. }else{
    43. regen(blocks); //My error
    44. this.cancel();
    45. }
    46. }
    47. }.runTaskTimer(plugin, 15,15);
    48.  
    49.  
    50. e.blockList().clear();
    51. }
    52. }
     
  3. Offline

    TryB4

    jusjus112
    you don't have the regen method in RegenBlocks

    Updated:

    Added a few parameters.

    effect - play the block breaking effect or not.
    speed - how fast to regenerate the blocks


    Added video to show what you can do with this.

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

    jusjus112

    TryB4
    It does not play the effect?
    And i dont know if this in the LIB, but you dont see the fallingblock?
     
  5. Offline

    TryB4

    jusjus112
    What?..


    EDIT: Oh, you mean like that.
    You have to do:

    fb.setVelocity(new Vector(0, 0.6, 0)); - or something like that to make it go upwards a bit.
     
  6. Offline

    jusjus112

    TryB4
    I have by "regen(blocks, true, 6);"
    But it dont play the effect!

    And BTW, Can you do so that the fallingblock is exploding up?
     
  7. Offline

    TryB4

  8. Offline

    jusjus112

    TryB4
    Code:java
    1.  
    2. import java.util.ArrayList;
    3. import java.util.List;
    4.  
    5. import org.bukkit.Effect;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.block.BlockState;
    9. import org.bukkit.entity.FallingBlock;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityExplodeEvent;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.scheduler.BukkitRunnable;
    15.  
    16.  
    17. public class RegenBlocks implements Listener {
    18.  
    19. Plugin plugin;
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void explode(EntityExplodeEvent e) {
    24. if (!e.blockList().isEmpty()) {
    25. final List<BlockState> blocks = new ArrayList<BlockState>();
    26. for (Block b : e.blockList()) {
    27. if (b.getType() != Material.AIR) {
    28. if (!blocks.contains(b.getState())) {
    29. blocks.add(b.getState());
    30. FallingBlock fb = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
    31. fb.setDropItem(false);
    32. b.setType(Material.AIR);
    33. }
    34. }
    35. }
    36. new BukkitRunnable() {
    37. int i = 17;
    38. public void run() {
    39. if (i>0){
    40. i--;
    41. }else{
    42. regen(blocks, true, 6);
    43. this.cancel();
    44. }
    45. }
    46. }.runTaskTimer(plugin, 15,15);
    47.  
    48.  
    49. e.blockList().clear();
    50. }
    51. }
    52.  
    53. public void regen(final List<BlockState> blocks, final boolean effect, final int speed) {
    54.  
    55. new BukkitRunnable() {
    56. int i = -1;
    57. @SuppressWarnings("deprecation")
    58. public void run() {
    59. if (i != blocks.size()-1) {
    60. i++;
    61. BlockState bs = blocks.get(i);
    62. bs.getBlock().setType(bs.getType());
    63. bs.getBlock().setData(bs.getBlock().getData());
    64. if (effect)
    65. bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
    66. }else{
    67. for (BlockState bs : blocks) {
    68. bs.getBlock().setType(bs.getType());
    69. bs.getBlock().setData(bs.getBlock().getData());
    70. }
    71. blocks.clear();
    72. this.cancel();
    73. }
    74. }
    75. }.runTaskTimer(plugin, speed, speed);
    76. }
    77. }
     
  9. Offline

    TryB4

    jusjus112

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4. import org.bukkit.Effect;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.block.BlockState;
    8. import org.bukkit.entity.FallingBlock;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityExplodeEvent;
    12. import org.bukkit.plugin.Plugin;
    13. import org.bukkit.scheduler.BukkitRunnable;
    14.  
    15.  
    16. public class RegenBlocks implements Listener {
    17.  
    18. Plugin plugin;
    19.  
    20. @SuppressWarnings("deprecation")
    21. @EventHandler
    22. public void explode(EntityExplodeEvent e) {
    23. if (!e.blockList().isEmpty()) {
    24. final List<BlockState> blocks = new ArrayList<BlockState>();
    25. for (Block b : e.blockList()) {
    26. if (b.getType() != Material.AIR) {
    27. if (!blocks.contains(b.getState())) {
    28. blocks.add(b.getState());
    29. FallingBlock fb = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
    30. fb.setDropItem(false);
    31. fb.setVelocity(new Vector(0, 0.6, 0));
    32. b.setType(Material.AIR);
    33. }
    34. }
    35. }
    36. new BukkitRunnable() {
    37. int i = 17;
    38. public void run() {
    39. if (i>0){
    40. i--;
    41. }else{
    42. regen(blocks, true, 6);
    43. this.cancel();
    44. }
    45. }
    46. }.runTaskTimer(plugin, 15,15);
    47.  
    48.  
    49. e.blockList().clear();
    50. }
    51. }
    52.  
    53. public void regen(final List<BlockState> blocks, final boolean effect, final int speed) {
    54.  
    55. new BukkitRunnable() {
    56. int i = -1;
    57. @SuppressWarnings("deprecation")
    58. public void run() {
    59. if (i != blocks.size()-1) {
    60. i++;
    61. BlockState bs = blocks.get(i);
    62. bs.getBlock().setType(bs.getType());
    63. bs.getBlock().setData(bs.getBlock().getData());
    64. if (effect)
    65. bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
    66. }else{
    67. for (BlockState bs : blocks) {
    68. bs.getBlock().setType(bs.getType());
    69. bs.getBlock().setData(bs.getBlock().getData());
    70. }
    71. blocks.clear();
    72. this.cancel();
    73. }
    74. }
    75. }.runTaskTimer(plugin, speed, speed);
    76. }
    77. }
     
  10. Offline

    jusjus112

    TryB4
    Thanks the fallingblocks works :D
    But i dont get it for the effects?
    He is not playing any effects?
    Do i something wrong, or must i enable something in my minecraft (I have enabled Animations?)
     
  11. Offline

    TryB4

    jusjus112
    You need particles to be set to ALL.
     
  12. Offline

    jusjus112

  13. Offline

    NuclearWolfMC

    Wow nice regen method but I am putting a walls minigame on a multiverse worlds (we only have 8 servers), would this be an effective regen mthod or would there be too many blocks to track.
    Also does this store chest data?
     
  14. Offline

    TryB4

    NuclearWolfMC
    BlockState stores the current state of a block. So it should... not 100% sure though.

    For resetting a walls minigame or similar, I would suggest disabling autosave for the world it is in.
    That way it will reset upon a reboot.

    (/save-all to save it after building etc.)
     
  15. Offline

    chasechocolate

  16. Why blocks dont regen? :C
    Code:java
    1. @EventHandler
    2. public void explode(EntityExplodeEvent e) {
    3. if (!e.blockList().isEmpty()) {
    4. final List<BlockState> blocks = new ArrayList<BlockState>();
    5. for (Block b : e.blockList()) {
    6. if (b.getType() != Material.AIR) {
    7. if (!blocks.contains(b.getState())) {
    8. blocks.add(b.getState());
    9. b.setType(Material.AIR);
    10. }
    11. }
    12. }
    13. new BukkitRunnable() {
    14. int i = 17;
    15. public void run() {
    16. if (i>0){
    17. i--;
    18. }else{
    19. regen(blocks, true, 1);
    20. this.cancel();
    21. }
    22. }
    23. }.runTaskTimer(this, 15,15);
    24.  
    25.  
    26. e.blockList().clear();
    27. }
    28. }
    29. protected void regen(List<BlockState> blocks, boolean b, int i) {
     
  17. Offline

    TryB4

    Doknesss
    Did you register your listener?
     
  18. yes
    Code:java
    1. public class glowny extends JavaPlugin implements Listener {
     
  19. Offline

    viper_monster

    Doknesss put this in your onEnable() method:

    PHP:
    Bukkit.getServer().getPluginManager().registerEvents(thisthis);
     
  20. not working :C

    Code:java
    1. package regeneration.moje;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.BlockState;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityExplodeEvent;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scheduler.BukkitRunnable;
    16.  
    17. public class Main extends JavaPlugin implements Listener {
    18.  
    19. @Override
    20. public void onDisable() {
    21. System.out.println("Zostalo wylaczone");
    22. }
    23.  
    24. @Override
    25. public void onEnable() {
    26. System.out.println("RespienieBlokow Dziala!!!");
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28.  
    29. }
    30. @EventHandler
    31. public void explode(EntityExplodeEvent e) {
    32. if (!e.blockList().isEmpty()) {
    33. final List<BlockState> blocks = new ArrayList<BlockState>();
    34. for (Block b : e.blockList()) {
    35. if (b.getType() != Material.AIR) {
    36. if (!blocks.contains(b.getState())) {
    37. blocks.add(b.getState());
    38. b.setType(Material.AIR);
    39. }
    40. }
    41. }
    42. new BukkitRunnable() {
    43. int i = 17;
    44. public void run() {
    45. if (i>0){
    46. i--;
    47. }else{
    48. regen(blocks, true, 1);
    49. this.cancel();
    50. }
    51. }
    52. }.runTaskTimer(this, 15,15);
    53.  
    54.  
    55. e.blockList().clear();
    56. }
    57. }
    58. protected void regen(List<BlockState> blocks, boolean b, int i) {
    59. }
    60. }
    61.  
     
  21. Offline

    viper_monster

    Doknesss That's cause there is nothing in your "regen" method.
     
  22. Offline

    TryB4

    Doknesss
    Really?
    Copy & Paste the "regen" method from the post above...
     
  23. i not english... can u give me kod?
     
  24. Offline

    97WaterPolo

    Holy sh*t, I downloaded this and tried it, it is one of the best things I have done in awhile, did you make it a Bukkit Dev plugin?
     
  25. Offline

    viper_monster

    Doknesss
    PHP:
        public void regen(final List<BlockStateblocks, final boolean effect, final int speed) {
            new 
    BukkitRunnable() {
                
    int i = -1;
                @
    SuppressWarnings("deprecation")
                public 
    void run() {
                    if (
    != blocks.size()-1) {
                        
    i++;
                        
    BlockState bs blocks.get(i);
                        
    bs.getBlock().setType(bs.getType());
                        
    bs.getBlock().setData(bs.getBlock().getData());
                        if (
    effect)
                            
    bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUNDbs.getBlock().getType());
                    }else{
                        for (
    BlockState bs blocks) {
                            
    bs.getBlock().setType(bs.getType());
                            
    bs.getBlock().setData(bs.getBlock().getData());
                        }
                        
    blocks.clear();
                        
    this.cancel();
                    }
                }
            }.
    runTaskTimer(thisspeedspeed);
        }
     
  26. spoljo666

    It doesnt work again..

    Code:java
    1. package regeneration.moje;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Effect;
    8. import org.bukkit.Material;
    9. import org.bukkit.block.Block;
    10. import org.bukkit.block.BlockState;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.EntityExplodeEvent;
    14. import org.bukkit.plugin.Plugin;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.scheduler.BukkitRunnable;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19.  
    20. @Override
    21. public void onDisable() {
    22. System.out.println("Zostalo wylaczone");
    23. }
    24.  
    25. @Override
    26. public void onEnable() {
    27. System.out.println("RespienieBlokow Dziala!!!");
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29.  
    30. }
    31. public void regen(final List<BlockState> blocks, final boolean effect, final int speed) {
    32. new BukkitRunnable() {
    33. int i = 3;
    34. @SuppressWarnings("deprecation")
    35. public void run() {
    36. if (i != blocks.size()-1) {
    37. i++;
    38. BlockState bs = blocks.get(i);
    39. bs.getBlock().setType(bs.getType());
    40. bs.getBlock().setData(bs.getBlock().getData());
    41. if (effect)
    42. bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, bs.getBlock().getType());
    43. }else{
    44. for (BlockState bs : blocks) {
    45. bs.getBlock().setType(bs.getType());
    46. bs.getBlock().setData(bs.getBlock().getData());
    47. }
    48. blocks.clear();
    49. this.cancel();
    50. }
    51. }
    52. }.runTaskTimer(this, speed, speed);
    53. }
    54. }
    55.  
     
  27. Offline

    viper_monster

    Doknesss OH MY GOD! Where are you using that method?! You have only copied it! I suggest you first learn Java.
     
    GrandmaJam likes this.
  28. spoljo666 I wanted to make plugin because regenblocks is in active. I dont want learn Java.
     
  29. Offline

    viper_monster

    Doknesss Well, I don't think you will be able to make that plugin without knowing Java.
     
Thread Status:
Not open for further replies.

Share This Page