diff --git a/README.md b/README.md index 0e7679f..7785e03 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ A Scoreboard API for Bukkit with 1.7-1.15 support fr.mrmicky FastBoard - 1.0.1 + 1.1.0 compile @@ -82,9 +82,9 @@ board.updateTitle(ChatColor.GOLD + "FastBoard"); // Change the lines board.updateLines( - null, // Empty line + "", // Empty line "One line", - "", // Empty line too + "", // Empty line "Second line" ); ``` diff --git a/pom.xml b/pom.xml index c706925..a30b3d4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ fr.mrmicky fastboard - 1.0.1 + 1.1.0 FastBoard https://github.com/MrMicky-FR/FastBoard @@ -41,7 +41,7 @@ org.spigotmc spigot-api - 1.15-R0.1-SNAPSHOT + 1.15.2-R0.1-SNAPSHOT provided diff --git a/src/main/java/fr/mrmicky/fastboard/FastBoard.java b/src/main/java/fr/mrmicky/fastboard/FastBoard.java index 9e29b91..c594831 100644 --- a/src/main/java/fr/mrmicky/fastboard/FastBoard.java +++ b/src/main/java/fr/mrmicky/fastboard/FastBoard.java @@ -109,7 +109,7 @@ public class FastBoard { private boolean deleted = false; /** - * Create a new FastBoard for a player + * Creates a new FastBoard. * * @param player the player the scoreboard is for */ @@ -127,16 +127,16 @@ public FastBoard(Player player) { } /** - * Get the current title of the scoreboard. + * Get the scoreboard title. * - * @return current scoreboard title + * @return the scoreboard title */ public String getTitle() { return title; } /** - * Update the scoreboard title. The title can't be longer than 32 chars + * Update the scoreboard title. * * @param title the new scoreboard title * @throws IllegalArgumentException if the title is longer than 32 chars on 1.12 or lower @@ -161,18 +161,82 @@ public void updateTitle(String title) { } /** - * Get the current lines of the scoreboard + * Get the scoreboard lines. * - * @return the current lines of the scoreboard + * @return the scoreboard lines */ public List getLines() { return new ArrayList<>(lines); } /** - * Update the lines of the scoreboard + * Get the specified scoreboard line. * - * @param lines the new scoreboard lines + * @param line the line number + * @return the line + * @throws IndexOutOfBoundsException if the line is higher than {@code size} + */ + public String getLine(int line) { + checkLineNumber(line, true); + + return lines.get(line); + } + + /** + * Update a single scoreboard line. + * + * @param line the line number + * @param text the new line text + * @throws IndexOutOfBoundsException if the line is higher than {@code size} + 1 + */ + public void updateLine(int line, String text) { + checkLineNumber(line, false); + + try { + if (line < size()) { + lines.set(line, text); + + sendTeamPacket(getScoreByLine(line), TeamMode.UPDATE); + return; + } + + List newLines = new ArrayList<>(lines); + + if (line > size()) { + for (int i = size(); i < line; i++) { + newLines.add(""); + } + } + + newLines.add(text); + + updateLines(newLines); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + + /** + * Remove a scoreboard line. + * + * @param line the line number + */ + public void removeLine(int line) { + checkLineNumber(line, false); + + if (line >= size()) { + return; // The line don't exists + } + + List lines = new ArrayList<>(this.lines); + lines.remove(line); + updateLines(lines); + } + + /** + * Update all the scoreboard lines. + * + * @param lines the new lines * @throws IllegalArgumentException if one line is longer than 30 chars on 1.12 or lower * @throws IllegalStateException if {@link #delete()} was call before */ @@ -240,18 +304,7 @@ public void updateLines(Collection lines) { } /** - * Get the specified scoreboard line - * - * @param line the line number - * @return the line - * @throws IndexOutOfBoundsException if the number is higher than the number of lines - */ - public String getLine(int line) { - return lines.get(line); - } - - /** - * Get the player associated with this FastBoard + * Get the player who has the scoreboard. * * @return current player for this FastBoard */ @@ -260,18 +313,32 @@ public Player getPlayer() { } /** - * Get the id of theFastBoard + * Get the scoreboard id. * - * @return id + * @return the id */ public String getId() { return id; } + /** + * Get if the scoreboard is deleted. + * + * @return true if the scoreboard is deleted + */ public boolean isDeleted() { return deleted; } + /** + * Get the scoreboard size (the number of lines). + * + * @return the size + */ + public int size() { + return lines.size(); + } + /** * Delete this FastBoard, and will remove the scoreboard for the associated player if he is online. * After this, all uses of {@link #updateLines} and {@link #updateTitle} will throws an {@link IllegalStateException} @@ -292,6 +359,20 @@ public void delete() { deleted = true; } + private void checkLineNumber(int line, boolean checkMax) { + if (line < 0) { + throw new IllegalArgumentException("Line number must be positive"); + } + + if (checkMax && line >= lines.size()) { + throw new IllegalArgumentException("Line number must be under " + lines.size()); + } + } + + private int getScoreByLine(int line) { + return lines.size() - line - 1; + } + private String getLineByScore(int score) { return getLineByScore(lines, score); } diff --git a/src/main/java/fr/mrmicky/fastboard/FastReflection.java b/src/main/java/fr/mrmicky/fastboard/FastReflection.java index aad3c96..63049ca 100644 --- a/src/main/java/fr/mrmicky/fastboard/FastReflection.java +++ b/src/main/java/fr/mrmicky/fastboard/FastReflection.java @@ -2,6 +2,7 @@ import org.bukkit.Bukkit; +import java.util.Locale; import java.util.Optional; /** @@ -52,8 +53,8 @@ public static Optional> optionalClass(String className) { } } - @SuppressWarnings({"unchecked", "rawtypes"}) - public static Object enumValueOf(Class enumClass, String enumName) { - return Enum.valueOf((Class) enumClass, enumName.toUpperCase()); + @SuppressWarnings("unchecked") + public static > E enumValueOf(Class enumClass, String enumName) { + return Enum.valueOf((Class) enumClass, enumName.toUpperCase(Locale.ROOT)); } }