-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
ScoreboardTrait.java
89 lines (77 loc) · 2.94 KB
/
ScoreboardTrait.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package net.citizensnpcs.trait;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
@TraitName("scoreboardtrait")
public class ScoreboardTrait extends Trait {
@Persist
private ChatColor color;
private ChatColor previousGlowingColor;
@Persist
private final Set<String> tags = new HashSet<String>();
public ScoreboardTrait() {
super("scoreboardtrait");
}
public void addTag(String tag) {
tags.add(tag);
}
public void apply(Team team, boolean nameVisibility) {
Set<String> newTags = new HashSet<String>(tags);
for (String oldTag : npc.getEntity().getScoreboardTags()) {
if (!newTags.remove(oldTag)) {
npc.getEntity().removeScoreboardTag(oldTag);
}
}
for (String tag : newTags) {
npc.getEntity().addScoreboardTag(tag);
}
if (SUPPORT_TEAM_SETOPTION) {
try {
team.setOption(Option.NAME_TAG_VISIBILITY, nameVisibility ? OptionStatus.ALWAYS : OptionStatus.NEVER);
} catch (NoSuchMethodError e) {
SUPPORT_TEAM_SETOPTION = false;
} catch (NoClassDefFoundError e) {
SUPPORT_TEAM_SETOPTION = false;
}
}
if (npc.data().has(NPC.GLOWING_COLOR_METADATA)) {
color = ChatColor.valueOf(npc.data().get(NPC.GLOWING_COLOR_METADATA));
npc.data().remove(NPC.GLOWING_COLOR_METADATA);
}
if (color != null) {
if (SUPPORT_GLOWING_COLOR) {
try {
if (team.getColor() == null || previousGlowingColor == null
|| (previousGlowingColor != null && color != previousGlowingColor)) {
team.setColor(color);
previousGlowingColor = color;
}
} catch (NoSuchMethodError err) {
SUPPORT_GLOWING_COLOR = false;
}
} else {
if (team.getPrefix() == null || team.getPrefix().length() == 0 || previousGlowingColor == null
|| (previousGlowingColor != null
&& !team.getPrefix().equals(previousGlowingColor.toString()))) {
team.setPrefix(color.toString());
previousGlowingColor = color;
}
}
}
}
public void removeTag(String tag) {
tags.remove(tag);
}
public void setColor(ChatColor color) {
this.color = color;
}
private static boolean SUPPORT_GLOWING_COLOR = true;
private static boolean SUPPORT_TEAM_SETOPTION = true;
}