[Lib] Regenerating Blocks

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

Thread Status:
Not open for further replies.
  1. spoljo666 But, I wanted to try, so... if you cant help me i will search...
     
  2. Offline

    viper_monster

    Doknesss you have to call that method somewhere.
     
  3. Offline

    viper_monster

    Doknesss you can see an example in the main post.
     
  4. spoljo666

    Code:java
    1. public void regen(final List<BlockState> blocks, final boolean effect, final int speed)


    This?
     
  5. Offline

    viper_monster

  6. ;-; i dont understand...
    on regen i have error and blocks too... Cant you give me finish code? its not hard
     
  7. Offline

    97WaterPolo

    He is giving you the code, he can't be any more clearer. Read the original post, read the post above yours, he is literally giving it to you. I agree with him, suggest you learn some stuff about Java...
     
    spoljo666 likes this.
  8. Offline

    Wizehh

    Thank you for this resource, I'm definitely going to use this!
     
  9. Offline

    Omletowy

    Anyone has idea how to rotate these blocks?

    @edit //Nevermind :)
     
  10. Offline

    calebbfmv

  11. Offline

    TryB4

    calebbfmv
    radius++ is just short for radius += 1
     
  12. Offline

    calebbfmv

    :O No way!
    Bro, I know, don't worry. I just mainly see radius++ not += except in mainstream coding....
     
  13. Offline

    Norbu10

    I have an problem it will shoot the the blocks up but wont remove them and place them right!
    EDIT : I already know what i did wrong!

    Sorry im quite a noob but how do you enable Effects?

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

    TryB4

    Norbu10
    Code:java
    1. public void regen(final List<BlockState> blocks, final boolean effect, final int speed) {


    set effect as true.
     
  15. Offline

    Norbu10

    Example Usage? Full Code? I dont understeand
     
  16. Offline

    mrgreen33gamer

    For some reason, the blocks don't regenerate.....

    Here is my code:

    Code:
    public void regen(final List<BlockState> blocks, final boolean effect, final int speed) {
     
    new BukkitRunnable() {
    int i = -1;
    @SuppressWarnings("deprecation")
    public void run() {
    if (i != blocks.size()-1) {
    i++;
    BlockState bs = blocks.get(i);
    bs.getBlock().setType(bs.getType());
    bs.getBlock().setData(bs.getBlock().getData());
    if (true)
    bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.POTION_BREAK, bs.getBlock().getType());
    }else{
    for (BlockState bs : blocks) {
    bs.getBlock().setType(bs.getType());
    bs.getBlock().setData(bs.getBlock().getData());
    }
    blocks.clear();
    this.cancel();
    }
    }
    }.runTaskTimer(plugin, 30L, 30L);
    }
     
    @EventHandler
    public void explode(EntityExplodeEvent e) {
    if (!e.blockList().isEmpty()) {
    final List<BlockState> blocks = new ArrayList<BlockState>();
    for (Block b : e.blockList()) {
    if (b.getType() != Material.AIR) {
    if (!blocks.contains(b.getState())) {
    blocks.add(b.getState());
    }
    }
    }
    new BukkitRunnable() {
    int i = 17;
    public void run() {
    if (i>0){
    i--;
    }else{
    regen(blocks, false, 6);
    this.cancel();
    }
    }
    }.runTaskTimer(plugin, 30L, 30L);
     
     
    e.blockList().clear();
    }
    }
    
    I already have the event registered in the
    Code:
    onEnable(){}
     
  17. Offline

    97WaterPolo

    TryB4
    Possible help please. Currently using:

    Code:java
    1. List<Location> locs = Resource.circle(event.getEntity().getLocation(), radius, height, false, true, 0);
    2. List<Block> blockList = new ArrayList<Block>();
    3. for (Location l : locs){
    4. Location locfinal = l.subtract(0, 1 , 0);
    5. Block block = locfinal.getBlock();
    6. block.setType(Material.WOOL);
    7. block.setData((byte)Resource.randomizeColor());
    8. blockList.add(block);
    9. }
    10. if (!blockList.isEmpty()) {
    11. final List<BlockState> blocks = new ArrayList<BlockState>();
    12. for (Block b : blockList) {
    13. if (b.getType() != Material.AIR) {
    14. if (!blocks.contains(b.getState())) {
    15. blocks.add(b.getState());
    16. }
    17. }
    18. new BukkitRunnable() {
    19. int i = 17;
    20. public void run() {
    21. if (i>0){
    22. i--;
    23. }else{
    24. Resource.regen(blocks, true, 1);
    25. this.cancel();
    26. }
    27. }
    28. }.runTaskTimer(this, 15, 15);
    29. player.sendMessage(blocks.toString());
    30. blockList.clear();
    31. }
    32. }

    I am trying to get these blocks to regen after a time . I added a check and it seems that it is adding the block to the list yet it isn't working. My regen code is exactly identical to the one in the OP. Any thoughts would be awesome!
     
  18. Offline

    TryB4

  19. Offline

    97WaterPolo

    TryB4
    Both classes need the eu.thegamecube.Physics.Physics class?

    The way I am calling it, is that correct?
     
  20. Offline

    TryB4

    97WaterPolo
    Physics.getInstance() you can replace with your main class.
     
    97WaterPolo likes this.
  21. Offline

    Wizehh

    Will this restore sign states (lines)?
     
  22. Offline

    97WaterPolo

    TryB4

    Updated the code and nothing is running when using

    Code:java
    1. @EventHandler
    2. public void onHit(ProjectileHitEvent event){
    3. if (event.getEntity().getShooter() instanceof Player){
    4. if (event.getEntity() instanceof EnderPearl){
    5. Block hitBlock = event.getEntity().getWorld().getBlockAt(event.getEntity().getLocation().subtract(0, 1, 0));
    6. Player player = (Player) event.getEntity().getShooter();
    7.  
    8. if (hitBlock.getType() == Material.GRASS) {
    9. hitBlock.getWorld().playEffect(hitBlock.getLocation(), Effect.STEP_SOUND, Material.OBSIDIAN.getId());
    10. List<Location> locs = Resource.circle(event.getEntity().getLocation(), radius, height, false, true, 0);
    11. List<Block> blockList = new ArrayList<Block>();
    12. for (Location l : locs){
    13. Location locfinal = l.subtract(0, 1 , 0);
    14. Block block = locfinal.getBlock();
    15. block.setType(Material.WOOL);
    16. block.setData((byte)Resource.randomizeColor());
    17. blockList.add(block);
    18. }
    19. if (!blockList.isEmpty()) {
    20. final List<BlockState> blocks = new ArrayList<BlockState>();
    21. for (Block b : blockList) {
    22. if (b.getType() != Material.AIR) {
    23. if (!blocks.contains(b.getState())) {
    24. blocks.add(b.getState());
    25. }
    26. }
    27. new BukkitRunnable() {
    28. int i = 17;
    29. public void run() {
    30. if (i>0){
    31. i--;
    32. }else{
    33. Utilities.regen(blocks, true, 1);
    34. this.cancel();
    35. }
    36. }
    37. }.runTaskTimer(this, 15, 15);
    38. player.sendMessage(blocks.toString());
    39. blockList.clear();
    40. }
    41. }
    42. }
    43. }
    44. }
    45. }
     
  23. Offline

    TryB4

    Wizehh
    Signs are a blockstate, so I believe so, You can try it. If it doesn't work let me know and I'll try to add that. 97WaterPolo
    Add debug messages
     
  24. Offline

    97WaterPolo

    Show Spoiler

    Code:java
    1. @EventHandler
    2. public void onHit(ProjectileHitEvent event){
    3. if (event.getEntity().getShooter() instanceof Player){
    4. if (event.getEntity() instanceof EnderPearl){
    5. Block hitBlock = event.getEntity().getWorld().getBlockAt(event.getEntity().getLocation().subtract(0, 1, 0));
    6. final Player player = (Player) event.getEntity().getShooter();
    7.  
    8. if (hitBlock.getType() == Material.GRASS) {
    9. hitBlock.getWorld().playEffect(hitBlock.getLocation(), Effect.STEP_SOUND, Material.OBSIDIAN.getId());
    10. List<Location> locs = Resource.circle(event.getEntity().getLocation(), radius, height, false, true, 0);
    11. List<Block> blockList = new ArrayList<Block>();
    12. for (Location l : locs){
    13. Location locfinal = l.subtract(0, 1 , 0);
    14. Block block = locfinal.getBlock();
    15. block.setType(Material.WOOL);
    16. block.setData((byte)Resource.randomizeColor());
    17. blockList.add(block);
    18. }
    19. if (!blockList.isEmpty()) {
    20. player.sendMessage("Test");
    21. final List<BlockState> blocks = new ArrayList<BlockState>();
    22. for (Block b : blockList) {
    23. player.sendMessage("Test2");
    24. if (b.getType() != Material.AIR) {
    25. player.sendMessage("Test3");
    26. if (!blocks.contains(b.getState())) {
    27. player.sendMessage("Test4");
    28. blocks.add(b.getState());
    29. }
    30. }
    31. new BukkitRunnable() {
    32. int i = 17;
    33. public void run() {
    34. if (i>0){
    35. player.sendMessage("Test5");
    36. i--;
    37. }else{
    38. Utilities.regen(blocks, true, 1);
    39. player.sendMessage("Test6");
    40. this.cancel();
    41. }
    42. }
    43. }.runTaskTimer(this, 15, 15);
    44. player.sendMessage(blocks.toString());
    45. blockList.clear();
    46. }
    47. }
    48. }
    49. }
    50. }
    51. }

    [/SPOLER]

    Makes it to Test 5, but not to Test 6? I moved the Utilities.regen(blocks, true, 1) to the if statement and still nothing happens.
     
  25. Offline

    TryB4

  26. Offline

    97WaterPolo

    TryB4
    Hallo! Managed to get the regenerating part to work, only problem now is the block that is regenerating, is wrong. I have added checks and it shows the the correct block is being added to the list, but after I modify it, it seems to change to the modified block instead of sticking to the original one.

    Both test messages 5 and 6 return the actual item, while 8 and 9 both return wool. Any ideas?

    Show Spoiler

    Code:java
    1. List<Block> blockPaint = new ArrayList<Block>();
    2. @EventHandler
    3. public void onHit(ProjectileHitEvent event){
    4. if (event.getEntity().getShooter() instanceof Player){
    5. final Player player = (Player) event.getEntity().getShooter();
    6. final Block originalblock = event.getEntity().getWorld().getBlockAt(event.getEntity().getLocation().subtract(0, 1, 0));
    7. player.sendMessage(originalblock.getType().toString() + "5");
    8. blockPaint.add(originalblock);
    9. player.sendMessage(originalblock.getType().toString() + "6");
    10. if (event.getEntity() instanceof EnderPearl){
    11. if (Resource.isItem(originalblock.getType())) {
    12. if (!originalblock.hasMetadata("Paintball")){
    13. originalblock.getWorld().playEffect(originalblock.getLocation(), Effect.STEP_SOUND, Material.LAPIS_BLOCK.getId());
    14. originalblock.setType(Material.WOOL);
    15. originalblock.setData((byte)Resource.randomizeColor());
    16. originalblock.setMetadata("Paintball", new FixedMetadataValue(this, player.getName()));
    17. player.sendMessage(originalblock.getType().toString() + "8");
    18. if (!blockPaint.isEmpty()) {
    19. final List<BlockState> blocks = new ArrayList<BlockState>();
    20. for (Block b : blockPaint) {
    21. if (b !=null){
    22. if (b.getType() != Material.AIR) {
    23. if (!blocks.contains(b.getState())) {
    24. blocks.add(b.getState());
    25. player.sendMessage(originalblock.getType().toString() + "9");
    26. }
    27. }
    28. }
    29. }
    30. new BukkitRunnable() {
    31. //int i = 17;
    32. int i = 10;
    33. public void run() {
    34. if (i>0){
    35. i--;
    36. }else{
    37. regen(blocks, true, 5);
    38. this.cancel();
    39. removeMetaData(originalblock);
    40.  
    41. }
    42. }
    43. }.runTaskTimer(this, 5, 5);
    44. player.sendMessage(blocks.toString());
    45. blockPaint.clear();
    46. }
    47. }
    48. }
    49. }
    50. }
    51. }

     
  27. Offline

    sipsi133

    Grass (ID 31:1) regenerates as block Shrub (ID 31). When i explode grass and when it regenerates, it regenerates as Shrub instead of Grass.
    Here is regen() method:
    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.getBlock().getType());
    11. bs.getBlock().setData(bs.getBlock().getData());
    12. if (effect) {
    13. if(bs.getBlock().getType() == Material.GLASS
    14. || bs.getBlock().getType() == Material.GLOWSTONE
    15. || bs.getBlock().getType() == Material.REDSTONE_LAMP_OFF
    16. || bs.getBlock().getType() == Material.REDSTONE_TORCH_ON
    17. || bs.getBlock().getType() == Material.THIN_GLASS
    18. || bs.getBlock().getType() == Material.GLASS) {
    19. bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, Material.GLASS);
    20. }else{
    21. bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, Material.WOOL);
    22. }
    23. }
    24. }else{
    25. for (BlockState bs : blocks) {
    26. bs.getBlock().setType(bs.getBlock().getType());
    27. bs.getBlock().setData(bs.getBlock().getData());
    28. }
    29. blocks.clear();
    30. this.cancel();
    31. }
    32. }
    33. }.runTaskTimer(this, speed, speed);
    34. }
     
  28. Offline

    TryB4

    sipsi133
    try replacing:
    Code:java
    1. bs.getBlock().setData(bs.getBlock().getData());
    2. //with
    3. bs.getBlock().setData(bs.getData().getData());
     
  29. Offline

    sipsi133

    TryB4 It worked! :) Thank you.
     
Thread Status:
Not open for further replies.

Share This Page