Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CommandInterpreter {
- private Client client;
- private DirectoryManager dirMan;
- private DirectoryChanger dirChange;
- private Thread iThread;
- public CommandInterpreter(final Client client) {
- this.client = client;
- this.startInterpreter();
- }
- private void startInterpreter() {
- this.iThread = new Thread(){
- @Override
- public void run() {
- CommandManager CM = new CommandManager();
- CM.registerStore(new CmdStore());
- System.out.println("Remote File Access Terminal, Version 1.0a. Copyright © 2015 JuNK Software. All rights reserved.");
- while(true) {
- Scanner scan = new Scanner(System.in);
- System.out.print(""/*Current Directory Prompt (EX: ~/bin># _ )*/); // Execution Halts here until we receive a command.
- String input = scan.nextLine();
- String[] args = input.split("\\s+");
- ParseResult pResult = this.CM.parseCommand(args);
- // If the Parser returned a Command
- if (pResult.getResult() == ResultType.SUCCESS) {
- /*
- * A new command instance must be created when a command is called,
- * so as to prevent static commands from being served to players.
- * This prevents any form of data swappage between two different
- * players executing the same command at the same time.
- */
- // New Command Instance.
- Command cmd = null;
- // The dirty dynamic hack :D
- try {
- // Fetch Class Type
- Class<?> clazz = Class.forName(
- pResult.getCommand().getClass().getName());
- // Create the Constructor for the Command class. This works
- // only because all commands extend Command, which requires
- // these three arguments to be constructed.
- Constructor<?> ctor = clazz.getConstructor(String[].class, CommandType.class);
- // Create the specified Command Instance
- cmd = (Command) ctor.newInstance(
- pResult.getCommand().getCallArgs(),
- pResult.getCommand().getType());
- } catch (Exception e) {
- e.printStackTrace();
- }
- // If Player is calling this command, check Permissions and execute
- if (cmd != null) {
- cmd.onExecute(args); // *** Execution halts here until Command has finished executing ***
- } else {
- System.out.println("An error occurred while generating a dynamic instance of the requested command.");
- }
- // If the Parser returned a Command, but the Argument count was bad
- } else if (pResult.getResult() == ResultType.BAD_NUM_ARGS) {
- System.out.println("An invalid number of arguments was specified.");
- // If the Parser did not return a Command
- } else if (pResult.getResult() == ResultType.FAIL) {
- System.out.println("Command specified does not exist.");
- }
- }
- }
- };
- this.iThread.start();
- }
- }
- public class Command {
- private String name, syntax, help;
- private String[] callArgs;
- private CommandType type;
- public Command(String[] callArgs, CommandType type) {
- this.callArgs = callArgs;
- this.type = type;
- }
- public void onExecute(String[] args) {
- throw new UnsupportedOperationException(
- "Base class does not implement.");
- }
- public final CommandType getType() {
- return this.type;
- }
- /**
- * Returns the Name of this Command.
- *
- * @return String
- */
- public final String getName() {
- return this.name;
- }
- /**
- * Sets the Display Name for this Command.</br>
- * <p/>
- * </br>The name can be any string.
- *
- * @param cmdName
- */
- public final void setName(String cmdName) {
- this.name = cmdName;
- }
- /**
- * Returns the Syntax of this Command, minus the root.
- *
- * @return String
- */
- public final String getSyntax() {
- return this.syntax;
- }
- /**
- * Sets the syntax for this Command.</br>
- * <p/>
- * </br>When set, the string passed should contain the literal (sic) syntax
- * to be used when calling your command, <strong>minus the root command
- * </strong>. The syntax string is generally used for displaying help
- * information and should be relatively easy to understand.</br>
- * <p/>
- * </br>Ex: <em>ban {@literal <}player{@literal >} [message]</em>
- *
- * @param cmdSyntax
- */
- public final void setSyntax(String cmdSyntax) {
- this.syntax = cmdSyntax;
- }
- /**
- * Returns the Help Description text for this Command.
- *
- * @return String
- */
- public final String getHelp() {
- return this.help;
- }
- /**
- * Sets the Help Description text for this command.</br>
- * <p/>
- * </br>This text is displayed in the Help Command and is used to describe
- * what this Command does, as well as any other information the end user may
- * need to know.
- *
- * @param helpDesc
- */
- public final void setHelp(String helpDesc) {
- this.help = helpDesc;
- }
- /**
- * Returns a String[] containing all the arguments required to be typed by
- * the end user for this Command to be identified by the Command Manager.
- *
- * @return String[]
- */
- public final String[] getCallArgs() {
- return this.callArgs;
- }
- }
- public class CmdListCommand extends Command {
- public ListCommand(String[] callArgs, CommandType type) {
- super(callArgs, type);
- setName("List Directory");
- setHelp("Displays a list of files and folders for the current directory. \n'/W' switch will display contents in multiple columns, '/L' switch \nwill list files in a single column.");
- setSyntax("dir [/W || /L]");
- }
- @Override
- public void onExecute(String[] args) {
- }
- }
- public class CmdChangeDirCommand extends Command {
- public ListCommand(String[] callArgs, CommandType type) {
- super(callArgs, type);
- setHelp("Change Directory");
- setHelp("Changes your current operating directory. Arguments accepted are either a case sensitive file name (for Unix support) or the UP operator \"..\".");
- setSyntax("cd <dir_name || ..>");
- }
- @Override
- public void onExecute(String[] args) {
- }
- }
- public enum CommandType {
- STATIC,
- VARIABLE,
- DYNAMIC;
- }
- public class CommandManager {
- private ArrayList<Command> commands = new ArrayList<Command>();
- private boolean debugMode = false;
- public final void registerStore(CommandStore cmdStore) {
- this.commands = cmdStore.getCommands();
- }
- public final boolean registerCommand(Command cmd) {
- if (this.commands.add(cmd)) {
- return true;
- } else {
- System.out.println("Failed to register command. Perhaps it is already registered?");
- return false;
- }
- }
- public final Command getCommand(String identifier) {
- for (Command cmd : this.commands) {
- if (cmd.getName().equalsIgnoreCase(identifier)) return cmd;
- }
- return null;
- }
- public final ArrayList<Command> getAllCommands() {
- return this.commands;
- }
- public final ParseResult parseCommand(String[] args) {
- for (Command cmd : this.commands) {
- Parser parser;
- if (cmd.getType() == CommandType.STATIC) {
- parser = new StaticParser(this, cmd, args);
- } else if (cmd.getType() == CommandType.VARIABLE) {
- parser = new VariableParser(this, cmd, args);
- } else if (cmd.getType() == CommandType.DYNAMIC) {
- parser = new DynamicParser(this, cmd, args);
- } else {
- parser = new StaticParser(this, cmd, args);
- }
- ComparisonResult result = parser.parseCommand();
- if (result.equals(ComparisonResult.GOOD)) {
- return new ParseResult(ResultType.SUCCESS, cmd);
- } else if (result.equals(ComparisonResult.ARG_ERR)) {
- return new ParseResult(ResultType.BAD_NUM_ARGS, cmd);
- }
- }
- return new ParseResult(ResultType.FAIL, null);
- }
- public void setDebugMode(boolean flag) {
- this.debugMode = flag;
- }
- public boolean debugMode() {
- return this.debugMode;
- }
- }
- public class CommandStore extends Store {
- private ArrayList<Command> commands;
- public CommandStore() {
- this.commands = new ArrayList<Command>();
- initializeStore();
- }
- public void initializeStore() {
- throw new UnsupportedOperationException("Base class does not implement.");
- }
- public final void add(Command cmd) {
- this.commands.add(cmd);
- }
- public final ArrayList<Command> getCommands() {
- return this.commands;
- }
- }
- public class CmdStore extends CommandStore {
- public CmdStore() {
- super();
- }
- @Override
- public void initializeStore() {
- // CD Command
- this.add(
- new CmdChangeDirCommand(new String[]{"cd", "VAR_ARG"},
- CommandType.DYNAMIC));
- // DIR Command
- this.add(
- new CmdListCommand(new String[]{"dir", "VAR_ARG_OPT"},
- CommandType.VARIABLE));
- }
- }
- public enum ComparisonResult {
- GOOD,
- ARG_ERR,
- BAD;
- }
- public class ParseResult {
- private final Command cmd;
- private final ResultType type;
- public ParseResult(final ResultType type, final Command cmd) {
- this.type = type;
- this.cmd = cmd;
- }
- public final ResultType getResult() {
- return this.type;
- }
- public final Command getCommand() {
- return this.cmd;
- }
- }
- public enum ResultType {
- SUCCESS,
- BAD_NUM_ARGS,
- FAIL;
- }
- public class Parser {
- public CommandManager cmdManager;
- public Command cmd;
- public String[] input;
- public Parser(CommandManager mngr, Command cmd, String[] input) {
- this.cmdManager = mngr;
- this.cmd = cmd;
- this.input = input;
- }
- public ComparisonResult parseCommand(){
- throw new UnsupportedOperationException(
- "The parse instructions for this parser have not been specified!");
- }
- }
- // PARSER CLASSES
- public class DynamicParser extends Parser {
- public DynamicParser(CommandManager mngr, Command cmd, String[] args) {
- super(mngr, cmd, args);
- }
- @Override
- public ComparisonResult parseCommand() {
- String[] COMMAND_ARGS = new String[this.cmd.getCallArgs().length];
- String[] INPUT_ARGS = new String[this.cmd.getCallArgs().length];
- for (int i = 0; i < this.cmd.getCallArgs().length; i++) {
- COMMAND_ARGS[i] = this.cmd.getCallArgs()[i];
- }
- for (int i = 0; i < this.cmd.getCallArgs().length; i++) {
- if (i >= this.input.length) {
- INPUT_ARGS[i] = "null";
- } else {
- INPUT_ARGS[i] = this.input[i];
- }
- }
- // Check for Matching Arguments
- for (int i = 0; i < COMMAND_ARGS.length; i++) {
- // Debug Output
- if (this.cmdManager.debugMode()) {
- System.out.println("Command Expected: " + COMMAND_ARGS[i]);
- System.out.println("Received: " + INPUT_ARGS[i]);
- }
- if (COMMAND_ARGS[i].equals("VAR_ARG_OPT")) continue;
- if (COMMAND_ARGS[i].equals("VAR_ARG") &&
- INPUT_ARGS[i].equals("null")) {
- return ComparisonResult.ARG_ERR;
- } else if (COMMAND_ARGS[i].equals("VAR_ARG")) {
- continue;
- }
- if (!INPUT_ARGS[i].equalsIgnoreCase(COMMAND_ARGS[i])) {
- return ComparisonResult.BAD;
- }
- }
- return ComparisonResult.GOOD;
- }
- }
- public class StaticParser extends Parser {
- public StaticParser(CommandManager mngr, Command cmd, String[] args) {
- super(mngr, cmd, args);
- }
- @Override
- public ComparisonResult parseCommand() {
- int callSize = this.cmd.getCallArgs().length;
- int inputSize = this.input.length;
- String[] COMMAND_ARGS;
- String[] INPUT_ARGS;
- if (callSize > inputSize) {
- COMMAND_ARGS = new String[callSize];
- INPUT_ARGS = new String[callSize];
- } else {
- COMMAND_ARGS = new String[inputSize];
- INPUT_ARGS = new String[inputSize];
- }
- for (int i = 0; i < this.cmd.getCallArgs().length; i++) {
- COMMAND_ARGS[i] = this.cmd.getCallArgs()[i];
- }
- for (int i = 0; i < this.input.length; i++) {
- INPUT_ARGS[i] = this.input[i];
- }
- if (callSize > inputSize) {
- for (int i = inputSize; i < INPUT_ARGS.length; i++) {
- INPUT_ARGS[i] = "null";
- }
- } else {
- for (int i = callSize; i < COMMAND_ARGS.length; i++) {
- COMMAND_ARGS[i] = "null";
- }
- }
- // Check for Matching Arguments
- for (int i = 0; i < COMMAND_ARGS.length; i++) {
- // Debug Output
- if (this.cmdManager.debugMode()) {
- System.out.println("Command Expected: " + COMMAND_ARGS[i]);
- System.out.println("Received: " + INPUT_ARGS[i]);
- }
- if (COMMAND_ARGS[i].equals("null")) {
- return ComparisonResult.ARG_ERR;
- }
- if (!INPUT_ARGS[i].equalsIgnoreCase(COMMAND_ARGS[i])) {
- return ComparisonResult.BAD;
- }
- }
- return ComparisonResult.GOOD;
- }
- }
- public class VariableParser extends Parser {
- public VariableParser(CommandManager mngr, Command cmd, String[] args) {
- super(mngr, cmd, args);
- }
- @Override
- public ComparisonResult parseCommand() {
- int callSize = this.cmd.getCallArgs().length;
- int inputSize = this.input.length;
- String[] COMMAND_ARGS;
- String[] INPUT_ARGS;
- if (callSize > inputSize) {
- COMMAND_ARGS = new String[callSize];
- INPUT_ARGS = new String[callSize];
- } else {
- COMMAND_ARGS = new String[inputSize];
- INPUT_ARGS = new String[inputSize];
- }
- for (int i = 0; i < this.cmd.getCallArgs().length; i++) {
- COMMAND_ARGS[i] = this.cmd.getCallArgs()[i];
- }
- for (int i = 0; i < this.input.length; i++) {
- INPUT_ARGS[i] = this.input[i];
- }
- if (callSize > inputSize) {
- for (int i = inputSize; i < INPUT_ARGS.length; i++) {
- INPUT_ARGS[i] = "null";
- }
- } else {
- for (int i = callSize; i < COMMAND_ARGS.length; i++) {
- COMMAND_ARGS[i] = "null";
- }
- }
- for (int i = 0; i < COMMAND_ARGS.length; i++) {
- // Debug Output
- if (this.cmdManager.debugMode()) {
- System.out.println("Command Expected: " + COMMAND_ARGS[i]);
- System.out.println("Received: " + INPUT_ARGS[i]);
- }
- if (COMMAND_ARGS[i].equals("VAR_ARG_OPT")) continue;
- if (COMMAND_ARGS[i].equals("VAR_ARG") &&
- INPUT_ARGS[i].equals("null")) {
- return ComparisonResult.ARG_ERR;
- } else if (COMMAND_ARGS[i].equals("VAR_ARG")) {
- continue;
- }
- if (COMMAND_ARGS[i].equals("null")) {
- return ComparisonResult.ARG_ERR;
- }
- if (!INPUT_ARGS[i].equalsIgnoreCase(COMMAND_ARGS[i])) {
- return ComparisonResult.BAD;
- }
- }
- return ComparisonResult.GOOD;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement