/
DefaultCommands.java
204 lines (191 loc) · 11 KB
/
DefaultCommands.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package net.ME1312.Galaxi.Engine.Library;
import net.ME1312.Galaxi.Engine.GalaxiEngine;
import net.ME1312.Galaxi.Engine.PluginManager;
import net.ME1312.Galaxi.Library.Config.YAMLSection;
import net.ME1312.Galaxi.Library.Log.Logger;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.Galaxi.Plugin.Command;
import net.ME1312.Galaxi.Plugin.PluginInfo;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
/**
* Default Command Class
*/
public class DefaultCommands {
private DefaultCommands() {}
/**
* Load the Default Commands
*
* @param engine GalaxiEngine
*/
public static void load(GalaxiEngine engine) {
Logger log = engine.getAppInfo().getLogger();
new Command(engine.getEngineInfo()) {
@Override
public void command(String handle, String[] args) {
if (args.length == 0 || engine.getPluginManager().getPlugins().get(args[0].toLowerCase()) != null) {
log.message.println(
"These are the platforms and versions that are running " + ((args.length == 0)?engine.getAppInfo().getName():engine.getPluginManager().getPlugin(args[0]).getName()) +":",
" " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ',',
" Java " + System.getProperty("java.version") + ',',
" " + engine.getEngineInfo().getName() + " v" + engine.getEngineInfo().getVersion().toExtendedString() + ((engine.getEngineInfo().getSignature() != null)?" (" + engine.getEngineInfo().getSignature() + ')':"")
+ ((engine.getEngineInfo() == engine.getAppInfo())?" [Standalone]"+((args.length == 0)?"":","):((GalaxiEngine.class.getProtectionDomain().getCodeSource().getLocation().equals(engine.getAppInfo().get().getClass().getProtectionDomain().getCodeSource().getLocation()))?" [Patched],":",")));
if (engine.getEngineInfo() != engine.getAppInfo())
log.message.println(" " + engine.getAppInfo().getName() + " v" + engine.getAppInfo().getVersion().toExtendedString() + ((engine.getAppInfo().getSignature() != null)?" (" + engine.getAppInfo().getSignature() + ')':"") + ((args.length == 0)?"":","));
if (args.length == 0) {
log.message.println("");
new Thread(() -> {
try {
YAMLSection tags = new YAMLSection(new JSONObject("{\"tags\":" + Util.readAll(new BufferedReader(new InputStreamReader(new URL("https://api.github.com/repos/ME1312/GalaxiEngine/git/refs/tags").openStream(), Charset.forName("UTF-8")))) + '}'));
List<Version> versions = new LinkedList<Version>();
Version updversion = engine.getEngineInfo().getVersion();
int updcount = 0;
for (YAMLSection tag : tags.getSectionList("tags")) versions.add(Version.fromString(tag.getString("ref").substring(10)));
Collections.sort(versions);
for (Version version : versions) {
if (version.compareTo(updversion) > 0) {
updversion = version;
updcount++;
}
}
if (updcount == 0) {
log.message.println("You are on the latest version.");
} else {
log.message.println(engine.getEngineInfo().getName() + " v" + updversion + " is available. You are " + updcount + " version" + ((updcount == 1)?"":"s") + " behind.");
}
} catch (Exception e) {}
}).start();
} else {
PluginInfo plugin = engine.getPluginManager().getPlugin(args[0]);
String title = " " + plugin.getDisplayName() + " v" + plugin.getVersion().toExtendedString() + ((engine.getEngineInfo().getSignature() != null)?" (" + plugin.getSignature() + ')':"");
String subtitle = " by ";
int i = 0;
for (String author : plugin.getAuthors()) {
i++;
if (i > 1) {
if (plugin.getAuthors().size() > 2) subtitle += ", ";
else if (plugin.getAuthors().size() == 2) subtitle += ' ';
if (i == plugin.getAuthors().size()) subtitle += "and ";
}
subtitle += author;
}
if (plugin.getWebsite() != null) {
if (title.length() > subtitle.length() + 5 + plugin.getWebsite().toString().length()) {
i = subtitle.length();
while (i < title.length() - plugin.getWebsite().toString().length() - 2) {
i++;
subtitle += ' ';
}
} else {
subtitle += " - ";
}
subtitle += plugin.getWebsite().toString();
}
log.message.println(title, subtitle);
if (plugin.getDescription() != null) log.message.println("", plugin.getDescription());
}
} else {
log.message.println("There is no plugin with that name");
}
}
}.usage("[plugin]").description("Gets the version of the System, Engine, and the specified Plugin").help(
"This command will print what OS you're running, your OS version,",
"your Java version, and the GalaxiEngine version.",
"",
"If the [plugin] option is provided, it will print information about the specified plugin as well.",
"",
"Examples:",
" /version",
" /version ExamplePlugin"
).register("ver", "version");
new Command(engine.getEngineInfo()) {
@SuppressWarnings("unchecked")
public void command(String handle, String[] args) {
HashMap<String, String> result = new LinkedHashMap<String, String>();
HashMap<Command, String> reverse = new LinkedHashMap<Command, String>();
TreeMap<String, Command> commands;
try {
Field f = PluginManager.class.getDeclaredField("commands");
f.setAccessible(true);
commands = (TreeMap<String, Command>) f.get(engine.getPluginManager());
f.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
commands = new TreeMap<String, Command>();
}
int length = 0;
for(String command : commands.keySet()) {
String formatted = "/ ";
Command cmd = commands.get(command);
String alias = (reverse.keySet().contains(cmd))?reverse.get(cmd):null;
if (alias != null) formatted = result.get(alias);
if (cmd.usage().length == 0 || alias != null) {
formatted = formatted.replaceFirst("\\s", ((alias != null)?"|":"") + command + ' ');
} else {
String usage = "";
for (String str : cmd.usage()) usage += ((usage.length() == 0)?"":" ") + str;
formatted = formatted.replaceFirst("\\s", command + ' ' + usage + ' ');
}
if(formatted.length() > length) {
length = formatted.length();
}
if (alias == null) {
result.put(command, formatted);
reverse.put(cmd, command);
} else {
result.put(alias, formatted);
}
}
if (args.length == 0) {
log.message.println("Command List:");
for (String command : result.keySet()) {
String formatted = result.get(command);
Command cmd = commands.get(command);
while (formatted.length() < length) {
formatted += ' ';
}
formatted += ((cmd.description() == null || cmd.description().length() == 0)?" ":"- "+cmd.description());
log.message.println(formatted);
}
} else if (commands.keySet().contains((args[0].startsWith("/"))?args[0].toLowerCase().substring(1):args[0].toLowerCase())) {
Command cmd = commands.get((args[0].startsWith("/"))?args[0].toLowerCase().substring(1):args[0].toLowerCase());
String formatted = result.get(Util.getBackwards(commands, cmd).get(0));
log.message.println(formatted.substring(0, formatted.length() - 1));
for (String line : cmd.help()) {
log.message.println(" " + line);
}
} else {
log.message.println("There is no command with that name");
}
}
}.usage("[command]").description("Prints a list of the commands and/or their descriptions").help(
"This command will print a list of all currently registered commands and aliases,",
"along with their usage and a short description.",
"",
"If the [command] option is provided, it will print that command, it's aliases,",
"it's usage, and an extended description like the one you see here instead.",
"",
"Examples:",
" /help",
" /help version"
).register("help", "?");
new Command(engine.getEngineInfo()) {
@Override
public void command(String handle, String[] args) {
engine.stop();
}
}.description("Stops this instance of the app").help(
"This command will shutdown this instance of the app,",
"and any plugins currently running on it.",
"",
"Example:",
" /exit"
).register("exit", "end");
}
}