This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiaoshen
committed
May 17, 2020
1 parent
814b642
commit 6e801a6
Showing
6 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
PicqBotX-PluginManager.iml | ||
*.log | ||
target/* | ||
run/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/cc/moecraft/icq/pluginmanager/console/ConsoleCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/cc/moecraft/icq/pluginmanager/console/ConsoleCommandListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ")); | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/cc/moecraft/icq/pluginmanager/console/command/CommandStop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |