package de.gurkenlabs.litiengine.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConsoleCommandListener extends Thread implements ICommandListener {
  private static final Logger log = Logger.getLogger(ConsoleCommandListener.class.getName());

  private final List<ICommandManager> commandManagers;

  public ConsoleCommandListener(final ICommandManager... commandManagers) {
    this.commandManagers = new ArrayList<>();

    if (commandManagers != null && commandManagers.length > 0) {
      for (final ICommandManager manager : commandManagers) {
        if (!this.commandManagers.contains(manager)) {
          this.commandManagers.add(manager);
        }
      }
    }
  }

  @Override
  public void register(final ICommandManager manager) {
    if (!this.commandManagers.contains(manager)) {
      this.commandManagers.add(manager);
    }
  }

  @Override
  public void run() {
    while (!interrupted()) {

      final BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
      String s;
      try {
        s = bufferRead.readLine();

        this.commandManagers.forEach(manager -> manager.executeCommand(s));

      } catch (final IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
      }

      try {
        Thread.sleep(500);
      } catch (final InterruptedException e) {
        break;
      }
    }
  }

  @Override
  public void terminate() {
    interrupt();
  }
}
@@ -39,104 +39,8 @@
 %0D%0A%0D%0A
-import java.io.BufferedReader;%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.InputStreamReader;%0D%0A
 impo
@@ -109,53 +109,13 @@
 til.
-logging.Level;%0D%0Aimport java.util.logging.Logg
+Scann
 er;%0D
@@ -203,105 +203,8 @@
  %7B%0D%0A
-  private static final Logger log = Logger.getLogger(ConsoleCommandListener.class.getName());%0D%0A%0D%0A
   pr
@@ -330,24 +330,51 @@
 anagers) %7B%0D%0A
+    this.setDaemon(true);%0D%0A
     this.com
@@ -897,114 +897,41 @@
     
-while (!interrupted()) %7B%0D%0A%0D%0A      final BufferedReader bufferRead = new BufferedReader(new InputStreamRead
+final Scanner scanner = new Scann
 er(S
@@ -943,35 +943,47 @@
 .in)
-)
 ;%0D%0A    
-  String s;
+while (!interrupted()) %7B
 %0D%0A      
 try 
@@ -974,33 +974,35 @@
 )) %7B%0D%0A      
+S
 tr
-y %7B%0D%0A  
+ing s;%0D%0A
       s = bu
@@ -1003,23 +1003,20 @@
 s = 
-bufferRead.read
+scanner.next
 Line
@@ -1018,26 +1018,24 @@
 tLine();%0D%0A%0D%0A
-  
       this.c
@@ -1101,230 +1101,35 @@
 );%0D%0A
-%0D%0A  
     %7D
- catch (final IOException e) %7B%0D%0A        log.log(Level.SEVERE, e.getMessage(), e);%0D%0A      %7D%0D%0A%0D%0A      try %7B%0D%0A        Thread.sleep(500);%0D%0A      %7D catch (final InterruptedException e) %7B%0D%0A        break;%0D%0A      %7D%0D%0A    %7D
+%0D%0A    scanner.close();
 %0D%0A  

Improve this implementation