Search in sources :

Example 1 with Objective

use of org.spongepowered.api.scoreboard.objective.Objective in project LanternServer by LanternPowered.

the class JsonTextScoreSerializer method serialize.

@Override
public JsonElement serialize(ScoreText src, Type typeOfSrc, JsonSerializationContext context) {
    // Lantern:
    // This is a field added by sponge to be able to override
    // the text provided by this component.
    final Optional<String> override = src.getOverride();
    // the override as a literal text object
    if (this.networkingFormat && override.isPresent()) {
        return new JsonPrimitive(override.get());
    }
    // There are here some extra fields to represent the (lantern/sponge) score text object,
    // while they are not supported by sponge itself, it seems worth it to provide this
    // This will still remain compatible with vanilla.
    final JsonObject obj = new JsonObject();
    final Score score = src.getScore();
    obj.addProperty(SCORE_NAME, LanternTexts.toLegacy(score.getName()));
    final Iterator<Objective> it = score.getObjectives().iterator();
    if (it.hasNext()) {
        obj.addProperty(SCORE_MAIN_OBJECTIVE, it.next().getName());
        // There is no need to send this to the client.
        if (!this.networkingFormat) {
            if (it.hasNext()) {
                final JsonArray array = new JsonArray();
                while (it.hasNext()) {
                    array.add(new JsonPrimitive(it.next().getName()));
                }
                obj.add(SCORE_EXTRA_OBJECTIVES, array);
            }
        }
    } else {
        // This field must always be specified to be valid score json,
        // making it empty will prevent issues
        obj.addProperty(SCORE_MAIN_OBJECTIVE, "");
    }
    override.ifPresent(v -> obj.addProperty(SCORE_OVERRIDE, override.get()));
    obj.addProperty(SCORE_VALUE, Integer.toString(score.getScore()));
    serialize(obj, src, context);
    return obj;
}
Also used : JsonArray(com.google.gson.JsonArray) Objective(org.spongepowered.api.scoreboard.objective.Objective) Score(org.spongepowered.api.scoreboard.Score) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 2 with Objective

use of org.spongepowered.api.scoreboard.objective.Objective in project LanternServer by LanternPowered.

the class LanternScore method setScore.

@Override
public void setScore(int score) {
    if (this.score == score) {
        return;
    }
    this.score = score;
    final Multimap<Scoreboard, Objective> scoreboards = HashMultimap.create();
    for (Objective objective : this.objectives) {
        for (Scoreboard scoreboard : ((LanternObjective) objective).scoreboards) {
            scoreboards.put(scoreboard, objective);
        }
    }
    if (!scoreboards.isEmpty()) {
        final Map<Objective, Message> messages = new HashMap<>();
        for (Map.Entry<Scoreboard, Objective> entry : scoreboards.entries()) {
            ((LanternScoreboard) entry.getKey()).sendToPlayers(() -> Collections.singletonList(messages.computeIfAbsent(entry.getValue(), obj -> new MessagePlayOutScoreboardScore.CreateOrUpdate(obj.getName(), this.legacyName, score))));
        }
    }
}
Also used : Objective(org.spongepowered.api.scoreboard.objective.Objective) MessagePlayOutScoreboardScore(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutScoreboardScore) Message(org.lanternpowered.server.network.message.Message) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Objective

use of org.spongepowered.api.scoreboard.objective.Objective in project SpongeCommon by SpongePowered.

the class ServerScoreboardMixin method bridge$removeObjective.

