Solved Animated Scoreboard Title

Discussion in 'Plugin Development' started by malikdbuseck, Jan 9, 2015.

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

    malikdbuseck

    Ive seen this on a bunch of servers. Im just curious is the animated title on the scoreboard just a loop that the have going? Or a runnable that just changes the title? I would love to have this on my server just wasnt 100% on how they did it, or what was the most effiecent. I didnt think that have a runnable would be but idk.
     
  2. Offline

    Skionz

    @malikdbuseck They have a repeating scheduler which constantly changes each players scoreboard
     
    malikdbuseck likes this.
  3. Offline

    1Rogue

    It's much, much easier to set everyone with a single scoreboard and then update that scoreboard.
     
    Skionz and malikdbuseck like this.
  4. Offline

    malikdbuseck

    Thats what I thought!

    Clearly this is wrong. Like I know why but Im not sure how to write it another way that would work or be effiecent. I know that in this same method of run its updating it twice so really its only updating the first. Still unsure how to do this :/

    Code:
        public void scoreboard(final Player player){
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            final Scoreboard board = manager.getNewScoreboard();
            final Objective objective = board.registerNewObjective("test", "dummy");
           
            //Setting where to display the scoreboard/objective (either SIDEBAR, PLAYER_LIST or BELOW_NAME)
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            
            //Setting the display name of the scoreboard/objective
            Score score = objective.getScore("§6Coins:"); //Get a fake offline player
            score.setScore(1);
           
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    objective.setDisplayName("§aTEST!");
                    player.setScoreboard(board);
                }
            }, 0L, 20L);
           
            BukkitScheduler scheduler1 = Bukkit.getServer().getScheduler();
            scheduler1.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    objective.setDisplayName("§aTEST!");
                    player.setScoreboard(board);
                }
            }, 0L, 20L);
           
           
        }

    In my onEnable I have

    for(Player online : Bukkit.getOnlinePlayers()){
    this.scoreboard(online);
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  5. Offline

    1Rogue

    Make a single Scoreboard, and then use that one scoreboard object for every player.

    Also, you have two tasks doing the same thing, remove one of them.
     
  6. Offline

    malikdbuseck

    I have one scoreboard, also yea I figured having two would be point less. But I dont know how to set it so that it will update with a title thats another color. It will only add one and not the other
     
  7. Offline

    1Rogue

    Your method creates a new scoreboard for every player.

    At any rate, you can change the title at any time. Store a reference to your scoreboard somewhere, and then refer back to it when you want to change the title.
     
  8. Offline

    malikdbuseck

    So do this instead

    for(final Player online: Bukkit.getOnlinePlayers()){

    and then online.setScoreboard
     
  9. Offline

    1Rogue

    That literally would change nothing in your code. You need to do something like make the scoreboard, return it from the method, and then set it to players.
     
  10. Offline

    malikdbuseck

    Okay ill keep trying to figure this out. Still really new to all this. Ill watch some more tutorials and read some more docs.

    Yea I still cant do this :/
     
    Last edited by a moderator: Jan 9, 2015
  11. Offline

    Skionz

    Post all relevant classes and stack-traces in their entirety.
     
    malikdbuseck likes this.
  12. Offline

    malikdbuseck

    I just cant wrap my head around it. I also tried return and then adding players


    MAIN:
    Code:
    package me.dabuseck.kits;
    
    import me.dabuseck.kits.commands.commands;
    import me.dabuseck.kits.listeners.kitsGUI;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    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;
    
    public class Main extends JavaPlugin implements Listener {
       
        static int num = 0;
        static Scoreboard board;
    
        private static Plugin plugin;
    
        public String pre = ChatColor.DARK_AQUA + "AdvancedKits> ";
       
        SettingsManager settings = SettingsManager.getInstance();
       
       
    
        // ======================================================================
    
        @Override
        public void onEnable() {
           
            settings.setup(this);
           
            registerEvents(this,
                    new kitsGUI(this));
    
            getCommand("ak").setExecutor(new commands(this));
            getCommand("advkits").setExecutor(new commands(this));
           
            for(Player online : Bukkit.getOnlinePlayers()){
                this.scoreboard(online);
            }
        }
    
        // ======================================================================
    
        public static void registerEvents(org.bukkit.plugin.Plugin plugin,
                Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager()
                        .registerEvents(listener, plugin);
            }
        }
    
        // ======================================================================
    
        public static Plugin getPlugin() {
            return plugin;
        }
       
        public void scoreboard(final Player player){
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            final Scoreboard board = manager.getNewScoreboard();
            final Objective objective = board.registerNewObjective("test", "dummy");
          
            //Setting where to display the scoreboard/objective (either SIDEBAR, PLAYER_LIST or BELOW_NAME)
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
          
            //Setting the display name of the scoreboard/objective
            Score score = objective.getScore("§6Coins:"); //Get a fake offline player
            score.setScore(1);
          
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    objective.setDisplayName("§aTEST!");
                    player.setScoreboard(board);
                }
            }, 0L, 20L);
          
            BukkitScheduler scheduler1 = Bukkit.getServer().getScheduler();
            scheduler1.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    objective.setDisplayName("§aTEST!");
                    player.setScoreboard(board);
                }
            }, 0L, 20L);
          
          
        }
       
       
       
    }
    Kit:
    Code:
    package me.dabuseck.kits.listeners;
    
    import java.util.ArrayList;
    
    import me.dabuseck.kits.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.PermissionAttachment;
    
    public class kitsGUI implements Listener {
    
        public static Main plugin;
    
        @SuppressWarnings("static-access")
        public kitsGUI(Main plugin) {
            this.plugin = plugin;
        }
    
        final static Inventory i = Bukkit.createInventory(null, 27,
                "§3Current Kits");
    
        public static void Kit1(Player player) {
    
            // THIS CREATES THE ITEM=====================================
    
            ItemStack itemone = new ItemStack(Material.valueOf(plugin.getConfig()
                    .getString("Kit1.DisplayItem")));
            ItemMeta itemoneMeta = itemone.getItemMeta();
    
            // BOW META DATA=====================================
    
            itemoneMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',
                    plugin.getConfig().getString("Kit1.KitName")));
    
            ArrayList<String> blore = new ArrayList<String>();
            blore.add(ChatColor.translateAlternateColorCodes('&', plugin
                    .getConfig().getString("Kit1.KitLore")));
            itemoneMeta.setLore(blore);
    
            itemone.setItemMeta(itemoneMeta);
    
            i.setItem(10, itemone);
    
            player.openInventory(i);
    
        }
    
        @EventHandler
        public void onInvenClick(InventoryClickEvent event) {
    
            Player player = (Player) event.getWhoClicked();
    
            PermissionAttachment perm = player.addAttachment(plugin);
    
            PlayerInventory playerinv = player.getInventory();
            if (event.getInventory().getName().equalsIgnoreCase(i.getName())) {
                if (event
                        .getCurrentItem()
                        .getType()
                        .equals(Material.valueOf(plugin.getConfig().getString(
                                "Kit1.DisplayItem")))) {
                    if (player.hasPermission("ak.nokit")) {
                        player.closeInventory();
                        player.sendMessage(plugin.pre + ChatColor.RED
                                + " You have Selected The Archer Kit");
                    } else {
                        ItemStack itemone = new ItemStack(Material.valueOf(plugin
                                .getConfig().getString("Kit1.1.Item")));
                        playerinv.addItem(itemone);
                        player.addAttachment(plugin, "ak.archer", true);
                        perm.setPermission("ak.archer", true);
                        playerinv.addItem(itemone);
                    }
    
                    event.setCancelled(true);
                    return;
                }
            }
        }
       
        public void onJoin(PlayerJoinEvent event){
           
        }
    
    }
    
     
  13. Offline

    Skionz

    @malikdbuseck I don't think you ever actually change the scoreboard and why do you have two identical schedulers?
     
  14. Offline

    malikdbuseck

    yea I got rid of that. I realized that its doing nothing.

    New Main:

    Code:
    package me.dabuseck.kits;
    
    import me.dabuseck.kits.commands.commands;
    import me.dabuseck.kits.listeners.kitsGUI;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    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;
    
    public class Main extends JavaPlugin implements Listener {
    
        static int num = 0;
        static Scoreboard board;
    
        private static Plugin plugin;
    
        public String pre = ChatColor.DARK_AQUA + "AdvancedKits> ";
    
        SettingsManager settings = SettingsManager.getInstance();
    
        // ======================================================================
    
        @Override
        public void onEnable() {
    
            settings.setup(this);
    
            registerEvents(this, new kitsGUI(this));
    
            getCommand("ak").setExecutor(new commands(this));
            getCommand("advkits").setExecutor(new commands(this));
    
            for (Player online : Bukkit.getOnlinePlayers()) {
                this.scoreboard(online);
            }
        }
    
        // ======================================================================
    
        public static void registerEvents(org.bukkit.plugin.Plugin plugin,
                Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager()
                        .registerEvents(listener, plugin);
            }
        }
    
        // ======================================================================
    
        public static Plugin getPlugin() {
            return plugin;
        }
    
        public void scoreboard(final Player player) {
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            final Scoreboard board = manager.getNewScoreboard();
            final Objective objective = board.registerNewObjective("test", "dummy");
    
            // Setting where to display the scoreboard/objective (either SIDEBAR,
            // PLAYER_LIST or BELOW_NAME)
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    
            // Setting the display name of the scoreboard/objective
            Score score = objective.getScore("§6Coins:"); // Get a fake offline
                                                            // player
            score.setScore(1);
    
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    objective.setDisplayName("§aTEST!");
                    player.setScoreboard(board);
                }
            }, 0L, 20L);
    
        }
    
    }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  15. Offline

    teej107

    Is there a valid reason to why you are using static and using an uninitialized Plugin variable?
     
  16. Offline

    malikdbuseck

    Im pretty sure it was initialized before but I've been changing code and it doesnt really affect what Im doing
     
  17. Offline

    teej107

    It will when you get a NullPointerException. Remove it for now since you aren't doing anything with it. It's making me cringe as well as your misuse of static.
     
    malikdbuseck likes this.
  18. Offline

    malikdbuseck

    lol done

    SO DUMB. Just throw else ifs in the void run:


    Code:
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    if(objective.getDisplayName() == "§aTEST!"){
                        objective.setDisplayName("§bTEST!");
                    }else if(objective.getDisplayName() == "§bTEST!"){
                        objective.setDisplayName("§aTEST!");
                    }
                    player.setScoreboard(board);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  19. Offline

    Skionz

    @malikdbuseck You can't compare Strings with the == operator. Use Object#equals(Object) instead.
     
  20. Offline

    malikdbuseck

  21. Offline

    1Rogue

    http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
    It can work, it is not guaranteed to work.

    What I was saying before:

    Code:java
    1. for (Player online : Bukkit.getOnlinePlayers()) {
    2. this.scoreboard(online);
    3. }


    For every single player in the loop, you are passing the player to the scoreboard method. So that means the scoreboard method runs once for every player online, which means you create a new scoreboard for every player. 46 players? That's 46 scoreboards, and a hell of a lot of resources wasted.

    What you should do is create a single scoreboard, and then pass that scoreboard to Player#setScoreboard:

    Code:java
    1. public void onEnable() {
    2. //...
    3. Scoreboard s = this.scoreboard();
    4. for (Player p : Bukkit.getOnlinePlayers()) {
    5. p.setScoreboard(s);
    6. }
    7. }
    8.  
    9. //note change from void to Scoreboard
    10. public Scoreboard scoreboard() {
    11. //...
    12. Scoreboard board = manager.getNewScoreboard();
    13. //other code...
    14. return board;
    15. }
     
    malikdbuseck likes this.
  22. Offline

    malikdbuseck

    Gotcha so ill just change somethings around
     
Thread Status:
Not open for further replies.

Share This Page