Skip to content

Commit

Permalink
[ADD] Added the HashBoard
Browse files Browse the repository at this point in the history
  • Loading branch information
nl1x committed Feb 7, 2024
0 parents commit 99093c4
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/spigot.jar
92 changes: 92 additions & 0 deletions src/fr/hashktek/spigot/hashboard/HashBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package fr.hashktek.spigot.hashboard;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team;

import javax.annotation.Nullable;

public class HashBoard
{

private final Scoreboard scoreboard;
private HashSideBar sideBar;


/**
* Create a custom scoreboard (Used for the teams, the tablist and the sidebar)
*/
public HashBoard()
{
ScoreboardManager manager = Bukkit.getScoreboardManager();

this.scoreboard = manager.getNewScoreboard();
this.sideBar = null;
}

/**
* Remove the team from the scoreboard
* @param hashTeam The team to unregister
*/
public void unregisterTeam(HashTeam hashTeam)
{
hashTeam.getBoardTeam().unregister();
}

/**
* Add the team to the scoreboard.
* @param hashTeam The team to register
* @return The team itself
*/
public Team registerTeam(HashTeam hashTeam)
{
Team team = this.scoreboard.registerNewTeam(hashTeam.getTablistPriority());

team.setPrefix(hashTeam.getPrefix());
team.setSuffix(hashTeam.getSuffix());
hashTeam.getPlayersUUID().forEach(
uuid -> team.addEntry(Bukkit.getPlayer(uuid).getName())
);

return team;
}

/**
* Set the scoreboard to a list of player.
* @param players The list of player to add the scoreboard to.
*/
public void setToPlayers(Player... players)
{
for (Player player : players)
player.setScoreboard(scoreboard);
}

/**
* Get the HashSideBar of the scoreboard.
* @return The sidebar of the scoreboard.
*/
public @Nullable HashSideBar getSideBar()
{
return this.sideBar;
}

/**
* @param sideBar The sidebar to set to the scoreboard
*/
protected void setSideBar(HashSideBar sideBar)
{
this.sideBar = sideBar;
this.sideBar.setBoard(this);
}

/**
* @return The scoreboard
*/
protected Scoreboard getScoreboard()
{
return this.scoreboard;
}

}
96 changes: 96 additions & 0 deletions src/fr/hashktek/spigot/hashboard/HashSideBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package fr.hashktek.spigot.hashboard;

import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;

import java.util.HashMap;

public class HashSideBar
{

private HashBoard board;
private Objective objective;

private String title;
private final HashMap<Integer, String> lines;


/**
* Create a sidebar
* @param board The board where the sidebar will be set.
*/
public HashSideBar(HashBoard board)
{
this.title = "NULL";
this.lines = new HashMap<Integer, String>();
this.setBoard(board);
}


/**
* Set the title of the sidebar
* @param title The new title of the sidebar.
* @return The sidebar itself
*/
public HashSideBar setTitle(String title)
{
if (this.objective != null)
this.objective.setDisplayName(title);
this.title = title;
return this;
}

/**
* Set a line in the sidebar.
* @param index The index of the line.
* @param value The value of the line.
* @return The sidebar itself.
*/
public HashSideBar setLine(int index, String value)
{
String previousValue = this.lines.get(index);

if (this.board != null)
this.updateSideBarLine(index, previousValue, value);

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

return this;
}

/**
* @param board The board.
*/
protected void setBoard(HashBoard board)
{
Objective previousObjective = board.getScoreboard().getObjective("sidebar");

if (previousObjective != null)
previousObjective.unregister();

this.objective = board.getScoreboard().registerNewObjective("sidebar", "dummy");
this.board = board;
this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
this.objective.setDisplayName(this.title);
this.lines.forEach(
(index, value) -> updateSideBarLine(index, null, value)
);
}

/**
* @param index The index of the line to update.
* @param previousValue The previous value of the line.
* @param newValue The new value of the line.
*/
private void updateSideBarLine(int index, String previousValue, String newValue)
{
if (previousValue != null)
this.board.getScoreboard().resetScores(previousValue);
this.objective.getScore(newValue).setScore(index);
}

}
97 changes: 97 additions & 0 deletions src/fr/hashktek/spigot/hashboard/HashTabList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package fr.hashktek.spigot.hashboard;

import fr.hashktek.spigot.hashboard.exceptions.StrangeException;
import net.minecraft.server.v1_8_R3.ChatComponentText;
import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerListHeaderFooter;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.Collection;

public class HashTabList
{

private final PacketPlayOutPlayerListHeaderFooter packet;
private String header;
private String footer;


/**
* Create a custom tablist header and footer.
*/
public HashTabList()
{
this.packet = new PacketPlayOutPlayerListHeaderFooter();
}

/**
* Update the tablist of the players.
* @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)
throws StrangeException
{
this.update(players.toArray(new Player[0]));
}

/**
* Update the tablist of the players.
* @param players The list of players to update the tablist to.
* @throws StrangeException If the impossible happen. (See the exception message)
*/
public void update(Player... players)
throws StrangeException
{
Class<?> packetClass = this.packet.getClass();
CraftPlayer craftPlayer = null;

this.setFieldValue(packetClass, "a", this.header);
this.setFieldValue(packetClass, "b", this.footer);

for (Player player : players) {
craftPlayer = ((CraftPlayer) player);
craftPlayer.getHandle().playerConnection.sendPacket(this.packet);
}
}

/**
* Set a new header to the tablist. (Require an update to see the changes)
* @param header The new header text.
*/
public void setHeader(String header)
{
this.header = header;
}

/**
* Set a new footer to the tablist. (Require an update to see the changes)
* @param footer The new footer text.
*/
public void setFooter(String footer)
{
this.footer = footer;
}

/**
* @param packetClass The class of the packet.
* @param key The declared field name
* @param value The new value to set to the field
* @throws StrangeException If the impossible happen (See the exception message)
*/
private void setFieldValue(Class<?> packetClass, String key, String value)
throws StrangeException
{
Field field = null;

try {
field = packetClass.getDeclaredField(key);
field.setAccessible(true);
field.set(this.packet, new ChatComponentText(value));
} catch (Exception exception) {
throw new StrangeException(exception.getLocalizedMessage());
}
}

}
Loading

0 comments on commit 99093c4

Please sign in to comment.