@Override
public void bridge$removeObjective(final Objective objective) {
    final net.minecraft.world.scores.Objective scoreObjective = ((SpongeObjective) objective).getObjectiveFor(this);
    ((ScoreboardAccessor) this).accessor$objectivesByName().remove(scoreObjective.getName());
    for (int i = 0; i < 19; ++i) {
        if (this.getDisplayObjective(i) == scoreObjective) {
            this.setDisplayObjective(i, null);
        }
    }
    ((ServerScoreboardBridge) this).bridge$sendToPlayers(new ClientboundSetObjectivePacket(scoreObjective, Constants.Scoreboards.OBJECTIVE_PACKET_REMOVE));
    final List list = ((ScoreboardAccessor) this).accessor$objectivesByCriteria().get(scoreObjective.getCriteria());
    if (list != null) {
        list.remove(scoreObjective);
    }
    for (final Map<net.minecraft.world.scores.Objective, Score> scoreMap : ((ScoreboardAccessor) this).accessor$playerScores().values()) {
        final Score score = scoreMap.remove(scoreObjective);
        if (score != null) {
            ((ScoreBridge) score).bridge$getSpongeScore().removeScoreFor(scoreObjective);
        }
    }
    this.shadow$setDirty();
    ((SpongeObjective) objective).removeObjectiveFor(this);
}
Also used : ClientboundSetObjectivePacket(net.minecraft.network.protocol.game.ClientboundSetObjectivePacket) SpongeObjective(org.spongepowered.common.scoreboard.SpongeObjective) Objective(org.spongepowered.api.scoreboard.objective.Objective) SpongeObjective(org.spongepowered.common.scoreboard.SpongeObjective) Score(net.minecraft.world.scores.Score) SpongeScore(org.spongepowered.common.scoreboard.SpongeScore) PlayerList(net.minecraft.server.players.PlayerList) ArrayList(java.util.ArrayList) List(java.util.List) ServerScoreboardBridge(org.spongepowered.common.bridge.server.ServerScoreboardBridge)

Example 4 with Objective

use of org.spongepowered.api.scoreboard.objective.Objective in project SpongeCommon by SpongePowered.

the class ServerScoreboardMixin method setDisplayObjective.

/**
 * @author Aaron1011 - December 28th, 2015
 * @reason use our mixin scoreboard implementation.
 *
 * @param slot The slot of the display
 * @param objective The objective
 */
@Override
@Overwrite
public void setDisplayObjective(final int slot, @Nullable final net.minecraft.world.scores.Objective objective) {
    final Objective apiObjective = objective == null ? null : ((ObjectiveBridge) objective).bridge$getSpongeObjective();
    this.bridge$updateDisplaySlot(apiObjective, slot);
}
Also used : Objective(org.spongepowered.api.scoreboard.objective.Objective) SpongeObjective(org.spongepowered.common.scoreboard.SpongeObjective) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 5 with Objective

use of org.spongepowered.api.scoreboard.objective.Objective in project LanternServer by LanternPowered.

the class ScoreboardIO method read.

