Skip to content

Commit

Permalink
1.1.0: Update JavaDoc and add methods to change single line
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Feb 19, 2020
1 parent bc43baf commit 8a5f97f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ A Scoreboard API for Bukkit with 1.7-1.15 support
<dependency>
<groupId>fr.mrmicky</groupId>
<artifactId>FastBoard</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand All @@ -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"
);
```
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.mrmicky</groupId>
<artifactId>fastboard</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>

<name>FastBoard</name>
<url>https://github.com/MrMicky-FR/FastBoard</url>
Expand Down Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15-R0.1-SNAPSHOT</version>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
125 changes: 103 additions & 22 deletions src/main/java/fr/mrmicky/fastboard/FastBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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<String> 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<String> 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<String> 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
*/
Expand Down Expand Up @@ -240,18 +304,7 @@ public void updateLines(Collection<String> 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
*/
Expand All @@ -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}
Expand All @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/fr/mrmicky/fastboard/FastReflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.Bukkit;

import java.util.Locale;
import java.util.Optional;

/**
Expand Down Expand Up @@ -52,8 +53,8 @@ public static Optional<Class<?>> optionalClass(String className) {
}
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static Object enumValueOf(Class<?> enumClass, String enumName) {
return Enum.valueOf((Class<Enum>) enumClass, enumName.toUpperCase());
@SuppressWarnings("unchecked")
public static <E extends Enum<E>> E enumValueOf(Class<?> enumClass, String enumName) {
return Enum.valueOf((Class<E>) enumClass, enumName.toUpperCase(Locale.ROOT));
}
}

0 comments on commit 8a5f97f

Please sign in to comment.