Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
vytskalt committed Oct 8, 2023
2 parents 67a8f20 + 07d8a37 commit 83f44ec
Show file tree
Hide file tree
Showing 49 changed files with 1,597 additions and 675 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Join the [Discord](https://discord.gg/v7nmTDTW8W) or create an issue for support
- Sidebars. Up to 42 characters (depends on the formatting) for 1.12.2 and below, no limit for newer versions
- Teams. Supports showing different properties (display name, prefix, entries etc.) of the same team to different
players
- Objectives.
- Doesn't require extra dependencies (assuming you're targeting the latest version of Paper)
- Packet-level, meaning it works with other scoreboard plugins (and is faster)
- Supports [Folia](https://github.com/PaperMC/Folia)
Expand Down Expand Up @@ -264,3 +265,17 @@ team.display(player, newTeamDisplay);
// After you've finished using the TeamManager, make sure to close it to prevent a memory leak:
teamManager.close();
```

### ObjectiveManager

```java
ObjectiveManager objectiveManager = scoreboardLibrary.createObjectiveManager();
ScoreboardObjective objective = objectiveManager.create("coolobjective");
objective.value(Component.text("Display name"));
objective.score(player.getName(), 69420);

objectiveManager.addPlayer(player); // Make a player see the objectives

// After you've finished using the ObjectiveManager, make sure to close it to prevent a memory leak:
objectiveManager.close();
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.megavex.scoreboardlibrary.api;

import net.megavex.scoreboardlibrary.api.exception.NoPacketAdapterAvailableException;
import net.megavex.scoreboardlibrary.api.objective.ObjectiveManager;
import net.megavex.scoreboardlibrary.api.sidebar.Sidebar;
import net.megavex.scoreboardlibrary.api.team.TeamManager;
import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -86,6 +87,8 @@ public interface ScoreboardLibrary {
*/
@NotNull TeamManager createTeamManager();

@NotNull ObjectiveManager createObjectiveManager();

/**
* Closes this scoreboard-library instance.
* Should always be called when the plugin is disabled
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.megavex.scoreboardlibrary.api.noop;

import net.megavex.scoreboardlibrary.api.objective.ObjectiveDisplaySlot;
import net.megavex.scoreboardlibrary.api.objective.ObjectiveManager;
import net.megavex.scoreboardlibrary.api.objective.ScoreboardObjective;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.*;

class NoopObjectiveManager implements ObjectiveManager {
private final Map<String, ScoreboardObjective> objectives = new HashMap<>();
private final Map<ObjectiveDisplaySlot, ScoreboardObjective> displaySlots = new HashMap<>();
private final Set<Player> players = new HashSet<>();
private boolean isClosed = true;

@Override
public @NotNull ScoreboardObjective create(@NotNull String name) {
return objectives.computeIfAbsent(name, i -> null);
}

@Override
public void remove(@NotNull ScoreboardObjective objective) {
objectives.values().remove(objective);
}

@Override
public void display(@NotNull ObjectiveDisplaySlot displaySlot, @NotNull ScoreboardObjective objective) {
displaySlots.put(displaySlot, objective);
}

@Override
public @NotNull Collection<Player> players() {
return Collections.unmodifiableSet(players);
}

@Override
public boolean addPlayer(@NotNull Player player) {
return players.add(player);
}

@Override
public boolean removePlayer(@NotNull Player player) {
return players.remove(player);
}

@Override
public void close() {
isClosed = true;
}

@Override
public boolean closed() {
return isClosed;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.megavex.scoreboardlibrary.api.noop;

import net.megavex.scoreboardlibrary.api.ScoreboardLibrary;
import net.megavex.scoreboardlibrary.api.objective.ObjectiveManager;
import net.megavex.scoreboardlibrary.api.sidebar.Sidebar;
import net.megavex.scoreboardlibrary.api.team.TeamManager;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -28,6 +29,12 @@ public final class NoopScoreboardLibrary implements ScoreboardLibrary {
return new NoopTeamManager();
}

@Override
public @NotNull ObjectiveManager createObjectiveManager() {
checkClosed();
return new NoopObjectiveManager();
}

@Override
public void close() {
closed = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.megavex.scoreboardlibrary.api.noop;

import net.kyori.adventure.text.Component;
import net.megavex.scoreboardlibrary.api.objective.ObjectiveRenderType;
import net.megavex.scoreboardlibrary.api.objective.ScoreboardObjective;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class NoopScoreboardObjective implements ScoreboardObjective {
private final Map<String, Integer> scores = new HashMap<>();
private Component value;
private ObjectiveRenderType renderType;

@Override
public @NotNull Component value() {
return value;
}

@Override
public @NotNull ScoreboardObjective value(@NotNull Component value) {
this.value = value;
return this;
}

@Override
public @NotNull ObjectiveRenderType renderType() {
return renderType;
}

@Override
public @NotNull ScoreboardObjective renderType(@NotNull ObjectiveRenderType renderType) {
this.renderType = renderType;
return this;
}

@Override
public Integer score(@NotNull String entry) {
return scores.get(entry);
}

@Override
public @NotNull ScoreboardObjective score(@NotNull String entry, int score) {
scores.put(entry, score);
return this;
}

@Override
public @NotNull ScoreboardObjective removeScore(@NotNull String entry) {
scores.remove(entry);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.megavex.scoreboardlibrary.api.objective;

import com.google.common.base.Preconditions;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* Represents all valid display slots that objectives can be shown in.
*
* @see <a href="https://minecraft.wiki/w/Scoreboard#Display_slots">Minecraft Wiki</a>
*/
@ApiStatus.NonExtendable
public interface ObjectiveDisplaySlot {
static @NotNull PlayerList playerList() {
return PlayerList.INSTANCE;
}

static @NotNull Sidebar sidebar() {
return Sidebar.INSTANCE;
}

static @NotNull BelowName belowName() {
return BelowName.INSTANCE;
}

static @NotNull TeamSidebar teamSidebar(@NotNull NamedTextColor teamColor) {
Preconditions.checkNotNull(teamColor);
return new TeamSidebar(teamColor);
}

class PlayerList implements ObjectiveDisplaySlot {
private static final PlayerList INSTANCE = new PlayerList();

private PlayerList() {
}

@Override
public String toString() {
return "PlayerList";
}
}

class Sidebar implements ObjectiveDisplaySlot {
private static final Sidebar INSTANCE = new Sidebar();

private Sidebar() {
}

@Override
public String toString() {
return "Sidebar";
}
}

class BelowName implements ObjectiveDisplaySlot {
private static final BelowName INSTANCE = new BelowName();

private BelowName() {
}

@Override
public String toString() {
return "BelowName";
}
}

class TeamSidebar implements ObjectiveDisplaySlot {
private final NamedTextColor teamColor;

private TeamSidebar(@NotNull NamedTextColor teamColor) {
this.teamColor = teamColor;
}

public @NotNull NamedTextColor teamColor() {
return teamColor;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TeamSidebar that = (TeamSidebar) o;
return teamColor.equals(that.teamColor);
}

@Override
public int hashCode() {
return teamColor.hashCode();
}

@Override
public String toString() {
return "TeamSidebar{teamColor=" + teamColor + "}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package net.megavex.scoreboardlibrary.api.objective;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

/**
* Manages a group of {@link ScoreboardObjective}s.
* Note: this class is not thread-safe.
*/
public interface ObjectiveManager {
/**
* Creates an objective
*
* @param name Name of the objective
* @return A newly created objective, or an existing one if the name was already registered
*/
@NotNull ScoreboardObjective create(@NotNull String name);

/**
* Removes an objective.
*
* @param objective Objective to remove
*/
void remove(@NotNull ScoreboardObjective objective);

/**
* Updates the objective shown at a display slot
*
* @param displaySlot Display slot value
* @param objective Objective to display at that slot
*/
void display(@NotNull ObjectiveDisplaySlot displaySlot, @NotNull ScoreboardObjective objective);

/**
* @return Players in this ObjectiveManager
*/
@NotNull Collection<Player> players();

/**
* Adds a player to this ObjectiveManager
*
* @param player Player to add to ObjectiveManager
* @return Whether the player was added
*/
boolean addPlayer(@NotNull Player player);

/**
* Adds multiple players to this ObjectiveManager
*
* @param players Players to add
*/
default void addPlayers(@NotNull Collection<Player> players) {
for (Player player : players) {
addPlayer(player);
}
}

/**
* Removes a player from this ObjectiveManager
*
* @param player Player to remove
* @return Whether the player was removed
*/
boolean removePlayer(@NotNull Player player);

/**
* Removes multiple players to this ObjectiveManager
*
* @param players Players to add
*/
default void removePlayers(@NotNull Collection<Player> players) {
for (Player player : players) {
removePlayer(player);
}
}

/**
* Closes this ObjectiveManager.
*/
void close();

/**
* @return Whether this ObjectiveManager is closed
*/
boolean closed();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.megavex.scoreboardlibrary.api.objective;

/**
* Represents all valid render types of an objective.
*/
public enum ObjectiveRenderType {
INTEGER,
HEARTS
}
Loading

0 comments on commit 83f44ec

Please sign in to comment.