From 0bce6b81d0a67924ea04ea23cee0d6a6d8780dd4 Mon Sep 17 00:00:00 2001 From: Anselm Brehme Date: Sun, 11 Feb 2024 17:00:58 +0100 Subject: [PATCH] scoreboard scores for entity/gameprofile --- .../api/scoreboard/objective/Objective.java | 101 ++++++++++++++++-- 1 file changed, 91 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/spongepowered/api/scoreboard/objective/Objective.java b/src/main/java/org/spongepowered/api/scoreboard/objective/Objective.java index d3a96d2362..a988e36fb3 100644 --- a/src/main/java/org/spongepowered/api/scoreboard/objective/Objective.java +++ b/src/main/java/org/spongepowered/api/scoreboard/objective/Objective.java @@ -26,6 +26,8 @@ import net.kyori.adventure.text.Component; import org.spongepowered.api.Sponge; +import org.spongepowered.api.entity.Entity; +import org.spongepowered.api.profile.GameProfile; import org.spongepowered.api.scoreboard.Score; import org.spongepowered.api.scoreboard.Scoreboard; import org.spongepowered.api.scoreboard.criteria.Criterion; @@ -35,6 +37,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.UUID; import java.util.function.Supplier; /** @@ -113,6 +116,24 @@ static Builder builder() { */ boolean hasScore(String name); + + /** + * Returns whether this objective has a {@link Score} with the given entity. + * + * @param entity The entity of the {@link Score} to check for. + * @return Whether this objective has a {@link Score} with the given entity. + */ + boolean hasScore(Entity entity); + + + /** + * Returns whether this objective has a {@link Score} with the given profile. + * + * @param profile The profile of the {@link Score} to check for. + * @return Whether this objective has a {@link Score} with the given profile. + */ + boolean hasScore(GameProfile profile); + /** * Adds the specified {@link Score} to this objective. * @@ -121,13 +142,11 @@ static Builder builder() { */ void addScore(Score score) throws IllegalArgumentException; - // TODO findScore for player/entity? - /** * Gets an entry's {@link Score} for this objective, if it exists. * * @param name The name of the {@link Score} to get. - * @return The {@link Score} for te specified {@link Component}, if it exists. + * @return The {@link Score} for the specified name, if it exists. */ default Optional findScore(final String name) { if (!this.hasScore(name)) { @@ -142,25 +161,87 @@ default Optional findScore(final String name) { *

If the {@link Score} does not exist, it will be created.

* * @param name The name of the {@link Score} to get - * @return The {@link Score} for the specified {@link Component} + * @return The {@link Score} for the specified name */ Score findOrCreateScore(String name); /** - * Removes the specified {@link Score} from this objective, if present. + * Removes the {@link Score} with the specified name from this objective, if present. * - * @param score The {@link Score} to remove + * @param name The name of the {@link Score} to remove. * @return Whether the score existed on this objective */ - boolean removeScore(Score score); + boolean removeScore(String name); /** - * Removes the {@link Score} with the specified name from this objective, if present. + * Gets an entry's {@link Score} for this objective, if it exists. * - * @param name The name of the {@link Score} to remove. + * @param entity The entity of the {@link Score} to get. + * @return The {@link Score} for the specified entity, if it exists. + */ + default Optional findScore(final Entity entity) { + if (!this.hasScore(entity)) { + return Optional.empty(); + } + return Optional.of(this.findOrCreateScore(entity)); + } + + /** + * Gets an entry's {@link Score} for this objective. + * + *

If the {@link Score} does not exist, it will be created.

+ * + * @param entity The name of the {@link Score} to get + * @return The {@link Score} for the specified entity + */ + Score findOrCreateScore(Entity entity); + + /** + * Removes the {@link Score} with the specified entity from this objective, if present. + * + * @param entity The entity of the {@link Score} to remove. * @return Whether the score existed on this objective */ - boolean removeScore(String name); + boolean removeScore(Entity entity); + + /** + * Gets an entry's {@link Score} for this objective, if it exists. + * + * @param profile The profile of the {@link Score} to get. + * @return The {@link Score} for the specified profile, if it exists. + */ + default Optional findScore(final GameProfile profile) { + if (!this.hasScore(profile)) { + return Optional.empty(); + } + return Optional.of(this.findOrCreateScore(profile)); + } + + /** + * Gets an entry's {@link Score} for this objective. + * + *

If the {@link Score} does not exist, it will be created.

+ * + * @param profile The profile of the {@link Score} to get + * @return The {@link Score} for the specified profile + */ + Score findOrCreateScore(GameProfile profile); + + /** + * Removes the {@link Score} with the specified profile from this objective, if present. + * + * @param profile The profile of the {@link Score} to remove. + * @return Whether the score existed on this objective + */ + boolean removeScore(GameProfile profile); + + /** + * Removes the specified {@link Score} from this objective, if present. + * + * @param score The {@link Score} to remove + * @return Whether the score existed on this objective + */ + boolean removeScore(Score score); /** * Returns a {@link Set} of parent {@link Scoreboard}s this