Tutorial [1.8] Using Boss Bar Display

Discussion in 'Resources' started by AppleBabies, Mar 1, 2016.

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

    AppleBabies

    Hello all! Today I will be showing how to properly set up and display a boss bar. You may see this on many servers such as Mineplex or Hypixel, and it's a really neat way to display information. Enough talk, let's get started! This is really simple once you understand it.

    Of course, you want your class to implement Listener and register your events:
    Code:
    public class Bar extends JavaPlugin implements Listener {
    
                 public void onEnable() {
                   getServer().getPluginManager().registerEvents(this, this);
                  }
    
    }
    Next, let's get started with our method. We'll call it...newBar().
    Code:
    private void newBar(Player player){
    
    }
    So first we need to get the player's location, and their world. Notice we are using Minecraft and not Bukkit for the world. We do this so it is displayed client side and not server side:
    Code:
    private void newBar(Player player){
    Location loc = player.getLocation();
    WorldServer world = ((CraftWorld) player.getLocation().getWorld()).getHandle();
    }
    Cool, that's done. Now we have to create our entity for the bar to be displayed.
    Code:
    private void newBar(Player player){
    Location loc = player.getLocation();
    WorldServer world = ((CraftWorld) player.getLocation().getWorld()).getHandle();
    
    EntityEnderDragon dragon = new EntityEnderDragon(world);
            dragon.setLocation(loc.getX() - 30, loc.getY() - 100, loc.getZ(), 0, 0);
            dragon.setCustomName(ChatColor.GOLD + "Test"); //You can replace this with any character below 72 characters
            dragon.setInvisible(true);
    }
    So, up there we created the dragon client side for the player and we set it's location 30 blocks in front of and 100 blocks below the player, so you cannot hear it but still see the bar. Then we make the dragon invisible so it cannot be seen.

    Finally, we send the packet to spawn the dragon.
    Code:
    private void newBar(Player player){
    Location loc = player.getLocation();
    WorldServer world = ((CraftWorld) player.getLocation().getWorld()).getHandle();
    
    EntityEnderDragon dragon = new EntityEnderDragon(world);
            dragon.setLocation(loc.getX() - 30, loc.getY() - 100, loc.getZ(), 0, 0);
            dragon.setCustomName(ChatColor.GOLD + "Test");
            dragon.setInvisible(true);
    
        Packet<?> packet = new PacketPlayOutSpawnEntityLiving(dragon);
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    }
    Now, we have it created. How can this be used? Simple.
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            newBar(e.getPlayer());
        }
    Once the player joins, the bar will be sent! And once they leave, since it is spawned client side, it will despawn. The only downside to this is you have to be looking near where the dragon is to see the bar, but this could be fixed by moving the dragon on the X scale when the player moves.

    Hope you found this useful! Please correct any errors I may have made, I may have made a couple!
     
    Zombie_Striker and jusjus112 like this.
  2. Offline

    DominicGamesHD

    Thanks <3
     
    AppleBabies likes this.
  3. Offline

    Zarlen

    You can add in your topic:

    For those that want to change the name via method (example: newBar(player, text)
    Change
    dragon.setCustomName(ChatColor.GOLD+"Test");
    to
    dragon.setCustomName(text);
    and set
    privatevoid newBar(Player player){
    to
    privatevoid newBar(Player player, String text){
     
  4. Offline

    MCMastery

    Why not use Bukkit.newBossBar or whatever it is
     
  5. This tutorial was created for 1.8, these methods didn't exist pre 1.9
     
    cococow123 and MCMastery like this.
Thread Status:
Not open for further replies.

Share This Page