use of org.spongepowered.api.scoreboard.Score in project SpongeCommon by SpongePowered.
the class SpongeObjective method updateScores.
public void updateScores(net.minecraft.scoreboard.Scoreboard scoreboard) {
ScoreObjective objective = this.getObjectiveFor(scoreboard);
for (Score score : this.getScores().values()) {
SpongeScore spongeScore = (SpongeScore) score;
this.addScoreToScoreboard(scoreboard, spongeScore.getScoreFor(objective));
}
}
use of org.spongepowered.api.scoreboard.Score 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.Score in project LanternServer by LanternPowered.
the class LanternObjective method removeScore.
@Override
public boolean removeScore(Text name) {
final Score score = this.scores.remove(checkNotNull(name, "name"));
if (score != null) {
((LanternScore) score).removeObjective(this);
this.updateClientAfterRemove(score);
return true;
}
return false;
}
use of org.spongepowered.api.scoreboard.Score in project SpongeCommon by SpongePowered.
the class SpongeObjective method removeScore.
@Override
public boolean removeScore(Score spongeScore) {
String name = ((SpongeScore) spongeScore).legacyName;
if (!this.scores.containsKey(spongeScore.getName())) {
return false;
}
for (ScoreObjective objective : this.objectives.values()) {
net.minecraft.scoreboard.Scoreboard scoreboard = objective.scoreboard;
Map<?, ?> map = scoreboard.entitiesScoreObjectives.get(name);
if (map != null) {
net.minecraft.scoreboard.Score score = (net.minecraft.scoreboard.Score) map.remove(objective);
if (map.size() < 1) {
Map<?, ?> map1 = scoreboard.entitiesScoreObjectives.remove(name);
if (map1 != null) {
scoreboard.broadcastScoreUpdate(name);
}
} else if (score != null) {
scoreboard.broadcastScoreUpdate(name, objective);
}
}
((SpongeScore) spongeScore).removeScoreFor(objective);
}
this.scores.remove(spongeScore.getName());
return true;
}
use of org.spongepowered.api.scoreboard.Score in project SpongeAPI by SpongePowered.
the class Text method of.
/**
* Builds a {@link Text} from a given array of objects.
*
* <p>For instance, you can use this like
* <code>Text.of(TextColors.DARK_AQUA, "Hi", TextColors.AQUA, "Bye")</code>
* </p>
*
* <p>This will create the correct {@link Text} instance if the input object
* is the input for one of the {@link Text} types or convert the object to a
* string otherwise.</p>
*
* <p>For instances of type {@link TextRepresentable} (e.g. {@link Text},
* {@link Builder}, ...) the formatting of appended text has priority over
* the current formatting in the method, e.g. the following results in a
* green, then yellow and at the end green again {@link Text}:</p>
*
* <code>Text.of(TextColors.GREEN, "Hello ", Text.of(TextColors.YELLOW,
* "Spongie"), '!');</code>
*
* @param objects The object array
* @return The built text object
*/
public static Text of(Object... objects) {
// Shortcut for lonely TextRepresentables
if (objects.length == 1 && objects[0] instanceof TextRepresentable) {
return ((TextRepresentable) objects[0]).toText();
}
final Text.Builder builder = builder();
TextFormat format = TextFormat.NONE;
HoverAction<?> hoverAction = null;
ClickAction<?> clickAction = null;
ShiftClickAction<?> shiftClickAction = null;
boolean changedFormat = false;
for (Object obj : objects) {
// Text formatting + actions
if (obj instanceof TextFormat) {
changedFormat = true;
format = (TextFormat) obj;
} else if (obj instanceof TextColor) {
changedFormat = true;
format = format.color((TextColor) obj);
} else if (obj instanceof TextStyle) {
changedFormat = true;
format = format.style(obj.equals(TextStyles.RESET) ? TextStyles.NONE : format.getStyle().and((TextStyle) obj));
} else if (obj instanceof TextAction) {
changedFormat = true;
if (obj instanceof HoverAction) {
hoverAction = (HoverAction<?>) obj;
} else if (obj instanceof ClickAction) {
clickAction = (ClickAction<?>) obj;
} else if (obj instanceof ShiftClickAction) {
shiftClickAction = (ShiftClickAction<?>) obj;
} else {
// Unsupported TextAction
}
} else if (obj instanceof TextRepresentable) {
// Special content
changedFormat = false;
Text.Builder childBuilder = ((TextRepresentable) obj).toText().toBuilder();
// Merge format (existing format has priority)
childBuilder.format(format.merge(childBuilder.format));
// Overwrite text actions if *NOT* present
if (childBuilder.clickAction == null) {
childBuilder.clickAction = clickAction;
}
if (childBuilder.hoverAction == null) {
childBuilder.hoverAction = hoverAction;
}
if (childBuilder.shiftClickAction == null) {
childBuilder.shiftClickAction = shiftClickAction;
}
builder.append(childBuilder.build());
} else {
// Simple content
changedFormat = false;
Text.Builder childBuilder;
if (obj instanceof String) {
childBuilder = builder((String) obj);
} else if (obj instanceof Translation) {
childBuilder = builder((Translation) obj);
} else if (obj instanceof Translatable) {
childBuilder = builder(((Translatable) obj).getTranslation());
} else if (obj instanceof Selector) {
childBuilder = builder((Selector) obj);
} else if (obj instanceof Score) {
childBuilder = builder((Score) obj);
} else {
childBuilder = builder(String.valueOf(obj));
}
if (hoverAction != null) {
childBuilder.onHover(hoverAction);
}
if (clickAction != null) {
childBuilder.onClick(clickAction);
}
if (shiftClickAction != null) {
childBuilder.onShiftClick(shiftClickAction);
}
builder.append(childBuilder.format(format).build());
}
}
if (changedFormat) {
// Did the formatting change without being applied to something?
// Then just append an empty text with that formatting
final Text.Builder childBuilder = builder();
if (hoverAction != null) {
childBuilder.onHover(hoverAction);
}
if (clickAction != null) {
childBuilder.onClick(clickAction);
}
if (shiftClickAction != null) {
childBuilder.onShiftClick(shiftClickAction);
}
builder.append(childBuilder.format(format).build());
}
if (builder.children.size() == 1) {
// Single content, reduce Text depth
return builder.children.get(0);
}
return builder.build();
}
Aggregations