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()); NMS.replaceTrackerEntry((Player) getEntity());
PlayerUpdateTask.registerPlayer(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 { try {
entity.setNoDamageTicks(data().get(NPC.Metadata.SPAWN_NODAMAGE_TICKS, entity.setNoDamageTicks(data().get(NPC.Metadata.SPAWN_NODAMAGE_TICKS,
Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt())); Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt()));

View File

@ -38,6 +38,16 @@ public class BossBarTrait extends Trait {
super("bossbar"); 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() { public BarColor getColor() {
return color; return color;
} }
@ -67,13 +77,20 @@ public class BossBarTrait extends Trait {
return visible; return visible;
} }
@Override
public void onDespawn() {
if (barCache != null) {
barCache.removeAll();
barCache.hide();
barCache = null;
}
}
@Override @Override
public void run() { public void run() {
if (!npc.isSpawned()) if (!npc.isSpawned())
return; return;
BossBar bar = isBoss(npc.getEntity()) ? (BossBar) NMS.getBossBar(npc.getEntity()) BossBar bar = getBar();
: barCache == null ? barCache = Bukkit.getServer().createBossBar(npc.getFullName(), color, style,
flags.toArray(new BarFlag[flags.size()])) : barCache;
if (bar == null) { if (bar == null) {
return; return;
} }