Add bossbar provider

This commit is contained in:
fullwall 2023-10-20 22:08:37 +08:00
parent 9d9b3c4349
commit 33dce9174b

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.trait.versioned;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
@ -42,6 +43,7 @@ public class BossBarTrait extends Trait {
private BarColor color = BarColor.PURPLE;
@Persist
private List<BarFlag> flags = Lists.newArrayList();
private Supplier<Double> progressProvider;
@Persist
private int range = -1;
@Persist
@ -62,10 +64,12 @@ public class BossBarTrait extends Trait {
private BossBar getBar() {
if (npc.isSpawned() && isBoss(npc.getEntity()) && NMS.getBossBar(npc.getEntity()) != null)
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;
}
@ -102,6 +106,7 @@ public class BossBarTrait extends Trait {
if (isBoss) {
onDespawn();
}
return isBoss;
}
@ -113,6 +118,7 @@ public class BossBarTrait extends Trait {
public void onDespawn() {
if (barCache == null)
return;
barCache.removeAll();
barCache.hide();
barCache = null;
@ -127,6 +133,7 @@ public class BossBarTrait extends Trait {
public void run() {
if (!npc.isSpawned())
return;
BossBar bar = getBar();
if (bar == null)
return;
@ -157,21 +164,31 @@ public class BossBarTrait extends Trait {
bar.setProgress(Math.max(0, Math.min(1, number)));
}
}
bar.setTitle(title);
bar.setVisible(visible);
if (progressProvider != null) {
bar.setProgress(progressProvider.get());
}
if (style != null) {
bar.setStyle(style);
}
if (color != null) {
bar.setColor(color);
}
for (BarFlag flag : BarFlag.values()) {
bar.removeFlag(flag);
}
for (BarFlag flag : flags) {
bar.addFlag(flag);
}
bar.removeAll();
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getEntity().getLocation(),
range > 0 ? range : Setting.BOSSBAR_RANGE.asInt())) {
if (viewPermission != null && !player.hasPermission(viewPermission))
@ -192,6 +209,10 @@ public class BossBarTrait extends Trait {
this.flags = flags;
}
public void setProgressProvider(Supplier<Double> provider) {
this.progressProvider = provider;
}
public void setRange(int range) {
this.range = range;
}
@ -232,24 +253,31 @@ public class BossBarTrait extends Trait {
if (style != null) {
trait.setStyle(style);
}
if (color != null) {
trait.setColor(color);
}
if (track != null) {
trait.setTrackVariable(track);
}
if (title != null) {
trait.setTitle(Messaging.parseComponents(title));
}
if (visible != null) {
trait.setVisible(visible);
}
if (range != null) {
trait.setRange(range);
}
if (viewpermission != null) {
trait.setViewPermission(viewpermission);
}
if (flags != null) {
List<BarFlag> parsed = Lists.newArrayList();
for (String s : Splitter.on(',').omitEmptyStrings().trimResults().split(flags)) {