Skip to content

Commit

Permalink
Merge pull request #1 from hashtek-mc/main
Browse files Browse the repository at this point in the history
[ADD] Added .gitignore. Added features such as setLine for the HashSi…
  • Loading branch information
nl1x authored Feb 8, 2024
2 parents 99093c4 + de05779 commit bd6d4ea
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.idea
*.vscode
*.eclipse
*.iml
out
11 changes: 11 additions & 0 deletions src/fr/hashktek/spigot/hashboard/HashBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.scoreboard.Team;

import javax.annotation.Nullable;
import java.util.Collection;

public class HashBoard
{
Expand All @@ -26,6 +27,7 @@ public HashBoard()
this.sideBar = null;
}


/**
* Remove the team from the scoreboard
* @param hashTeam The team to unregister
Expand Down Expand Up @@ -63,6 +65,15 @@ public void setToPlayers(Player... players)
player.setScoreboard(scoreboard);
}

/**
* Set the scoreboard to a list of player.
* @param players The list of player to add the scoreboard to.
*/
public void setToPlayers(Collection<? extends Player> players)
{
this.setToPlayers(players.toArray(new Player[0]));
}

/**
* Get the HashSideBar of the scoreboard.
* @return The sidebar of the scoreboard.
Expand Down
127 changes: 123 additions & 4 deletions src/fr/hashktek/spigot/hashboard/HashSideBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.bukkit.scoreboard.Objective;

import java.util.HashMap;
import java.util.Optional;
import java.util.Set;

public class HashSideBar
{
Expand Down Expand Up @@ -40,6 +42,32 @@ public HashSideBar setTitle(String title)
return this;
}


/**
* Add a line on the top of the sidebar.
* @param value The value of the new line.
* @return The sidebar itself.
*/
public HashSideBar addLine(String value)
{
StringBuilder valueBuilder = new StringBuilder().append(value);
Set<Integer> keySet = null;
int index = 0;

if (!this.lines.isEmpty()) {
keySet = this.lines.keySet();
for (Integer key : keySet) {
if (this.lines.get(key).contentEquals(valueBuilder))
valueBuilder.append(" ");
if (key > index)
index = key;
}
}

this.setLine(index + 1, valueBuilder.toString());
return this;
}

/**
* Set a line in the sidebar.
* @param index The index of the line.
Expand All @@ -53,15 +81,83 @@ public HashSideBar setLine(int index, String value)
if (this.board != null)
this.updateSideBarLine(index, previousValue, value);

if (previousValue == null) {
this.lines.put(index, value);
} else {
this.lines.put(index, value);
this.lines.put(index, value);

return this;
}

/**
* Set multiple lines to the same value in the sidebar.
* @param value The value of the line
* @param indexes The indexes.
* @return The sidebar itself.
*/
public HashSideBar setLines(String value, int... indexes)
{
StringBuilder spaces = new StringBuilder();

for (int index : indexes) {
this.setLine(index, value + spaces);
spaces.append(" ");
}
return this;

}

/**
* Set the lines between `from` and `to` to value.
* @param value The value to set to the lines.
* @param from The start of the filling.
* @param to The end of the filling.
* @return The sidebar itself.
*/
public HashSideBar fillLines(String value, int from, int to)
{
int[] indexes = this.getIndexes(from, to, false);

this.setLines(value, indexes);
return this;
}

/**
* Remove a line from the sidebar.
* @param index The index of the line.
* @return The sidebar itself.
*/
public HashSideBar removeLine(int index)
{
if (this.lines.containsKey(index))
this.board.getScoreboard().resetScores(this.lines.remove(index));
return this;
}

/**
* Remove multiple lines from the sidebar.
* @param indexes The indexes of the lines to remove from the sidebar.
* @return The sidebar itself.
*/
public HashSideBar removeLines(int... indexes)
{
for (int index : indexes)
this.removeLine(index);
return this;
}

/**
* Remove the lines in the range from `from` to `to`.
* @param from The start of the clearing.
* @param to The end of the clearing.
* @return The sidebar itself.
*/
public HashSideBar clearLines(int from, int to)
{
int[] indexes = this.getIndexes(from, to, true);

this.removeLines(indexes);
return this;
}


/**
* @param board The board.
*/
Expand Down Expand Up @@ -93,4 +189,27 @@ private void updateSideBarLine(int index, String previousValue, String newValue)
this.objective.getScore(newValue).setScore(index);
}


/**
* Get the indexes from `from` to `to` in an array.
* @param from The start of the indexes
* @param to The end of the indexes
* @return The indexes in an array
*/
private int[] getIndexes(int from, int to, boolean checkIndex)
{
int min = Math.min(from, to);
int max = Math.max(from, to);
int index = 0;
final int[] indexes = new int[max - min];

for (int i = 0; i < indexes.length; i++) {
index = max - i;
if (!checkIndex || this.lines.containsKey(index))
indexes[i] = index;
}

return indexes;
}

}
2 changes: 1 addition & 1 deletion src/fr/hashktek/spigot/hashboard/HashTabList.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public HashTabList()
* @param players The list of players to update the tablist to.
* @throws StrangeException If the impossible happen. (See the exception message)
*/
public void update(Collection<Player> players)
public void update(Collection<? extends Player> players)
throws StrangeException
{
this.update(players.toArray(new Player[0]));
Expand Down

0 comments on commit bd6d4ea

Please sign in to comment.