Skip to content

Commit 743e365

Browse files
committed
Release 1.0.0
1 parent 4627049 commit 743e365

File tree

4 files changed

+227
-1
lines changed

4 files changed

+227
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
SBoards.iml
12
target/
3+
.idea/
4+
.idea/discord.xml
25
pom.xml.tag
36
pom.xml.releaseBackup
47
pom.xml.versionsBackup

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
11
# SBoards
2-
Easy to use Scoreboard api for Minestom
2+
The Minestom Scoreboard api is not very good if you want to make a scoreboard that is different for each player.
3+
<br> So i made this easy to use api
4+
<br><br>
5+
This api is inspired by [JScoreboards](https://github.com/JordanOsterberg/JScoreboards)
6+
7+
## Usage
8+
````
9+
// First we need to construct an SBoard
10+
SBoard board = new SBoard(
11+
// Each supplier has a Player object (This is the player the scoreboard is beign showed to)
12+
(player) -> {
13+
// This is a Title supplier.
14+
// You have to return an component
15+
return Component.text("Hello " + player.getUsername());
16+
},
17+
(player) -> {
18+
// This is the lines supplier
19+
// You have to return a List<Component>
20+
// Note that 15 lines is the max
21+
return Arrays.asList(
22+
Component.text("Line1"),
23+
Component.text("Line2"),
24+
Component.text("Line3")
25+
);
26+
}
27+
);
28+
29+
// Now that we have a scoreboard we need to add a player to it
30+
board.addPlayer(player);
31+
32+
33+
// Now we have made a scoreboard and added a player
34+
// Now everytime you need to update the scoreboard you call
35+
36+
// To update for 1 player
37+
board.update(player);
38+
39+
// Todo update it for every player
40+
board.updateAll();
41+
42+
43+
44+
// You can also remove a player by using
45+
board.removePlayer(player);
46+
47+
// Or to remove all
48+
board.removeAll();
49+
50+
````
51+
52+
There are 2 exceptions:
53+
<br>SBoardNotFoundException - Gets called when you are trying to update or remove a player from a scoreboard that doesnt exist
54+
<br>SBoardMaxLinesException - Gets called when you have more then 15 lines specified
55+
<br><br>
56+
## Support
57+
If you find any bugs: Create an Issue <br>
58+
or if want to contact me <br>
59+
Discord: sqcred#1143
60+
<br><br>
61+
## Contribute
62+
You can always make a pull request if you want to add and/or improve some things

pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.sqcred</groupId>
8+
<artifactId>SBoards</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
</properties>
15+
16+
<repositories>
17+
<repository>
18+
<id>spongepowered</id>
19+
<url>https://repo.spongepowered.org/maven</url>
20+
</repository>
21+
<repository>
22+
<id>jitpack</id>
23+
<url>https://jitpack.io</url>
24+
</repository>
25+
</repositories>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.github.Minestom</groupId>
30+
<artifactId>Minestom</artifactId>
31+
<version>-SNAPSHOT</version>
32+
<exclusions>
33+
<exclusion>
34+
<groupId>org.jboss.shrinkwrap.resolver</groupId>
35+
<artifactId>shrinkwrap-resolver-depchain</artifactId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
</dependencies>
40+
41+
</project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.sqcred.sboards;
2+
3+
import net.kyori.adventure.text.Component;
4+
5+
import net.minestom.server.entity.Player;
6+
import net.minestom.server.scoreboard.Sidebar;
7+
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.function.Function;
12+
13+
/*
14+
MIT License
15+
16+
Copyright (c) 2022 sqcred
17+
18+
Permission is hereby granted, free of charge, to any person obtaining a copy
19+
of this software and associated documentation files (the "Software"), to deal
20+
in the Software without restriction, including without limitation the rights
21+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22+
copies of the Software, and to permit persons to whom the Software is
23+
furnished to do so, subject to the following conditions:
24+
25+
The above copyright notice and this permission notice shall be included in all
26+
copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34+
SOFTWARE.
35+
*/
36+
37+
public class SBoard {
38+
39+
private static final int MAX_LINES = 15;
40+
41+
private Map<Player, Sidebar> boards = new HashMap<>();
42+
43+
private final Function<Player, Component> title;
44+
private final Function<Player, List<Component>> lines;
45+
46+
public SBoard(Function<Player, Component> title, Function<Player, List<Component>> lines) {
47+
this.title = title;
48+
this.lines = lines;
49+
}
50+
51+
public void addPlayer(Player player){
52+
if(boards.containsKey(player)) return;
53+
Sidebar sidebar = new Sidebar(title.apply(player));
54+
List<Component> lineList = lines.apply(player);
55+
int lineNameInt = 0;
56+
int lineInt = lineList.size();
57+
for (Component component : lineList) {
58+
if(lineInt == 0 || lineList.size() > MAX_LINES) {
59+
throw new SBoardMaxLinesException();
60+
}
61+
Sidebar.ScoreboardLine scoreboardLine = new Sidebar.ScoreboardLine("line" + lineNameInt, component, lineInt);
62+
lineNameInt++;
63+
lineInt--;
64+
sidebar.createLine(scoreboardLine);
65+
}
66+
sidebar.addViewer(player);
67+
boards.put(player, sidebar);
68+
}
69+
70+
public void removePlayer(Player player){
71+
if(boards.containsKey(player)){
72+
Sidebar sidebar = boards.get(player);
73+
sidebar.removeViewer(player);
74+
boards.remove(player);
75+
return;
76+
}
77+
throw new SBoardNotFoundException(player);
78+
}
79+
80+
public void removeAll(){
81+
for (Player player : boards.keySet()){
82+
removePlayer(player);
83+
}
84+
}
85+
86+
public void update(Player player){
87+
if(boards.containsKey(player)){
88+
Sidebar sidebar = boards.get(player);
89+
sidebar.setTitle(title.apply(player));
90+
List<Component> linesList = lines.apply(player);
91+
for(Sidebar.ScoreboardLine line : sidebar.getLines()){
92+
int number = Integer.parseInt(line.getId().split("line")[1]);
93+
sidebar.updateLineContent(line.getId(), linesList.get(number));
94+
}
95+
return;
96+
}
97+
throw new SBoardNotFoundException(player);
98+
}
99+
100+
public void updateAll(){
101+
for(Player player : boards.keySet()){
102+
update(player);
103+
}
104+
}
105+
106+
public static class SBoardNotFoundException extends RuntimeException {
107+
108+
public SBoardNotFoundException(Player player) {
109+
super("SBoard for player: " + player.getUsername() + " not found!");
110+
}
111+
112+
}
113+
114+
public static class SBoardMaxLinesException extends RuntimeException {
115+
116+
public SBoardMaxLinesException() {
117+
super("15 lines is the max.");
118+
}
119+
120+
}
121+
122+
}

0 commit comments

Comments
 (0)