Skip to content

Commit

Permalink
Add bossbar provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Oct 20, 2023
1 parent 9d9b3c4 commit 33dce91
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;

import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
Expand Down Expand Up @@ -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
Expand All @@ -62,10 +64,12 @@ public BossBarTrait() {
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;
}

Expand Down Expand Up @@ -102,6 +106,7 @@ private boolean isBoss(Entity entity) {
if (isBoss) {
onDespawn();
}

return isBoss;
}

Expand All @@ -113,6 +118,7 @@ public boolean isVisible() {
public void onDespawn() {
if (barCache == null)
return;

barCache.removeAll();
barCache.hide();
barCache = null;
Expand All @@ -127,6 +133,7 @@ public void onRemove() {
public void run() {
if (!npc.isSpawned())
return;

BossBar bar = getBar();
if (bar == null)
return;
Expand Down Expand Up @@ -157,21 +164,31 @@ public void run() {
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))
Expand All @@ -192,6 +209,10 @@ public void setFlags(List<BarFlag> flags) {
this.flags = flags;
}

public void setProgressProvider(Supplier<Double> provider) {
this.progressProvider = provider;
}

public void setRange(int range) {
this.range = range;
}
Expand Down Expand Up @@ -232,24 +253,31 @@ public static void bossbar(CommandContext args, CommandSender sender, NPC npc, @
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)) {
Expand Down

0 comments on commit 33dce91

Please sign in to comment.