Delay in a for loop.

Discussion in 'Plugin Development' started by TheChinski, Jan 22, 2013.

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

    TheChinski

    Hey all. I've been working on abit of a hefty plugin over the past few days, and have finally got stuck on this:

    Code:
    while(nxtrnd = true) {
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    //This is the walls sinking thing
    for(int w = 1; w < 90; w++) {
    File Wall1 = new File("BorgPlug/" + "Wall1.yml");
    FileConfiguration Wall1file = YamlConfiguration.loadConfiguration(Wall1);
    Location Wally1 = new Location(Bukkit.getWorld("BorgCraft"), Wall1file.getInt(w + ".x"), Wall1file.getInt(w + ".y") + 2, Wall1file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Wally1).setType(Material.AIR);
    File Wall2 = new File("BorgPlug/" + "Wall2.yml");
    FileConfiguration Wall2file = YamlConfiguration.loadConfiguration(Wall2);
    Location Walle1 = new Location(Bukkit.getWorld("BorgCraft"), Wall2file.getInt(w + ".x"), Wall2file.getInt(w + ".y") + 2, Wall2file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Walle1).setType(Material.AIR);
    }
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    for(int w = 1; w < 90; w++) {
    File Wall1 = new File("BorgPlug/" + "Wall1.yml");
    FileConfiguration Wall1file = YamlConfiguration.loadConfiguration(Wall1);
    Location Wally1 = new Location(Bukkit.getWorld("BorgCraft"), Wall1file.getInt(w + ".x"), Wall1file.getInt(w + ".y") + 1, Wall1file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Wally1).setType(Material.AIR);
    File Wall2 = new File("BorgPlug/" + "Wall2.yml");
    FileConfiguration Wall2file = YamlConfiguration.loadConfiguration(Wall2);
    Location Walle1 = new Location(Bukkit.getWorld("BorgCraft"), Wall2file.getInt(w + ".x"), Wall2file.getInt(w + ".y") + 1, Wall2file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Walle1).setType(Material.AIR);
    }
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    for(int w = 1; w < 90; w++) {
    File Wall1 = new File("BorgPlug/" + "Wall1.yml");
    FileConfiguration Wall1file = YamlConfiguration.loadConfiguration(Wall1);
    Location Wally1 = new Location(Bukkit.getWorld("BorgCraft"), Wall1file.getInt(w + ".x"), Wall1file.getInt(w + ".y"), Wall1file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Wally1).setType(Material.AIR);
    File Wall2 = new File("BorgPlug/" + "Wall2.yml");
    FileConfiguration Wall2file = YamlConfiguration.loadConfiguration(Wall2);
    Location Walle1 = new Location(Bukkit.getWorld("BorgCraft"), Wall2file.getInt(w + ".x"), Wall2file.getInt(w + ".y"), Wall2file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Walle1).setType(Material.AIR);
    }
    File PlayerList = new File("BorgPlug/" + "PlayerList.yml");
    FileConfiguration PlayerListfile = YamlConfiguration.loadConfiguration(PlayerList);
    PlayerListfile.set(("RoundLosses." + r), 0);
    try {
    PlayerListfile.save(PlayerList);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Bukkit.broadcastMessage(colorize("&4[&8BorgCraft&4] &6Round " + r + " has begun! The walls have dropped. Humans have 30 seconds"));
    if(Main.Round1 == true) {
    for(int p = 1; p < Bukkit.getOnlinePlayers().length; p++) {
    PlayerListfile.set(PlayerListfile.getString("" + p + "crossedwall2"), false);
    PlayerListfile.set(PlayerListfile.getString("" + p + "crossedwall1"), false);
    }
    }
    if(Main.Round1 == false) {
    for(int p = 1; p < Bukkit.getOnlinePlayers().length; p++) {
    PlayerListfile.set(PlayerListfile.getString("" + p + "crossedwall1"), false);
    PlayerListfile.set(PlayerListfile.getString("" + p + "crossedwall2"), false);
    }
    }
    if(r/2 == 0) {
    Main.Round1 = false;
    Main.Round2 = true;
    } else {
    Main.Round2 = false;
    Main.Round1 = true;
    }
    }
    }, 20);
    }
    }, 20);
    //Here is the bottom of the walls sinking
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    File ClassList = new File("BorgPlug/" + "ClassList.yml");
    FileConfiguration ClassListfile = YamlConfiguration.loadConfiguration(ClassList);
    //This is the walls rising thing
    for(int w = 1; w < 90; w++) {
    File Wall1 = new File("BorgPlug/" + "Wall1.yml");
    FileConfiguration Wall1file = YamlConfiguration.loadConfiguration(Wall1);
    Location Wally1 = new Location(Bukkit.getWorld("BorgCraft"), Wall1file.getInt(w + ".x"), Wall1file.getInt(w + ".y"), Wall1file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Wally1).setType(Material.GLOWSTONE);
    File Wall2 = new File("BorgPlug/" + "Wall2.yml");
    FileConfiguration Wall2file = YamlConfiguration.loadConfiguration(Wall2);
    Location Walle1 = new Location(Bukkit.getWorld("BorgCraft"), Wall2file.getInt(w + ".x"), Wall2file.getInt(w + ".y"), Wall2file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Walle1).setType(Material.GLOWSTONE);
    }
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    for(int w = 1; w < 90; w++) {
    File Wall1 = new File("BorgPlug/" + "Wall1.yml");
    FileConfiguration Wall1file = YamlConfiguration.loadConfiguration(Wall1);
    Location Wally1 = new Location(Bukkit.getWorld("BorgCraft"), Wall1file.getInt(w + ".x"), Wall1file.getInt(w + ".y") + 1, Wall1file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Wally1).setType(Material.GLASS);
    File Wall2 = new File("BorgPlug/" + "Wall2.yml");
    FileConfiguration Wall2file = YamlConfiguration.loadConfiguration(Wall2);
    Location Walle1 = new Location(Bukkit.getWorld("BorgCraft"), Wall2file.getInt(w + ".x"), Wall2file.getInt(w + ".y") + 1, Wall2file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Walle1).setType(Material.GLASS);
    }
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    for(int w = 1; w < 90; w++) {
    File Wall1 = new File("BorgPlug/" + "Wall1.yml");
    FileConfiguration Wall1file = YamlConfiguration.loadConfiguration(Wall1);
    Location Wally1 = new Location(Bukkit.getWorld("BorgCraft"), Wall1file.getInt(w + ".x"), Wall1file.getInt(w + ".y") + 2, Wall1file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Wally1).setType(Material.GLOWSTONE);
    File Wall2 = new File("BorgPlug/" + "Wall2.yml");
    FileConfiguration Wall2file = YamlConfiguration.loadConfiguration(Wall2);
    Location Walle1 = new Location(Bukkit.getWorld("BorgCraft"), Wall2file.getInt(w + ".x"), Wall2file.getInt(w + ".y") + 2, Wall2file.getInt(w + ".z"));
    Bukkit.getWorld("BorgCraft").getBlockAt(Walle1).setType(Material.GLOWSTONE);
    }
    //Lets clear up any humans who didnt make it
    for (int i = 1; i <= Bukkit.getMaxPlayers(); i++) {
    File PlayerList = new File("BorgPlug/" + "PlayerList.yml");
    FileConfiguration PlayerListfile = YamlConfiguration.loadConfiguration(PlayerList);
    File ClassList = new File("BorgPlug/" + "ClassList.yml");
    FileConfiguration ClassListfile = YamlConfiguration.loadConfiguration(ClassList);
    String player = PlayerListfile.getString("" + i);
    if(Main.Round1 == true) {
    if(PlayerListfile.getBoolean(Bukkit.getPlayer(player) + ".crossedwall2") == false) {
    PlayerListfile.set(player + ".team", 1);
    //Gives lazy Borgs their random class after 30 seconds
    PlayerListfile.set("RoundLosses." + r, PlayerListfile.getInt("RoundLosses." + r) + 1);
    int ranclass = new Random().nextInt(ClassListfile.getInt("BorgClasses.Total")) + 1;
    totaldeaths++;
    Bukkit.getPlayer(player).sendMessage(colorize("&4[&8BorgCraft&4] &6You did not get across in time and have been assigned the " + ClassListfile.getString("HumanClasses.Classes." + ranclass + ".Name") + " class."));
    PlayerListfile.set(Bukkit.getPlayer(player).getName() + ".class", ranclass);
    try {
    PlayerListfile.save(PlayerList);
    } catch (IOException e) {
    e.printStackTrace();
    }
    //And teleporting the player to the start point
    Location BorgSpawn = new Location(Bukkit.getWorld("BorgCraft"), 152, 21, 196);
    Bukkit.getPlayer(player).teleport(BorgSpawn);
    }
    }
    if(Main.Round2 == true) {
    if(PlayerListfile.getBoolean(Bukkit.getPlayer(player) + ".crossedwall1") == false) {
    PlayerListfile.set(player + ".team", 1);
    //Gives lazy Borgs their random class after 30 seconds
    PlayerListfile.set("RoundLosses." + r, PlayerListfile.getInt("RoundLosses." + r) + 1);
    int ranclass = new Random().nextInt(ClassListfile.getInt("BorgClasses.Total")) + 1;
    totaldeaths++;
    Bukkit.getPlayer(player).sendMessage(colorize("&4[&8BorgCraft&4] &6You did not get across in time and have been assigned the " + ClassListfile.getString("HumanClasses.Classes." + ranclass + ".Name") + " Borg class."));
    PlayerListfile.set(Bukkit.getPlayer(player).getName() + ".class", ranclass);
    try {
    PlayerListfile.save(PlayerList);
    } catch (IOException e) {
    e.printStackTrace();
    }
    //And teleporting the player to the start point
    Location BorgSpawn = new Location(Bukkit.getWorld("BorgCraft"), 152, 21, 196);
    Bukkit.getPlayer(player).teleport(BorgSpawn);
    }
    }
    }
    File PlayerList = new File("BorgPlug/" + "PlayerList.yml");
    FileConfiguration PlayerListfile = YamlConfiguration.loadConfiguration(PlayerList);
    if(totaldeaths == 30) {
    Bukkit.broadcastMessage(colorize("&4[&8BorgCraft&4] &6The game is over!"));
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    //Kick everyone from the game
    }
    }, 100L);
    }
    Bukkit.broadcastMessage(colorize("&4[&8BorgCraft&4] &6Round " + r + " has ended! The walls have risen. " + PlayerListfile.getInt("RoundLosses." + r) + " humans died. Round " + (r + 1) + " starts in 30 seconds!"));
    r++;
    if(r/2 == 0) {
    Main.Round1 = false;
    Main.Round2 = true;
    } else {
    Main.Round2 = false;
    Main.Round1 = true;
    }
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    nxtrnd = true;
    }
    }, 600);
    }
    }, 20);
    }
    }, 20);
    //Here is the bottom of the walls rising
    }
     
    }, 600);
     
    }
    }, 100);
    }
    
    Basically, I want this ugly thing to repeat until nxtrnd = false;. My problem is, with the while(nxtrnd = true) the plugin crashes the server when it gets to that stage. And I know it's that bit because when I remove it it all works, although my code doesn't repeat.

    I know alot of the code will probably not be the best way of going about things, but i'm new to coding and this is only my 4th plugin. And it's for this reason that I probably can't figure this bit out. :) Oh, and you may want to paste all that into eclipse, it makes it much easier to read.

    Thanks,
     
  2. Offline

    Rprrr

    You want to loop with delay?
    Code:
            new BukkitRunnable(){
                List<String> list = //Some list.
                int count = 0;
                @Override
                public void run() {
                    if (count < list.size()){
                        list.get(count);
                        count++;                   
                    }
                    else {
                        cancel();
                    }
                }
            }.runTaskTimer(plugin, initial_delay_in_ticks, delay_in_ticks);
    I didn't read all of your code, just your title, so sorry if this is not what you're looking for.
     
  3. Offline

    TheChinski

    Yeah, I think so... xD

    Code gets confusing when it becomes too long :p Thank goodness for eclipse!
     
  4. Offline

    camyono

    Or if you just learning java ;)

    Btw: It's not a nice style to delay a for loop. Maybe you should think about your doing there.
    1. It's not sync
    2. It makes no sense
    3. Creates a new thread / costs performance
     
Thread Status:
Not open for further replies.

Share This Page