-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractPluginCommand.java
More file actions
99 lines (87 loc) · 2.96 KB
/
AbstractPluginCommand.java
File metadata and controls
99 lines (87 loc) · 2.96 KB
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
package dansplugins.wildpets.commands;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Abstract base class for plugin commands.
*/
public abstract class AbstractPluginCommand {
private final ArrayList<String> names;
private final ArrayList<String> permissions;
public AbstractPluginCommand(ArrayList<String> names, ArrayList<String> permissions) {
this.names = names;
this.permissions = permissions;
}
/**
* Method to execute the command with no arguments.
* @param sender The sender of the command.
* @return Whether the execution of the command was successful.
*/
public abstract boolean execute(CommandSender sender);
/**
* @param sender The sender of the command.
* @param args The arguments of the command.
* @return Whether the execution of the command was successful.
*/
public abstract boolean execute(CommandSender sender, String[] args);
/**
* @param message to send.
* @param args to check.
* @param sender to send message to.
* @param color of the message.
* @return Boolean signifying whether there were no arguments.
*/
public boolean sendMessageIfNoArguments(String message, String[] args, CommandSender sender, ChatColor color) {
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + message);
return true;
}
return false;
}
/**
* @param line to convert into an Integer.
* @param orElse if the conversion fails.
* @return {@link Integer} numeric.
*/
public int getIntSafe(String line, int orElse) {
try {
return Integer.parseInt(line);
} catch (Exception e) {
return orElse;
}
}
/**
* Method to test if something matches any goal string.
*
* @param matchCase for the comparison (or not)
* @param what to test
* @param goals to compare with
* @return {@code true} if something in goals matches what.
*/
public boolean safeEquals(boolean matchCase, String what, String... goals) {
return Arrays.stream(goals).anyMatch(goal ->
matchCase && goal.equals(what) || !matchCase && goal.equalsIgnoreCase(what)
);
}
/**
* @return A list of names of the command.
*/
public ArrayList<String> getNames() {
return names;
}
/**
* @return A list of permissions of the command.
*/
public ArrayList<String> getPermissions() {
return permissions;
}
public ArrayList<String> extractArgumentsInsideDoubleQuotes(String[] args) throws Exception {
ArgumentParser argumentParser = new ArgumentParser();
ArrayList<String> doubleQuoteArgs = argumentParser.getArgumentsInsideDoubleQuotes(args);
if (doubleQuoteArgs.size() < 2) {
throw new Exception();
}
return doubleQuoteArgs;
}
}