Search in sources :

Example 11 with Score

use of org.spongepowered.api.scoreboard.Score 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();
}
Also used : LanternScore(org.lanternpowered.server.scoreboard.LanternScore) ScoreText(org.spongepowered.api.text.ScoreText) JsonObject(com.google.gson.JsonObject) ScoreText(org.spongepowered.api.text.ScoreText) Text(org.spongepowered.api.text.Text) JsonArray(com.google.gson.JsonArray) Objective(org.spongepowered.api.scoreboard.objective.Objective) Score(org.spongepowered.api.scoreboard.Score) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet)

Aggregations

Score (org.spongepowered.api.scoreboard.Score)11 Objective (org.spongepowered.api.scoreboard.objective.Objective)6 LanternScore (org.lanternpowered.server.scoreboard.LanternScore)5 Text (org.spongepowered.api.text.Text)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 MessagePlayOutScoreboardScore (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutScoreboardScore)3 LanternTeam (org.lanternpowered.server.scoreboard.LanternTeam)3 LanternTexts (org.lanternpowered.server.text.LanternTexts)3 Scoreboard (org.spongepowered.api.scoreboard.Scoreboard)3 Team (org.spongepowered.api.scoreboard.Team)3 Criterion (org.spongepowered.api.scoreboard.critieria.Criterion)3 ObjectiveDisplayMode (org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayMode)3 TextColor (org.spongepowered.api.text.format.TextColor)3 TextColors (org.spongepowered.api.text.format.TextColors)3 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2