Solved BossBar Levels Issue.

Discussion in 'Spigot Plugin Development' started by Nosmakos, Jun 4, 2017.

  1. I have made a level system and it works in the Scoreboard i have. So i thought i could move the levels to a BossBar so it can be cool, the thing is that i am online in the server with my brother and what level i have it shows that on his BossBar but in the Scoreboard it works correctly. So if i am 50 level and he is 100 it will show him 50 this might be opposite too if i log of i guess and he is first online.
    This is what i have.

    Code (Text):
        // ================================================================= //
        private static BossBar bb = Bukkit.createBossBar(ChatColor.GRAY + " ", BarColor.BLUE, BarStyle.SEGMENTED_10);
        // ================================================================= //

        public static void setupLevelsBossBar(Player player){
            bb.addPlayer(player);

            int exp = plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.ExperiencePoints");
            int level = plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.Level");
         
            if (level == 100){
                bb.setTitle(ChatColor.GRAY + "Your current level is: " + ChatColor.AQUA + level);
                bb.setProgress(1);
            }else{
                bb.setTitle(ChatColor.GRAY + "Your current level is: " + ChatColor.AQUA + level + ChatColor.GRAY + " / " + ChatColor.AQUA + exp);

                if ((exp >= 0) && (exp < 100)){
                    bb.setProgress(0.0);
                }
                if ((exp >= 100) && (exp < 200)){
                    bb.setProgress(0.1);
                }
                if ((exp >= 200) && (exp < 300)){
                    bb.setProgress(0.2);
                }
                if ((exp >= 300) && (exp < 400)){
                    bb.setProgress(0.3);
                }
                if ((exp >= 400) && (exp < 500)){
                    bb.setProgress(0.4);
                }
                if ((exp >= 500) && (exp < 600)){
                    bb.setProgress(0.5);
                }
                if ((exp >= 600) && (exp < 700)){
                    bb.setProgress(0.6);
                }
                if ((exp >= 700) && (exp < 800)){
                    bb.setProgress(0.7);
                }
                if ((exp >= 800) && (exp < 900)){
                    bb.setProgress(0.8);
                }
                if ((exp >= 900) && (exp < 1000)){
                    bb.setProgress(0.9);
                }
            }
        }
    }

    This is in the onEnable:

    Code (Text):
            new BukkitRunnable() {
                @Override
                public void run() {
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        SetupLevels.setupLevelsBossBar(p);
                    }
                }
            }.runTaskTimer(this, 0, 40L);
        }
    I would appreciate any help.
     
    #1 Nosmakos, Jun 4, 2017
    Last edited: Jun 4, 2017
  2. you using the same bossbar for every player so the they will all share the same title. you should create one bossbar for every player and add the owning player to the bossbar. also you could reduce your progress code with some math to one line.
     
  3. Yeah I. Know about the progress,I was checking if this would work correctly. I will try that, thanks. I will let you know.
     
    #4 Nosmakos, Jun 4, 2017
    Last edited: Jun 4, 2017
  4. I tried few things but didn't seem to be working. How could you create a bossbar for each player? i tried:
    Code (Text):
                    for (Object id : level.keySet().toArray()) {
                        Player p = Bukkit.getPlayer((UUID)id);
    but didn't seem to be working either.
     
  5. electronicboy

    IRC Staff

    you'd call the Bukkit method to create a new bossbar everytime somebody logged in and use that, one per player. You'd probably want a map to map the players to the bossbar instances so that you can update the correct one for the player
     
  6. So best way to do this could be an arraylist, hashmap?
     
  7. electronicboy

    IRC Staff

    hashmap, hence *map*ping players to bossbar instances ;)
     
  8. Hmmm the point is how can i do that with the hashmap about Bukkit.createBossBar. I mean Bukkit.createBossBar has to be out from the levels method because if you put it in the method then you going to get multiple titles. I never used a hashmap before on a bossbar and i think this might be confused until i find the way to do that.
    Any help would be appreciated.
     
  9. electronicboy

    IRC Staff

    The way I would do it is to create a class that basically;

    Code (Text):

    Map<UUID, Bossbar> bossbars = new HashMap<>();

    setLevel(Player player, Double level) {
        Bossbar bb = bossbars.get(Player.uuid)
       
        if (bb = null) {
            bb = Bukkit.createBossbar...
            bossbars.put(player.uuid, bb)
            bb.addPlayer(player)
         }
        bb.set....
    }
     
    you'd basically then be able to handle setting a bossbar easily per player, you probably also wanna be sane and remove the player from there when they log out too
     
    • Like Like x 1
  10. This could work?
    Code (Text):
        Map<UUID, BossBar> bossbars = new HashMap<>();

        public void setupLevelsBossBar(Player player) {
            BossBar bb = bossbars.get(player.getUniqueId());

            if (bb == null) {

                bb = Bukkit.createBossBar(ChatColor.GRAY + " ", BarColor.BLUE, BarStyle.SEGMENTED_10);
                bossbars.put(player.getUniqueId(), bb);
                bb.addPlayer(player);
            }
            if (plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.Level") == 100){
                bb.setTitle(ChatColor.GRAY + "Your current level is: " + ChatColor.AQUA + plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.Level"));
                bb.setProgress(1);
            }else{
                bb.setTitle(ChatColor.GRAY + "Your current level is: " + ChatColor.AQUA + plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.Level") + ChatColor.GRAY + " / " + ChatColor.AQUA + plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.ExperiencePoints"));
                int exp = plugin.getUsersConfig().getInt(player.getUniqueId() + ".Statistics.ExperiencePoints");

                if ((exp >= 0) && (exp < 100)){
                    bb.setProgress(0.0);
                }
                if ((exp >= 100) && (exp < 200)){
                    bb.setProgress(0.1);
                }
                if ((exp >= 200) && (exp < 300)){
                    bb.setProgress(0.2);
                }
                if ((exp >= 300) && (exp < 400)){
                    bb.setProgress(0.3);
                }
                if ((exp >= 400) && (exp < 500)){
                    bb.setProgress(0.4);
                }
                if ((exp >= 500) && (exp < 600)){
                    bb.setProgress(0.5);
                }
                if ((exp >= 600) && (exp < 700)){
                    bb.setProgress(0.6);
                }
                if ((exp >= 700) && (exp < 800)){
                    bb.setProgress(0.7);
                }
                if ((exp >= 800) && (exp < 900)){
                    bb.setProgress(0.8);
                }
                if ((exp >= 900) && (exp < 1000)){
                    bb.setProgress(0.9);
                }
            }
        }
    }
    Code (Text):
        @EventHandler
        public void onLeaving(PlayerQuitEvent e){
            BossBar bb = bossbars.get(e.getPlayer().getUniqueId());
            bb = Bukkit.createBossBar(ChatColor.GRAY + " ", BarColor.BLUE, BarStyle.SEGMENTED_10);
            bossbars.remove(e.getPlayer().getUniqueId(), bb);
        }
    }
     
     
  11. Seems that it's working, but before i become happy about it, is there anything else i have to remove them from hashmap or something that shouldn't work? i mean what if user reloads the server? and other things that might cause something to it.

    EDIT: Seems like when i am logging of and logging back in i don't have the bossbar only when server starts for first time.
    This is the only problem i have right now.

    EDIT: Solved this too, i had to put bb.addPlayer(player); down from the null check. Also like i said, if i am missing something i would appreciate to know what else should i check.
     
    #12 Nosmakos, Jun 4, 2017
    Last edited: Jun 4, 2017