Timer and For loop

Discussion in 'Plugin Development' started by xCyanide, Jun 24, 2013.

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

    xCyanide

    What are for loops usually used for? And can someone give me an example and explain it? I have only used a for loop for looping through all the online players. Also, how would I make timer for example, when a like a survival games start(just an example) I want there to be a timer to see when the game ends. Like, at 15 minutes in the game it would broadcast 15 minutes remaining until deathmatch. When the timer reaches 0:00 then it will run the death match method
     
  2. Offline

    Rocoty

    This forum is for Bukkit development help, not basic Java help. There are plenty of tutorials about for-loops on the internet. Check out http://www.thenewboston.org/ for instance
     
  3. Offline

    Minnymin3

    for (i = 0; i < javaKnowledge; i++) {
    just.learn();
    }
     
  4. Offline

    xCyanide

    Rocoty
    This is plugin development, I need to know how to fully use a for loop so it can be useful when I make plugins :) and I know how to make a for loop it is just that I dont't understand how it works
     
  5. Offline

    jackwilsdon

    Well, there's something called an enhanced for loop, which can be done like this;
    Code:java
    1. for (Player player: Bukkit.getOnlinePlayers())
    2. {
    3. player.sendMessage("Huh, it looks like you're online!");
    4. }

    This can also be done with the traditional for loop like so;
    Code:java
    1. Player[] players = Bukkit.getOnlinePlayers();
    2. for (int i = 0; i < players.length; i++)
    3. {
    4. players.sendMessage("Huh, it looks like you're online!");
    5. }


    For the timer, it would work something like this;
    Code:java
    1. public void startTimer(Plugin plugin)
    2. {
    3. GameTimer timer = new GameTimer();
    4. Bukkit.getServer().getScheduler().runTaskTimer(plugin, timer, 20, 20); // 20 ticks is 1 second
    5. }
    6.  
    7. private class GameTimer implements Runnable
    8. {
    9. int remainingTime = 30; // Seconds
    10.  
    11. @Override
    12. public void run() {
    13. if ((remainingTime % 10) == 0) // Print every 10 seconds
    14. {
    15. Bukkit.getServer().broadcastMessage(remainingTime+" seconds remaining");
    16. }
    17. remainingTime -= 1;
    18. }
     
  6. Offline

    xCyanide

    jackwilsdon
    I know how to use the enhanced for loop it is the easiest for me to understand, but can you explain a little about the traditional for loop like how it works? I know how to setup a traditional for loop I am just confused about how it works
     
  7. Offline

    joehot2000

    for (int i; i<50, i++);

    For example, if i wanted to get all blocks in a chunk:
    Code:
    for(int xx = bx; xx < bx+16; xx++) {
    for(int zz = bz; zz < bz+16; zz++) {
    for(int yy = 0; yy < 128; yy++) {
    int typeId = world.getBlockTypeIdAt(xx, yy, zz);
    if(typeId == 1) {
    world.getBlockAt(xx,yy,zz).setType(Material.TNT);
    }
    }
    }
    }
    
    So, it gets the block at the X Y and Z locations, by getting the value of XX, YY, and ZZ, which are being added by 1 every time.
     
  8. Offline

    jackwilsdon

    So, let's use this simple traditional for loop
    Code:java
    1. Player[] players = Bukkit.getOnlinePlayers();
    2. for (int i = 0; i < players.length; i++)
    3. {
    4. [I]players.sendMessage("Hello, player!");[/I]
    5. [I]}[/I]

    The first line creates an array of players that are online. An array is a list of things that can be referenced individually.
    So, now on to the for loop. First of all, we set the integer i to 0. Zero is always the beginning of an array, so we start off with that. Next, inside the for loop we check if the integer i is less than the length of players. If this is false, we run the contents of the loop, and continue to add 1 to i. We keep doing this until i becomes equal to the length, which means i has been every value possible for array. Hope that didn't confuse you, it confused me :p

    Basically, every time the loop runs, it checks if i (the array index) is equal to the length of the array, and if it is, the loop is done. It goes like this;
    1. i = 0
    2. i = 1
    3. i = 2
    And so on up until the end of the array. We then use this to access different parts of the array with players.
     
    joehot2000 likes this.
  9. Offline

    joehot2000

    Just want to point out that though that for loop worked, in this situation it would be easier to do
    Code:
    for (Player p : bukkit.getOnlinePlayers(){ p.sendMessage("Hello, player!);}
    I think its a thing of knowing when to use the traditional version of a for loop, and when using the newer version would be quicker and more efficient.

    (Btw nice demonstration there @jackwilsdon i could never have explained as well)
     
Thread Status:
Not open for further replies.

Share This Page