use of org.lanternpowered.server.scoreboard.LanternScore in project LanternServer by LanternPowered.
the class ScoreboardIO method write.
public static void write(Path folder, Scoreboard scoreboard) throws IOException {
final List<DataView> objectives = scoreboard.getObjectives().stream().map(objective -> DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(NAME, objective.getName()).set(DISPLAY_NAME, ((LanternObjective) objective).getLegacyDisplayName()).set(CRITERION_NAME, objective.getCriterion().getId()).set(DISPLAY_MODE, objective.getDisplayMode().getId())).collect(Collectors.toList());
final List<DataView> scores = new ArrayList<>();
for (Score score : scoreboard.getScores()) {
final Iterator<Objective> it = score.getObjectives().iterator();
final DataView baseView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(NAME, ((LanternScore) score).getLegacyName()).set(SCORE, score.getScore());
// TODO: Locked state
final DataView mainView = baseView.copy().set(OBJECTIVE, it.next().getName());
final List<String> extraObjectives = new ArrayList<>();
while (it.hasNext()) {
final String extraObjectiveName = it.next().getName();
scores.add(baseView.copy().set(OBJECTIVE, extraObjectiveName).set(INVALID, (byte) 1));
extraObjectives.add(extraObjectiveName);
}
if (!extraObjectives.isEmpty()) {
mainView.set(EXTRA_OBJECTIVES, extraObjectives);
}
}
final List<DataView> teams = new ArrayList<>();
for (Team team : scoreboard.getTeams()) {
final DataView container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(ALLOW_FRIENDLY_FIRE, (byte) (team.allowFriendlyFire() ? 1 : 0)).set(CAN_SEE_FRIENDLY_INVISIBLES, (byte) (team.canSeeFriendlyInvisibles() ? 1 : 0)).set(NAME_TAG_VISIBILITY, team.getNameTagVisibility().getName()).set(NAME, team.getName()).set(DISPLAY_NAME, ((LanternTeam) team).getLegacyDisplayName()).set(DEATH_MESSAGE_VISIBILITY, team.getDeathMessageVisibility().getName()).set(COLLISION_RULE, team.getCollisionRule().getName()).set(PREFIX, ((LanternTeam) team).getLegacyPrefix()).set(SUFFIX, ((LanternTeam) team).getLegacySuffix());
final TextColor teamColor = team.getColor();
if (teamColor != TextColors.NONE) {
container.set(TEAM_COLOR, teamColor.getId());
}
final Set<Text> members = team.getMembers();
container.set(MEMBERS, members.stream().map(LanternTexts::toLegacy).collect(Collectors.toList()));
teams.add(container);
}
final DataContainer rootDataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
final DataView dataView = rootDataContainer.createView(DATA).set(OBJECTIVES, objectives).set(SCORES, scores).set(TEAMS, teams);
final DataView displaySlots = dataView.createView(DISPLAY_SLOTS);
((LanternScoreboard) scoreboard).getObjectivesInSlot().entrySet().forEach(entry -> displaySlots.set(DataQuery.of("slot_" + ((LanternDisplaySlot) entry.getKey()).getInternalId()), entry.getValue().getName()));
IOHelper.write(folder.resolve(SCOREBOARD_DATA), file -> {
NbtStreamUtils.write(rootDataContainer, Files.newOutputStream(file), true);
return true;
});
}
use of org.lanternpowered.server.scoreboard.LanternScore in project LanternServer by LanternPowered.
the class JsonTextScoreSerializer method deserialize.
@Override
public ScoreText deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
final JsonObject obj = element.getAsJsonObject();
final Text name = LanternTexts.fromLegacy(obj.get(SCORE_NAME).getAsString());
// Try to parse the value
int value = 0;
try {
value = Integer.parseInt(obj.get(SCORE_VALUE).getAsString());
} catch (NumberFormatException ignored) {
}
final String baseObjective = obj.get(SCORE_MAIN_OBJECTIVE).getAsString();
final Set<Objective> objectives = new HashSet<>();
if (!baseObjective.isEmpty()) {
this.tryAddObjective(baseObjective, objectives);
}
if ((element = obj.get(SCORE_EXTRA_OBJECTIVES)) != null) {
final JsonArray array = element.getAsJsonArray();
for (JsonElement jsonElement : array) {
this.tryAddObjective(jsonElement.getAsString(), objectives);
}
}
String override = null;
if ((element = obj.get(SCORE_OVERRIDE)) != null) {
override = element.getAsString();
}
final Score score = new LanternScore(name);
// TODO: How to handle the objectives?
// We cannot add them to the score without attaching the
// score to the objective
score.setScore(value);
final ScoreText.Builder builder = Text.builder(score).override(override);
deserialize(obj, builder, context);
return builder.build();
}
Aggregations