-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
ScoreboardTrait.java
115 lines (101 loc) · 3.79 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package net.citizensnpcs.trait;
import java.util.HashSet;
import java.util.Iterator;
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;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
@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);
if (SUPPORT_TAGS) {
try {
for (Iterator<String> iterator = npc.getEntity().getScoreboardTags().iterator(); iterator.hasNext();) {
String oldTag = iterator.next();
if (!newTags.remove(oldTag)) {
iterator.remove();
}
}
for (String tag : newTags) {
npc.getEntity().addScoreboardTag(tag);
}
} catch (NoSuchMethodError e) {
SUPPORT_TAGS = false;
}
}
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 (!SUPPORT_TEAM_SETOPTION) {
NMS.setTeamNameTagVisible(team, nameVisibility);
}
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 && Util.getMinecraftRevision().contains("1_12_R1")) {
SUPPORT_GLOWING_COLOR = false;
}
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;
}
}
}
Util.sendTeamPacketToOnlinePlayers(team, 2);
}
public ChatColor getColor() {
return color;
}
public Set<String> getTags() {
return tags;
}
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_TAGS = true;
private static boolean SUPPORT_TEAM_SETOPTION = true;
}