-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
BossBarCommand.java
199 lines (173 loc) · 8.38 KB
/
BossBarCommand.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
package net.aufdemrand.denizen.scripts.commands.server;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dPlayer;
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 net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BossBarCommand extends AbstractCommand {
private enum Action {
CREATE, UPDATE, REMOVE
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("title")
&& arg.matchesPrefix("title", "t")) {
scriptEntry.addObject("title", arg.asElement());
}
else if (!scriptEntry.hasObject("progress")
&& arg.matchesPrefix("progress", "health", "p", "h")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)) {
scriptEntry.addObject("progress", arg.asElement());
}
else if (!scriptEntry.hasObject("color")
&& arg.matchesPrefix("color", "c")
&& arg.matchesEnum(BarColor.values())) {
scriptEntry.addObject("color", arg.asElement());
}
else if (!scriptEntry.hasObject("style")
&& arg.matchesPrefix("style", "s")
&& arg.matchesEnum(BarStyle.values())) {
scriptEntry.addObject("style", arg.asElement());
}
else if (!scriptEntry.hasObject("flags")
&& arg.matchesPrefix("flags", "flag", "f")
&& arg.matchesEnumList(BarFlag.values())) {
scriptEntry.addObject("flags", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("players")
&& arg.matchesPrefix("players")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("id")) {
scriptEntry.addObject("id", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("id")) {
throw new InvalidArgumentsException("Must specify an ID!");
}
if ((!scriptEntry.hasObject("action") || scriptEntry.getElement("action").asString().equalsIgnoreCase("CREATE"))
&& !scriptEntry.hasObject("players")) {
BukkitScriptEntryData data = (BukkitScriptEntryData) scriptEntry.entryData;
if (data.hasPlayer() && data.getPlayer().isOnline()) {
scriptEntry.addObject("players", new dList(Collections.singleton(data.getPlayer().identify())));
}
else {
throw new InvalidArgumentsException("Must specify valid player(s)!");
}
}
scriptEntry.defaultObject("action", new Element("CREATE"));
}
public final static Map<String, BossBar> bossBarMap = new HashMap<String, BossBar>();
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element id = scriptEntry.getElement("id");
Element action = scriptEntry.getElement("action");
dList players = scriptEntry.getdObject("players");
Element title = scriptEntry.getElement("title");
Element progress = scriptEntry.getElement("progress");
Element color = scriptEntry.getElement("color");
Element style = scriptEntry.getElement("style");
dList flags = scriptEntry.getdObject("flags");
dB.report(scriptEntry, getName(), id.debug() + action.debug()
+ (players != null ? players.debug() : "")
+ (title != null ? title.debug() : "")
+ (progress != null ? progress.debug() : "")
+ (color != null ? color.debug() : "")
+ (style != null ? style.debug() : "")
+ (flags != null ? flags.debug() : ""));
String idString = CoreUtilities.toLowerCase(id.asString());
switch (Action.valueOf(action.asString().toUpperCase())) {
case CREATE:
if (bossBarMap.containsKey(idString)) {
dB.echoError("BossBar '" + idString + "' already exists!");
return;
}
String barTitle = title != null ? title.asString() : "";
List<dPlayer> barPlayers = players.filter(dPlayer.class);
double barProgress = progress != null ? progress.asDouble() : 1D;
BarColor barColor = color != null ? BarColor.valueOf(color.asString().toUpperCase()) : BarColor.WHITE;
BarStyle barStyle = style != null ? BarStyle.valueOf(style.asString().toUpperCase()) : BarStyle.SOLID;
BarFlag[] barFlags = new BarFlag[flags != null? flags.size() : 0];
if (flags != null) {
for (int i = 0; i < flags.size(); i++) {
barFlags[i] = (BarFlag.valueOf(flags.get(i).toUpperCase()));
}
}
BossBar bossBar = Bukkit.createBossBar(barTitle, barColor, barStyle, barFlags);
bossBar.setProgress(barProgress);
for (dPlayer player : barPlayers) {
if (!player.isOnline()) {
dB.echoError("Player must be online to show a BossBar to them!");
continue;
}
bossBar.addPlayer(player.getPlayerEntity());
}
bossBar.show();
bossBarMap.put(idString, bossBar);
break;
case UPDATE:
if (!bossBarMap.containsKey(idString)) {
dB.echoError("BossBar '" + idString + "' does not exist!");
return;
}
BossBar bossBar1 = bossBarMap.get(idString);
if (title != null) {
bossBar1.setTitle(title.asString());
}
if (progress != null) {
bossBar1.setProgress(progress.asDouble());
}
if (color != null) {
bossBar1.setColor(BarColor.valueOf(color.asString().toUpperCase()));
}
if (style != null) {
bossBar1.setStyle(BarStyle.valueOf(style.asString().toUpperCase()));
}
if (players != null) {
for (dPlayer player : players.filter(dPlayer.class)) {
bossBar1.addPlayer(player.getPlayerEntity());
}
}
break;
case REMOVE:
if (!bossBarMap.containsKey(idString)) {
dB.echoError("BossBar '" + idString + "' does not exist!");
return;
}
if (players != null) {
BossBar bar = bossBarMap.get(idString);
for (dPlayer player : players.filter(dPlayer.class)) {
bar.removePlayer(player.getPlayerEntity());
}
break;
}
bossBarMap.get(idString).hide();
bossBarMap.remove(idString);
break;
}
}
}