Solved Making a block builder?

Discussion in 'Spigot Plugin Development' started by ondrej008, Jun 2, 2017.

  1. Hello guys,
    It's me again.
    This time I use WE to load a schematic, but it freezes the server.
    My idea was to use a Runnable which places blocks but it just freezes the server even more ;-;
    Here's what I've got so far:
    Code (Text):
    new BukkitRunnable() {
                    @Override
                    public void run() {
                        for (int x = 0; x < size.getBlockX(); ++x) {
                            for (int y = 0; y < size.getBlockY(); ++y) {
                                for (int z = 0; z < size.getBlockZ(); ++z) {
                                    final BaseBlock block = data[x][y][z];
                                    if (block == null || block.isAir()) {
                                        continue;
                                    }
                                   
                                   
                                    try {
                                        es.setBlock(new Vector(x, y, z).add(origin), block);
                                    } catch (MaxChangedBlocksException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {}
                        }
                    }
                }.runTask(this);
    es is EditSession which just places the block.
    data is where the blocks are stored.
    size is the size of the data thing.
     
  2. If you want a looping task you can do this which works for me:

    Code (Text):

                            new BukkitRunnable() {                            
                                private Material place = blockPlacing;
                                private int startY = changedY;
                                private int finalZ = changedZ;
                                private int finalX = changedX;
                                @Override
                                public void run()
                                {
                                    if(startY + 1 == upLimit)
                                    {
                                        cancel();
                                        return;
                                    }

                                    Block replacing = p.getWorld().getBlockAt(finalX, startY, finalZ);
                                    startY = startY + 1;

                                    // Ignoring if the block isn't AIR
                                    if(replacing.getType() != Material.AIR)
                                    {
                                        cancel();
                                        return;
                                    }

                                    // Replacing the block
                                    replacing.setType(place);
                                }
                            }.runTaskTimer(plugin, 10L, 10L);      
                        }
     
    .runTaskTimer will run through the code until you tell it till stop. The first 10L is for the start delay, 10 ticks and the second is the delay after the first run between each execution. This code right there will build a block all the way to sky limit.
     
  3. @TheRedstoneGeek
    how do I stop it?
    It's cancel(); right? What does return; do in that case?
    EDIT:
    Here's what I got now:
    Code (Text):

    new BukkitRunnable() {
             private int x;
             private int y;
             private int z;
      @Override
      public void run()
      {
         l.info(String.format("X: %d, Y: %d, Z: %d", x, y, z));
         if(z >= size.getBlockZ()) {
           z = 0;
           y++;
         }
         if(y >= size.getBlockY()) {
           y = 0;
           x++;
         }
         if(x >= size.getBlockX()) {
           l.info("Fin.");
           cancel();
           //I want to stop here!!!!
         }
         //Skip air blocks and NULL blocks.
         final BaseBlock block = data[x][y][z];
      if(block == null || block.isAir())
      {
         z++;
      return;
      }
      try {
                 es.setBlock(new Vector(x, y, z).add(origin), block);
                 z++;
               } catch (MaxChangedBlocksException e) {} //This won't happen, I set it to 999 million blocks.

      }
      }.runTaskTimer(this, 0L, 5L);  

     
     
    #3 ondrej008, Jun 2, 2017
    Last edited: Jun 2, 2017
  4. cancel(); I believe is where it cancels the loop
    return; I don't really know but I guess it is to return out of the bukkit Runnable();

    But that should work once youa dd return;! Good luck
     
  5. @TheRedstoneGeek
    cancel(); doesn't stop it.
    I'm throwing a "RunnableCompleted" exception, as an exception is the only way it stops itself.
     
  6. Add return; It works perfectly for me
     
  7. return; doesn't work for me :(
     
  8. I believe it is because the condition you are trying to do. You want to check if the current x coordinate is equal to x coordinate of the last block. I suggest creating a variable called XCorner and set it to the final x coordinate and pass that in under override. Then check if the x coordiante is == to the current x
     
    • Agree Agree x 1
  9. Why do you sleep on the thread?
     
  10. Adding a if(true) { cancel(); return; }
    made it exit! Thanks to TheRedstoneGeek
     
    • Like Like x 1