11package com .nisovin .magicspells .spelleffects .effecttypes ;
22
3- import java .util .Map ;
4- import java .util .HashMap ;
3+ import java .util .UUID ;
4+
5+ import it .unimi .dsi .fastutil .objects .Object2IntMap ;
6+ import it .unimi .dsi .fastutil .objects .Object2IntOpenHashMap ;
57
68import org .bukkit .Bukkit ;
79import org .bukkit .boss .BarColor ;
1315import com .nisovin .magicspells .util .Util ;
1416import com .nisovin .magicspells .MagicSpells ;
1517import com .nisovin .magicspells .util .SpellData ;
16- import com .nisovin .magicspells .util .config .ConfigData ;
17- import com .nisovin .magicspells .util .config .ConfigDataUtil ;
1818import com .nisovin .magicspells .variables .Variable ;
19+ import com .nisovin .magicspells .util .config .ConfigData ;
1920import com .nisovin .magicspells .spelleffects .SpellEffect ;
21+ import com .nisovin .magicspells .util .config .ConfigDataUtil ;
2022import com .nisovin .magicspells .util .managers .BossBarManager .Bar ;
2123
2224public class BossBarEffect extends SpellEffect {
2325
24- private static final Map < String , Integer > tasks = new HashMap <>() ;
26+ private static final Object2IntMap < TaskData > tasks ;
2527
26- private String namespaceKey ;
28+ static {
29+ tasks = new Object2IntOpenHashMap <>();
30+ tasks .defaultReturnValue (-1 );
31+ }
32+
33+ private ConfigData <String > namespaceKey ;
2734
2835 private String title ;
2936
@@ -46,10 +53,7 @@ public class BossBarEffect extends SpellEffect {
4653
4754 @ Override
4855 protected void loadFromConfig (ConfigurationSection config ) {
49- namespaceKey = config .getString ("namespace-key" );
50- if (namespaceKey != null && !MagicSpells .getBossBarManager ().isNamespaceKey (namespaceKey )) {
51- MagicSpells .error ("Wrong namespace-key defined! '" + namespaceKey + "'" );
52- }
56+ namespaceKey = ConfigDataUtil .getString (config , "namespace-key" , null );
5357
5458 title = config .getString ("title" , "" );
5559
@@ -108,6 +112,9 @@ public void turnOff() {
108112 private void updateBar (Player player , SpellData data ) {
109113 boolean remove = this .remove .get (data );
110114
115+ String namespaceKey = this .namespaceKey .get (data );
116+ if (namespaceKey != null && !MagicSpells .getBossBarManager ().isNamespaceKey (namespaceKey )) return ;
117+
111118 Bar bar = MagicSpells .getBossBarManager ().getBar (player , namespaceKey , !remove );
112119 if (remove ) {
113120 if (bar != null ) bar .remove ();
@@ -139,16 +146,19 @@ private void updateBar(Player player, SpellData data) {
139146
140147 int duration = this .duration .get (data );
141148 if (duration > 0 ) {
142- String key = bar .getNamespaceKey ();
143- if (tasks .containsKey (key )) Bukkit .getScheduler ().cancelTask (tasks .get (key ));
149+ TaskData taskData = new TaskData (bar .getNamespaceKey (), player .getUniqueId ());
144150
145- int task = MagicSpells .scheduleDelayedTask (() -> {
146- tasks .remove ( key );
151+ int newTask = MagicSpells .scheduleDelayedTask (() -> {
152+ tasks .removeInt ( taskData );
147153 bar .remove ();
148154 }, duration );
149155
150- tasks .put (key , task );
156+ int oldTask = tasks .put (taskData , newTask );
157+ if (oldTask != -1 ) Bukkit .getScheduler ().cancelTask (oldTask );
151158 }
152159 }
153160
161+ private record TaskData (String key , UUID uuid ) {
162+ }
163+
154164}
0 commit comments