-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
BossBarTrait.java
235 lines (210 loc) · 7.25 KB
/
BossBarTrait.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 net.citizensnpcs.trait.versioned;
import java.util.Collection;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
@TraitName("bossbar")
public class BossBarTrait extends Trait {
private BossBar barCache;
@Persist("color")
private BarColor color = BarColor.PURPLE;
@Persist("flags")
private List<BarFlag> flags = Lists.newArrayList();
@Persist("style")
private BarStyle style = BarStyle.SOLID;
@Persist("title")
private String title = "";
@Persist("track")
private String track;
@Persist("visible")
private boolean visible = true;
public BossBarTrait() {
super("bossbar");
}
private BossBar getBar() {
if (npc.isSpawned() && isBoss(npc.getEntity()))
return (BossBar) NMS.getBossBar(npc.getEntity());
if (barCache == null) {
barCache = Bukkit.getServer().createBossBar(npc.getFullName(), color, style,
flags.toArray(new BarFlag[flags.size()]));
}
return barCache;
}
public BarColor getColor() {
return color;
}
public List<BarFlag> getFlags() {
return flags;
}
public BarStyle getStyle() {
return style;
}
public String getTitle() {
return title;
}
public String getTrackingVariable() {
return track;
}
private boolean isBoss(Entity entity) {
boolean isBoss = entity.getType() == EntityType.ENDER_DRAGON || entity.getType() == EntityType.WITHER
|| entity.getType() == EntityType.GUARDIAN;
if (isBoss) {
barCache = null;
}
return isBoss;
}
public boolean isVisible() {
return visible;
}
@Override
public void onDespawn() {
if (barCache != null) {
barCache.removeAll();
barCache.hide();
barCache = null;
}
}
@Override
public void run() {
if (!npc.isSpawned())
return;
BossBar bar = getBar();
if (bar == null) {
return;
}
if (track != null && !track.isEmpty() && npc.getEntity() instanceof LivingEntity) {
LivingEntity entity = (LivingEntity) npc.getEntity();
if (track.equalsIgnoreCase("health")) {
double maxHealth = entity.getMaxHealth();
if (SUPPORT_ATTRIBUTES) {
try {
maxHealth = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
} catch (Throwable t) {
SUPPORT_ATTRIBUTES = false;
}
}
bar.setProgress(entity.getHealth() / maxHealth);
} else {
String replaced = Placeholders.replace(track,
npc.getEntity() instanceof Player ? (Player) npc.getEntity() : null);
if (!track.equals(replaced)) {
try {
bar.setProgress(Double.parseDouble(replaced));
} catch (NumberFormatException ex) {
}
}
}
}
bar.setStyle(style);
bar.setVisible(visible);
if (color != null) {
bar.setColor(color);
}
if (title != null) {
bar.setTitle(title);
}
for (BarFlag flag : BarFlag.values()) {
bar.removeFlag(flag);
}
for (BarFlag flag : flags) {
bar.addFlag(flag);
}
if (barCache != null) {
barCache.removeAll();
for (Entity entity : npc.getEntity().getNearbyEntities(Setting.BOSSBAR_RANGE.asInt() / 2,
Setting.BOSSBAR_RANGE.asInt() / 2, Setting.BOSSBAR_RANGE.asInt() / 2)) {
if (entity instanceof Player) {
barCache.addPlayer((Player) entity);
}
}
}
}
public void setColor(BarColor color) {
this.color = color;
}
public void setFlags(Collection<BarFlag> flags) {
this.flags = Lists.newArrayList(flags);
}
public void setFlags(List<BarFlag> flags) {
this.flags = flags;
}
public void setStyle(BarStyle style) {
this.style = style;
}
public void setTitle(String title) {
this.title = title;
}
public void setTrackVariable(String variable) {
this.track = variable;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
@Command(
aliases = { "npc" },
usage = "bossbar --style [style] --color [color] --title [title] --visible [visible] --flags [flags] --track [health | placeholder]",
desc = "Edit bossbar properties",
modifiers = { "bossbar" },
min = 1,
max = 1)
@Requirements(selected = true, ownership = true)
public static void bossbar(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
BossBarTrait trait = npc.getOrAddTrait(BossBarTrait.class);
if (args.hasValueFlag("style")) {
BarStyle style = Util.matchEnum(BarStyle.values(), args.getFlag("style"));
if (style != null) {
trait.setStyle(style);
}
}
if (args.hasValueFlag("color")) {
BarColor color = Util.matchEnum(BarColor.values(), args.getFlag("color"));
if (color != null) {
trait.setColor(color);
}
}
if (args.hasValueFlag("track")) {
trait.setTrackVariable(args.getFlag("track"));
}
if (args.hasValueFlag("title")) {
trait.setTitle(Colorizer.parseColors(args.getFlag("title")));
}
if (args.hasValueFlag("visible")) {
trait.setVisible(Boolean.parseBoolean(args.getFlag("visible")));
}
if (args.hasValueFlag("flags")) {
List<BarFlag> flags = Lists.newArrayList();
for (String s : Splitter.on(',').omitEmptyStrings().trimResults().split(args.getFlag("flags"))) {
BarFlag flag = Util.matchEnum(BarFlag.values(), s);
if (flag != null) {
flags.add(flag);
}
}
trait.setFlags(flags);
}
}
private static boolean SUPPORT_ATTRIBUTES = true;
}