NMS Boss Bar Utility [SIMPLE CODE]

Discussion in 'Resources' started by EnervateD, Apr 17, 2016.

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

    EnervateD

    Hello, guys!
    Recently I have been developing a plugin that contains boss bar functions. I didn't want to use some big library with a lot of reflection classes, so I made my own small class that can help you a lot to use a boss bar for your plugin
    Here is a code:

    Code:
    package ru.enervate.bossbar;
    
    import net.minecraft.server.v1_8_R3.*;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    
    import java.lang.reflect.Field;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.ConcurrentHashMap;
    
    /**
    * Bukkit/Spigot/Bungee Java Plugin Maker.
    * EnervateD. All rights reserved.
    */
    public class BarUtil {
    
        private static Map<String, EntityEnderDragon> dragons = new ConcurrentHashMap<>();
    
        public static void setBar(Player p, String text, float healthPercent) {
            Location loc = p.getLocation();
            WorldServer world = ((CraftWorld) p.getLocation().getWorld()).getHandle();
    
            EntityEnderDragon dragon = new EntityEnderDragon(world);
            dragon.setLocation(loc.getX(), loc.getY() - 100, loc.getZ(), 0, 0);
    
            PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(dragon);
    
            DataWatcher watcher = new DataWatcher(null);
            watcher.a(0, (byte) 0x20);
            watcher.a(6, (healthPercent * 200) / 100);
            watcher.a(10, text);
            watcher.a(2, text);
            watcher.a(11, (byte) 1);
            watcher.a(3, (byte) 1);
    
            try{
                Field t = PacketPlayOutSpawnEntityLiving.class.getDeclaredField("l");
                t.setAccessible(true);
                t.set(packet, watcher);
            } catch(Exception ex){
                ex.printStackTrace();
            }
    
            dragons.put(p.getName(), dragon);
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
        }
    
        public static void removeBar(Player p) {
            if(dragons.containsKey(p.getName())) {
                PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(dragons.get(p.getName()).getId());
                dragons.remove(p.getName());
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
            }
        }
    
        public static void teleportBar(Player p) {
            if(dragons.containsKey(p.getName())) {
                Location loc = p.getLocation();
                PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport(dragons.get(p.getName()).getId(),
                        (int) loc.getX() * 32, (int) (loc.getY() - 100) * 32, (int) loc.getZ() * 32,
                        (byte) ((int) loc.getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360), false);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
            }
        }
    
        public static void updateText(Player p, String text) {
            updateBar(p, text, -1);
        }
    
        public static void updateHealth(Player p, float healthPercent) {
            updateBar(p, null, healthPercent);
        }
    
        public static void updateBar(Player p, String text, float healthPercent) {
            if(dragons.containsKey(p.getName())) {
                DataWatcher watcher = new DataWatcher(null);
                watcher.a(0, (byte) 0x20);
                if (healthPercent != -1) watcher.a(6, (healthPercent * 200) / 100);
                if (text != null) {
                    watcher.a(10, text);
                    watcher.a(2, text);
                }
                watcher.a(11, (byte) 1);
                watcher.a(3, (byte) 1);
    
                PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(dragons.get(p.getName()).getId(), watcher, true);
                ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
            }
        }
    
        public static Set<String> getPlayers() {
            Set<String> set = new HashSet<>();
    
            for(Map.Entry<String, EntityEnderDragon> entry : dragons.entrySet()) {
                set.add(entry.getKey());
            }
    
            return set;
        }
    
    }
    
    You can send a bar for player like this:
    Code:
    BarUtil.sendBar(event.getPlayer(), "§6Hello, §a" + e.getPlayer().getName() + "§6!", 50); // It will send a bar for player with text "Hello, %Player!" and with 50 percents of bar health
    When player quit the game, you can remove bar:
    Code:
    BarUtil.removeBar(event.getPlayer());
    At the main class, in onEnable() you should do something like this:
    Code:
        public void onEnable() {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    while(true) {
                        for(String s : BarUtil.getPlayers()) {
                            Player o = Bukkit.getPlayer(s);
                            if(o != null) BarUtil.teleportBar(o);
                        }
    
                        try {
                            Thread.sleep(1000); // 1000 = 1 sec
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
            }).start();
        }
    To update bar text, you should make this:
    Code:
    BarUtil.updateText(event.getPlayer(), "New text goes here");
    To update bar health, type following code:
    Code:
    BarUtil.updateHealth(event.getPlayer(), 35);
    To update text and health, you can do this:
    Code:
    BarUtil.updateBar(event.getPlayer(), "New text", 50);
    I wish you good luck in using my utility.
    Thanks for watching!
     
    Last edited: Apr 26, 2016
    ChipDev likes this.
  2. Offline

    mcdorli

    Some questions:

    1.: Why is everything static?
    2.: Why are you lazy to put the public modifier before every method?
     
  3. Offline

    EnervateD

    1 - Because if it wouldn't be static, you should create new var for class utility
    2 - Because it will be public even if I won't put public modifier
     
  4. Offline

    I Al Istannen

    @EnervateD
    2. If i am not mistaken I am not. It won't be public. It will be package private. Look here.
    No modifier is package private.
     
    Last edited: Apr 26, 2016
  5. Offline

    EnervateD

    I was using this class in my plugin so I had no problems. Anyway, okay, will be fixed
     
    I Al Istannen likes this.
  6. Offline

    mcdorli

    It will be default.
     
  7. Offline

    ChipDev

    As shown in the tutorial, default is package-private. But if it works, it is fine.
     
  8. Offline

    MCMastery

    Can't you use Bukkit.createBossBar now?
     
  9. Offline

    Zarlen

    This isnt working, bossbar doesnt appear
     
Thread Status:
Not open for further replies.

Share This Page