DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamScoreboardLines.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamScoreboard;
25
26import net.kyori.adventure.text.Component;
27import org.bukkit.scoreboard.Criteria;
28import org.bukkit.scoreboard.DisplaySlot;
29import org.bukkit.scoreboard.Objective;
30import org.bukkit.scoreboard.Scoreboard;
31
32import java.util.LinkedHashMap;
33import java.util.Map;
34import java.util.TreeSet;
35import java.util.function.Supplier;
36
52
53 private Supplier<String> titleSupplier;
54 private Map<Integer, DreamScoreboardData> lines = new LinkedHashMap<>();
55
61 public void createSidebar(Scoreboard scoreboard, String objectiveId) {
62 Objective obj = scoreboard.getObjective(objectiveId);
63 if (obj == null) {
64 obj = scoreboard.registerNewObjective(objectiveId, Criteria.DUMMY, Component.text(titleSupplier.get()));
65 }
66 obj.displayName(Component.text(titleSupplier.get()));
67 obj.setDisplaySlot(DisplaySlot.SIDEBAR);
68
69 // Build lines once (higher score is rendered nearer the top)
70 int teamCount = 0;
71 for (Integer score : new TreeSet<>(lines.keySet()).descendingSet()) {
72 lines.get(score).createLine(scoreboard, score, teamCount++);
73 }
74 }
75
80 public void updateSidebar(Scoreboard scoreboard) {
81 Objective obj = scoreboard.getObjective(DisplaySlot.SIDEBAR);
82 if (obj != null) obj.displayName(Component.text(titleSupplier.get()));
83 for (Map.Entry<Integer, DreamScoreboardData> e : lines.entrySet()) {
84 e.getValue().updateLine(scoreboard, e.getKey());
85 }
86 }
87
88 // ---- Backward-compat names (match older calls) ----
89
91 @Deprecated
92 public void CreateLine(Scoreboard scoreboard, String objectiveId) {
93 createSidebar(scoreboard, objectiveId);
94 }
95
97 @Deprecated
98 public void UpdateLine(Scoreboard scoreboard) {
99 updateSidebar(scoreboard);
100 }
101
102 // ---- Builder ----
103
108
112 public static class PulseScoreboardLinesBuilder {
113 private final Map<Integer, DreamScoreboardData> lines = new LinkedHashMap<>();
114
122 lines.put(score, line);
123 return this;
124 }
125
131 public DreamScoreboardLines build(Supplier<String> titleSupplier){
133 f.titleSupplier = titleSupplier;
134 f.lines = this.lines;
135 return f;
136 }
137 }
138}
DreamScoreboardLines build(Supplier< String > titleSupplier)
Finalizes the frame with a title supplier.
PulseScoreboardLinesBuilder addLine(Integer score, DreamScoreboardData line)
Adds a line at a particular score.
Represents a single “frame” of a sidebar scoreboard: a title plus a set of numbered lines.
void updateSidebar(Scoreboard scoreboard)
Updates the sidebar title and each line’s text for this frame.
static PulseScoreboardLinesBuilder builder()
Creates a builder for DreamScoreboardLines.
void UpdateLine(Scoreboard scoreboard)
Back-compat. Use updateSidebar(Scoreboard).
void createSidebar(Scoreboard scoreboard, String objectiveId)
Creates the sidebar objective and line teams/entries for this frame.
void CreateLine(Scoreboard scoreboard, String objectiveId)
Back-compat. Use createSidebar(Scoreboard, String).
record DreamScoreboardData(Function< Integer, String > text)
Holds a single scoreboard line's dynamic text function.