Repeating task

Discussion in 'Spigot Plugin Development' started by Bear53, May 24, 2015.

  1. Hey so before anyone hates know one thing I'm not good at is runnables and repeating tasks. I'm trying to make it so every second the players scoreboard and tab updates. This is what I have and for some reason it is not working, please help

    Code (Text):
    public static Scoreboard board;
        public static Team lobby;
        public static Team game;
        public static Team staff;
        public static Team donor;

        public void setup() {
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
            Objective obj = board.registerNewObjective("this", "that");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);

            obj.setDisplayName(prefix);
            {
                Score ingame = obj.getScore(ChatColor.GREEN + "InGame");
                ingame.setScore(Main.ingame.size());

                Score inlobby = obj.getScore(ChatColor.DARK_AQUA + "Lobby");
                inlobby.setScore(Main.spec.size());

                Score coins = obj.getScore(ChatColor.GOLD + "Coins");
                coins.setScore(0);
            }

            lobby = board.getTeam("Lobby");
            lobby.setPrefix(ChatColor.GOLD + "");

            game = board.getTeam("Game");
            game.setPrefix(ChatColor.DARK_AQUA + "");

            staff = board.getTeam("Staff");
            staff.setPrefix(ChatColor.RED + "");

            donor = board.getTeam("Donor");
            donor.setPrefix(ChatColor.DARK_PURPLE + "");
        }

        @EventHandler
        public void setTeam(PlayerJoinEvent e) {

            List<String> moderators = getConfig().getStringList("moderators");
            List<String> owners = getConfig().getStringList("owners");
            List<String> admins = getConfig().getStringList("admins");
            List<String> donors = getConfig().getStringList("donors");
            List<String> defaults = getConfig().getStringList("defaults");
            Player p = e.getPlayer();
            p.setScoreboard(board);
            if (admins.contains(p.getName())
                    || (owners.contains(p.getName()) || (moderators.contains(p
                            .getName())))) {
                staff.addPlayer(p);
            } else if (donors.contains(p.getName())) {
                donor.addPlayer(p);
            } else {
                lobby.addPlayer(p);
            }
        }

        public void setTab() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    List<String> moderators = getConfig().getStringList(
                            "moderators");
                    List<String> owners = getConfig().getStringList("owners");
                    List<String> admins = getConfig().getStringList("admins");
                    List<String> donors = getConfig().getStringList("donors");
                    List<String> defaults = getConfig().getStringList("defaults");
                    setup();
                    p.setScoreboard(board);
                    if (admins.contains(p.getName())
                            || (owners.contains(p.getName()) || (moderators
                                    .contains(p.getName())))) {
                        staff.addPlayer(p);
                    } else if (donors.contains(p.getName())) {
                        donor.addPlayer(p);
                    } else {
                        lobby.addPlayer(p);
                    }
                }
            }, 0L, 300L);
        }
    }
     
     
  2. Hi Bear53:

    A delay of 300L is 15 seconds, not one second. Also, could you be more clear as to what isn't working.

    Thanks, and good luck solving your issue.
     
  3. There is a error in my console, I'll send it when I get home and yeah realized that after I posted Thanks!
     
  4. Code (Text):
    24.05 20:39:34 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:641) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:34 [Server] INFO at com.bear53.Main$1.run(Main.java:218) ~[?:?]
    24.05 20:39:34 [Server] INFO at com.bear53.Main.setup(Main.java:176) ~[?:?]
    24.05 20:39:34 [Server] INFO java.lang.NullPointerException
    24.05 20:39:34 [Server] WARN Task #337 for Knockoff v2.0 generated an exception
    24.05 20:39:19 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:641) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot_1649.jar:git-Spigot-1649]
    24.05 20:39:19 [Server] INFO at com.bear53.Main$1.run(Main.java:218) ~[?:?]
    24.05 20:39:19 [Server] INFO at com.bear53.Main.setup(Main.java:176) ~[?:?]
    24.05 20:39:19 [Server] INFO java.lang.NullPointerException
    24.05 20:39:19 [Server] WARN Task #337 for Knockoff v2.0 generated an exception
     
  5. Ok so what is on line 176 in your main class. what ever it is its null for what ever reason, hence "java.lang.NullPointerException" and "com.bear53.Main.setup(Main.java:176)"
     
  6. Code (Text):
    lobby.setPrefix(ChatColor.GOLD + "");
    Its the prefix for a team, there are 3 other teams...
     
  7. Have you checked if lobby is null with
    Code (Text):
    if(!lobby == null)
    before you run all your methods with it?
     
  8. The error is on com.bear53.Main$1.run(Main.java:218)

    It's line 218 inside your Runnable.
     
  9. Code (Text):
    setup();
    that is 218, it is to run the setup for the players
     
  10. You have to register the team before you get the team.
    For example, if you add the lines
    Code (Text):
    board.registerNewTeam("Lobby");
    board.registerNewTeam("Game");
    board.registerNewTeam("Staff");
    board.registerNewTeam("Donor");
    after you've defined the board and before you call board.getTeam(String), I suspect that it will either work or that you'll get a different error, either way it'd help us move forwards to a solution.

    EDIT: Just to make it clearer. When I said "Make sure that the team 'Lobby' exists" I wasn't talking about the variable lobby.
     
  11. I'm just going to post the main class
    Main.java:
    Code (Text):
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import me.AgentRiddler.ultimateparticles.UltimateParticles;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;

    import com.bear53.Commands.CommandGroupSet;
    import com.bear53.Commands.Freeze;
    import com.bear53.Commands.Knockoff;
    import com.bear53.Commands.Rank;
    import com.bear53.Commands.Report;
    import com.bear53.Commands.UnFreeze;
    import com.bear53.events.Achievement;
    import com.bear53.events.BlockPlaceEvent;
    import com.bear53.events.ChatEvent;
    import com.bear53.events.DropEvent;
    import com.bear53.events.EntityDamage;
    import com.bear53.events.HungerEvent;
    import com.bear53.events.InteractEvent;
    import com.bear53.events.JoinEvent;
    import com.bear53.events.LeaveEvent;
    import com.bear53.events.MoveEvent;
    import com.bear53.events.OnBreak;
    import com.bear53.events.OnDeath;
    import com.bear53.events.PickupEvent;
    import com.bear53.events.PlayerRespawn;
    import com.bear53.events.ServerPing;
    import com.bear53.events.SignChange;
    import com.bear53.exceptions.UnloadedPluginException;

    public class Main extends JavaPlugin implements Listener {

        private Player p;
        Server server = getServer();
        PluginManager pm = server.getPluginManager();
        public static Plugin instance;
        public static ArrayList<String> ingame = new ArrayList();
        public static ArrayList<String> spec = new ArrayList();
        public static HashMap<String, Integer> deaths = new HashMap<>();
        public static String prefix = ChatColor.GRAY + "[" + ChatColor.DARK_AQUA
                + "KnockOff" + ChatColor.GRAY + "] ";

        private UltimateParticles getUltimateParticles() {
            Plugin plugin = Bukkit.getServer().getPluginManager()
                    .getPlugin("UltimateParticles");
            if (plugin == null || !(plugin instanceof UltimateParticles)) {
                try {
                    throw new UnloadedPluginException(
                            "Unloaded plugin 'UltimateParticles'");
                } catch (UnloadedPluginException e) {
                    Bukkit.getServer().getConsoleSender()
                            .sendMessage("Unloaded plugin 'UltimateParticles'");
                }
            }
            return (UltimateParticles) plugin;
        }

        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().addDefault("moderators",
                    getConfig().getStringList("moderators"));
            getConfig().addDefault("owners", getConfig().getStringList("owners"));
            getConfig().addDefault("admins", getConfig().getStringList("admins"));
            getConfig().addDefault("donors", getConfig().getStringList("donors"));
            getConfig().addDefault("defaults",
                    getConfig().getStringList("defaults"));

            getConfig().options().copyDefaults(true);
            saveConfig();

            instance = this;
            getLogger().info(
                    String.format("[%s] Enabled Version %s", new Object[] {
                            getDescription().getName(),
                            getDescription().getVersion() }));
            loadEvents();
            loadCommands();
            setTab(p);
        }

        public void onDisable() {

            getLogger().info(
                    String.format("[%s] Disabled Version %s", new Object[] {
                            getDescription().getName(),
                            getDescription().getVersion() }));
        }

        public void loadCommands() {
            getCommand("knockoff").setExecutor(new Knockoff());
            getCommand("groupset").setExecutor(new CommandGroupSet());
            getCommand("rank").setExecutor(new Rank());
            getCommand("report").setExecutor(new Report());
            getCommand("freeze").setExecutor(new Freeze());
            getCommand("unfreeze").setExecutor(new UnFreeze());
            // getCommand("list").setExecutor(new StaffList());
        }

        public void loadEvents() {
            pm.registerEvents(new Achievement(), this);
            pm.registerEvents(new BlockPlaceEvent(), this);
            pm.registerEvents(new ChatEvent(), this);
            pm.registerEvents(new DropEvent(), this);
            pm.registerEvents(new EntityDamage(), this);
            pm.registerEvents(new HungerEvent(), this);
            pm.registerEvents(new InteractEvent(), this);
            pm.registerEvents(new JoinEvent(), this);
            pm.registerEvents(new LeaveEvent(), this);
            pm.registerEvents(new MoveEvent(), this);
            pm.registerEvents(new OnBreak(), this);
            pm.registerEvents(new OnDeath(), this);
            pm.registerEvents(new PickupEvent(), this);
            pm.registerEvents(new PlayerRespawn(), this);
            pm.registerEvents(new ServerPing(), this);
            pm.registerEvents(new SignChange(), this);
            pm.registerEvents(this, this);
        }

        public static String getPrefix() {
            return prefix;
        }

        public static FileConfiguration getConfig(Player p) {
            return instance.getConfig();
        }

        public static Plugin getInstance() {
            return instance;
        }

        public static Scoreboard board;
        public static Team lobby;
        public static Team game;
        public static Team staff;
        public static Team donor;

        public void setup() {
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
            Objective obj = board.registerNewObjective("this", "that");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);

            obj.setDisplayName(prefix);
            {
                Score ingame = obj.getScore(ChatColor.GREEN + "InGame");
                ingame.setScore(Main.ingame.size());

                Score inlobby = obj.getScore(ChatColor.DARK_AQUA + "Lobby");
                inlobby.setScore(Main.spec.size());

                Score coins = obj.getScore(ChatColor.GOLD + "Coins");
                coins.setScore(0);
            }

            board.registerNewTeam("Lobby");
            board.registerNewTeam("Game");
            board.registerNewTeam("Staff");
            board.registerNewTeam("Donor");

            lobby = board.getTeam("Lobby");
            lobby.setPrefix(ChatColor.GOLD + "");

            game = board.getTeam("Game");
            game.setPrefix(ChatColor.DARK_AQUA + "");

            staff = board.getTeam("Staff");
            staff.setPrefix(ChatColor.RED + "");

            donor = board.getTeam("Donor");
            donor.setPrefix(ChatColor.DARK_PURPLE + "");
        }

        @EventHandler
        public void setTeam(PlayerJoinEvent e) {

            List<String> moderators = getConfig().getStringList("moderators");
            List<String> owners = getConfig().getStringList("owners");
            List<String> admins = getConfig().getStringList("admins");
            List<String> donors = getConfig().getStringList("donors");
            List<String> defaults = getConfig().getStringList("defaults");
            Player p = e.getPlayer();
            p.setScoreboard(board);
            if (admins.contains(p.getName())
                    || (owners.contains(p.getName()) || (moderators.contains(p
                            .getName())))) {
                staff.addPlayer(p);
            } else if (donors.contains(p.getName())) {
                donor.addPlayer(p);
            } else {
                lobby.addPlayer(p);
            }
        }

        public void setTab(final Player p) {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    List<String> moderators = getConfig().getStringList(
                            "moderators");
                    List<String> owners = getConfig().getStringList("owners");
                    List<String> admins = getConfig().getStringList("admins");
                    List<String> donors = getConfig().getStringList("donors");
                    List<String> defaults = getConfig().getStringList("defaults");
                    setup();
                    p.setScoreboard(board);
                    if (admins.contains(p.getName())
                            || (owners.contains(p.getName()) || (moderators
                                    .contains(p.getName())))) {
                        staff.addPlayer(p);
                    } else if (donors.contains(p.getName())) {
                        donor.addPlayer(p);
                    } else {
                        lobby.addPlayer(p);
                    }
                }
            }, 0L, 300L);
        }
    }
     
  12. I see you added the registerNewTeam(String) lines.
    Did the error change?

    Edit: You should also only setup the Scoreboard once.