I am making a skyblock plugin, but when I am creating the scoreboard, the team object which I use to change the balance in the scoreboard becomes null after initial call. This is the exception:
[21:18:40 WARN]: [CustomSkyblock] Task #2 for CustomSkyblock v1 generated an exception
java.lang.NullPointerException: Cannot invoke "org.bukkit.scoreboard.Team.setSuffix(String)" because "balance" is null
at io.github.github9636dev.CustomSkyblock.Main.lambda$onEnable$1(Main.java:102) ~[?:?]
at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[Server.jar:git-Spigot-21fe707-741a1bd]
at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [Server.jar:git-Spigot-21fe707-741a1bd]
at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [Server.jar:git-Spigot-21fe707-741a1bd]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Server.jar:git-Spigot-21fe707-741a1bd]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Server.jar:git-Spigot-21fe707-741a1bd]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:
Here is the main class:
package io.github.github9636dev.CustomSkyblock;
import io.github.github9636dev.CustomSkyblock.core.commands.Eco;
import io.github.github9636dev.CustomSkyblock.core.commands.ExampleCommand;
import io.github.github9636dev.CustomSkyblock.core.economy.Economy;
import io.github.github9636dev.CustomSkyblock.core.init.BalanceInit;
import io.github.github9636dev.CustomSkyblock.core.listeners.PlayerJoinEvent;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main extends JavaPlugin {
public static final String SERVER_NAME = "§b9636's Test Server";
private static String name, version;
public static boolean requiresSave;
public static final List<Player> playerScoreboardNeedsUpdate = new ArrayList<>();
@Override
public void onEnable() {
super.onEnable();
name = getDescription().getName();
version = getDescription().getVersion();
requiresSave = false;
PluginManager pluginManager = Bukkit.getPluginManager();
FileConfiguration config = getConfig();
config.options().copyDefaults(true);
if (!new File(config.getCurrentPath()).exists()) saveDefaultConfig();
//Inits
BalanceInit.init(config, 2500);
//Commands
Eco ecoCommand = new Eco(this);
//Scoreboard
ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
Scoreboard scoreboard = scoreboardManager.getMainScoreboard();
Objective title;
Team balance,islandLevel,rank;
if (scoreboard.getObjective("scoreboardTitle") != null) {
balance = scoreboard.getTeam("§b§a§l§r");
}
else {
title = scoreboard.registerNewObjective("scoreboardTitle","dummy");
title.setDisplayName(SERVER_NAME.replaceFirst("§b","§7"));
title.setDisplaySlot(DisplaySlot.SIDEBAR);
title.getScore("").setScore(7);
title.getScore("§7>> §l§6Rank: ").setScore(6);
title.getScore("§7>> §l§6Balance: ").setScore(4);
title.getScore("§7>> §l§6Island Level:").setScore(2);
title.getScore("§r Default").setScore(5);
title.getScore("§b§a§l§r").setScore(3);
// Score islandLevel = title.getScore("§r No island");
// islandLevel.setScore(1);
balance = scoreboard.registerNewTeam("");
balance.addEntry("§b§a§l§r");
balance.setPrefix("§r $");
}
initializeListeners(pluginManager);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
if (BalanceInit.requiresSave) {
BalanceInit.CHANGED.forEach(((uuid, bal) -> {
config.set("player-data.balance." + uuid, bal.toInt());
}));
BalanceInit.CHANGED.clear();
BalanceInit.requiresSave = false;
saveConfig();
}
if (requiresSave) saveConfig();
if (!playerScoreboardNeedsUpdate.isEmpty()) {
for (Player p : playerScoreboardNeedsUpdate) {
// if (balance == null) {
//System.out.println("Balance is null");
// continue;
// };
balance.setSuffix("" + Economy.getBalance(p));
p.setScoreboard(scoreboard);
}
}
},40,40);
}
@Override
public void onDisable() {
super.onDisable();
FileConfiguration config = getConfig();
BalanceInit.CHANGED.forEach(((uuid, balance) -> {
config.set("player-data.balance." + uuid, balance.toInt());
}));
saveConfig();
}
private void initializeListeners(PluginManager pm) {
pm.registerEvents(new PlayerJoinEvent(),this);
}
public static String getPluginName() {
return name;
}
}
It works fine for the first iteration, it sets my scoreboard to my balance, but then every iteration after it just returns null. I cannot find any reason to this.
JoinListener:
package io.github.github9636dev.CustomSkyblock.core.listeners;
import io.github.github9636dev.CustomSkyblock.Main;
import io.github.github9636dev.CustomSkyblock.common.chat.Message;
import io.github.github9636dev.CustomSkyblock.core.init.BalanceInit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.UUID;
public class PlayerJoinEvent implements Listener {
@EventHandler
private void onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
if (!BalanceInit.check(uuid)) {
Message.broadcastWelcome(player);
Message.sendWelcome(player, BalanceInit.getAndAdd(uuid));
}
else {
Message.sendWB(player, BalanceInit.getAndAdd(uuid));
}
event.setJoinMessage(" ");
}
}
package io.github.github9636dev.CustomSkyblock.core.listeners;
import io.github.github9636dev.CustomSkyblock.Main;
import io.github.github9636dev.CustomSkyblock.common.chat.Message;
import io.github.github9636dev.CustomSkyblock.core.init.BalanceInit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.UUID;
public class PlayerJoinEvent implements Listener {
@EventHandler
private void onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
if (!BalanceInit.check(uuid)) {
Message.broadcastWelcome(player);
Message.sendWelcome(player, BalanceInit.getAndAdd(uuid));
}
else {
Message.sendWB(player, BalanceInit.getAndAdd(uuid));
}
event.setJoinMessage(" ");
}
}
The version is:
CraftBukkit version git-Spigot-21fe707-741a1bd (MC: 1.8.8) (Implementing API version 1.8.8-R0.1-SNAPSHOT)