public static Scoreboard read(Path worldFolder) throws IOException {
    DataView dataView = IOHelper.<DataView>read(worldFolder.resolve(SCOREBOARD_DATA), file -> {
        try {
            return NbtStreamUtils.read(Files.newInputStream(file), true);
        } catch (IOException e) {
            throw new IOException("Unable to access " + file.getFileName() + "!", e);
        }
    }).orElse(null);
    final Scoreboard.Builder scoreboardBuilder = Scoreboard.builder();
    if (dataView == null) {
        return scoreboardBuilder.build();
    }
    final Map<String, Objective> objectives = new HashMap<>();
    dataView = dataView.getView(DATA).orElseThrow(() -> new IllegalStateException("Unable to find the data compound."));
    dataView.getViewList(OBJECTIVES).ifPresent(list -> list.forEach(entry -> {
        final String name = entry.getString(NAME).get();
        final Text displayName = LanternTexts.fromLegacy(entry.getString(DISPLAY_NAME).get());
        final Criterion criterion = Sponge.getRegistry().getType(Criterion.class, entry.getString(CRITERION_NAME).get()).orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a criterion with id: {}, default to dummy.", entry.getString(CRITERION_NAME).get());
            return Criteria.DUMMY;
        });
        final ObjectiveDisplayMode objectiveDisplayMode = Sponge.getRegistry().getType(ObjectiveDisplayMode.class, entry.getString(DISPLAY_MODE).get()).orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a display mode with id: {}, default to integer.", entry.getString(CRITERION_NAME).get());
            return ObjectiveDisplayModes.INTEGER;
        });
        objectives.put(name, Objective.builder().name(name).displayName(displayName).criterion(criterion).objectiveDisplayMode(objectiveDisplayMode).build());
    }));
    dataView.getViewList(SCORES).ifPresent(list -> list.forEach(entry -> {
        // We have to keep all the entries to remain compatible with vanilla mc.
        if (entry.getInt(INVALID).orElse(0) > 0) {
            return;
        }
        final Text name = LanternTexts.fromLegacy(entry.getString(NAME).get());
        final int value = entry.getInt(SCORE).get();
        // TODO
        final boolean locked = entry.getInt(LOCKED).orElse(0) > 0;
        final String objectiveName = entry.getString(OBJECTIVE).get();
        Score score = null;
        Objective objective = objectives.get(objectiveName);
        if (objective != null) {
            score = addToObjective(objective, null, name, value);
        }
        final List<String> extraObjectives = entry.getStringList(EXTRA_OBJECTIVES).orElse(null);
        if (extraObjectives != null) {
            for (String extraObjective : extraObjectives) {
                objective = objectives.get(extraObjective);
                if (objective != null) {
                    score = addToObjective(objective, score, name, value);
                }
            }
        }
    }));
    final List<Team> teams = new ArrayList<>();
    dataView.getViewList(TEAMS).ifPresent(list -> list.forEach(entry -> {
        final Team.Builder builder = Team.builder().allowFriendlyFire(entry.getInt(ALLOW_FRIENDLY_FIRE).orElse(0) > 0).canSeeFriendlyInvisibles(entry.getInt(CAN_SEE_FRIENDLY_INVISIBLES).orElse(0) > 0).name(entry.getString(NAME).get()).displayName(LanternTexts.fromLegacy(entry.getString(DISPLAY_NAME).get())).prefix(LanternTexts.fromLegacy(entry.getString(PREFIX).get())).suffix(LanternTexts.fromLegacy(entry.getString(SUFFIX).get())).members(entry.getStringList(MEMBERS).get().stream().map(LanternTexts::fromLegacy).collect(Collectors.toSet()));
        entry.getString(NAME_TAG_VISIBILITY).ifPresent(value -> builder.nameTagVisibility(Sponge.getRegistry().getAllOf(Visibility.class).stream().filter(visibility -> visibility.getName().equals(value)).findFirst().orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a name tag visibility with id: {}, default to always.", value);
            return Visibilities.ALWAYS;
        })));
        entry.getString(DEATH_MESSAGE_VISIBILITY).ifPresent(value -> builder.deathTextVisibility(Sponge.getRegistry().getAllOf(Visibility.class).stream().filter(visibility -> visibility.getName().equals(value)).findFirst().orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a death message visibility with id: {}, default to always.", value);
            return Visibilities.ALWAYS;
        })));
        entry.getString(COLLISION_RULE).ifPresent(value -> builder.collisionRule(Sponge.getRegistry().getAllOf(CollisionRule.class).stream().filter(visibility -> visibility.getName().equals(value)).findFirst().orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a collision rule with id: {}, default to never.", value);
            return CollisionRules.NEVER;
        })));
        entry.getString(TEAM_COLOR).ifPresent(color -> {
            TextColor textColor = Sponge.getRegistry().getType(TextColor.class, color).orElseGet(() -> {
                Lantern.getLogger().warn("Unable to find a team color with id: {}, default to none.", color);
                return TextColors.NONE;
            });
            if (textColor != TextColors.NONE && textColor != TextColors.RESET) {
                builder.color(textColor);
            }
        });
        teams.add(builder.build());
    }));
    final Scoreboard scoreboard = scoreboardBuilder.objectives(new ArrayList<>(objectives.values())).teams(teams).build();
    dataView.getView(DISPLAY_SLOTS).ifPresent(displaySlots -> {
        for (DataQuery key : displaySlots.getKeys(false)) {
            final Matcher matcher = DISPLAY_SLOT_PATTERN.matcher(key.getParts().get(0));
            if (matcher.matches()) {
                final int internalId = Integer.parseInt(matcher.group(1));
                Lantern.getRegistry().getRegistryModule(DisplaySlotRegistryModule.class).get().getByInternalId(internalId).ifPresent(slot -> {
                    final Objective objective = objectives.get(displaySlots.getString(key).get());
                    if (objective != null) {
                        scoreboard.updateDisplaySlot(objective, slot);
                    }
                });
            }
        }
    });
    return scoreboard;
}
Also used : CollisionRule(org.spongepowered.api.scoreboard.CollisionRule) Visibility(org.spongepowered.api.scoreboard.Visibility) HashMap(java.util.HashMap) LanternTexts(org.lanternpowered.server.text.LanternTexts) DataQuery(org.spongepowered.api.data.DataQuery) LanternObjective(org.lanternpowered.server.scoreboard.LanternObjective) ArrayList(java.util.ArrayList) ObjectiveDisplayMode(org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayMode) NbtStreamUtils(org.lanternpowered.server.data.persistence.nbt.NbtStreamUtils) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Objective(org.spongepowered.api.scoreboard.objective.Objective) TextColors(org.spongepowered.api.text.format.TextColors) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) DisplaySlotRegistryModule(org.lanternpowered.server.game.registry.type.scoreboard.DisplaySlotRegistryModule) Iterator(java.util.Iterator) Files(java.nio.file.Files) Criterion(org.spongepowered.api.scoreboard.critieria.Criterion) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) Visibilities(org.spongepowered.api.scoreboard.Visibilities) Set(java.util.Set) LanternDisplaySlot(org.lanternpowered.server.scoreboard.LanternDisplaySlot) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) Team(org.spongepowered.api.scoreboard.Team) IOException(java.io.IOException) LanternTeam(org.lanternpowered.server.scoreboard.LanternTeam) Collectors(java.util.stream.Collectors) Score(org.spongepowered.api.scoreboard.Score) Criteria(org.spongepowered.api.scoreboard.critieria.Criteria) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) List(java.util.List) DataView(org.spongepowered.api.data.DataView) CollisionRules(org.spongepowered.api.scoreboard.CollisionRules) Lantern(org.lanternpowered.server.game.Lantern) ObjectiveDisplayModes(org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayModes) Pattern(java.util.regex.Pattern) LanternScoreboard(org.lanternpowered.server.scoreboard.LanternScoreboard) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) LanternScoreboard(org.lanternpowered.server.scoreboard.LanternScoreboard) Criterion(org.spongepowered.api.scoreboard.critieria.Criterion) ArrayList(java.util.ArrayList) List(java.util.List) Team(org.spongepowered.api.scoreboard.Team) LanternTeam(org.lanternpowered.server.scoreboard.LanternTeam) DataQuery(org.spongepowered.api.data.DataQuery) TextColor(org.spongepowered.api.text.format.TextColor) ObjectiveDisplayMode(org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayMode) CollisionRule(org.spongepowered.api.scoreboard.CollisionRule) Text(org.spongepowered.api.text.Text) IOException(java.io.IOException) DataView(org.spongepowered.api.data.DataView) LanternObjective(org.lanternpowered.server.scoreboard.LanternObjective) Objective(org.spongepowered.api.scoreboard.objective.Objective) Score(org.spongepowered.api.scoreboard.Score) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) DisplaySlotRegistryModule(org.lanternpowered.server.game.registry.type.scoreboard.DisplaySlotRegistryModule) Visibility(org.spongepowered.api.scoreboard.Visibility)

Aggregations

Objective (org.spongepowered.api.scoreboard.objective.Objective)9 Score (org.spongepowered.api.scoreboard.Score)6 LanternScore (org.lanternpowered.server.scoreboard.LanternScore)5 List (java.util.List)4 Scoreboard (org.spongepowered.api.scoreboard.Scoreboard)4 Text (org.spongepowered.api.text.Text)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 LanternTeam (org.lanternpowered.server.scoreboard.LanternTeam)3 LanternTexts (org.lanternpowered.server.text.LanternTexts)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 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