From 7caa5e2644580840c14dabcb8caad32122957a0a Mon Sep 17 00:00:00 2001 From: Maxopoly Date: Mon, 19 Nov 2018 23:42:22 +0100 Subject: [PATCH] Allow running command on startup --- .../java/com/github/maxopoly/angelia_cmd/Main.java | 4 +++- .../maxopoly/angelia_cmd/StartUpCommandParser.java | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/maxopoly/angelia_cmd/Main.java b/src/main/java/com/github/maxopoly/angelia_cmd/Main.java index 4169a99..72d0abf 100644 --- a/src/main/java/com/github/maxopoly/angelia_cmd/Main.java +++ b/src/main/java/com/github/maxopoly/angelia_cmd/Main.java @@ -15,7 +15,8 @@ public class Main { public static void main(String[] args) { connManager = ActiveConnectionManager.getInstance(); - ServerConnection connection = StartUpCommandParser.parse(args, logger); + StartUpCommandParser parser = new StartUpCommandParser(); + ServerConnection connection = parser.parse(args, logger); if (connection == null) { System.exit(0); return; @@ -26,6 +27,7 @@ public static void main(String[] args) { cmdHandler = new CommandHandler(logger); CommandLineReader reader = new CommandLineReader(logger, connManager, connection.getPlayerName(), cmdHandler); reader.start(); + cmdHandler.handle(parser.getCmdToRun(), connection); } public static CommandHandler getCommandHandler() { diff --git a/src/main/java/com/github/maxopoly/angelia_cmd/StartUpCommandParser.java b/src/main/java/com/github/maxopoly/angelia_cmd/StartUpCommandParser.java index 77674af..6bd5b46 100644 --- a/src/main/java/com/github/maxopoly/angelia_cmd/StartUpCommandParser.java +++ b/src/main/java/com/github/maxopoly/angelia_cmd/StartUpCommandParser.java @@ -15,7 +15,8 @@ public class StartUpCommandParser { private static Options options; - + private String cmdToRun; + static { options = new Options(); options.addOption("user", true, "Username"); @@ -23,14 +24,18 @@ public class StartUpCommandParser { options.addOption("ip", true, "Server IP"); options.addOption("port", true, "Server port"); options.addOption("saveFile", true, "Auth token save file"); + options.addOption("cmd", true, "Command to run once client is fully connected"); } - public static ServerConnection parse(String[] args, Logger logger) { + public ServerConnection parse(String[] args, Logger logger) { CommandLineParser parser = new DefaultParser(); Console c = System.console(); try { CommandLine cmd = parser.parse(options, args); File saveFile = null; + if (cmd.hasOption("cmd")) { + cmdToRun = cmd.getOptionValue("cmd"); + } if (cmd.hasOption("saveFile")) { saveFile = new File(cmd.getOptionValue("saveFile")); } @@ -101,4 +106,8 @@ public static ServerConnection parse(String[] args, Logger logger) { return null; } } + + public String getCmdToRun() { + return cmdToRun; + } }