Tutorial [1.8] Actionbar Packets

Discussion in 'Resources' started by Orange Tabby, Sep 17, 2015.

?

Was this helpful?

  1. Yes!

    77.8%
  2. No.

    0 vote(s)
    0.0%
  3. Could be better.

    22.2%
Thread Status:
Not open for further replies.
  1. NOTE: You must be using 1.8 or this will not work!
    Okay lets get started:

    Setting up your class (open)
    Okay you will have to make a class dedicated to the Actionbar I named mine Actionbar
    Code:
    package me.skylordjay_.titles;
    
    public class ActionBar {
    
    }
    
    Now you are going to make your Action bar constructor take in a String I'll named mine text.
    You should have something like this:
    Code:
    package me.skylordjay_.titles;
    
    public class ActionBar {
       
        public ActionBar(String text) {
           
        }
    
    }
    Its time to create your packet, Yay!
    You are going to need a PacketPlayOutChat variable I named mine "packet"
    your new PacketPlayOutChat should contain a IChatBaseComponent and a byte
    set your byte to 2 this is the id for the action bar I believe, and set your IChatBaseComponent to a ChatSerializer.a() once you have your ChatSerializer add this "{\"text\":\"" + text + "\"}" this is json formatting you can mess around with it here.
    Hopefully you have something a bit like this:

    Code:
    package me.skylordjay_.titles;
    
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    
    public class ActionBar {
       
        public ActionBar(String text) {
            PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + text + "\"}"), (byte) 2);
        }
    
    }
    
    Now you want to create a private variable and have it equal your PacketPlayOutChat.

    Code:
    package me.skylordjay_.titles;
    
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    
    public class ActionBar {
       
        private PacketPlayOutChat packet;
    
        public ActionBar(String text) {
            PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + text + "\"}"), (byte) 2);
    
            this.packet = packet;
        }
    
    }
    Your almost done, you need to create 2 void methods one call sendToPlayer and the other called sendToAll this is how we are going to tell the packet where to go.

    Let your sendToPlayer method take in the Player entity.

    Code:
    package me.skylordjay_.titles;
    
    import org.bukkit.entity.Player;
    
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    
    public class ActionBar {
       
        private PacketPlayOutChat packet;
    
        public ActionBar(String text) {
            PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + text + "\"}"), (byte) 2);
            this.packet = packet;
        }
       
        public void sendToPlayer(Player p) {
           
        }
       
        public void sendToAll() {
           
        }
    
    }
    Now that your sendToPlayer method has a Player entity you are going to cast it to a CraftPlayer like this
    Code:
    ((CraftPlayer)p)
    Once you get your Craft Player you want to get its Handle and then playerConnection;
    Code:
    ((CraftPlayer)p).getHandle().playerConnection;
    You want to send this playerConnection the packet once you did this your code should look something like this:
    Code:
    package me.skylordjay_.titles;
    
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    
    public class ActionBar {
       
        private PacketPlayOutChat packet;
    
        public ActionBar(String text) {
            PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + text + "\"}"), (byte) 2);
            this.packet = packet;
        }
       
        public void sendToPlayer(Player p) {
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
        }
       
        public void sendToAll() {
           
        }
    
    }
    
    Your sendToPlayer Method is done but there's one more your sendToAll method
    this method should contain a for loop, looping thru all the players on the server:
    Code:
    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
               
        }
    You are going to do the same as you did for the sendToPlayer method, cast each Player to a CraftPlayer
    and get there handle then playerConnection.
    Code:
        public void sendToAll() {
            for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                ((CraftPlayer)p).getHandle().playerConnection;
            }
        }
    Now send the packet!
    Code:
    package me.skylordjay_.titles;
    
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    
    public class ActionBar {
       
        private PacketPlayOutChat packet;
    
        public ActionBar(String text) {
            PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + text + "\"}"), (byte) 2);
            this.packet = packet;
        }
       
        public void sendToPlayer(Player p) {
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
        }
       
        public void sendToAll() {
            for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);;
            }
        }
    
    }
    
    Your done with this class, If you wish to see an example of how to use the code you just wrote Please continue to the next Spoiler titled 'Action Bar Example'.

    Action Bar Example (open)
    If you have not please read the tutorial above :D
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            ActionBar actionBar = new ActionBar(ChatColor.GOLD + "Hello world");
            actionBar.sendToPlayer(p);
        }

     
    NIK220V, dumbasPL, htmlman1 and 6 others like this.
Thread Status:
Not open for further replies.

Share This Page