Add bossbar provider

This commit is contained in:
fullwall 2023-10-20 22:08:37 +08:00
parent 9d9b3c4349
commit 33dce9174b
1 changed files with 28 additions and 0 deletions

View File

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