/
ConsoleCommand.java
218 lines (202 loc) · 10.6 KB
/
ConsoleCommand.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package net.ME1312.SubServers.Console;
import net.ME1312.SubServers.Bungee.Host.Host;
import net.ME1312.SubServers.Bungee.Host.SubServer;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.command.ConsoleCommandSender;
import java.util.List;
import java.util.function.Consumer;
@SuppressWarnings("unchecked")
public final class ConsoleCommand {
private ConsoleCommand() {}
public static class POPOUT extends Command {
private ConsolePlugin plugin;
private String label;
public POPOUT(ConsolePlugin plugin, String command) {
super(command);
this.plugin = plugin;
this.label = command;
}
@Override
public void execute(final CommandSender sender, String[] args) {
if (sender instanceof ConsoleCommandSender) {
if (args.length > 0) {
final String type = (args.length > 1)?args[0]:null;
final String name = args[(type != null)?1:0];
final Runnable forServer = new Runnable() {
@Override
public void run() {
SubServer server = plugin.getProxy().api.getSubServer(name);
if (server != null) {
if (server.isRunning()) {
Logger.get("SubConsole").info("Opening console window...");
if (!plugin.sCurrent.keySet().contains(name.toLowerCase())) {
ConsoleWindow window = new ConsoleWindow(plugin, server.getLogger());
plugin.sCurrent.put(name.toLowerCase(), window);
window.open();
} else {
plugin.sCurrent.get(name.toLowerCase()).open();
}
} else {
sender.sendMessage("SubConsole > That SubServer is not running right now");
}
} else {
if (type == null) {
sender.sendMessage("SubConsole > There is no object with that name");
} else {
sender.sendMessage("SubConsole > There is no subserver with that name");
}
}
}
};
final Runnable forCreator = new Runnable() {
@Override
public void run() {
Host host = plugin.getProxy().api.getHost(name);
if (host != null) {
if (host.getCreator().getReservedNames().size() > 0) {
sender.sendMessage("SubConsole > Opening console window" + ((host.getCreator().getReservedNames().size() == 1)?"":"s") + "...");
for (String reserved : host.getCreator().getReservedNames()) {
if (!plugin.cCurrent.keySet().contains(reserved.toLowerCase())) {
ConsoleWindow window = new ConsoleWindow(plugin, host.getCreator().getLogger(reserved));
plugin.cCurrent.put(reserved.toLowerCase(), window);
window.open();
} else {
plugin.cCurrent.get(reserved.toLowerCase()).open();
}
}
} else {
sender.sendMessage("SubConsole > That Host is not running SubCreator right now");
}
} else {
if (type == null) {
forServer.run();
} else {
sender.sendMessage("SubConsole > There is no host with that name");
}
}
}
};
if (type == null) {
forCreator.run();
} else {
switch (type.toLowerCase()) {
case "h":
case "host":
case "c":
case "creator":
case "subcreator":
forCreator.run();
break;
case "s":
case "server":
case "subserver":
forServer.run();
break;
default:
sender.sendMessage("SubConsole > There is no object type with that name");
}
}
} else {
Logger.get("SubConsole").info("Usage: /" + label + " [host|server] <Name>");
}
} else {
String str = label;
for (String arg : args) str += ' ' + arg;
((ProxiedPlayer) sender).chat(str);
}
}
}
public static class AUTO_POPOUT extends Command {
private ConsolePlugin plugin;
private String label;
public AUTO_POPOUT(ConsolePlugin plugin, String command) {
super(command);
this.plugin = plugin;
this.label = command;
}
@Override
public void execute(final CommandSender sender, String[] args) {
if (sender instanceof ConsoleCommandSender) {
if (args.length > 0) {
final String type = (args.length > 1)?args[0]:null;
final String name = args[(type != null)?1:0];
final Runnable forServer = new Runnable() {
@Override
public void run() {
SubServer server = plugin.getProxy().api.getSubServer(name);
List<String> list = plugin.config.get().getStringList("Enabled-Servers");
if (!plugin.config.get().getStringList("Enabled-Servers").contains(name.toLowerCase())) {
list.add(name.toLowerCase());
if (server == null) plugin.getProxy().getLogger().warning("SubConsole > SubServer with name \"" + name + "\" does not exist");
sender.sendMessage("SubConsole > " + ((server == null)?name:server.getName()) + " will now popout its console by default");
} else {
list.remove(name.toLowerCase());
sender.sendMessage("SubConsole > " + ((server == null)?name:server.getName()) + " will no longer popout its console by default");
}
plugin.config.get().set("Enabled-Servers", list);
try {
plugin.config.save();
} catch (Exception e) {
e.printStackTrace();
}
}
};
final Consumer<Boolean> forCreator = new Consumer<Boolean>() {
@Override
public void accept(Boolean force) {
Host host = plugin.getProxy().api.getHost(name);
if (force || host != null) {
List<String> list = plugin.config.get().getStringList("Enabled-Creators");
if (!plugin.config.get().getStringList("Enabled-Creators").contains(name.toLowerCase())) {
list.add(name.toLowerCase());
if (host == null) plugin.getProxy().getLogger().warning("SubConsole > Host with name \"" + name + "\" does not exist");
sender.sendMessage("SubConsole > " + ((host == null)?name:host.getName()) + " will now popout SubCreator's console by default");
} else {
list.remove(name.toLowerCase());
sender.sendMessage("SubConsole > " + ((host == null)?name:host.getName()) + " will no longer popout SubCreator's console by default");
}
plugin.config.get().set("Enabled-Creators", list);
try {
plugin.config.save();
} catch (Exception e) {
e.printStackTrace();
}
} else {
forServer.run();
}
}
};
if (type == null) {
forCreator.accept(false);
} else {
switch (type.toLowerCase()) {
case "h":
case "host":
case "c":
case "creator":
case "subcreator":
forCreator.accept(true);
break;
case "s":
case "server":
case "subserver":
forServer.run();
break;
default:
sender.sendMessage("SubConsole > There is no object type with that name");
}
}
} else {
Logger.get("SubConsole").info("Usage: /" + label + " [host|server] <Name>");
}
} else {
String str = label;
for (String arg : args) str += ' ' + arg;
((ProxiedPlayer) sender).chat(str);
}
}
}
}