Send title to player - Packets

Jul 16, 2017 at 7:24 PM
Send title to player - Packets
  • You may be wondering, "How do I send a title to a player?" I'll answer you with a single word known as 'Packets'. With them you can do things that minecraft makes available but the spigot does not send it easily to a player. In this tutorial I'll teach you how to send titles to a player. So let's start:

    Titles:
    To send a title to a player, you will use a very simple and easy to use 'Packet'. You could very well use a method provided by spigot called sendTitle (title, subtitle) ;, it is in the Player class. This method it is depreciated and in a while it will disappear. Using packets you can configure everything from the title. See how:

    1st You will have to create a method called 'send title', as shown below:

    Code (Text):
    public void sendTitle () {
    }
    You will have to add the second parameters in it: Player player, String title, String subtitle, int fadein, int stay, int fadeout, as shown below

    Code (Text):
    public void sendTitle (Player player, String title, String subtitle, int fadein, int say, int fadeout) {
    }
    3rd Then you have to send a package to the player. The name of this package is PacketPlayOutTitle. Before sending it to the player, you will need to create text in JSON format, as shown below

    Code (Text):
    public void sendTitle (Player player, String title, String subtitle, int fadein, int say, int fadeout) {
    IChatBaseComponent chatTitle = ChatSerializer.a ("{\" text \ ": \" "+ title +" \ "}");
    IChatBaseComponent chatsubtitle = ChatSerializer.a ("{\" text \ ": \" "+ subtitle +" \ "}");
    }
    Then you'll create the title, subtitle, and time it will be visible, as shown below

    Code (Text):
    public void sendTitle (Player player, String title, String subtitle, int fadein, int say, int fadeout) {
    IChatBaseComponent chatTitle = ChatSerializer.a ("{\" text \ ": \" "+ title +" \ "}");
    IChatBaseComponent chatsubtitle = ChatSerializer.a ("{\" text \ ": \" "+ subtitle +" \ "}");

    PacketPlayOutTitle t = new PacketPlayOutTitle (EnumTitleAction.TITLE, chatTitle);
    PacketPlayOutTitle s = new PacketPlayOutTitle (EnumTitleAction.SUBTITLE, chatsubtitle);
    PacketPlayOutTitle length = new PacketPlayOutTitle (fadeIn * 20, stay * 20, fadeout * 20);

    }

    4th You will have to send the 'Packets' to a player, the 'Packets' registered above, as shown below

    Code (Text):
    public void sendTitle (Player player, String title, String subtitle, int fadein, int say, int fadeout) {
    IChatBaseComponent chatTitle = ChatSerializer.a ("{\" text \ ": \" "+ title +" \ "}");
    IChatBaseComponent chatsubtitle = ChatSerializer.a ("{\" text \ ": \" "+ subtitle +" \ "}");

    PacketPlayOutTitle t = new PacketPlayOutTitle (EnumTitleAction.TITLE, chatTitle);
    PacketPlayOutTitle s = new PacketPlayOutTitle (EnumTitleAction.SUBTITLE, chatsubtitle);
    PacketPlayOutTitle length = new PacketPlayOutTitle (fadeIn * 20, stay * 20, fadeout * 20);

    ((CraftPlayer) player) .getHandle (). PlayerConnection.sendPacket (t);
    ((CraftPlayer) player) .getHandle (). PlayerConnection.sendPacket (s);
    ((CraftPlayer) player) .getHandle (). PlayerConnection.sendPacket (length);
    }
    And that's it, the method is done. You then need to send a title to the player, using the following:

    Code (Text):
    @Override
    void onEnable () {
       For (Player p: Bukkit.getOnlinePlayers)
             SendTitle (p, "Title", "Subtitle", 1, 2, 1);
    }
    When your plug-in starts, it will send a title to all players who enter
    Help? Add me in Skype: facebook:featcraft123
  • Loading...
  • Loading...