/
Command.java
156 lines (142 loc) · 4.1 KB
/
Command.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
package net.ME1312.Galaxi.Plugin.Command;
import net.ME1312.Galaxi.Galaxi;
import net.ME1312.Galaxi.Library.Exception.IllegalStringValueException;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Plugin.PluginInfo;
/**
* Command Layout Class
*/
public abstract class Command implements CommandExecutor {
private String desc = null;
private String[] exDesc = new String[0];
private String[] usage = new String[0];
private CompletionHandler autocomplete = (this instanceof CompletionHandler)?(CompletionHandler)this:null;
private PluginInfo plugin;
/**
* Creates a new Command
*
* @param plugin Plugin
*/
public Command(PluginInfo plugin) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
}
/**
* Creates a new Command (by wrapping an existing executor)
*
* @param plugin Plugin
* @param executor Executor
*/
public static Command create(PluginInfo plugin, CommandExecutor executor) {
if (Util.isNull(plugin, executor)) throw new NullPointerException();
return new Command(plugin) {
@Override
public void command(CommandSender sender, String handle, String[] args) {
executor.command(sender, handle, args);
}
};
}
/**
* Gets the AutoComplete handler for this Command
*
* @return AutoComplete Handler
*/
public CompletionHandler autocomplete() {
return this.autocomplete;
}
/**
* Sets an AutoComplete handler for this Command
*
* @param value AutoComplete Handler
* @return The Command
*/
public Command autocomplete(CompletionHandler value) {
this.autocomplete = value;
return this;
}
/**
* Gets the Plugin that registering this Command
*
* @return Plugin Info
*/
public PluginInfo plugin() {
return this.plugin;
}
/**
* Gets the Description of this Command
*
* @return Command Description
*/
public String description() {
return this.desc;
}
/**
* Set the Description of this Command
*
* @param value Value
* @return The Command
*/
public Command description(String value) {
if (value != null) {
if (value.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use empty string for description");
if (value.contains("\n")) throw new IllegalStringValueException("String contains illegal character(s)");
}
this.desc = value;
return this;
}
/**
* Get the Help Page for this Command
*
* @return Help Page
*/
public String[] help() {
if (exDesc.length == 0 && desc != null) {
return new String[]{desc};
} else {
return exDesc;
}
}
/**
* Set the Help Page for this Command
*
* @param lines Help Page Lines
* @return The Command
*/
public Command help(String... lines) {
for (String line : lines) {
if (line.contains("\n")) throw new IllegalStringValueException("String contains illegal character(s)");
}
this.exDesc = lines;
return this;
}
/**
* Get the Usage of this Command
*
* @return Command Usage
*/
public String[] usage() {
return this.usage;
}
/**
* Set the Usage of this Command
*
* @param args Argument Placeholders
* @return The Command
*/
public Command usage(String... args) {
for (String arg : args) {
if (arg.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use empty string for usage");
if (arg.contains(" ") || arg.contains("\n")) throw new IllegalStringValueException("String contains illegal character(s)");
}
this.usage = args;
return this;
}
/**
* Register this Command
*
* @param handles Aliases
*/
public void register(String... handles) {
Galaxi.getInstance().getPluginManager().addCommand(this, handles);
}
}