/
TeamCommand.java
152 lines (125 loc) · 5.53 KB
/
TeamCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.ScoreboardHelper;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
public class TeamCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
String name = null;
String prefix = null;
String suffix = null;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesPrefix("id")
&& !scriptEntry.hasObject("id")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (arg.matchesPrefix("name")
&& !scriptEntry.hasObject("name")) {
Element nameElement = arg.asElement();
name = nameElement.asString();
scriptEntry.addObject("name", nameElement);
}
else if (arg.matchesPrefix("add")
&& !scriptEntry.hasObject("add")) {
scriptEntry.addObject("add", arg.asType(dList.class));
}
else if (arg.matchesPrefix("remove")
&& !scriptEntry.hasObject("remove")) {
scriptEntry.addObject("remove", arg.asType(dList.class));
}
else if (arg.matchesPrefix("prefix")
&& !scriptEntry.hasObject("prefix")) {
Element prefixElement = arg.asElement();
prefix = prefixElement.asString();
scriptEntry.addObject("prefix", prefixElement);
}
else if (arg.matchesPrefix("suffix")
&& !scriptEntry.hasObject("suffix")) {
Element suffixElement = arg.asElement();
suffix = suffixElement.asString();
scriptEntry.addObject("suffix", suffixElement);
}
}
if (name == null || name.length() == 0 || name.length() > 16) {
throw new InvalidArgumentsException("Must specify a team name between 1 and 16 characters!");
}
if (!scriptEntry.hasObject("add") && !scriptEntry.hasObject("remove")
&& !scriptEntry.hasObject("prefix") && !scriptEntry.hasObject("suffix")) {
throw new InvalidArgumentsException("Must specify something to do with the team!");
}
if ((prefix != null && prefix.length() > 16) || (suffix != null && suffix.length() > 16)) {
throw new InvalidArgumentsException("Prefixes and suffixes must be 16 characters or less!");
}
scriptEntry.defaultObject("id", new Element("main"));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element id = scriptEntry.getElement("id");
Element name = scriptEntry.getElement("name");
dList add = scriptEntry.getdObject("add");
dList remove = scriptEntry.getdObject("remove");
Element prefix = scriptEntry.getElement("prefix");
Element suffix = scriptEntry.getElement("suffix");
dB.report(scriptEntry, getName(),
id.debug() +
name.debug() +
(add != null ? add.debug() : "") +
(remove != null ? remove.debug() : "") +
(prefix != null ? prefix.debug() : "") +
(suffix != null ? suffix.debug() : ""));
Scoreboard board;
if (id.asString().equalsIgnoreCase("main")) {
board = ScoreboardHelper.getMain();
}
else {
if (ScoreboardHelper.hasScoreboard(id.asString())) {
board = ScoreboardHelper.getScoreboard(id.asString());
}
else {
board = ScoreboardHelper.createScoreboard(id.asString());
}
}
Team team = board.getTeam(name.asString());
if (team == null) {
team = board.registerNewTeam(name.asString());
}
if (add != null) {
for (String string : add) {
if (string.startsWith("p@")) {
string = dPlayer.valueOf(string).getName();
}
if (!team.hasEntry(string)) {
team.addEntry(string);
}
}
}
if (remove != null) {
for (String string : remove) {
if (string.startsWith("p@")) {
string = dPlayer.valueOf(string).getName();
}
if (team.hasEntry(string)) {
team.removeEntry(string);
}
}
}
if (prefix != null) {
team.setPrefix(prefix.asString());
}
if (suffix != null) {
team.setSuffix(suffix.asString());
}
if (team.getEntries().isEmpty()) {
team.unregister();
}
}
}