Fixed npe in scoreboard manager

This commit is contained in:
Fabrizio La Rosa 2020-08-06 15:46:10 +02:00
parent 8f41ae6ee3
commit 0da43a9b55
7 changed files with 112 additions and 88 deletions

View File

@ -151,11 +151,7 @@ public class SkyBlock extends SongodaPlugin {
cooldownManager = new CooldownManager(this);
limitationHandler = new LimitationInstanceHandler();
fabledChallenge = new FabledChallenge(this);
if (fileManager.getConfig(new File(getDataFolder(), "config.yml")).getFileConfiguration().getBoolean("Island.Scoreboard.Enable")) {
scoreboardManager = new ScoreboardManager(this);
}
scoreboardManager = new ScoreboardManager(this);
inviteManager = new InviteManager(this);
biomeManager = new BiomeManager(this);
levellingManager = new IslandLevelManager(this);

View File

@ -16,12 +16,12 @@ class Board {
private final Player player;
private final SkyBlock plugin;
public Scoreboard board;
private final Scoreboard board;
private final Objective objective;
private final HashMap<Integer, String> cache = new HashMap<>();
public Board(SkyBlock plugin, Player player, int lineCount) {
Board(SkyBlock plugin, Player player, int lineCount) {
this.player = player;
this.plugin = plugin;
this.board = this.plugin.getServer().getScoreboardManager().getNewScoreboard();
@ -41,7 +41,7 @@ class Board {
}
}
public void setTitle(String string) {
void setTitle(String string) {
PlaceholderManager placeholderManager = plugin.getPlaceholderManager();
if(string == null) string = "";
@ -54,7 +54,7 @@ class Board {
}
}
public void setLine(int line, String string) {
void setLine(int line, String string) {
Team t = board.getTeam(String.valueOf(line));
if(string == null) string = "";
@ -74,6 +74,10 @@ class Board {
}
}
Scoreboard getBoard() {
return board;
}
private ScoreboardLine convertIntoPieces(String line, int allowed_line_size) {
String prefixLine = line.substring(0, Math.min(line.length(), allowed_line_size));
String suffixLine = line.length() <= allowed_line_size ? "" : line.substring(allowed_line_size, Math.min(line.length(), allowed_line_size*2));

View File

@ -21,7 +21,7 @@ class Driver extends BukkitRunnable {
private final List<Holder> holders;
private final ScoreboardType boardType;
public Driver(SkyBlock plugin, ScoreboardType boardType) {
Driver(SkyBlock plugin, ScoreboardType boardType) {
this.plugin = plugin;
FileManager fileManager = plugin.getFileManager();
FileConfiguration scoreboardLoad = fileManager.getConfig(
@ -50,27 +50,27 @@ class Driver extends BukkitRunnable {
}
}
public List<Row> getRows() {
List<Row> getRows() {
return rows;
}
public Row getTitle() {
Row getTitle() {
return title;
}
public void registerHolder(Holder holder) {
void registerHolder(Holder holder) {
holders.add(holder);
}
public void unregisterHolder(Holder holder) {
void unregisterHolder(Holder holder) {
holders.remove(holder);
}
public void unregisterHolder(Player player) {
void unregisterHolder(Player player) {
Iterator<Holder> it = holders.iterator();
while(it.hasNext()) {
Holder holder = it.next();
if(holder.player.equals(player)) {
if(holder.getPlayer().equals(player)) {
it.remove();
break;
}
@ -89,7 +89,7 @@ class Driver extends BukkitRunnable {
}
}
public ScoreboardType getBoardType() {
ScoreboardType getBoardType() {
return boardType;
}
}

View File

@ -9,11 +9,11 @@ class Holder {
private final SkyBlock plugin;
private final Driver driver;
public final Player player;
private final Player player;
private final Board board;
public Holder(SkyBlock plugin, Driver driver, Player player) {
Holder(SkyBlock plugin, Driver driver, Player player) {
this.plugin = plugin;
this.driver = driver;
this.player = player;
@ -22,26 +22,28 @@ class Holder {
update();
}
public void update() {
void update() {
PlaceholderManager placeholderManager = plugin.getPlaceholderManager();
ScoreboardManager scoreboardManager = plugin.getScoreboardManager();
if(scoreboardManager != null) {
if (!scoreboardManager.isPlayerDisabled(player)) {
board.setTitle(driver.getTitle().getLine());
if (!scoreboardManager.isPlayerDisabled(player)) {
board.setTitle(driver.getTitle().getLine());
int count = 0;
for(Row row : driver.getRows()) {
String line = placeholderManager.parsePlaceholders(player, row.getLine());
board.setLine(count, line);
count++;
}
this.player.setScoreboard(board.board);
} else {
this.player.setScoreboard(scoreboardManager.getEmptyScoreboard());
int count = 0;
for(Row row : driver.getRows()) {
String line = placeholderManager.parsePlaceholders(player, row.getLine());
board.setLine(count, line);
count++;
}
this.player.setScoreboard(board.getBoard());
} else {
this.player.setScoreboard(scoreboardManager.getEmptyScoreboard());
}
}
Player getPlayer() {
return player;
}
}

View File

@ -14,7 +14,7 @@ class Row {
public boolean static_line;
public Row(List<String> lines, int interval) {
Row(List<String> lines, int interval) {
this.lines = lines;
this.interval = interval;
this.current = 0;
@ -29,7 +29,7 @@ class Row {
}
}
public void update() {
void update() {
if (!static_line) {
if (count >= interval) {
count = 0;
@ -43,7 +43,7 @@ class Row {
}
}
public String getLine() {
String getLine() {
return this.line;
}

View File

@ -5,7 +5,7 @@ class ScoreboardLine {
private final String prefix;
private final String suffix;
public ScoreboardLine(String prefix, String suffix, int limit) {
ScoreboardLine(String prefix, String suffix, int limit) {
if(prefix.length() > limit) {
prefix = prefix.substring(0, limit);
}
@ -16,11 +16,11 @@ class ScoreboardLine {
this.suffix = suffix;
}
public String getPrefix() {
String getPrefix() {
return prefix;
}
public String getSuffix() {
String getSuffix() {
return suffix;
}
}

View File

@ -21,21 +21,29 @@ import java.util.Set;
public class ScoreboardManager extends Manager {
private final boolean enabled;
private final Scoreboard emptyScoreboard;
private final List<Driver> drivers;
private final Set<Player> disabledPlayers;
public ScoreboardManager(SkyBlock plugin) {
super(plugin);
FileManager fileManager = plugin.getFileManager();
this.enabled = fileManager.getConfig(new File(plugin.getDataFolder(), "config.yml"))
.getFileConfiguration().getBoolean("Island.Scoreboard.Enable", true);
this.drivers = new ArrayList<>();
this.disabledPlayers = new ConcurrentSet<>();
this.emptyScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
for(ScoreboardType type : ScoreboardType.values()) {
newDriver(type);
if(enabled) {
for(ScoreboardType type : ScoreboardType.values()) {
newDriver(type);
}
updateOnlinePlayers();
}
updateOnlinePlayers();
}
@Override
@ -50,46 +58,50 @@ public class ScoreboardManager extends Manager {
}
public void updateOnlinePlayers() {
for(Player player : plugin.getServer().getOnlinePlayers()) {
updatePlayerScoreboardType(player);
if(enabled) {
for(Player player : plugin.getServer().getOnlinePlayers()) {
updatePlayerScoreboardType(player);
}
}
}
public void updatePlayerScoreboardType(Player player) {
PlayerDataManager playerDataManager = plugin.getPlayerDataManager();
IslandManager islandManager = plugin.getIslandManager();
PlayerData playerData = playerDataManager.getPlayerData(player);
Island island = islandManager.getIslandByPlayer(player);
if(playerData.isScoreboard()) {
ScoreboardType type;
if(island != null) {
Visit islandVisit = island.getVisit();
boolean hasVisitors = (islandVisit != null &&
islandVisit.getVisitors() != null &&
islandVisit.getVisitors().size() > 1);
boolean hasMembers = (islandVisit != null &&
islandVisit.getMembers() > 1);
if(hasMembers) {
if(hasVisitors) {
type = ScoreboardType.ISLAND_TEAM_VISITORS;
if(enabled) {
PlayerDataManager playerDataManager = plugin.getPlayerDataManager();
IslandManager islandManager = plugin.getIslandManager();
PlayerData playerData = playerDataManager.getPlayerData(player);
Island island = islandManager.getIslandByPlayer(player);
if(playerData.isScoreboard()) {
ScoreboardType type;
if(island != null) {
Visit islandVisit = island.getVisit();
boolean hasVisitors = (islandVisit != null &&
islandVisit.getVisitors() != null &&
islandVisit.getVisitors().size() > 1);
boolean hasMembers = (islandVisit != null &&
islandVisit.getMembers() > 1);
if(hasMembers) {
if(hasVisitors) {
type = ScoreboardType.ISLAND_TEAM_VISITORS;
} else {
type = ScoreboardType.ISLAND_TEAM_EMPTY;
}
} else {
type = ScoreboardType.ISLAND_TEAM_EMPTY;
if(hasVisitors) {
type = ScoreboardType.ISLAND_SOLO_VISITORS;
} else {
type = ScoreboardType.ISLAND_SOLO_EMPTY;
}
}
} else {
if(hasVisitors) {
type = ScoreboardType.ISLAND_SOLO_VISITORS;
} else {
type = ScoreboardType.ISLAND_SOLO_EMPTY;
}
type = ScoreboardType.NO_ISLAND;
}
synchronized (player) {
setPlayerScoreboard(player, type);
}
} else {
type = ScoreboardType.NO_ISLAND;
}
synchronized (player) {
setPlayerScoreboard(player, type);
}
}
}
@ -97,28 +109,36 @@ public class ScoreboardManager extends Manager {
public void setPlayerScoreboard(Player player, ScoreboardType type) {
for(Driver driver : drivers) {
driver.unregisterHolder(player);
if(driver.getBoardType().equals(type)) {
driver.registerHolder(new Holder(plugin, driver, player));
if(enabled) {
for(Driver driver : drivers) {
driver.unregisterHolder(player);
if(driver.getBoardType().equals(type)) {
driver.registerHolder(new Holder(plugin, driver, player));
}
}
}
}
public void unregisterPlayer(Player player) {
for(Driver driver : drivers) {
driver.unregisterHolder(player);
if(enabled) {
for(Driver driver : drivers) {
driver.unregisterHolder(player);
}
player.setScoreboard(emptyScoreboard);
}
player.setScoreboard(emptyScoreboard);
}
public void addDisabledPlayer(Player player) {
disabledPlayers.add(player);
Bukkit.getScheduler().runTask(plugin, () -> this.unregisterPlayer(player));
if(enabled) {
disabledPlayers.add(player);
Bukkit.getScheduler().runTask(plugin, () -> this.unregisterPlayer(player));
}
}
public void removeDisabledPlayer(Player player) {
disabledPlayers.remove(player);
if(enabled) {
disabledPlayers.remove(player);
}
}
public boolean isPlayerDisabled(Player player) {
@ -140,9 +160,11 @@ public class ScoreboardManager extends Manager {
}
public void clearDrivers() {
for(Driver driver : drivers)
driver.cancel();
drivers.clear();
if(enabled) {
for(Driver driver : drivers)
driver.cancel();
drivers.clear();
}
}
public Scoreboard getEmptyScoreboard() {