Creating a loop for reading commands

Discussion in 'Plugin Development' started by Whalum, Mar 7, 2011.

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

    Whalum

    In Bukkit, is it possible to create a loop that reads commands like the example below?

    Code:
    Scanner in = new Scanner(System.in);
    boolean quit = false;
    String command;
    
    while(!quit) {
    
       command = in.nextLine();
    
       if (command.equals("hit"))
          this.getCard();
       else if (command.equals("quit"))
          quit = true;
    
    }
    
     
  2. Offline

    Nohup

    Maybe I am not following what you are asking, but wouldn't this just be the onCommand method in your plugin?
     
  3. Offline

    Whalum

    Let's say I have a command "/play", which calls a method called "play()". And the loop is supposed to be inside that method. I haven't yet tried to create the loop in the onCommand method, 'cause I think it will just result in an infinite loop.
     
  4. Offline

    bekvon

    You need to do your loop in another thread.
    Code:
    Thread loopThread = new Thread(new Runnable() {
         public void run() {
             //do stuff in loop here
         }
    });
    loopThread.start();
     
  5. Offline

    Plague

    That's a bad architecture. Use the commands in onCommand, set variables there and the loop will read those variables and behave on their values.
     
  6. Offline

    Nohup

    yah I wasn't understanding why he was trying to loop on console input, didn't seem right. So basically you are asking for what is mentioned above, the ability to kick off a loop with a single console command, loop while it is active and then stop with another command. Is that correct?
     
  7. Offline

    Whalum

    Ah, I thought the example code would help, but it seems to cause more confusion. Sorry about that...

    But yes, I'm trying to start a loop with a command from a player in-game and stop it with another command. And in between the start and the stop, the loop has to respond to other commands the player gives.
     
  8. Offline

    Plague

    Then again, let commands be used like they should and only run the loop in an independent thread and the commands will then only stop the loop externally.
     
  9. Offline

    Nohup

    so what will the loop do other than listen for the next commands? It almost sounds like you are trying to do a custom listener
     
  10. Offline

    Whalum

    Sorry, I must have missed your first reply. But anyways, it worked, thank you! And nice job on deciphering my vague question... [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page