From 2a5b62aae06740fe09a0a53a8b41ee93697639fd Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 9 Apr 2022 11:41:12 +0800 Subject: [PATCH] Remove bossbar on despawn --- .../net/citizensnpcs/npc/CitizensNPC.java | 3 ++- .../trait/versioned/BossBarTrait.java | 23 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 9d8a2bf2f..1169ffff7 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -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())); diff --git a/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java b/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java index 277e9c639..f4399fd61 100644 --- a/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java @@ -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; }