Placing Blocks

Discussion in 'Spigot Plugin Development' started by petrux25, Apr 13, 2019.

  1. Hy everyone, I'm developing a plugin that builds (places) blocks with a delat between them

    Code (Java):
    if(args[0].equals("build")) {
             
                    for(int i=0; i<Main.blocks.size(); i++) {
                        Main.blocks.get(i).getLoc().getBlock().setType(Main.blocks.get(i).getMat());
                     
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            }
    When I try to execute the command the blocks don't appear one by one but all together at the end of the for
     
  2. SteelPhoenix

    Moderator

    ..don't use Thread#sleep(). There's a BukkitScheduler for a reason
     
  3. Should I use runTaskLater?
     
  4. If you use Thread#sleep, you are freezing the whole server thread. Use this

    Code (Text):
    new BukkitRunnable() {
                int i = 0;
                @Override
                public void run() {
                    Main.blocks.get(i).getLoc().getBlock().setType(Main.blocks.get(i).getMat());
                    i++;
                }
            }.runTaskTimer(Main.getPlugin(Main.class), 0, 20*4 /* 4 seconds */);
     
    • Like Like x 1
  5. This however will throw errors as it does never stop running. Assuming the list isn't limitless, obviously, you'd have to add a check also so as to cancel the task.
     
    • Like Like x 1
  6. Oh, I forgot it

    Code (Text):
    new BukkitRunnable() {
                int i = 0;
                @Override
                public void run() {
                    if (i >= Main.blocks.size()) {
                        this.cancel();
                        return;
                    }
                    Main.blocks.get(i).getLoc().getBlock().setType(Main.blocks.get(i).getMat());
                    i++;
                }
            }.runTaskTimer(Main.getPlugin(Main.class), 0, 20*4 /* 4 seconds */);
     
    • Like Like x 2
  7. okay, Thanks to all