Plugin Help Help with how to add delay between loops

Discussion in 'Plugin Help/Development/Requests' started by benthomas7777, Jun 16, 2015.

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

    benthomas7777

    Hi,
    I have a plugin that basically uses for loops to place an 11 by 11 by 11 structure that is saved in a hashmap.
    What I want to try to do is add a second delay between the placement of each block, but after looking and trying BukkitRunnables, I am still not sure.

    Code section
    Section of Code (open)

    Code:
    @EventHandler
        public void onBlockPlace(BlockPlaceEvent event) {
            if (event.getBlock().getType() == Material.SEA_LANTERN) {
                if (event.getPlayer().getItemInHand().hasItemMeta()) {
                    outerloop:
                    for (int i = 0; i < structures.length; i++) {
                        String check = structures[i].toLowerCase();
                        if (event.getPlayer().getItemInHand().getItemMeta()
                                .getDisplayName().toString().toLowerCase()
                                .contains(check)) {
                            List<String> s = getConfig().getStringList(structures[i]);
                            for (String str : s) {
                                String[] words = str.split(":");
                                build.put(words[0], Material.getMaterial(words[1]));
                            }
                            Block b = event.getBlock();
                            for (int x = -5; x < 6; x++) {
                                for (int z = -5; z < 6; z++) {
                                    for (int y = 0; y < 11; y++) {
                                        Material current = build.get(x + 5 + "," + y
                                                + "," + z + 5);
                                        b.getLocation().add(x, y, z).getBlock()
                                                .setType(current);
                                        b.getLocation().getBlock()
                                                .setType(Material.SEA_LANTERN);
                                    }
                                }
                            }
                        break outerloop;   
                        }
                    }
                   
                }
    
            }
     
  2. Offline

    BizarrePlatinum

    @benthomas7777 Use a scheduler, a sync repeating task will allow you to do exactly what you are wanting (may have to rewrite parts of your code).
     
  3. Offline

    benthomas7777

    I am not sure how to go about it?
     
  4. Offline

    BizarrePlatinum

    @benthomas7777 take a look at the repeating example under bukkit scheduler.
     
  5. Offline

    benthomas7777

    I have done that, but I have no clue where to put it. I can't put it in the for loop as it wouldn't make each block appear one at a time

    Has anyone got an idea? Or code? With what I've tried it crashes, spawns the whole structure every second or spawns a single block every second

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

    BizarrePlatinum

    @benthomas7777 no need to even use a for loop, create a counter, stop it after the eleventh time, cancel the scheduler.
     
  7. Offline

    benthomas7777

    That wouldn't work, it spawns in an 11 by 11 by 11 area
     
  8. Offline

    L3N

    There are two ways for you to do it:
    First:

    Code:
    Try{
    Thread.sleep(hereYouWriteTheAmountOfTimeInMILISECONDS);
    } catch(Exception e){
    e.printStackTrace;
    }
    or the other way by Bukkit scheduler:

    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run(){
                    // the code you want to run delayed here
                }
            }, timeInTicks);
     
Thread Status:
Not open for further replies.

Share This Page