190 lines
7.3 KiB
Java
190 lines
7.3 KiB
Java
package com.bizarrealex.aether;
|
|
|
|
import com.bizarrealex.aether.event.BoardCreateEvent;
|
|
import com.bizarrealex.aether.scoreboard.Board;
|
|
import com.bizarrealex.aether.scoreboard.BoardAdapter;
|
|
import com.bizarrealex.aether.scoreboard.BoardEntry;
|
|
import lombok.Getter;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
import org.bukkit.scoreboard.DisplaySlot;
|
|
import org.bukkit.scoreboard.Objective;
|
|
import org.bukkit.scoreboard.Score;
|
|
import org.bukkit.scoreboard.Scoreboard;
|
|
|
|
import java.util.*;
|
|
|
|
import static com.bizarrealex.aether.AetherOptions.defaultOptions;
|
|
|
|
/**
|
|
* TODO: Add documentation to methods, etc
|
|
* TODO: Fix inconsistent cooldown scores
|
|
* TODO: Finish other board formats
|
|
*/
|
|
|
|
public class Aether implements Listener {
|
|
|
|
@Getter private JavaPlugin plugin;
|
|
@Getter private AetherOptions options;
|
|
@Getter BoardAdapter adapter;
|
|
|
|
public Aether(JavaPlugin plugin, BoardAdapter adapter, AetherOptions options) {
|
|
this.options = options;
|
|
this.plugin = plugin;
|
|
|
|
Bukkit.getPluginManager().registerEvents(this, plugin);
|
|
|
|
setAdapter(adapter);
|
|
run();
|
|
}
|
|
|
|
public Aether(JavaPlugin plugin, BoardAdapter adapter) {
|
|
this(plugin, adapter, defaultOptions());
|
|
}
|
|
|
|
public Aether(JavaPlugin plugin) {
|
|
this(plugin, null, defaultOptions());
|
|
}
|
|
|
|
private void run() {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
if (adapter == null) return;
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
Board board = Board.getByPlayer(player);
|
|
if (board != null) {
|
|
List<String> scores = adapter.getScoreboard(player, board, board.getCooldowns());
|
|
List<String> translatedScores = new ArrayList<>();
|
|
|
|
if (scores == null) {
|
|
|
|
if (!board.getEntries().isEmpty()) {
|
|
|
|
for (BoardEntry boardEntry : board.getEntries()) {
|
|
boardEntry.remove();
|
|
}
|
|
|
|
board.getEntries().clear();
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
for (String line : scores) {
|
|
translatedScores.add(ChatColor.translateAlternateColorCodes('&', line));
|
|
}
|
|
|
|
if (!options.scoreDirectionDown()) {
|
|
Collections.reverse(scores);
|
|
}
|
|
|
|
Scoreboard scoreboard = board.getScoreboard();
|
|
Objective objective = board.getObjective();
|
|
|
|
if (!(objective.getDisplayName().equals(adapter.getTitle(player)))) {
|
|
objective.setDisplayName(ChatColor.translateAlternateColorCodes('&', adapter.getTitle(player)));
|
|
}
|
|
|
|
outer:for (int i = 0; i < scores.size(); i++) {
|
|
String text = scores.get(i);
|
|
int position;
|
|
if (options.scoreDirectionDown()) {
|
|
position = 15 - i;
|
|
} else {
|
|
position = i + 1;
|
|
}
|
|
|
|
Iterator<BoardEntry> iterator = new ArrayList<>(board.getEntries()).iterator();
|
|
while (iterator.hasNext()) {
|
|
BoardEntry boardEntry = iterator.next();
|
|
Score score = objective.getScore(boardEntry.getKey());
|
|
|
|
if (score != null && boardEntry.getText().equals(ChatColor.translateAlternateColorCodes('&', text))) {
|
|
if (score.getScore() == position) {
|
|
continue outer;
|
|
}
|
|
}
|
|
}
|
|
|
|
int positionToSearch = options.scoreDirectionDown() ? 15 - position : position - 1;
|
|
|
|
iterator = board.getEntries().iterator();
|
|
while (iterator.hasNext()) {
|
|
BoardEntry boardEntry = iterator.next();
|
|
int entryPosition = scoreboard.getObjective(DisplaySlot.SIDEBAR).getScore(boardEntry.getKey()).getScore();
|
|
|
|
if (!options.scoreDirectionDown()) {
|
|
if (entryPosition > scores.size()) {
|
|
iterator.remove();
|
|
boardEntry.remove();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
BoardEntry entry = board.getByPosition(positionToSearch);
|
|
if (entry == null) {
|
|
new BoardEntry(board, text).send(position);
|
|
} else {
|
|
entry.setText(text).setup().send(position);
|
|
}
|
|
|
|
if (board.getEntries().size() > scores.size()) {
|
|
iterator = board.getEntries().iterator();
|
|
while (iterator.hasNext()) {
|
|
BoardEntry boardEntry = iterator.next();
|
|
if ((!translatedScores.contains(boardEntry.getText())) || Collections.frequency(board.getBoardEntriesFormatted(), boardEntry.getText()) > 1) {
|
|
iterator.remove();
|
|
boardEntry.remove();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
player.setScoreboard(scoreboard);
|
|
}
|
|
}
|
|
}
|
|
}.runTaskTimerAsynchronously(plugin, 20L, 2L);
|
|
}
|
|
|
|
public void setAdapter(BoardAdapter adapter) {
|
|
this.adapter = adapter;
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
Board board = Board.getByPlayer(player);
|
|
|
|
if (board != null) {
|
|
Board.getBoards().remove(board);
|
|
}
|
|
|
|
Bukkit.getPluginManager().callEvent(new BoardCreateEvent(new Board(player, this, options), player));
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
public void onPlayerJoinEvent(PlayerJoinEvent event) {
|
|
if (Board.getByPlayer(event.getPlayer()) == null) {
|
|
Bukkit.getPluginManager().callEvent(new BoardCreateEvent(new Board(event.getPlayer(), this, options), event.getPlayer()));
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
public void onPlayerQuitEvent(PlayerQuitEvent event) {
|
|
Board board = Board.getByPlayer(event.getPlayer());
|
|
if (board != null) {
|
|
Board.getBoards().remove(board);
|
|
}
|
|
}
|
|
|
|
}
|