/
TeamCommand.java
235 lines (219 loc) · 10 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.ScoreboardHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.ChatColor;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
public class TeamCommand extends AbstractCommand {
public TeamCommand() {
setName("team");
setSyntax("team (id:<scoreboard>/{main}) [name:<team>] (add:<entry>|...) (remove:<entry>|...) (prefix:<prefix>) (suffix:<suffix>) (option:<type> status:<status>) (color:<color>)");
setRequiredArguments(2, 9);
isProcedural = false;
}
// <--[command]
// @Name Team
// @Syntax team (id:<scoreboard>/{main}) [name:<team>] (add:<entry>|...) (remove:<entry>|...) (prefix:<prefix>) (suffix:<suffix>) (option:<type> status:<status>) (color:<color>)
// @Required 2
// @Maximum 9
// @Short Controls scoreboard teams.
// @Group player
//
// @Description
// The Team command allows you to control a scoreboard team.
//
// Use the "prefix" or "suffix" arguments to modify a team's playername prefix and suffix.
// NOTE: Prefixes and suffixes cannot be longer than 16 characters!
//
// Use the "color" argument to set the team color (for glowing, names, etc). Must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/ChatColor.html>.
//
// Use the "add" and "remove" arguments to add or remove players by name to/from the team.
//
// Use the "option" and "status" arguments together to set a team option's status.
// Option can be "COLLISION_RULE", "DEATH_MESSAGE_VISIBILITY", or "NAME_TAG_VISIBILITY", with status "ALWAYS", "FOR_OTHER_TEAMS", "FOR_OWN_TEAM", or "NEVER".
// Option can instead be "FRIENDLY_FIRE" or "SEE_INVISIBLE", only allowing status "ALWAYS" or "NEVER".
//
// @Tags
// <server.scoreboard[(<board>)].team[<team>].members>
//
// @Usage
// Use to add a player to a team.
// - team name:red add:<player.name>
//
// @Usage
// Use to add an NPC to a team.
// - team name:blue add:<npc.name>
//
// @Usage
// Use to change the prefix for a team.
// - team name:red "prefix:[<red>Red Team<reset>]"
//
// @Usage
// Use to hide nameplates for members of a team.
// - team name:red option:name_tag_visibility status:never
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
String name = null;
String prefix = null;
String suffix = null;
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesPrefix("id")
&& !scriptEntry.hasObject("id")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (arg.matchesPrefix("name")
&& !scriptEntry.hasObject("name")) {
ElementTag nameElement = arg.asElement();
name = nameElement.asString();
scriptEntry.addObject("name", nameElement);
}
else if (arg.matchesPrefix("add")
&& !scriptEntry.hasObject("add")) {
scriptEntry.addObject("add", arg.asType(ListTag.class));
}
else if (arg.matchesPrefix("remove")
&& !scriptEntry.hasObject("remove")) {
scriptEntry.addObject("remove", arg.asType(ListTag.class));
}
else if (arg.matchesPrefix("prefix")
&& !scriptEntry.hasObject("prefix")) {
ElementTag prefixElement = arg.asElement();
prefix = prefixElement.asString();
scriptEntry.addObject("prefix", prefixElement);
}
else if (arg.matchesPrefix("suffix")
&& !scriptEntry.hasObject("suffix")) {
ElementTag suffixElement = arg.asElement();
suffix = suffixElement.asString();
scriptEntry.addObject("suffix", suffixElement);
}
else if (arg.matchesPrefix("color")
&& arg.matchesEnum(ChatColor.values())
&& !scriptEntry.hasObject("color")) {
scriptEntry.addObject("color", arg.asElement());
}
else if (arg.matchesPrefix("option")
&& !scriptEntry.hasObject("option")
&& (arg.matchesEnum(Team.Option.values())
|| arg.matches("friendly_fire", "see_invisible"))) {
scriptEntry.addObject("option", arg.asElement());
}
else if (arg.matchesPrefix("status")
&& !scriptEntry.hasObject("status")
&& arg.matchesEnum(Team.OptionStatus.values())) {
scriptEntry.addObject("status", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
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("option") && !scriptEntry.hasObject("color")
&& !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!");
}
if (scriptEntry.hasObject("option") != scriptEntry.hasObject("status")) {
throw new InvalidArgumentsException("Option and Status arguments must go together!");
}
scriptEntry.defaultObject("id", new ElementTag("main"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag name = scriptEntry.getElement("name");
ListTag add = scriptEntry.getObjectTag("add");
ListTag remove = scriptEntry.getObjectTag("remove");
ElementTag prefix = scriptEntry.getElement("prefix");
ElementTag suffix = scriptEntry.getElement("suffix");
ElementTag option = scriptEntry.getElement("option");
ElementTag status = scriptEntry.getElement("status");
ElementTag color = scriptEntry.getElement("color");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
id.debug() +
name.debug() +
(add != null ? add.debug() : "") +
(remove != null ? remove.debug() : "") +
(prefix != null ? prefix.debug() : "") +
(suffix != null ? suffix.debug() : "") +
(color != null ? color.debug() : "") +
(option != null ? option.debug() + status.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 = PlayerTag.valueOf(string, scriptEntry.context).getName();
}
if (!team.hasEntry(string)) {
team.addEntry(string);
}
}
}
if (remove != null) {
for (String string : remove) {
if (string.startsWith("p@")) {
string = PlayerTag.valueOf(string, scriptEntry.context).getName();
}
if (team.hasEntry(string)) {
team.removeEntry(string);
}
}
}
if (option != null) {
String optName = CoreUtilities.toLowerCase(option.asString());
String statusName = CoreUtilities.toLowerCase(status.asString());
if (optName.equals("friendly_fire")) {
team.setAllowFriendlyFire(statusName.equals("always"));
}
else if (optName.equals("see_invisible")) {
team.setCanSeeFriendlyInvisibles(statusName.equals("always"));
}
else {
team.setOption(Team.Option.valueOf(optName.toUpperCase()), Team.OptionStatus.valueOf(statusName.toUpperCase()));
}
}
if (prefix != null) {
team.setPrefix(prefix.asString());
}
if (suffix != null) {
team.setSuffix(suffix.asString());
}
if (color != null) {
team.setColor(ChatColor.valueOf(color.asString().toUpperCase()));
}
if (team.getEntries().isEmpty()) {
team.unregister();
}
}
}