-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
NPCScoreboard.java
109 lines (98 loc) · 3.85 KB
/
NPCScoreboard.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
package net.citizensnpcs.trait;
import java.util.Set;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import com.google.common.collect.Sets;
public class NPCScoreboard extends Trait {
private final Set<Objective> objectives = Sets.newHashSet();
private Scoreboard scoreboard;
private String team;
public NPCScoreboard() {
super("npcscoreboard");
}
private void checkScoreboard() {
if (scoreboard == null) {
if (npc.isSpawned() && !npc.getEntity().getMetadata("citizens.scoreboard").isEmpty()) {
scoreboard = (Scoreboard) npc.getEntity().getMetadata("citizens.scoreboard").iterator().next().value();
} else {
scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
}
}
if (npc.getEntity() instanceof Player && npc.getEntity().getMetadata("citizens.scoreboard").isEmpty()) {
npc.getEntity().setMetadata("citizens.scoreboard",
new FixedMetadataValue(CitizensAPI.getPlugin(), scoreboard));
}
}
@Override
public void load(DataKey root) {
checkScoreboard();
if (root.keyExists("team")) {
team = root.getString("team");
}
if (!root.keyExists("objectives"))
return;
for (DataKey sub : root.getRelative("objectives").getSubKeys()) {
String objName = sub.name();
Objective objective;
if (scoreboard.getObjective(objName) != null) {
objective = scoreboard.getObjective(objName);
} else {
objective = scoreboard.registerNewObjective(objName, root.getString("criteria"));
}
objective.setDisplaySlot(DisplaySlot.valueOf(sub.getString("display")));
objectives.add(objective);
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
objective.getScore((Player) npc.getEntity()).setScore(sub.getInt("score"));
}
}
}
@Override
public void onSpawn() {
if (npc.getEntity() instanceof Player && team != null) {
checkScoreboard();
scoreboard.getTeam(team).addPlayer((Player) npc.getEntity());
}
}
public void persistObjective(Objective objective) {
if (!objectives.contains(objective)) {
objectives.add(objective);
}
Team playerTeam = objectives.iterator().next().getScoreboard().getPlayerTeam((Player) npc.getEntity());
if (team == null) {
team = playerTeam.getName();
}
if (scoreboard == null) {
scoreboard = objective.getScoreboard();
}
}
@Override
public void save(DataKey root) {
root.removeKey("objectives");
root.removeKey("team");
if (objectives.isEmpty()) {
return;
}
for (Objective objective : objectives) {
DataKey key = root.getRelative("objectives." + objective.getName());
key.setString("criteria", objective.getCriteria());
key.setString("display", objective.getDisplaySlot().name());
if (npc.getEntity() instanceof Player) {
key.setInt("score", objective.getScore((Player) npc.getEntity()).getScore());
}
}
if (npc.getEntity() instanceof Player && team == null) {
team = objectives.iterator().next().getScoreboard().getPlayerTeam((Player) npc.getEntity()).getName();
}
if (team != null) {
root.setString("team", team);
}
}
}