Remove bossbar on despawn

This commit is contained in:
fullwall 2022-04-09 11:41:12 +08:00
parent af08a77b8e
commit 2a5b62aae0
2 changed files with 22 additions and 4 deletions

View File

@ -342,7 +342,8 @@ public class CitizensNPC extends AbstractNPC {
NMS.replaceTrackerEntry((Player) getEntity());
PlayerUpdateTask.registerPlayer(getEntity());
}
if (SUPPORT_NODAMAGE_TICKS && Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt() != 20) {
if (SUPPORT_NODAMAGE_TICKS && (data().has(NPC.Metadata.SPAWN_NODAMAGE_TICKS)
|| Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt() != 20)) {
try {
entity.setNoDamageTicks(data().get(NPC.Metadata.SPAWN_NODAMAGE_TICKS,
Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt()));

View File

@ -38,6 +38,16 @@ public class BossBarTrait extends Trait {
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;
}
@ -67,13 +77,20 @@ public class BossBarTrait extends Trait {
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 = 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;
BossBar bar = getBar();
if (bar == null) {
return;
}