Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
Add Console Command
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshen committed May 17, 2020
1 parent 814b642 commit 6e801a6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 10 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
PicqBotX-PluginManager.iml
*.log
target/*
run/*
25 changes: 15 additions & 10 deletions src/main/java/cc/moecraft/icq/pluginmanager/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import cc.moecraft.icq.PicqBotX;
import cc.moecraft.icq.PicqConfig;
import cc.moecraft.icq.accounts.BotAccount;
import cc.moecraft.icq.pluginmanager.console.ConsoleCommandListener;
import cc.moecraft.icq.pluginmanager.console.command.CommandStop;
import cc.moecraft.icq.pluginmanager.plugin.PluginManager;
import cc.moecraft.logger.HyLogger;
import cc.moecraft.logger.LoggerInstanceManager;
import cc.moecraft.logger.environments.ColorSupportLevel;
import cc.moecraft.logger.format.AnsiColor;
import cc.moecraft.utils.FileUtils;
import lombok.Getter;
Expand All @@ -16,6 +17,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

/**
* 此类由 Hykilpikonna 在 2018/06/21 创建!
Expand Down Expand Up @@ -48,20 +50,17 @@ public class Launcher
@Getter
private static boolean debug;

@Getter
private static ConsoleCommandListener consoleCommand;

public static void main(String[] args) throws Exception
{
//Console
initializeConsoleCommandManager();

initializeConfig();

debug = config.getBoolean("LoggerSettings.Debug");
/*
bot = new PicqBotX(
config.getInt("ConnectionSettings.ListeningPort"),
debug, ColorSupportLevel.valueOf(config.getString("LoggerSettings.ColorSupportLevel")),
config.getString("LoggerSettings.LogFileRelativePath"),
config.getString("LoggerSettings.LogFileName"));
*/


PicqConfig botConfig = new PicqConfig(config.getInt("ConnectionSettings.ListeningPort"));
botConfig.setUseAsyncCommands(config.getBoolean("CommandSettings.Async", true));
Expand Down Expand Up @@ -149,4 +148,10 @@ private static void initializePlugins(PicqBotX bot)
logger.timing.clear();
}

private static void initializeConsoleCommandManager() {
consoleCommand = new ConsoleCommandListener(new Scanner(System.in));
consoleCommand.addCommand("stop", new CommandStop());
consoleCommand.listenInNewThread();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cc.moecraft.icq.pluginmanager.console;

public interface ConsoleCommand {
void onCommand(String[] args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cc.moecraft.icq.pluginmanager.console;

import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class ConsoleCommandListener {
HashMap<String, ConsoleCommand> answers = new HashMap<>();
Scanner scanner;


public void addCommand(String cmd, ConsoleCommand command) {
answers.put(cmd.toLowerCase(), command);
}


public ConsoleCommandListener(Scanner scanner) {
this.scanner = scanner;

if (scanner == null) {
throw new NullPointerException("Null");
}
}

public void removeCommand(String cmd, ConsoleCommand command) {
answers.remove(cmd, command);
}

public ConsoleCommand replaceCommand(String cmd, ConsoleCommand command) {
return answers.replace(cmd, command);
}

public void listenInNewThread() {
Thread t = new Thread() {
public void run() {
listen();
}
};
t.start();
}


public void listen() {
while (true) {
String line;
try {
line = scanner.nextLine();
} catch (NoSuchElementException ignored) {
line = "";
}

String input = line.replaceAll("[\\s]+", " ");

String[] args = input.split(" ");
String cmd = args[0];

ConsoleCommand command = answers.get(cmd.toLowerCase());
if (command != null) {
command.onCommand(input.replaceFirst(cmd + " ", "").split(" "));
}

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cc.moecraft.icq.pluginmanager.console.command;

import cc.moecraft.icq.pluginmanager.Launcher;
import cc.moecraft.icq.pluginmanager.console.ConsoleCommand;

public class CommandStop implements ConsoleCommand {
@Override
public void onCommand(String[] args) {
Launcher.logger.warning("退出 PicqBotX 中。。。。。");
System.exit(0);
}
}

0 comments on commit 6e801a6

Please sign in to comment.