Skip to content

Commit

Permalink
Refactor Scoreboard API
Browse files Browse the repository at this point in the history
Changes include:

  * Per-world scoreboards have been removed
  * The 'server' scoreboard is now accessible through the API
  * Teams are now registered to only one scoreboard at a time
  • Loading branch information
Aaron1011 committed Dec 29, 2015
1 parent 52130e2 commit 141ccf2
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 72 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/spongepowered/api/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.profile.GameProfileManager;
import org.spongepowered.api.resourcepack.ResourcePack;
import org.spongepowered.api.scoreboard.Scoreboard;
import org.spongepowered.api.world.ChunkTicketManager;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.sink.MessageSink;
Expand Down Expand Up @@ -262,6 +263,21 @@ public interface Server {
*/
boolean saveWorldProperties(WorldProperties properties);

/**
* Gets the 'server' scoreboard. In Vanilla, this is the scoreboard of
* dimension 0 (the overworld).
*
* <p>The sever scoreboard is used with the Vanilla /scoreboard command,
* automatic score updating through criteria, and other things.</p>
*
* <p>The server scoreboard may not be available if dimension 0
* is not yet loaded. In Vanilla, this will only occur when the
* server is first starting, as dimension 0 is normally always loaded.</p>
*
* @return the server scoreboard, if available.
*/
Optional<Scoreboard> getServerScoreboard();

/**
* Returns information about the chunk layout used by this server
* implementation.
Expand Down
72 changes: 40 additions & 32 deletions src/main/java/org/spongepowered/api/scoreboard/Scoreboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public interface Scoreboard {
static Builder builder() {
return Sponge.getRegistry().createBuilder(Builder.class);
}

This comment has been minimized.

Copy link
@TheRaspPie

TheRaspPie Dec 30, 2015

I think that blank line shouldn't be removed.

This comment has been minimized.

Copy link
@gabizou

gabizou Dec 30, 2015

Member

instead of commenting on a commit already made in master, comment on the #221 so it isn't forgotten.

/**
* Gets an {@link Objective} on this scoreboard by name, if it exists.
*
Expand All @@ -64,6 +63,16 @@ static Builder builder() {
*/
Optional<Objective> getObjective(String name);

/**
* Adds the specified {@link Objective} to this scoreboard.
*
* @param objective The {@link Objective} add
* @throws IllegalArgumentException if an {@link Objective} with the same
* {@link Objective#getName() name} already exists, or if the
* specified {@link Objective} has already been added.
*/
void addObjective(Objective objective) throws IllegalArgumentException;

/**
* Gets the {@link Objective} currently displayed in a {@link DisplaySlot} on this
* scoreboard, if one is present.
Expand All @@ -84,17 +93,16 @@ static Builder builder() {
* @throws IllegalStateException if the specified {@link Objective} does not exist
* on this scoreboard
*/
void addObjective(@Nullable Objective objective, DisplaySlot displaySlot) throws IllegalStateException;
void updateDisplaySlot(@Nullable Objective objective, DisplaySlot displaySlot) throws IllegalStateException;

/**
* Adds the specified {@link Objective} to this scoreboard.
* Clears any {@link Objective} in the specified slot.
*
* @param objective The {@link Objective} add
* @throws IllegalArgumentException if an {@link Objective} with the same
* {@link Objective#getName() name} already exists, or if the
* specified {@link Objective} has already been added.
* @param slot The {@link DisplaySlot} to remove any {@link Objective} for
*/
void addObjective(Objective objective) throws IllegalArgumentException;
default void clearSlot(DisplaySlot slot) {
this.updateDisplaySlot(null, slot);
}

/**
* Gets all {@link Objective}s of a Criteria on this scoreboard.
Expand All @@ -118,10 +126,23 @@ static Builder builder() {
*/
void removeObjective(Objective objective);

/**
* Gets all the scores on this scoreboard, across all objectives.
*
* <p>If the same {@link Score} has been added to multiple objectives,
* it will only appear once in the set.</p>
*
* @return A set of all scores
*/
Set<Score> getScores();

/**
* Gets all scores with the specified name on this scoreboard,
* across all objectives.
*
* <p>If the same {@link Score} has been added to multiple objectives,
* it will only appear once in the set.</p>
*
* @param name The name whose scores are being retrieved
* @return A set of all scores for the name
*/
Expand All @@ -135,15 +156,6 @@ static Builder builder() {
*/
void removeScores(Text name);

/**
* Gets a {@link Text} member's {@link Team} on this scoreboard.
*
* @param member The {@link Text} to search for
* @return The {@link Text} member's {@link Team}, or Optional.empty()
* if the member has no team
*/
Optional<Team> getMemberTeam(Text member);

/**
* Gets a {@link Team} by name on this scoreboard.
*
Expand All @@ -153,21 +165,15 @@ static Builder builder() {
Optional<Team> getTeam(String teamName);

/**
* Removes the specified {@link Team} to this scoreboard.
* Registers the specified {@link Team} to this scoreboard.
*
* @param team The {@link Team} to remove
*/
void removeTeam(Team team);

/**
* Adds the specified {@link Team} to this scoreboard.
*
* @param team The {@link Team} to add
* @param team The {@link Team} to register
* @throws IllegalArgumentException if a team with the same
* {@link Team#getName() name} already exists, or the specified
* {@link Team} has been added
* {@link Team#getName() name} already exists on this scoreboard, or if the specified
* {@link Team} is already registered to a scoreboard (this scoreboard,
* or another one).
*/
void addTeam(Team team) throws IllegalArgumentException;
void registerTeam(Team team) throws IllegalArgumentException;

/**
* Gets all the {@link Team}s on this scoreboard.
Expand All @@ -177,11 +183,13 @@ static Builder builder() {
Set<Team> getTeams();

/**
* Clears any {@link Objective} in the specified slot.
* Gets a {@link Text} member's {@link Team} on this scoreboard.
*
* @param slot The {@link DisplaySlot} to remove any {@link Objective} for
* @param member The {@link Text} to search for
* @return The {@link Text} member's {@link Team}, or Optional.empty()
* if the member has no team
*/
void clearSlot(DisplaySlot slot);
Optional<Team> getMemberTeam(Text member);

/**
* Represents a builder to create {@link Scoreboard} instances.
Expand Down
67 changes: 48 additions & 19 deletions src/main/java/org/spongepowered/api/scoreboard/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.ResettableBuilder;

import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -44,6 +45,16 @@
*
* <p>Examples of this include players, whose names gain the prefix and suffix of
* the team they are on.</p>
*
* <p>With the exception of {@link #getNameTagVisibility()} (which is handled client-side),
* all of the team options require players to have the same team object (and by extension,
* the same scoreboard).
*
* For example, consider two players who each have different scoreboards set. Each scoreboard has
* a team registered with identical names, each containing the same players. Both players would
* always be able to attack each other, regardless of the value of {@link #allowFriendlyFire()}.
* For it to work, both players must have the same scoreboard, and be on a team registered
* to said scoreboard.</p>
*/
public interface Team {

Expand All @@ -56,7 +67,6 @@ static Builder builder() {
return Sponge.getRegistry().createBuilder(Builder.class);
}


/**
* Gets the name of this team.
*
Expand All @@ -71,6 +81,15 @@ static Builder builder() {
*/
Text getDisplayName();

/**
* Sets the name displayed to users for this team.
*
* @param displayName The {@link Text} to use
* @throws IllegalArgumentException If displayName is longer than 32
* characters (in its legacy representation)
*/
void setDisplayName(Text displayName) throws IllegalArgumentException;

/**
* Gets the color of this team.
*
Expand All @@ -94,15 +113,6 @@ static Builder builder() {
*/
void setColor(TextColor color) throws IllegalArgumentException;

/**
* Sets the name displayed to users for this team.
*
* @param displayName The {@link Text} to use
* @throws IllegalArgumentException If displayName is longer than 32
* characters
*/
void setDisplayName(Text displayName) throws IllegalArgumentException;

/**
* Gets the prefix prepended to the display name of users on this team.
*
Expand Down Expand Up @@ -131,13 +141,17 @@ static Builder builder() {
*
* @param suffix The new suffix for this team.
* @throws IllegalArgumentException If suffix is longer than 16
* characters
* characters (in its legacy representation)
*/
void setSuffix(Text suffix) throws IllegalArgumentException;

/**
* Gets whether friendly fire is enabled.
*
* <p>This option only controls players attacking other players. It has no
* affect other entities attacking other entities, or players attacking
* other entities (or vice-versa).</p>
*
* @return Whether friendly fire is enabled
*/
boolean allowFriendlyFire();
Expand Down Expand Up @@ -185,15 +199,15 @@ static Builder builder() {
*
* @return The {@link Visibility} for this team's death Texts
*/
Visibility getDeathTextVisibility();
Visibility getDeathMessageVisibility();

/**
* Sets the {@link Visibility} which controls who death Texts
* of players on this team are visible to.
*
* @param visibility The {@link Visibility} for this team's death Texts
*/
void setDeathTextVisibility(Visibility visibility);
void setDeathMessageVisibility(Visibility visibility);

/**
* Gets the {@link Text}s representing the members of this team.
Expand Down Expand Up @@ -226,13 +240,26 @@ static Builder builder() {
boolean removeMember(Text member);

/**
* Returns a {@link Set} of parent {@link Scoreboard}s this {@link Team} is
* registered to.
* Returns the scoreboard this team is registered on, if available.
*
* @return A {@link Set} of parent {@link Scoreboard}s this {@link Team} is
* registered to
* <p>This will return {@link Optional#empty()} when a team has
* been removed from its {@link Scoreboard}, or has been created
* but not yet registered.</p>
*
* @return The scoreboard this team is registered on, if available.
*/
Set<Scoreboard> getScoreboards();
Optional<Scoreboard> getScoreboard();

/**
* Unregisters this team from its {@link Scoreboard}, if present.
*
* <p>A team can still be fully used after being unregistered. However,
* it will not affect the game in any way until registered to a {@link Scoreboard}
* again, through {@link Scoreboard#registerTeam(Team)}.</p>
*
* @return Whether this team was registered to a {@link Scoreboard}.
*/
boolean unregister();

/**
* Represents a builder tp create {@link Team} instances.
Expand Down Expand Up @@ -266,9 +293,11 @@ interface Builder extends ResettableBuilder<Team, Builder> {
* <p>Display names may be truncated in order to meet an implementation-defined length limit.
* In Vanilla, this is sixteen characters.</p>
*
* <p>By default, this is set to {@link #name(String)}</p>
*
* @param displayName The {@link Text} to set
* @return This builder
* @throws IllegalArgumentException If the name is invalid
* @throws IllegalArgumentException If the name is longer than 16 characters
*/
Builder displayName(Text displayName) throws IllegalArgumentException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.spongepowered.api.util.ResettableBuilder;

import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -71,7 +72,7 @@ static Builder builder() {
*
* @param displayName Display name to set
* @throws IllegalArgumentException if displayName is longer than 32
* characters
* characters (in its legacy representation)
*/
void setDisplayName(Text displayName) throws IllegalArgumentException;

Expand Down Expand Up @@ -103,6 +104,14 @@ static Builder builder() {
*/
Map<Text, Score> getScores();

/**
* Returns whether this objective has a {@link Score} with the given name.
*
* @param name The name of the {@link Score} to check for.
* @return Whether this objective has a {@link Score} with the given name.
*/

This comment has been minimized.

Copy link
@Zidane

Zidane Dec 30, 2015

Member

With returning optional on getScore, I'd argue that this method has no point.

This comment has been minimized.

Copy link
@kashike

kashike Dec 30, 2015

Contributor

getScore uses hasScore.

boolean hasScore(Text name);

/**
* Adds the specified {@link Score} to this objective.
*
Expand All @@ -112,21 +121,43 @@ static Builder builder() {
void addScore(Score score) throws IllegalArgumentException;

/**
* Gets an entry's {@link Score} for this Objective.
* 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 Text}, if it exists.
*/
default Optional<Score> getScore(Text name) {
if (!this.hasScore(name)) {
return Optional.empty();
}
return Optional.of(this.getOrCreateScore(name));
}

/**
* Gets an entry's {@link Score} for this objective.
*
* <p>If the {@link Score} does not exist, it will be created.</p>
*
* @param name The name of the {@link Score} to get
* @return The {@link Score} for the specified {@link Text}
*/
Score getScore(Text name);
Score getOrCreateScore(Text name);

/**
* Removes the specified {@link Score} to this objective.
* 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);

/**
* Removes the {@link Score} with the specified name from this objectie, if present.
*
* @param name The name of the {@link Score} to remove.
* @return Whether the score existed on this objective
*/
void removeScore(Score score);
boolean removeScore(Text name);

/**
* Returns a {@link Set} of parent {@link Scoreboard}s this
Expand Down
Loading

0 comments on commit 141ccf2

Please sign in to comment.