Skip to content

Commit c254ccb

Browse files
committed
Add #getId() and add null checks
1 parent 1188925 commit c254ccb

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

src/main/java/fr/mrmicky/fastboard/FastBoard.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public class FastBoard {
103103
private final Player player;
104104
private final String id;
105105

106-
private String title;
106+
private String title = ChatColor.RESET.toString();
107107
private List<String> lines = new ArrayList<>();
108108

109109
private boolean deleted = false;
@@ -114,7 +114,7 @@ public class FastBoard {
114114
* @param player the player the scoreboard is for
115115
*/
116116
public FastBoard(Player player) {
117-
this.player = player;
117+
this.player = Objects.requireNonNull(player, "player");
118118

119119
id = "fb-" + Double.toString(Math.random()).substring(2, 10);
120120

@@ -143,7 +143,7 @@ public String getTitle() {
143143
* @throws IllegalStateException if {@link #delete()} was call before
144144
*/
145145
public void updateTitle(String title) {
146-
if (title.equals(this.title)) {
146+
if (this.title.equals(Objects.requireNonNull(title, "title"))) {
147147
return;
148148
}
149149

@@ -185,51 +185,55 @@ public void updateLines(String... lines) {
185185
/**
186186
* Update the lines of the scoreboard
187187
*
188-
* @param newLines the new scoreboard lines
188+
* @param lines the new scoreboard lines
189189
* @throws IllegalArgumentException if one line is longer than 30 chars on 1.12 or lower
190190
* @throws IllegalStateException if {@link #delete()} was call before
191191
*/
192-
public void updateLines(Collection<String> newLines) {
192+
public void updateLines(Collection<String> lines) {
193+
Objects.requireNonNull(lines, "lines");
194+
193195
if (!VersionType.V1_13.isHigherOrEqual()) {
194196
int lineCount = 0;
195-
for (String s : newLines) {
197+
for (String s : lines) {
196198
if (s != null && s.length() > 30) {
197199
throw new IllegalArgumentException("Line " + lineCount + " is longer than 30 chars");
198200
}
199201
lineCount++;
200202
}
201203
}
202204

203-
List<String> oldLines = new ArrayList<>(lines);
204-
lines.clear();
205-
lines.addAll(newLines);
206-
Collections.reverse(lines);
205+
List<String> oldLines = new ArrayList<>(this.lines);
206+
this.lines.clear();
207+
this.lines.addAll(lines);
208+
Collections.reverse(this.lines);
209+
210+
int linesSize = this.lines.size();
207211

208212
try {
209-
if (oldLines.size() != lines.size()) {
213+
if (oldLines.size() != linesSize) {
210214
List<String> oldLinesCopy = new ArrayList<>(oldLines);
211215

212-
if (oldLinesCopy.size() > lines.size()) {
213-
for (int i = oldLinesCopy.size(); i > lines.size(); i--) {
216+
if (oldLinesCopy.size() > linesSize) {
217+
for (int i = oldLinesCopy.size(); i > linesSize; i--) {
214218
sendTeamPacket(i - 1, TeamMode.REMOVE);
215219

216220
sendScorePacket(i - 1, ScoreboardAction.REMOVE);
217221

218222
oldLines.remove(oldLines.size() - 1);
219223
}
220224
} else {
221-
for (int i = oldLinesCopy.size(); i < lines.size(); i++) {
225+
for (int i = oldLinesCopy.size(); i < linesSize; i++) {
222226
sendScorePacket(i, ScoreboardAction.CHANGE);
223227

224228
sendTeamPacket(i, TeamMode.CREATE);
225229

226-
oldLines.add(i, lines.get(i));
230+
oldLines.add(i, this.lines.get(i));
227231
}
228232
}
229233
}
230234

231-
for (int i = 0; i < lines.size(); i++) {
232-
if (!Objects.equals(oldLines.get(i), lines.get(i))) {
235+
for (int i = 0; i < linesSize; i++) {
236+
if (!Objects.equals(oldLines.get(i), this.lines.get(i))) {
233237
sendTeamPacket(i, TeamMode.UPDATE);
234238
}
235239
}
@@ -247,6 +251,15 @@ public Player getPlayer() {
247251
return player;
248252
}
249253

254+
/**
255+
* Get the id of theFastBoard
256+
*
257+
* @return id
258+
*/
259+
public String getId() {
260+
return id;
261+
}
262+
250263
public boolean isDeleted() {
251264
return deleted;
252265
}
@@ -278,7 +291,7 @@ private void sendObjectivePacket(ObjectiveMode mode) throws ReflectiveOperationE
278291
setField(packet, int.class, mode.ordinal());
279292

280293
if (mode != ObjectiveMode.REMOVE) {
281-
setComponentField(packet, title != null ? title : ChatColor.RESET.toString(), 1);
294+
setComponentField(packet, title, 1);
282295

283296
if (VersionType.V1_8.isHigherOrEqual()) {
284297
setField(packet, ENUM_SB_HEALTH_DISPLAY, ENUM_SB_HEALTH_DISPLAY_INTEGER);

0 commit comments

Comments
 (0)