This repository has been archived by the owner on Feb 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
5 changed files
with
213 additions
and
8 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
67 changes: 67 additions & 0 deletions
67
src/main/java/uk/co/hexeption/darkforge/command/Command.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,67 @@ | ||
/******************************************************************************* | ||
* DarkForge a Forge Hacked Client | ||
* Copyright (C) 2017 Hexeption (Keir Davis) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
package uk.co.hexeption.darkforge.command; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
public abstract class Command { | ||
|
||
private String[] name = getClass().getAnnotation(CMDInfo.class).name(); | ||
|
||
private String help = getClass().getAnnotation(CMDInfo.class).help(); | ||
|
||
private String description = getClass().getAnnotation(CMDInfo.class).descrption(); | ||
|
||
public String[] getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String[] name) { | ||
this.name = name; | ||
} | ||
|
||
public String getHelp() { | ||
return help; | ||
} | ||
|
||
public void setHelp(String help) { | ||
this.help = help; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public abstract void execute(String input, String[] args) throws Exception; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CMDInfo { | ||
String[] name(); | ||
|
||
String help(); | ||
|
||
String descrption(); | ||
|
||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/main/java/uk/co/hexeption/darkforge/command/CommandManager.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,103 @@ | ||
/******************************************************************************* | ||
* DarkForge a Forge Hacked Client | ||
* Copyright (C) 2017 Hexeption (Keir Davis) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
package uk.co.hexeption.darkforge.command; | ||
|
||
import uk.co.hexeption.darkforge.api.logger.LogHelper; | ||
import uk.co.hexeption.darkforge.command.commands.TestCommand; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CommandManager { | ||
|
||
private final Pattern patten = Pattern.compile("([^\\\"']\\\\S*|\\\".+?\\\"|'.+?')\\\\s*"); | ||
private final List<Command> commands = new ArrayList<>(); | ||
|
||
|
||
public void Initialization() { | ||
initCommands(); | ||
LogHelper.info(String.format("Commands Loaded: %s!", commands.size())); | ||
} | ||
|
||
private void initCommands() { | ||
this.commands.add(new TestCommand()); | ||
} | ||
|
||
public List<Command> getCommands() { | ||
return commands; | ||
} | ||
|
||
public boolean executeCommand(String message) { | ||
String commandName = message.contains(" ") ? message.split(" ")[0] : message; | ||
for (Command command : commands) { | ||
for (String alias : command.getName()) { | ||
if (message.contains(" ")) { | ||
if (message.split(" ")[1].equalsIgnoreCase("aliases")) { | ||
listAllNames(command); | ||
return true; | ||
} | ||
} | ||
tryCommand(command, message); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
public void addCommand(Command... command) { | ||
synchronized (this.commands) { | ||
for (final Command command1 : command) { | ||
this.commands.add(command1); | ||
} | ||
} | ||
} | ||
|
||
private String[] getArguments(String input) { | ||
Matcher matcher = patten.matcher(input); | ||
List<String> list = new ArrayList<>(); | ||
while (matcher.find()) { | ||
list.add(matcher.group(1).replaceAll("\"", "").replaceAll("'", "")); | ||
} | ||
return list.toArray(new String[list.size()]); | ||
} | ||
|
||
private void listAllNames(Command command) { | ||
String namesList = "Available names: "; | ||
String[] name = command.getName(); | ||
for (int i = 0; i < name.length; i++) { | ||
String names = name[i]; | ||
namesList += names + (i != name.length ? "," : ""); | ||
System.out.println(namesList); | ||
} | ||
} | ||
|
||
public void tryCommand(Command command, String input) { | ||
try { | ||
String[] args = input.contains(" ") ? getArguments(input.substring(input.indexOf("") + 1)) : null; | ||
command.execute(input, args); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
|
||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/uk/co/hexeption/darkforge/command/commands/TestCommand.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,31 @@ | ||
/******************************************************************************* | ||
* DarkForge a Forge Hacked Client | ||
* Copyright (C) 2017 Hexeption (Keir Davis) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
package uk.co.hexeption.darkforge.command.commands; | ||
|
||
import uk.co.hexeption.darkforge.command.Command; | ||
|
||
@Command.CMDInfo(name = {"test", "ts"}, help = "test is good", descrption = "This is a test") | ||
|
||
public class TestCommand extends Command { | ||
|
||
@Override | ||
public void execute(String input, String[] args) throws Exception { | ||
|
||
} | ||
} |
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