Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scoreboard Support and API #66

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/org/cloudburstmc/server/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@
import org.cloudburstmc.server.registry.CommandRegistry;
import org.cloudburstmc.server.registry.EntityRegistry;
import org.cloudburstmc.server.registry.ItemRegistry;
import org.cloudburstmc.server.scoreboard.Scoreboard;
import org.cloudburstmc.server.scoreboard.impl.CloudScoreboard;
import org.cloudburstmc.server.utils.*;

import javax.annotation.Nullable;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.*;
Expand Down Expand Up @@ -221,6 +224,8 @@ public class Player extends Human implements CommandSender, InventoryHolder, Chu

private final PlayerChunkManager chunkManager = new PlayerChunkManager(this);

private Scoreboard scoreboard;

public int packetsRecieved;

public long lastSkinChange;
Expand Down Expand Up @@ -2008,6 +2013,10 @@ public void close(TextContainer message, String reason, boolean notify) {
if (this.fishing != null) {
this.stopFishing(false);
}
if (this.scoreboard != null) {
((CloudScoreboard) this.scoreboard).getPlayers().remove(this);
this.scoreboard = null;
}
}

for (Player player : new ArrayList<>(this.server.getOnlinePlayers().values())) {
Expand Down Expand Up @@ -3343,6 +3352,33 @@ public boolean pickupEntity(Entity entity, boolean near) {
return false;
}

/**
* Gets the current {@link Scoreboard} of
* the player
*
* @return the current scoreboard of the player
*/
@Nullable
public Scoreboard getScoreboard() {
return this.scoreboard;
}

/**
* Sets the player's {@link Scoreboard} to the
* specified scoreboard
*
* @param scoreboard the scoreboard to set
*/
public void setScoreboard(@Nullable Scoreboard scoreboard) {
if (this.scoreboard != null) {
((CloudScoreboard) this.scoreboard).hide(this);
}
if (scoreboard != null) {
((CloudScoreboard) scoreboard).show(this);
}
this.scoreboard = scoreboard;
}

@Override
public String toString() {
return "Player(name=" + getName() + ")";
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/cloudburstmc/server/scoreboard/DisplayMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.cloudburstmc.server.scoreboard;

/**
* Represents where a {@link ScoreboardObjective} should be displayed
* on a {@link Scoreboard}.
*/
public enum DisplayMode {

/**
* Display mode that shows the objective
* in the player list.
*/
LIST,

/**
* Display mode that shows the objective
* ion the sidebar.
*/
SIDEBAR,

/**
* Display mode that shows the objective
* below the player's name.
*/
BELOWNAME
}
81 changes: 81 additions & 0 deletions src/main/java/org/cloudburstmc/server/scoreboard/Score.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.cloudburstmc.server.scoreboard;

import org.cloudburstmc.server.scoreboard.impl.CloudScore;

/**
* Represents a score for a {@link ScoreboardObjective}.
*/
public interface Score<T> {

/**
* Gets the {@link ScoreboardObjective} of
* this score
*
* @return the scoreboard objective of this score
*/
ScoreboardObjective getObjective();

/**
* Gets the amount
*
* @return the amount
*/
int getAmount();

/**
* Sets the amount
*
* @param value the amount
*/
void setAmount(int value);

/**
* Gets the name of this score
*
* @return the name of this score
*/
String getName();

/**
* Gets the type of score this is
*
* @return the type of score this is
*/
ScoreType<T> getScoreType();

/**
* Gets the value of this score
*
* @return the value of this score
*/
T getValue();

/**
* Sets the value of the score
*
* @param value the value of the score
*/
void setValue(T value);

/**
* Creates a new instance of a {@link ScoreBuilder}
*
* @param scoreType the score type
* @param <U> the value
* @return a new instance of a score builder
*/
static <U> ScoreBuilder<U> builder(ScoreType<U> scoreType) {
return CloudScore.providedBuilder(scoreType);
}

interface ScoreBuilder<T> {

ScoreBuilder<T> amount(int amount);

ScoreBuilder<T> name(String name);

ScoreBuilder<T> value(T value);

Score<T> build();
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/cloudburstmc/server/scoreboard/ScoreType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.cloudburstmc.server.scoreboard;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import org.cloudburstmc.server.entity.Entity;
import org.cloudburstmc.server.player.Player;

/**
* Represents the score type for the
* {@link Score} in a {@link Scoreboard}.
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ScoreType<T> {

/**
* A score type where the value is
* that of a player.
*/
public static final ScoreType<Player> PLAYER = new ScoreType<>("player", Player.class);

/**
* A score type where the value is
* that of an entity.
*/
public static final ScoreType<Entity> ENTITY = new ScoreType<>("entity", Entity.class);

/**
* A fake score type that does not
* require an entity or player.
*/
public static final ScoreType<String> FAKE = new ScoreType<>("fake", String.class);

private String name;
private Class<T> supportedType;

/**
* Gets the supported value type for
* this score type
*
* @return the supported value for this score type
*/
public Class<T> getSupportedType() {
return supportedType;
}

@Override
public String toString() {
return name;
}
}
61 changes: 61 additions & 0 deletions src/main/java/org/cloudburstmc/server/scoreboard/Scoreboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.cloudburstmc.server.scoreboard;

import org.cloudburstmc.server.player.Player;
import org.cloudburstmc.server.scoreboard.impl.CloudScoreboard;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Set;

/**
* Represents a scoreboard in the game, which can contain
* various information and be used to display data for players.
*/
public interface Scoreboard {

/**
* Gets the {@link ScoreboardObjective} with the specified name
*
* @param objectiveName the name of the objective
* @return the objective with the specified name
*/
@Nullable
ScoreboardObjective getObjective(String objectiveName);

Set<ScoreboardObjective> getObjectives();

/**
* Registers a new {@link ScoreboardObjective} to the scoreboard
* @param objective the objective
*/
void registerObjective(ScoreboardObjective objective);

/**
* Deregisters a {@link ScoreboardObjective} from the scoreboard.
*
* @param name the name of the objective
*/
void deregisterObjective(String name);

/**
* Creates a new instance of a {@link ScoreboardBuilder}
*
* @return a new instance of a scoreboard builder
*/
static ScoreboardBuilder builder() {
return CloudScoreboard.providedBuilder();
}

interface ScoreboardBuilder {

ScoreboardBuilder objectives(ScoreboardObjective... objectives);

ScoreboardBuilder players(Player... players);

default ScoreboardBuilder players(Collection<Player> players) {
return this.players(players.toArray(new Player[0]));
}

Scoreboard build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.cloudburstmc.server.scoreboard;

/**
* The criterion for a {@link ScoreboardObjective}.
*/
public enum ScoreboardCriteria {

/**
* Dummy scoreboard criterion.
*/
DUMMY
}
Loading