Server Console Cross-post

Sort of a cross-post from this, except oriented towards a server. Code has the same base, maybe more up to date.

Hi, lets not reinvent the wheel here when it comes to making a simple gui for Servers, especially when it is independent from the client (Not uncommon). It would be simplest if one could just tell the computer “use swing instead of console”, and then “if any of these are said, do xyz, otherwise do abc”. This is just that, except with two different levels. The lower level acts as close to System.out and a Scanner wrapping as is possible, and is shown in this pastebin.

The relevant code of the sort of things you could do with this look like:


/**
   * For testing purposes, shows an example usage of this program
   */
  public static void main(String[] args)
  {
    ConsoleGUI.toGUI();
    ConsoleGUI.setSaveText(true);
    TAScanner scanner = ConsoleGUI.getScanner();
    TAPrintStream out = ConsoleGUI.getStream();
    out.println("Type a string");
    String str = scanner.nextLine();
    out.println("Type an int");
    int i = scanner.nextInt();
    out.println("You typed "+str+" and "+i);
    out.println("Say something starting with y");
    boolean b = scanner.nextBoolean();
    out.println(b);
    out.println("Say something starting with n");
    b = scanner.nextBoolean("n", true);
    out.println(b);
    out.println("Say banana");
    b = scanner.nextBoolean("banana", false);
    out.println(b);
    
    out.println();
    RuntimeException exc = new RuntimeException();
    handleException("Test", exc, saveRun);
    //for(int i = 0; i < 50; i++) { out.println("Purposeful Spam"); }
  }

This has methods from ints to doubles, although throws exceptions if you ask for an int and get “asdf”. Intuitive and consistent, makes a console-based ASCII game easily converted into an executable jar, but besides that doesn’t seem like anything special. The sub-class however, will make the ‘gui’ part of your code be able to cleanly and nicely fit inside one class or several anonymous classes. That way, you can do more fun stuff, like that network code you “forgot” to do. Pastebin

This would let you do something like


public static void main(String[] args) {
  CommandListener listener = new CommandListener() {
    @Override
    public void onCommand(String nm, String[] params) {
      if(nm.equalsIgnoreCase("eatanapple") {
        String aniName = params[0];
        ConsoleGUI.getStream().println(aniName + " ate an apple!");
      }else if(..) {
        ...
      }
    }
  };
  ServerConsole.getGUI().registerCommand("eatanapple",
                                         "Makes an animal eat an apple. Example: eatanapple Bob", listener);
  ServerConsole.updateHelp(); // Called when you finish registering commands.
}

A few ‘special’ commands that it will have by default include:
default - If an unknown command is entered, same as help if not overriden
help - prints the list of commands. updateHelp() MUST be called after all registerCommands(…) to be up-to-date
memory - prints memory usage.

This is what typing memory might look like:


> memory
Allocated Memory / Max Memory: 259,522,560 (247.5 megabytes)
Total Memory: 16,318,464 (15.56 megabytes)
Free Memory: 11,736,760 (11.19 megabytes)
Used Memory: 4,581,704 (4.37 megabytes)

Thanks for sharing the code!