6
6
import org .bukkit .plugin .Plugin ;
7
7
import org .bukkit .scheduler .BukkitRunnable ;
8
8
9
- import java .util .HashMap ;
10
- import java .util .List ;
9
+ import java .util .* ;
10
+ import java .util .concurrent . ConcurrentHashMap ;
11
11
12
12
@ AllArgsConstructor
13
13
public class HologramManager {
14
14
15
15
private final Plugin plugin ;
16
16
17
17
@ Getter
18
- private final HashMap <TextHologram , BukkitRunnable > hologramAnimations = new HashMap <>();
18
+ private final Map <TextHologram , BukkitRunnable > hologramAnimations = new ConcurrentHashMap <>();
19
19
20
20
@ Getter
21
- private final HashMap <String , TextHologram > hologramsMap = new HashMap <>();
21
+ private final Map <String , TextHologram > hologramsMap = new ConcurrentHashMap <>();
22
22
23
23
public List <TextHologram > getHolograms () {
24
- return ( List < TextHologram >) this .hologramsMap .values ();
24
+ return new ArrayList <>( this .hologramsMap .values () );
25
25
}
26
26
27
27
public void spawn (TextHologram textHologram , Location location ) {
@@ -38,9 +38,7 @@ public void remove(TextHologram textHologram) {
38
38
}
39
39
40
40
public void remove (String id ) {
41
- if (!hologramsMap .containsKey (id )) return ;
42
- this .hologramsMap .get (id ).kill ();
43
- this .hologramsMap .remove (id );
41
+ Optional .ofNullable (this .hologramsMap .remove (id )).ifPresent (TextHologram ::kill );
44
42
}
45
43
46
44
public void removeAll () {
@@ -54,21 +52,17 @@ public void applyAnimation(TextHologram hologram, TextAnimation textAnimation) {
54
52
}
55
53
56
54
public void cancelAnimation (TextHologram hologram ) {
57
- if (hologramAnimations .containsKey (hologram )) {
58
- hologramAnimations .get (hologram ).cancel ();
59
- hologramAnimations .remove (hologram );
60
- }
55
+ Optional .ofNullable (hologramAnimations .remove (hologram )).ifPresent (BukkitRunnable ::cancel );
61
56
}
62
57
63
58
private BukkitRunnable animateHologram (TextHologram hologram , TextAnimation textAnimation ) {
64
59
final BukkitRunnable animation = new BukkitRunnable () {
65
60
int currentFrame = 0 ;
66
61
public void run () {
67
- if (textAnimation .getTextFrames ().isEmpty ()) return ;
62
+ if (textAnimation .getTextFrames ().isEmpty ()) return ;
68
63
hologram .setMiniMessageText (textAnimation .getTextFrames ().get (currentFrame ));
69
64
hologram .update ();
70
- currentFrame ++;
71
- if (currentFrame >= textAnimation .getTextFrames ().size ()) currentFrame = 0 ;
65
+ currentFrame = (currentFrame + 1 ) % textAnimation .getTextFrames ().size ();
72
66
}
73
67
};
74
68
0 commit comments