2019-12-11 17:13:54 +01:00
|
|
|
package net.citizensnpcs.trait.versioned;
|
2019-04-24 15:01:22 +02:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-07-19 17:02:41 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2019-04-24 15:01:22 +02:00
|
|
|
import org.bukkit.boss.BarColor;
|
|
|
|
import org.bukkit.boss.BarFlag;
|
2021-07-19 17:02:41 +02:00
|
|
|
import org.bukkit.boss.BarStyle;
|
2019-04-24 15:01:22 +02:00
|
|
|
import org.bukkit.boss.BossBar;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.EntityType;
|
2021-07-19 17:02:41 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2019-04-24 15:01:22 +02:00
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
2021-07-19 17:02:41 +02:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
2019-04-24 15:01:22 +02:00
|
|
|
import net.citizensnpcs.api.persistence.Persist;
|
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
|
|
|
import net.citizensnpcs.api.trait.TraitName;
|
2019-12-11 17:13:54 +01:00
|
|
|
import net.citizensnpcs.util.NMS;
|
2019-04-24 15:01:22 +02:00
|
|
|
|
|
|
|
@TraitName("bossbar")
|
|
|
|
public class BossBarTrait extends Trait {
|
2021-07-19 17:02:41 +02:00
|
|
|
private BossBar barCache;
|
2019-04-24 15:01:22 +02:00
|
|
|
@Persist("color")
|
2021-07-19 17:02:41 +02:00
|
|
|
private BarColor color = BarColor.PURPLE;
|
2019-04-24 15:01:22 +02:00
|
|
|
@Persist("flags")
|
|
|
|
private List<BarFlag> flags = Lists.newArrayList();
|
2021-07-19 17:02:41 +02:00
|
|
|
@Persist("style")
|
|
|
|
private BarStyle style = BarStyle.SOLID;
|
2019-04-24 15:01:22 +02:00
|
|
|
@Persist("title")
|
2021-07-19 17:02:41 +02:00
|
|
|
private String title = "";
|
2019-04-24 15:01:22 +02:00
|
|
|
@Persist("visible")
|
|
|
|
private boolean visible = true;
|
|
|
|
|
|
|
|
public BossBarTrait() {
|
|
|
|
super("bossbar");
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:16:41 +02:00
|
|
|
public BarColor getColor() {
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<BarFlag> getFlags() {
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:02:41 +02:00
|
|
|
public BarStyle getStyle() {
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:16:41 +02:00
|
|
|
public String getTitle() {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:01:22 +02:00
|
|
|
private boolean isBoss(Entity entity) {
|
2021-07-19 17:02:41 +02:00
|
|
|
boolean isBoss = entity.getType() == EntityType.ENDER_DRAGON || entity.getType() == EntityType.WITHER
|
2019-04-24 15:01:22 +02:00
|
|
|
|| entity.getType() == EntityType.GUARDIAN;
|
2021-07-19 17:02:41 +02:00
|
|
|
if (isBoss) {
|
|
|
|
barCache = null;
|
|
|
|
}
|
|
|
|
return isBoss;
|
2019-04-24 15:01:22 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 13:16:41 +02:00
|
|
|
public boolean isVisible() {
|
|
|
|
return visible;
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:01:22 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-07-19 17:02:41 +02:00
|
|
|
if (!npc.isSpawned())
|
2019-04-24 15:01:22 +02:00
|
|
|
return;
|
2021-07-19 17:02:41 +02:00
|
|
|
BossBar bar = isBoss(npc.getEntity()) ? (BossBar) NMS.getBossBar(npc.getEntity())
|
|
|
|
: barCache == null ? barCache = Bukkit.getServer().createBossBar(npc.getFullName(), color, style,
|
|
|
|
flags.toArray(new BarFlag[flags.size()])) : barCache;
|
2019-05-03 10:55:47 +02:00
|
|
|
if (bar == null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-19 17:02:41 +02:00
|
|
|
bar.setStyle(style);
|
2019-04-24 15:01:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-07-19 17:02:41 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 15:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setColor(BarColor color) {
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlags(Collection<BarFlag> flags) {
|
|
|
|
this.flags = Lists.newArrayList(flags);
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:16:41 +02:00
|
|
|
public void setFlags(List<BarFlag> flags) {
|
|
|
|
this.flags = flags;
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:02:41 +02:00
|
|
|
public void setStyle(BarStyle style) {
|
|
|
|
this.style = style;
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:01:22 +02:00
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setVisible(boolean visible) {
|
|
|
|
this.visible = visible;
|
|
|
|
}
|
|
|
|
}
|