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;
}
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))));
}
}
}
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);
}
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);
}
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;
}
Aggregations