Scoreboard does not update when variables change

Discussion in 'Plugin Development' started by mattdog0601, Feb 18, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    mattdog0601

    I was making a plugin called choice chamber based off the game, and when I wanted to create a scoreboard I put the variables in for a timer. since there was a string with the variables I did not want to use the plain red text of the scoreboard. So I decided to use a score to display the timer, but I soon ran into a problem. I don't know how to update the score so the correct time is shown. Right now I have the score being printed to chat for debugging an issue. thanks!

    Here is my code:
    Code:
    package main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    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;
    
    public class MainClass extends JavaPlugin implements Listener {
        int secondTimer = 30;
        int minuteTimer = 0;
        boolean running = false;
    
    
        public void onEnable() {
    
            getLogger().info("***********************");
            getLogger().info("Choice Chamber Enabled");
            getLogger().info("Choice Chamber V. " + getServer().getPluginManager().getPlugin("Choice Chamber").getDescription().getVersion());
            getLogger().info("Written By @McMattGames");
            getLogger().info("***********************");
    
        }
    
        public void onDisable() {
    
            getLogger().info("***********************");
            getLogger().info("Choice Chamber Disabled");
            getLogger().info("***********************");
    
        }
    
        //commands
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            secondTimer = 30;
            minuteTimer = 0;
            if (cmd.getName().equalsIgnoreCase("choice") || cmd.getName().equalsIgnoreCase("c")) {
                Scoreboard board = getServer().getScoreboardManager().getNewScoreboard();
                Objective objective = board.registerNewObjective("timer", "dummy");
                Score score = objective.getScore(ChatColor.GREEN + "Timer: ");
                Score score2 = objective.getScore(ChatColor.GREEN + "" + minuteTimer + ":" + secondTimer);
                objective.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Choice Chamber");
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                score.setScore(-1);
                score2.setScore(-2);
                if (sender instanceof Player) {
                    if (args.length > 0) {
                        Player p = (Player) sender;
                        if (args[0].equalsIgnoreCase("start")) {
                            if (running == !true) {
                                Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Starting!");
                                running = true;
                                p.setScoreboard(board);
                                Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                                    public void run() {
                                        secondTimer = secondTimer - 1;
                                        if (secondTimer <= 0) {
                                            minuteTimer = minuteTimer - 1;
                                            secondTimer = 59;
                                        }
                                        if (minuteTimer < 0) {
                                            Bukkit.getServer().getScheduler().cancelAllTasks();
                                            Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Over!");
                                            running = false;
                                        }
                                        if (minuteTimer >= 0) {
                                            if (secondTimer > 9) {
                                                getServer().broadcastMessage(minuteTimer + ":" + secondTimer);
                                            } else {
                                                getServer().broadcastMessage(minuteTimer + ":0" + secondTimer);
                                            }
                                        }
    
                                    }
                                }, 0, 20);
                            } else {
                                Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Round Already Started");
                            }
                        } else {
                            if (args[0].equalsIgnoreCase("join")) {
                                if (running == true) {
    
                                    p.setScoreboard(board);
                                    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + "Joining");
                                } else {
                                    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "No Game Started!");
    
                                }
                            } else {
                                p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Invalad Command!");
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
     
  2. Offline

    sgavster

    You'd have to change the variable every count down, and then update the board
     
  3. Offline

    mattdog0601

    How do I update the board though?
     
    Last edited: Feb 18, 2016
  4. You can try using this class :

    Code:
    //Set scoreboard
    Board board = new Board();
    board.setLine(0, "Text");
    
    player.setScoreboard(board);
    
    //Update scoreboard - Add check for null
    Board board = new Board(player);
    board.setLine(0, "Updated");
    
     
  5. Offline

    mattdog0601

    @sgavster
    I could not use this got the error: cannot be resolved as a type

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 19, 2016
  6. Let me see your code.
     
  7. Offline

    mattdog0601

    Code:
    package main;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    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;
    
    public class MainClass extends JavaPlugin implements Listener {
        int secondTimer = 30;
        int minuteTimer = 0;
        boolean running = false;
        ArrayList<String> ingamePlayers = new ArrayList<String>();
       
        public void onEnable() {
    
            getLogger().info("***********************");
            getLogger().info("Choice Chamber Enabled");
            getLogger().info("Choice Chamber V. " + getServer().getPluginManager().getPlugin("Choice Chamber").getDescription().getVersion());
            getLogger().info("Written By @McMattGames");
            getLogger().info("***********************");
    
        }
    
        public void onDisable() {
    
            getLogger().info("***********************");
            getLogger().info("Choice Chamber Disabled");
            getLogger().info("***********************");
    
        }
    
        //commands
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            secondTimer = 30;
            minuteTimer = 0;
            if (cmd.getName().equalsIgnoreCase("choice") || cmd.getName().equalsIgnoreCase("c")) {
                Scoreboard board = getServer().getScoreboardManager().getNewScoreboard();
                Objective objective = board.registerNewObjective("timer", "dummy");
                Score score = objective.getScore(ChatColor.GREEN + "Timer: ");
                Score score2 = objective.getScore(ChatColor.GREEN + "" + minuteTimer + ":" + secondTimer);
                objective.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Choice Chamber");
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                score.setScore(-1);
                score2.setScore(-2);
                if (sender instanceof Player) {
                    if (args.length > 0) {
                        Player p = (Player) sender;
                        if (args[0].equalsIgnoreCase("start")) {
                            if (running == !true) {
                                Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Starting!");
                                running = true;
                                p.setScoreboard(board);
                                Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                                    public void run() {
                                        secondTimer = secondTimer - 1;
                                        if (secondTimer <= 0) {
                                            minuteTimer = minuteTimer - 1;
                                            secondTimer = 59;
                                        }
                                        if (minuteTimer < 0) {
                                            score2.setScore(0);
                                            Bukkit.getServer().getScheduler().cancelAllTasks();
                                            Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Over!");
                                            running = false;
                                        }
                                        if (minuteTimer >= 0) {
                                            if (secondTimer > 9) {
                                                for(World w : getServer().getWorlds()){
                                                    for(Player c : w.getPlayers()){
                                                        c.setScoreboard(board);
                                                    }
                                                }
                                                getServer().broadcastMessage(minuteTimer + ":" + secondTimer);
                                            } else {
                                               
                                                getServer().broadcastMessage(minuteTimer + ":0" + secondTimer);
                                            }
                                        }
    
                                    }
                                }, 0, 20);
                            } else {
                                Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Round Already Started");
                            }
                        } else {
                            if (!(args[0].equalsIgnoreCase("join"))) {
                                if (running == true) {
                                    if(ingamePlayers.contains(p.getName())){
                                        ingamePlayers.add(p.getName());
                                    p.setScoreboard(board);
                                    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + "Joining");
                                } else {
                                    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "No Game Started!");
    
                                }
                            } else {
                                p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Invalad Command!");
                                }
                            }else{
                                p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Already in game!");
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
     
  8. Offline

    Zombie_Striker

    This is an invalid package name. Please use either a domain you own (Email adress or web adress) or use me.[name].[project]
    1. No class should ever have "Class" as a part of their name
    2. Do not name your main class "Main". The main class should be the name of the plugin.
    You do not need this. Bukkit already logs your plugins for you. This now just creates two messages that the plugin has been enabled

    1. You are looking for the @Override Annotation.
    2. Your class implments listener, you have not registered your listener for an event, and you do not even have any events! onCommand is not an event btw.
     
    mine-care likes this.
  9. Offline

    mattdog0601

    Thanks for that, I'm fairly new to bukkit programming and did not know these things were necessary , but I still do not know how to update the scoreboard.
     
  10. Offline

    mattdog0601

  11. Offline

    Graffetus

    I believe that this will work:
    Code:
    board.update();
     
  12. Offline

    Konato_K

    teej107 and WinX64 like this.
  13. Offline

    Zombie_Striker

    @Konato_K
    One thing: That is from oracle and the name explains exactly what it is. The reason why I said you should not use it is because just adding "Class" to the end of a class is neither descriptive of what the class does nor does it follow convention. Names of classes should be explain what type of object it is (E.g. You call it ChatColor because it is the chat's color. You should not call objects things like ChatColorClass or BlockBreakEventClass)
     
  14. Offline

    Konato_K

    @Zombie_Striker I know, I just think "No class should ever" is way too much.
     
    Zombie_Striker likes this.
  15. Offline

    mattdog0601

  16. Offline

    mattdog0601

  17. Offline

    mythbusterma

    @mattdog0601

    Did you look at the posts? Post your updated code if you did.
     
  18. Offline

    mattdog0601

    Yes I did but I did not change my code in any way since the last time I posted it
     
  19. Offline

    mine-care

    @mattdog0601 So mabe it is time to change it, and see how it goes. Then, if you still face dificulties we are here to help.
     
  20. Offline

    mattdog0601

    It does still difficult me because I don't know how to change it. All I want to do is update the scoreboard every second.
     
  21. Offline

    MrSlayer02

    Code:
    public class ChoiceChamber extends JavaPlugin{
    
        boolean running;
    
        @Override
        public void onEnable(){
            //Stuff to do on plugin enable
            running = false;
        }
    
        @Override
        public void onDisable(){
            //Stuff to do on plugin disable
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
       
            if (command.getName().equalsIgnoreCase("choice")) {
                if(sender instanceof Player && args[0].equalsIgnoreCase("start") && !running){
                    running = true;
                    getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Starting!");
                    timer();
                    return true;
                }
           
            }
            //other commands
            return false;
       
        }
    
        int seconds;
        int minutes;
        int id;
        private void timer(){
       
            seconds = 30;
            minutes = 0;
       
            id = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
           
                @Override
                public void run() {
               
                 
                    seconds--;
                    if(seconds <= 0){
                        minutes--;
                        seconds = 59;
                    }
                    if(minutes < 0){
                        Bukkit.getScheduler().cancelTask(id);
                        Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Over!");
                        running = false;
                    }
                    scoreboard();
               
                }
            }, 0L, 20L);
       
        }
    
        private void scoreboard(){
       
            Scoreboard board = getServer().getScoreboardManager().getNewScoreboard();
       
            Objective objective = board.registerNewObjective("timer", "dummy");
            objective.setDisplayName(ChatColor.GOLD.toString() + ChatColor.BOLD + "Choice Chamber");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
       
            Score timer;
            String secondsTimer;
            String minutesTimer;
       
            if(seconds < 10){
                secondsTimer = "0" + seconds;
            }
            else secondsTimer = Integer.toString(seconds);
       
            if(minutes < 10){
                minutesTimer = "0" + minutes;
            }
            else minutesTimer = Integer.toString(minutes);
       
            timer = objective.getScore(ChatColor.GREEN + "Timer : " + minutesTimer + ":" + secondsTimer);
            timer.setScore(-1);
       
            for(Player p : getServer().getOnlinePlayers()){
                p.setScoreboard(board);
            }
       
        }
    
    }
    
    That's it !
     
    Last edited: Feb 29, 2016
  22. Offline

    mine-care

    @mattdog0601 Well, you should be in a position to update your own code :/

    First of all, be sure that your code is error-safe before sending it off.
    Here for instance, you are getting args[0] without being certain if there are arguments to the command, (Try it yourself, do /c without arguments.) and secondly you dont need to check for aliases, you simply need to specify them in plugin.yml.
    Also can we not spoonfeed please? Or at least if you do spend the time to quote what each piece of code does so that the observer/user of the snippet is aware of what is going on.
     
  23. Offline

    mattdog0601

    Thanks for the help! But what is spoonfeed exactly? I will try to stop if it's annoying

    Thanks I've been waiting for this for like a week!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  24. Offline

    mine-care

    Yay! Someone willing to learn! Woo!
    Well, it is when you give code to someone to fix their issue without going into the effort of guiding them through it. This way, most will copy-paste it in their project which most of the time leads to:
    1. More questions.
    2. Discouragment.
    3. Errors.
    4. Inefficient code.
    5. Waste of time.
    So as an unwritten rule in Bukkit forums we dont spoonfeed. We prefer to guide people through the solution instead of pasing it on a plate ready to go.

    Also can you please fix the mistake i pointed out above?

    Cheers.
     
  25. Offline

    MrSlayer02

    When running a CommandExecutor, bukkit is making a try/catch so doing /c without arguments will just send something like "An internal error occured" to sender.

    I just took the line from @mattdog0601's code without checking. I'll fix it.

    Sorry, I'm new to bukkit forums
     
  26. Offline

    mine-care

Thread Status:
Not open for further replies.

Share This Page