From 9b220ec47c3abd36778406a8414a2e963a537f3b Mon Sep 17 00:00:00 2001 From: Damion Date: Sun, 16 Feb 2020 05:47:56 +1100 Subject: [PATCH] Implemented Command Processor (#375) * Added CommandConsole and CommandProcessor Interface * Added two commands, ListPlayers and Stop * Registered commands with CommandConsole * Added request for console from main. Made help prompt not print after requesting help... * Moved some files around You didn't see anything * Bug fix * swap `break;` for `continue;` * *cough* bug fix --- .../main/java/com/rebotted/GameEngine.java | 3 + .../com/rebotted/console/CommandConsole.java | 70 +++++++++++++++++++ .../rebotted/console/CommandProcessor.java | 13 ++++ .../console/commands/ListPlayers.java | 31 ++++++++ .../com/rebotted/console/commands/Stop.java | 28 ++++++++ 5 files changed, 145 insertions(+) create mode 100644 2006Redone Server/src/main/java/com/rebotted/console/CommandConsole.java create mode 100644 2006Redone Server/src/main/java/com/rebotted/console/CommandProcessor.java create mode 100644 2006Redone Server/src/main/java/com/rebotted/console/commands/ListPlayers.java create mode 100644 2006Redone Server/src/main/java/com/rebotted/console/commands/Stop.java diff --git a/2006Redone Server/src/main/java/com/rebotted/GameEngine.java b/2006Redone Server/src/main/java/com/rebotted/GameEngine.java index ca6fa259..1edee9a5 100644 --- a/2006Redone Server/src/main/java/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/main/java/com/rebotted/GameEngine.java @@ -13,6 +13,7 @@ import org.apache.mina.common.IoAcceptor; import org.apache.mina.transport.socket.nio.SocketAcceptor; import org.apache.mina.transport.socket.nio.SocketAcceptorConfig; +import com.rebotted.console.CommandConsole; import com.rebotted.event.CycleEventHandler; import com.rebotted.game.content.minigames.FightCaves; import com.rebotted.game.content.minigames.FightPits; @@ -213,6 +214,8 @@ public class GameEngine { } }, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS); + CommandConsole.getInstance(); + try { while (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) { // TODO diff --git a/2006Redone Server/src/main/java/com/rebotted/console/CommandConsole.java b/2006Redone Server/src/main/java/com/rebotted/console/CommandConsole.java new file mode 100644 index 00000000..f0873f70 --- /dev/null +++ b/2006Redone Server/src/main/java/com/rebotted/console/CommandConsole.java @@ -0,0 +1,70 @@ +package com.rebotted.console; + +import java.util.ArrayList; +import java.util.NoSuchElementException; +import java.util.Scanner; + +import com.rebotted.GameConstants; +import com.rebotted.console.commands.ListPlayers; +import com.rebotted.console.commands.Stop; + +/** + * + * @author RS-Emulators + * + */ +public class CommandConsole implements Runnable { + + private static CommandConsole cc; + private Thread self; + private Scanner scanner; + // Lazy, not intending to add runtime class loading. + private ArrayList cmds = new ArrayList(); + + private CommandConsole() { + cmds.add(new ListPlayers()); + cmds.add(new Stop()); + + scanner = new Scanner(System.in); + cc = this; + self = new Thread(cc); + self.start(); + } + + @Override + public void run() { + System.out.println("Welcome to " + GameConstants.SERVER_NAME + "."); + while (true) { + System.out.print("> "); + String input = null; + try { + input = scanner.nextLine(); + String[] splited = input.split("\\s+"); + if (splited[0].isEmpty()) { + System.out.println("Command not recognized. Try 'help'."); + continue; + } + if (splited[0].equalsIgnoreCase("help")) { + for (CommandProcessor cmd : cmds) { + System.out.println(cmd.help()); + } + continue; + } + for (CommandProcessor cmd : cmds) { + if (cmd.command(splited)) { + break; + } + } + } catch (NoSuchElementException | NullPointerException e) { + } + } + } + + public static CommandConsole getInstance() { + if (cc == null) { + cc = new CommandConsole(); + } + return cc; + } + +} diff --git a/2006Redone Server/src/main/java/com/rebotted/console/CommandProcessor.java b/2006Redone Server/src/main/java/com/rebotted/console/CommandProcessor.java new file mode 100644 index 00000000..23f32e16 --- /dev/null +++ b/2006Redone Server/src/main/java/com/rebotted/console/CommandProcessor.java @@ -0,0 +1,13 @@ +package com.rebotted.console; + +/** + * + * @author RS-Emulators + * + */ +public interface CommandProcessor { + + public boolean command(String[] cmd); + + public String help(); +} diff --git a/2006Redone Server/src/main/java/com/rebotted/console/commands/ListPlayers.java b/2006Redone Server/src/main/java/com/rebotted/console/commands/ListPlayers.java new file mode 100644 index 00000000..269cc56c --- /dev/null +++ b/2006Redone Server/src/main/java/com/rebotted/console/commands/ListPlayers.java @@ -0,0 +1,31 @@ +package com.rebotted.console.commands; + +import com.rebotted.console.CommandProcessor; +import com.rebotted.game.players.Player; +import com.rebotted.game.players.PlayerHandler; + +/** + * + * @author RS-Emulators + * + */ +public class ListPlayers implements CommandProcessor { + + @Override + public boolean command(String[] cmd) { + if (!cmd[0].equalsIgnoreCase("list")) { + return false; + } + System.out.println("Currently online: " + PlayerHandler.getPlayerCount()); + for (Player p : PlayerHandler.players) { + System.out.print(p.playerName + ","); + } + return true; + } + + @Override + public String help() { + return "list - lists online players."; + } + +} diff --git a/2006Redone Server/src/main/java/com/rebotted/console/commands/Stop.java b/2006Redone Server/src/main/java/com/rebotted/console/commands/Stop.java new file mode 100644 index 00000000..33eec3b2 --- /dev/null +++ b/2006Redone Server/src/main/java/com/rebotted/console/commands/Stop.java @@ -0,0 +1,28 @@ +package com.rebotted.console.commands; + +import com.rebotted.GameEngine; +import com.rebotted.console.CommandProcessor; + +/** + * + * @author RS-Emulators + * + */ +public class Stop implements CommandProcessor { + + @Override + public boolean command(String[] cmd) { + if (!cmd[0].equalsIgnoreCase("stop")) { + return false; + } + System.out.println("Setting shutdown flag to true..."); + GameEngine.shutdownServer = true; + return true; + } + + @Override + public String help() { + return "stop - sets GameEngine.shutdownServer to true."; + } + +}